Beefy Boxes and Bandwidth Generously Provided by pair Networks Cowboy Neal with Hat
Do you know where your variables are?
 
PerlMonks  

Underscores changed to hyphens by HTTP::Proxy header filter

by jds17 (Pilgrim)
on Apr 09, 2009 at 09:12 UTC ( #756540=perlquestion: print w/ replies, xml ) Need Help??
jds17 has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I have written a simple HTTP Proxy which I want to use to add an entry to the header of every request passing through it. My problem is that all underscores "_" get changed to hyphens "-" in the entries' keys and the key is converted to lowercase. The code looks as follows:

The ini file:

[proxy_setup] #port on which proxy listens, use in browser proxy settings port=84 #header to add header_key=SM_USER header_value=waldo
The proxy:
use HTTP::Proxy; use HTTP::Proxy::HeaderFilter::simple; use HTTP::Proxy::Engine::NoFork; use Config::IniFiles; #initialize ini file loading my $ini = 'SM_Proxy.ini'; my $cfg = new Config::IniFiles(-file => $ini); die "invalid ini file $ini" unless defined($cfg); #reads specified config file entry sub read_cfg { my ($section, $key) = @_; my $val = $cfg->val($section, $key); die "undefined key $key in section $section" unless defined($val); $val; } #read values from ini file my $port = read_cfg('proxy_setup', 'port'); my $header_key = read_cfg('proxy_setup', 'header_key'); my $header_value = read_cfg('proxy_setup', 'header_value'); #define proxy and header filter(adding entry read from ini file to eac +h request header) my $proxy = HTTP::Proxy->new( host => "127.0.0.1", port => $port ); my $filter = HTTP::Proxy::HeaderFilter::simple->new( sub { $_[1]->header( $header_key => $header_value); } ); $proxy->push_filter( request => $filter ); $proxy->start;
I.e. in the current example, the name of the header entry comes out as "sm-user" instead of "SM_USER". What am I doing wrong?

Update: Thank you guys, using the flag $HTTP::Headers::TRANSLATE_UNDERSCORE solved the issue!

Comment on Underscores changed to hyphens by HTTP::Proxy header filter
Select or Download Code
Re: Underscores changed to hyphens by HTTP::Proxy header filter
by Anonymous Monk on Apr 09, 2009 at 09:55 UTC
    HTTP::Header
    #!/usr/bin/perl --- use strict; use warnings; use HTTP::Headers; my $h = HTTP::Headers->new; $h->header( qw' SM_USER waldo '); print $h->as_string; __END__ SM-USER: waldo
    http://cpansearch.perl.org/src/GAAS/libwww-perl-5.825/t/base/headers.t
    # Check with FALSE $HTML::Headers::TRANSLATE_UNDERSCORE { local($HTTP::Headers::TRANSLATE_UNDERSCORE); $HTTP::Headers::TRANSLATE_UNDERSCORE = undef; # avoid -w warning $h = HTTP::Headers->new; $h->header(abc_abc => "foo"); $h->header("abc-abc" => "bar"); ok($h->header("ABC_ABC"), "foo"); ok($h->header("ABC-ABC"),"bar"); ok($h->remove_header("Abc_Abc")); ok(!defined($h->header("abc_abc"))); ok($h->header("ABC-ABC"), "bar"); }
      http://cpansearch.perl.org/src/GAAS/libwww-perl-5.825/Changes
      HTTP::Headers now special case field names that start with a ':'. This is used as an escape mechanism when you need the header names to not go through canonicalization. It means that you can force LWP to use a specific casing and even underscores in header names. The ugly $TRANSLATE_UNDERSCORE global has been undocumented as a result of this.
      seems to work
      #!/usr/bin/perl --- use strict; use warnings; use LWP 5.800; my $h = HTTP::Headers->new; $h->header( qw' :SM_USER waldo '); print $h->as_string; __END__ SM_USER: waldo

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://756540]
Approved by almut
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2014-04-20 08:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (485 votes), past polls