Getting UTF-8 Request Parameter Strings in mod_perl2

I'm using mod_perl2 for a website and use CGI::Apache2::Wrapper to get the request parameters for the page (e.g. post data). I've noticed that the string the $req->param("parameter") function returns is not UTF-8. If I use the string as-is I can end up with garbled results, so I need to decode it using Encode::decode_utf8(). Is there anyway to either get the parameters already decoded into UTF-8 strings or loop through the parameters and safely decode them?

Answers


To get the parameters already decoded, we would need to override the behaviour of the underlying class Apache2::Request from libapreq2, thus losing its XS speed advantage. But that is not even straightforward possible, as unfortunately we are sabotaged by the CGI::Apache2::Wrapper constructor:

unless (defined $r and ref($r) and ref($r) eq 'Apache2::RequestRec') {

This is wrong OO programming, it should say

… $r->isa('Apache2::RequestRec')

or perhaps forego class names altogether and just test for behaviour (… $r->can('param')).

I say, with those obstacles, it's not worth it. I recommend to keep your existing solution that decodes parameters explicitly. It's clear enough.

To loop over the request parameters, simply do not pass an argument to the param method and you get a list of the names. This is documented (1, 2), please read more carefully.


Need Your Help

Cannot record screen on Android 4.4.2

android terminal android-4.4-kitkat

I am trying to record my device screen using the shell command as outlined here:

Content of button adapts size according to button?

c# .net wpf wpfdatagrid

I want to resize content within a button so that when I "resize" my window, buttons also gets resized but size of content within button remains unchanged? What should I do in order to make conte...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.