Changeset 4971
- Timestamp:
- 01/19/08 22:30:17 (5 years ago)
- Location:
- lang/perl/Moxy/trunk/lib/Moxy
- Files:
-
- 2 modified
-
Plugin/Application.pm (modified) (5 diffs)
-
Server/HTTPProxy.pm (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
lang/perl/Moxy/trunk/lib/Moxy/Plugin/Application.pm
r4970 r4971 73 73 74 74 my $ua = LWP::UserAgent->new( 75 agent => "Moxy/$Moxy::VERSION",76 75 timeout => $TIMEOUT, 77 76 max_redirects => 0, 78 77 ); 79 $ua->proxy(['http'] => $proxy_url);80 78 $ua; 81 79 } … … 85 83 86 84 if ($url) { 87 my $req = $src_req->clone; 88 $req->uri($url); 89 $req->header('Host' => URI->new($url)->host); 90 $req->header('Proxy-Authorization' => "Basic @{[ encode_base64 $auth ]}"); 91 my $ua = $class->_ua($base); 92 my $res = $ua->request($req); 85 # do proxy 86 my $res = $class->do_request($context, $src_req, $url, $auth); 93 87 $context->log(debug => '-- response status: ' . $res->code); 88 94 89 if ($res->code == 302) { 95 my $myres = HTTP::Response->new(200, 'Redirect by Moxy'); 96 $myres->header('Content-Type' => 'text/html; charset=utf8'); 97 $myres->content( 98 sprintf( 99 q{<html><head></head><body>Redirect to <a href="%s?q=%s">%s</a></body></html>}, 100 $base, uri_escape($res->header('Location')), 101 encode_entities($res->header('Location')) 102 ) 103 ); 104 return $myres; 90 # rewrite redirect 91 $res->header( 'Location' => $base . '?q=' 92 . uri_escape( $res->header('Location') ) ); 105 93 } else { 106 94 my $content_type = $res->header('Content-Type'); 107 if (!$content_type) { 108 warn "+++++++++++++++++++++++++++++++++++"; 109 warn "++ Content-Type missing ++ " . $res->code; 110 warn "+++++++++++++++++++++++++++++++++++"; 111 } elsif ($content_type =~ /html/i) { 95 if ($content_type =~ /html/i) { 112 96 $res->content( _rewrite($base, $res->content, $url) ); 113 97 } … … 115 99 return $res; 116 100 } else { 101 # please input url. 117 102 my $res = HTTP::Response->new(200, 'about:blank'); 118 103 $res->header('Content-Type' => 'text/html; charset=utf8'); … … 121 106 return $res; 122 107 } 108 } 109 110 sub do_request { 111 my ($class, $context, $src_req, $url, $auth) = @_; 112 113 # make request 114 my $req = $src_req->clone; 115 $req->uri($url); 116 $req->header('Host' => URI->new($url)->host); 117 118 $context->run_hook( 119 'request_filter_process_agent', 120 { request => $req, # HTTP::Request object 121 user => $auth, 122 } 123 ); 124 my $agent = $context->get_ua_info($req->header('User-Agent')); 125 my $carrier = $agent->{agent} ? HTTP::MobileAgent->new($agent->{agent})->carrier : 'N'; 126 for my $hook ('request_filter', "request_filter_$carrier") { 127 my $response = $context->run_hook_and_get_response( 128 $hook, 129 +{ 130 request => $req, # HTTP::Request object 131 agent => $agent, 132 user => $auth, 133 } 134 ); 135 if ($response) { 136 return $response; # finished 137 } 138 } 139 140 # do request 141 my $ua = $class->_ua; 142 my $response = $ua->request($req); 143 my $bodyref = \($response->content); 144 my $response_filter = sub { 145 my $key = shift; 146 for my $hook ($key, "${key}_$carrier") { 147 $context->run_hook( 148 $hook, 149 { response => $response, # HTTP::Response object 150 content_ref => $bodyref, # response body's scalarref. 151 agent => $agent, 152 user => $auth, 153 } 154 ); 155 } 156 }; 157 $response_filter->('response_filter'); 158 $response_filter->('response_filter_header'); 159 $response->content($$bodyref); 160 $response; 123 161 } 124 162 … … 205 243 =head1 DISCLAIMER 206 244 207 THIS MODULE IS STILL ALPHA QUALITY 245 THIS MODULE IS EXPERIMENTAL. STILL ALPHA QUALITY. 246 247 =head1 KNOWN BUGS 248 249 Basic 認証かかってると、うまく見えない。 208 250 209 251 =head1 AUTHOR -
lang/perl/Moxy/trunk/lib/Moxy/Server/HTTPProxy.pm
r4970 r4971 32 32 $proxy->push_filter( 33 33 mime => undef, 34 response => HTTP::Proxy::BodyFilter::complete->new,35 34 request => HTTP::Proxy::HeaderFilter::simple->new( 36 35 sub { … … 48 47 49 48 # password is ignored by Moxy. 50 my ($user, $pass) = $ _[0]->proxy->hop_headers->proxy_authorization_basic();49 my ($user, $pass) = $filter->proxy->hop_headers->proxy_authorization_basic(); 51 50 if ($user) { 52 $ _[0]->proxy->stash(user => $user);51 $filter->proxy->stash(user => $user); 53 52 } else { 54 53 my $response = HTTP::Response->new( 407, 'Moxy Authentication required' ); 55 54 $response->header('Proxy-Authenticate' => 'Basic realm="Moxy(password is dummy)"'); 56 return $ _[0]->proxy->response($response);55 return $filter->proxy->response($response); 57 56 } 58 57 59 58 $context->run_hook( 60 59 'request_filter_process_agent', 61 { request => $ _[2], # HTTP::Request object60 { request => $request, # HTTP::Request object 62 61 user => $user, 63 62 } … … 82 81 } 83 82 ), 83 response => HTTP::Proxy::BodyFilter::complete->new, 84 84 response => HTTP::Proxy::BodyFilter::simple->new( 85 sub { 86 my ($filter, $bodyref, $response) = @_; 87 88 my $agent = $context->get_ua_info($response->request->header('User-Agent')); 89 my $carrier = $agent->{agent} ? HTTP::MobileAgent->new($agent->{agent})->carrier : 'N'; 90 91 for my $hook ('response_filter', "response_filter_$carrier") { 92 $context->run_hook( 93 $hook, 94 { response => $response, # HTTP::Response object 95 content_ref => $bodyref, # response body's scalarref. 96 agent => $agent, 97 user => $filter->proxy->stash('user'), 98 } 99 ); 100 } 101 } 85 make_response_filter($context, 'response_filter') 102 86 ), 103 87 response => HTTP::Proxy::HeaderFilter::simple->new( 104 sub { 105 my ($filter, $bodyref, $response) = @_; 106 107 my $agent = $context->get_ua_info($response->request->header('User-Agent')); 108 my $carrier = $agent->{agent} ? HTTP::MobileAgent->new($agent->{agent})->carrier : 'N'; 109 110 for my $hook ('response_filter_header', "response_filter_header_$carrier") { 111 $context->run_hook( 112 $hook, 113 { response => $response, 114 content_ref => $bodyref, 115 agent => $agent, 116 user => $filter->proxy->stash('user'), 117 } 118 ); 119 } 120 } 88 make_response_filter($context, 'response_filter_header') 121 89 ), 122 90 ); … … 137 105 } 138 106 107 sub make_response_filter { 108 my ($context, $key) = @_; 109 110 return sub { 111 my ($filter, $bodyref, $response) = @_; 112 113 my $agent = $context->get_ua_info($response->request->header('User-Agent')); 114 my $carrier = $agent->{agent} ? HTTP::MobileAgent->new($agent->{agent})->carrier : 'N'; 115 116 for my $hook ($key, "${key}_$carrier") { 117 $context->run_hook( 118 $hook, 119 { response => $response, 120 content_ref => $bodyref, 121 agent => $agent, 122 user => $filter->proxy->stash('user'), 123 } 124 ); 125 } 126 }; 127 } 128 139 129 1; 140 130 __END__
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)