Changeset 29266
- Timestamp:
- 01/30/09 12:21:15 (4 years ago)
- Location:
- lang/perl/HTTP-Engine-Middleware/trunk
- Files:
-
- 9 modified
-
. (modified) (1 prop)
-
lib/HTTP/Engine/Middleware/DoCoMoGUID.pm (modified) (1 diff)
-
lib/HTTP/Engine/Middleware/Encode.pm (modified) (1 diff)
-
lib/HTTP/Engine/Middleware/ModuleReload.pm (modified) (1 diff)
-
lib/HTTP/Engine/Middleware/ReverseProxy.pm (modified) (1 diff)
-
t/01_encode.t (modified) (2 diffs)
-
t/02_docomo_guid.t (modified) (3 diffs)
-
t/12_plugin_mobile_attribute.t (modified) (2 diffs)
-
t/15_middleware_reverseproxy.t (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
lang/perl/HTTP-Engine-Middleware/trunk
-
lang/perl/HTTP-Engine-Middleware/trunk/lib/HTTP/Engine/Middleware/DoCoMoGUID.pm
r11963 r29266 5 5 6 6 sub wrap { 7 my ($ next, $c) = @_;7 my ($class, $next) = @_; 8 8 9 $next->($c); 9 sub { 10 my $req = shift; 10 11 11 if ( $c->res->status == 200 12 && $c->res->content_type =~ /html/ 13 && not blessed $c->res->body 14 && $c->res->body ) 15 { 16 my $body = $c->res->body; 17 $c->res->body( 18 do { 19 my $guid = HTML::StickyQuery->new( 20 'abs' => 1, 21 ); 22 $guid->sticky( 23 scalarref => \$body, 24 param => { guid => 'ON' }, 25 ); 26 } 27 ); 12 my $res = $next->($req); 13 14 if ( $res->status == 200 15 && $res->content_type =~ /html/ 16 && not blessed $res->body 17 && $res->body ) 18 { 19 my $body = $res->body; 20 $res->body( 21 sub { 22 my $guid = HTML::StickyQuery->new( 23 'abs' => 1, 24 ); 25 $guid->sticky( 26 scalarref => \$body, 27 param => { guid => 'ON' }, 28 ); 29 }->() 30 ); 31 } 32 33 $res; 28 34 } 29 35 } -
lang/perl/HTTP-Engine-Middleware/trunk/lib/HTTP/Engine/Middleware/Encode.pm
r11888 r29266 5 5 6 6 sub wrap { 7 my ($ next, $c) = @_;7 my ($class, $next) = @_; 8 8 9 if (($c->req->headers->header('Content-Type')||'') =~ /charset=(.+);?$/){10 # decode parameters11 my $encoding = $1;12 my $dve = Data::Visitor::Encode->new;13 $c->req->query_parameters($dve->decode($encoding, $c->req->query_parameters));14 $c->req->body_parameters($dve->decode($encoding, $c->req->body_parameters));15 $c->req->parameters($dve->decode($encoding, $c->req->parameters));16 }9 sub { 10 my $req = shift; 11 if (($req->headers->header('Content-Type')||'') =~ /charset=(.+);?$/) { 12 # decode parameters 13 my $encoding = $1; 14 for my $method (qw/params query_params body_params/) { 15 $req->$method( Data::Visitor::Encode->decode($encoding, $req->$method) ); 16 } 17 17 18 $next->($c); 18 $next->($req); 19 } 20 }; 19 21 } 20 22 21 22 23 1; -
lang/perl/HTTP-Engine-Middleware/trunk/lib/HTTP/Engine/Middleware/ModuleReload.pm
r11881 r29266 4 4 5 5 sub wrap { 6 my $next= shift;6 my ($class, $next) = shift; 7 7 8 Module::Reload->check; 8 sub { 9 my $req = shift; 10 Module::Reload->check; 9 11 10 $next->(@_); 12 $next->($req); 13 }; 11 14 } 12 15 -
lang/perl/HTTP-Engine-Middleware/trunk/lib/HTTP/Engine/Middleware/ReverseProxy.pm
r11881 r29266 3 3 4 4 sub wrap { 5 my ($ next, $c) = @_;5 my ($class, $next) = @_; 6 6 7 # in apache httpd.conf (RequestHeader set X-Forwarded-HTTPS %{HTTPS}s) 8 $ENV{HTTPS} = $ENV{HTTP_X_FORWARDED_HTTPS} if $ENV{HTTP_X_FORWARDED_HTTPS}; 9 $ENV{HTTPS} = 'ON' if $ENV{HTTP_X_FORWARDED_PROTO}; # Pound 10 $c->req->secure(1) if $ENV{HTTPS} && uc $ENV{HTTPS} eq 'ON'; 7 sub { 8 my $req = shift; 11 9 12 # If we are running as a backend server, the user will always appear 13 # as 127.0.0.1. Select the most recent upstream IP (last in the list) 14 if ($ENV{HTTP_X_FORWARDED_FOR}) { 15 my ($ip, ) = $ENV{HTTP_X_FORWARDED_FOR} =~ /([^,\s]+)$/; 16 $c->req->address($ip); 17 } 10 my $env = $req->_connection->{env} || {}; 11 # in apache httpd.conf (RequestHeader set X-Forwarded-HTTPS %{HTTPS}s) 12 $env->{HTTPS} = $req->headers->{'x-forwarded-https'} if $req->headers->{'x-forwarded-https'}; 13 $env->{HTTPS} = 'ON' if $req->headers->{'x-forwarded-proto'}; # Pound 14 $req->secure(1) if $env->{HTTPS} && uc $env->{HTTPS} eq 'ON'; 18 15 19 if ($ENV{HTTP_X_FORWARDED_HOST}) { 20 my $host = $ENV{HTTP_X_FORWARDED_HOST}; 21 if ($host =~ /^(.+):(\d+)$/ ) { 22 $host = $1; 23 $ENV{SERVER_PORT} = $2; 24 } elsif ($ENV{HTTP_X_FORWARDED_PORT}) { 25 # in apache httpd.conf (RequestHeader set X-Forwarded-Port 8443) 26 $ENV{SERVER_PORT} = $ENV{HTTP_X_FORWARDED_PORT}; 16 # If we are running as a backend server, the user will always appear 17 # as 127.0.0.1. Select the most recent upstream IP (last in the list) 18 if ($req->headers->{'x-forwarded-for'}) { 19 my ($ip, ) = $req->headers->{'x-forwarded-for'} =~ /([^,\s]+)$/; 20 $req->address($ip); 27 21 } 28 $ENV{HTTP_HOST} = $host;29 22 30 $c->req->headers->header( 'Host' => $ENV{HTTP_HOST} ); 31 } 23 if ($req->headers->{'x-forwarded-host'}) { 24 my $host = $req->headers->{'x-forwarded-host'}; 25 if ($host =~ /^(.+):(\d+)$/ ) { 26 $host = $1; 27 $env->{SERVER_PORT} = $2; 28 } elsif ($req->headers->{'x-forwarded-port'}) { 29 # in apache httpd.conf (RequestHeader set X-Forwarded-Port 8443) 30 $env->{SERVER_PORT} = $req->headers->{'x-forwarded-port'}; 31 } 32 $env->{HTTP_HOST} = $host; 32 33 33 for my $attr (qw/uri base/) { 34 my $scheme = $c->req->secure ? 'https' : 'http'; 35 my $host = $ENV{HTTP_HOST} || $ENV{SERVER_NAME}; 36 my $port = $ENV{SERVER_PORT} || ( $c->req->secure ? 443 : 80 ); 34 $req->headers->header( 'Host' => $env->{HTTP_HOST} ); 35 } 36 $req->_connection->{env} = $env; 37 37 38 $c->req->$attr->scheme($scheme);39 $c->req->$attr->host($host);40 $c->req->$attr->port($port);41 $c->req->$attr( $c->req->$attr->canonical);42 }38 for my $attr (qw/uri base/) { 39 my $scheme = $req->secure ? 'https' : 'http'; 40 my $host = $env->{HTTP_HOST} || $env->{SERVER_NAME}; 41 my $port = $env->{SERVER_PORT} || ( $req->secure ? 443 : 80 ); 42 # my $path_info = $env->{PATH_INFO} || '/'; 43 43 44 $next->($c); 44 $req->$attr->scheme($scheme); 45 $req->$attr->host($host); 46 $req->$attr->port($port); 47 # $req->$attr->path($path_info); 48 # $req->$attr( $req->$attr->canonical ); 49 } 50 $next->($req); 51 }; 45 52 } 46 53 -
lang/perl/HTTP-Engine-Middleware/trunk/t/01_encode.t
r12846 r29266 5 5 eval q{ use Data::Visitor::Encode }; 6 6 plan skip_all => "Data::Visitor::Encode is not installed" if $@; 7 eval q{ use HTTP::Engine middlewares => ['+HTTP::Engine::Middleware::Encode'] }; 7 eval q{ use HTTP::Request }; 8 plan skip_all => "HTTP::Request is not installed" if $@; 9 eval q{ use HTTP::Engine }; 8 10 plan skip_all => "HTTP::Engine is not installed: $@" if $@; 11 12 eval q{ use HTTP::Engine::Middleware::Encode }; 9 13 10 14 plan tests => 3*blocks; … … 22 26 my $request = HTTP::Request->new( GET => $block->uri, ['Content-Type' => $block->content_type] ); 23 27 28 my $do_test = sub { 29 my $req = shift; 30 ok Encode::is_utf8($req->params->{'nite'}); 31 is_deeply $req->params, $block->params, $block->name; 32 HTTP::Engine::Response->new(body => 'OK!'); 33 }; 34 24 35 my $response = HTTP::Engine->new( 25 36 interface => { 26 37 module => 'Test', 27 request_handler => sub { 28 my $c = shift; 29 ok Encode::is_utf8($c->req->params->{'nite'}); 30 is_deeply $c->req->params, $block->params, $block->name; 31 $c->res->body('OK!'); 32 }, 38 request_handler => HTTP::Engine::Middleware::Encode->wrap($do_test), 33 39 }, 34 40 )->run($request); -
lang/perl/HTTP-Engine-Middleware/trunk/t/02_docomo_guid.t
r11963 r29266 4 4 use lib '.'; 5 5 use Test::Base; 6 eval q{ use HTTP::Engine middlewares => ['+HTTP::Engine::Middleware::DoCoMoGUID'] }; 7 plan skip_all => "some depended module is not installed.: $@" if $@; 6 eval q{ use HTML::StickyQuery }; 7 plan skip_all => "HTML::StickyQuery is not installed" if $@; 8 eval q{ use HTTP::Engine }; 9 plan skip_all => "HTTP::Engine is not installed: $@" if $@; 8 10 9 11 plan tests => 1*blocks; … … 11 13 use Encode; 12 14 use URI; 15 use HTTP::Request; 16 use HTTP::Engine::Response; 17 eval q{ use HTTP::Engine::Middleware::DoCoMoGUID }; 18 19 filters({ 20 expected => qw/ chomp /, 21 }); 13 22 14 23 run { 15 24 my $block = shift; 16 25 26 my $code = sub { 27 my $req = shift; 28 HTTP::Engine::Response->new( 29 content_type => 'text/html', 30 body => $block->input, 31 ); 32 }; 33 17 34 my $response = HTTP::Engine->new( 18 35 interface => { 19 36 module => 'Test', 20 request_handler => sub { 21 my $c = shift; 22 $c->res->content_type('text/html'); 23 $c->res->body($block->input); 24 }, 37 request_handler => HTTP::Engine::Middleware::DoCoMoGUID->wrap($code), 25 38 }, 26 39 )->run(HTTP::Request->new( GET => '/' )); … … 34 47 --- input 35 48 <a href="/foo">bar</a> 36 --- expected : <a href="/foo?guid=ON">bar</a>37 49 --- expected 50 <a href="/foo?guid=ON">bar</a> 38 51 === 39 52 --- input 40 53 <a href="http://192.168.1.3/?page=1">< 2008-05-18</a> 41 --- expected : <a href="http://192.168.1.3/?page=1&guid=ON">< 2008-05-18</a>42 54 --- expected 55 <a href="http://192.168.1.3/?page=1&guid=ON">< 2008-05-18</a> -
lang/perl/HTTP-Engine-Middleware/trunk/t/12_plugin_mobile_attribute.t
r11960 r29266 2 2 use warnings; 3 3 use Test::More; 4 5 eval { 6 require HTTP::Engine; 7 }; 8 plan skip_all => "HTTP::Engine is not installed." if $@; 4 9 5 10 eval { … … 13 18 HTTP::Engine::Middleware::MobileAttribute->setup(); 14 19 15 { 16 my $req = HTTP::Engine::Request->new(); 20 sub do_test { 21 my $coderef = shift; 22 23 HTTP::Engine->new( 24 interface => { 25 module => 'Test', 26 request_handler => sub { 27 my $req = shift; 28 29 $coderef->($req); 30 31 HTTP::Engine::Response->new(body => 'OK'); 32 }, 33 }, 34 )->run(HTTP::Request->new(GET => 'http://example.org')); 35 } 36 37 do_test(sub { 38 my $req = shift; 17 39 $req->user_agent('IE'); 18 40 isa_ok $req->mobile_attribute, 'HTTP::MobileAttribute::Agent::NonMobile'; 19 } 41 }); 20 42 21 {22 my $req = HTTP::Engine::Request->new();43 do_test(sub { 44 my $req = shift; 23 45 $req->user_agent('DoCoMo/1.0/D501i'); 24 46 isa_ok $req->mobile_attribute, 'HTTP::MobileAttribute::Agent::DoCoMo'; 25 } 47 }); 26 48 -
lang/perl/HTTP-Engine-Middleware/trunk/t/15_middleware_reverseproxy.t
r11960 r29266 2 2 use warnings; 3 3 use Test::Base; 4 use HTTP::Engine middlewares => [ 5 qw/ReverseProxy/ 6 ]; 4 use HTTP::Engine; 5 use HTTP::Engine::Middleware::ReverseProxy; 6 use HTTP::Request; 7 use HTTP::Headers; 7 8 8 9 filters { input => [qw/yaml/] }; 9 10 10 plan tests => 23;11 plan tests => 17; 11 12 12 13 run { 13 14 my $block = shift; 14 local %ENV = %{ $block->input };15 local %ENV = (); 15 16 $ENV{REMOTE_ADDR} = '127.0.0.1'; 16 17 $ENV{REQUEST_METHOD} = 'GET'; … … 19 20 $ENV{QUERY_STRING} = 'foo=bar'; 20 21 22 my $headers = HTTP::Headers->new; 23 $headers->header(%{ $block->input }); 24 # $headers->header(HOST => 'example.com:80'); 21 25 HTTP::Engine->new( 22 26 interface => { 23 module => 'CGI', 24 request_handler => sub { 25 my $c = shift; 26 eval $block->expected; 27 die $@ if $@; 28 }, 27 module => 'Test', 28 request_handler => HTTP::Engine::Middleware::ReverseProxy->wrap(sub { 29 my $req = shift; 30 31 for my $attr ( qw/secure address/ ) { 32 if ( $block->$attr ) { 33 is($req->$attr, $block->$attr, $block->name . " of $attr"); 34 } 35 } 36 for my $url ( qw/uri base / ) { 37 if ( $block->$url ) { 38 is($req->$url->as_string, $block->$url, $block->name . " of $url"); 39 } 40 } 41 42 HTTP::Engine::Response->new(body => 'OK'); 43 }), 29 44 }, 30 )->run ;45 )->run(HTTP::Request->new(GET => 'http://example.com/?foo=bar', $headers),env => \%ENV); 31 46 }; 32 47 33 48 __END__ 34 49 35 === 50 === with https 36 51 --- input 37 HTTP_X_FORWARDED_HTTPS: ON 38 --- expected 39 is $c->req->secure, 1; 40 is $c->req->uri->as_string, "https://example.com:80?foo=bar"; 41 is $c->req->base->as_string, "https://example.com:80/"; 52 x-forwarded-https: on 53 --- secure: 1 54 --- base: https://example.com:80/ 55 --- uri: https://example.com:80/?foo=bar 56 57 === without https 58 --- input 59 x-forwarded-https: off 60 --- secure: 0 61 --- base: http://example.com:80/ 62 --- uri: http://example.com:80/?foo=bar 42 63 43 64 === 44 65 --- input 45 HTTP_X_FORWARDED_HTTPS: OFF 46 --- expected 47 is $c->req->secure, 0; 48 is $c->req->uri->as_string, "http://example.com?foo=bar"; 49 is $c->req->base->as_string, "http://example.com/"; 66 dummy: 1 67 --- secure: 0 68 --- base: http://example.com:80/ 69 --- uri: http://example.com:80/?foo=bar 50 70 51 === 71 === https with HTTP_X_FORWARDED_PROTO 52 72 --- input 53 DUMMY: 1 54 --- expected 55 is $c->req->secure, 0; 56 is $c->req->uri->as_string, "http://example.com?foo=bar"; 57 is $c->req->base->as_string, "http://example.com/"; 73 x-forwarded-proto: https 74 --- secure: 1 75 --- base: https://example.com:80/ 76 --- uri: https://example.com:80/?foo=bar 58 77 59 === 78 === with HTTP_X_FORWARDED_FOR 60 79 --- input 61 HTTP_X_FORWARDED_PROTO: https 62 --- expected 63 is $c->req->secure, 1; 64 is $c->req->uri->as_string, "https://example.com:80?foo=bar"; 65 is $c->req->base->as_string, "https://example.com:80/"; 80 x-forwarded-for: 192.168.3.2 81 --- address: 192.168.3.2 82 --- base: http://example.com:80/ 83 --- uri: http://example.com:80/?foo=bar 66 84 67 === 85 === with HTTP_X_FORWARDED_HOST 68 86 --- input 69 HTTP_X_FORWARDED_FOR: 192.168.3.2 70 --- expected 71 is $c->req->address, '192.168.3.2'; 72 is $c->req->uri->as_string, "http://example.com?foo=bar"; 73 is $c->req->base->as_string, "http://example.com/"; 87 x-forwarded-host: 192.168.1.2:5235 88 --- base: http://192.168.1.2:5235/ 89 --- uri: http://192.168.1.2:5235/?foo=bar 74 90 75 === 91 === with HTTP_X_FORWARDED_HOST and HTTP_X_FORWARDED_PORT 76 92 --- input 77 HTTP_X_FORWARDED_HOST: 192.168.1.2:5235 78 --- expected 79 is $ENV{HTTP_HOST}, '192.168.1.2'; 80 is $ENV{SERVER_PORT}, 5235; 81 is $c->req->uri->as_string, "http://192.168.1.2:5235?foo=bar"; 82 is $c->req->base->as_string, "http://192.168.1.2:5235/"; 93 x-forwarded-host: 192.168.1.5 94 x-forwarded-port: 1984 95 --- base: http://192.168.1.5:1984/ 96 --- uri: http://192.168.1.5:1984/?foo=bar 83 97 84 ===85 --- input86 HTTP_X_FORWARDED_HOST: 192.168.1.587 HTTP_X_FORWARDED_PORT: 198488 --- expected89 is $ENV{HTTP_HOST}, '192.168.1.5';90 is $ENV{SERVER_PORT}, 1984;91 is $c->req->uri->as_string, "http://192.168.1.5:1984?foo=bar";92 is $c->req->base->as_string, "http://192.168.1.5:1984/";93
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)