| 1 | package POE::Component::Client::HTTP::Request; | ||
| 2 | # vim: ts=2 sw=2 expandtab | ||
| 3 | $POE::Component::Client::HTTP::Request::VERSION = '0.949'; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | |||
| 7 | use POE; | ||
| 8 | |||
| 9 | use Carp; | ||
| 10 | use HTTP::Status; | ||
| 11 | use Errno qw(ETIMEDOUT); | ||
| 12 | |||
| 13 | BEGIN { | ||
| 14 | local $SIG{'__DIE__'} = 'DEFAULT'; | ||
| 15 | # Allow more finely grained timeouts if Time::HiRes is available. | ||
| 16 | # This code is also in POE::Component::Client::HTTP | ||
| 17 | eval { | ||
| 18 | require Time::HiRes; | ||
| 19 | Time::HiRes->import("time"); | ||
| 20 | }; | ||
| 21 | } | ||
| 22 | |||
| 23 | # Unique request ID, independent of wheel and timer IDs. | ||
| 24 | my $request_seq = 0; | ||
| 25 | |||
| 26 | use constant DEBUG => 0; | ||
| 27 | |||
| 28 | # TODO CONNECT - Add a flag to indicate whether to generate an HTTP | ||
| 29 | # CONNECT request for proxying, or to return REQ_HTTP_REQUEST. Add a | ||
| 30 | # method to update that flag. | ||
| 31 | |||
| 32 | use constant { | ||
| 33 | REQ_ID => 0, | ||
| 34 | REQ_POSTBACK => 1, | ||
| 35 | REQ_CONNECTION => 2, | ||
| 36 | REQ_HTTP_REQUEST => 3, | ||
| 37 | REQ_STATE => 4, | ||
| 38 | REQ_RESPONSE => 5, | ||
| 39 | REQ_BUFFER => 6, | ||
| 40 | REQ_OCTETS_GOT => 8, | ||
| 41 | REQ_TIMER => 9, | ||
| 42 | REQ_PROG_POSTBACK => 10, | ||
| 43 | REQ_USING_PROXY => 11, | ||
| 44 | REQ_HOST => 12, | ||
| 45 | REQ_PORT => 13, | ||
| 46 | REQ_HISTORY => 14, | ||
| 47 | REQ_START_TIME => 15, | ||
| 48 | REQ_FACTORY => 16, | ||
| 49 | REQ_CONN_ID => 17, | ||
| 50 | REQ_PEERNAME => 18, | ||
| 51 | }; | ||
| 52 | |||
| 53 | use constant { | ||
| 54 | RS_CONNECT => 0x01, # establishing a connection | ||
| 55 | RS_SENDING => 0x02, # sending request to server | ||
| 56 | RS_IN_HEAD => 0x04, # waiting for or receiving headers | ||
| 57 | RS_REDIRECTED => 0x08, # request has been redirected | ||
| 58 | RS_IN_CONTENT => 0x20, # waiting for or receiving content | ||
| 59 | RS_DONE => 0x40, # received full content | ||
| 60 | RS_POSTED => 0x80, # we have posted back a response | ||
| 61 | }; | ||
| 62 | |||
| 63 | |||
| 64 | sub import { | ||
| 65 | my ($class) = shift; | ||
| 66 | |||
| 67 | my $package = caller(); | ||
| 68 | |||
| 69 | foreach my $tag (@_) { | ||
| 70 | if ($tag eq ':fields') { | ||
| 71 | foreach my $sub ( | ||
| 72 | qw( | ||
| 73 | REQ_ID REQ_POSTBACK REQ_CONNECTION REQ_HTTP_REQUEST REQ_STATE | ||
| 74 | REQ_RESPONSE REQ_BUFFER REQ_OCTETS_GOT REQ_TIMER | ||
| 75 | REQ_PROG_POSTBACK REQ_USING_PROXY REQ_HOST REQ_PORT | ||
| 76 | REQ_HISTORY REQ_START_TIME REQ_CONN_ID REQ_PEERNAME | ||
| 77 | ) | ||
| 78 | ) { | ||
| 79 | no strict 'refs'; | ||
| 80 | *{$package . "::$sub"} = \&$sub; | ||
| 81 | } | ||
| 82 | } | ||
| 83 | |||
| 84 | if ($tag eq ':states') { | ||
| 85 | foreach my $sub ( | ||
| 86 | qw( | ||
| 87 | RS_CONNECT RS_SENDING RS_IN_HEAD RS_REDIRECTED | ||
| 88 | RS_IN_CONTENT RS_DONE RS_POSTED | ||
| 89 | ) | ||
| 90 | ) { | ||
| 91 | no strict 'refs'; | ||
| 92 | *{$package . "::$sub"} = \&$sub; | ||
| 93 | } | ||
| 94 | } | ||
| 95 | } | ||
| 96 | } | ||
| 97 | |||
| 98 | |||
| 99 | sub ID { return $_[0][REQ_ID] } | ||
| 100 | |||
| 101 | |||
| 102 | sub new { | ||
| 103 | my $class = shift; | ||
| 104 | |||
| 105 | croak __PACKAGE__ . "expects its arguments to be key/value pairs" if @_ & 1; | ||
| 106 | my %params = @_; | ||
| 107 | |||
| 108 | croak "need a Request parameter" unless (defined $params{'Request'}); | ||
| 109 | croak "Request must be a HTTP::Request object" | ||
| 110 | unless (UNIVERSAL::isa ($params{'Request'}, "HTTP::Request")); | ||
| 111 | |||
| 112 | croak "need a Factory parameter" unless (defined $params{'Factory'}); | ||
| 113 | |||
| 114 | my ($http_request, $postback, $progress, $factory) = | ||
| 115 | @params{qw(Request Postback Progress Factory)}; | ||
| 116 | |||
| 117 | my $request_id = ++$request_seq; | ||
| 118 | DEBUG and warn "REQ: creating a request ($request_id)"; | ||
| 119 | |||
| 120 | # Get the host and port from the request object. | ||
| 121 | my ($host, $port, $scheme, $using_proxy); | ||
| 122 | |||
| 123 | eval { | ||
| 124 | $host = $http_request->uri()->host(); | ||
| 125 | $port = $http_request->uri()->port(); | ||
| 126 | $scheme = $http_request->uri()->scheme(); | ||
| 127 | }; | ||
| 128 | croak "Not a usable Request: $@" if ($@); | ||
| 129 | |||
| 130 | # Add a host header if one isn't included. Must do this before | ||
| 131 | # we reset the $host for the proxy! | ||
| 132 | unless ( | ||
| 133 | defined $http_request->header('Host') and | ||
| 134 | length $http_request->header('Host') | ||
| 135 | ) { | ||
| 136 | my $error = _set_host_header($http_request); | ||
| 137 | croak "Can't set Host header: $error" if $error; | ||
| 138 | } | ||
| 139 | |||
| 140 | if (defined $params{Proxy}) { | ||
| 141 | # This request qualifies for proxying. Replace the host and port | ||
| 142 | # with the proxy's host and port. This comes after the Host: | ||
| 143 | # header is set, so it doesn't break the request object. | ||
| 144 | ($host, $port) = @{$params{Proxy}->[rand @{$params{Proxy}}]}; | ||
| 145 | |||
| 146 | $using_proxy = 1; | ||
| 147 | } | ||
| 148 | else { | ||
| 149 | $using_proxy = 0; | ||
| 150 | } | ||
| 151 | |||
| 152 | # Build the request. | ||
| 153 | my $self = [ | ||
| 154 | $request_id, # REQ_ID | ||
| 155 | $postback, # REQ_POSTBACK | ||
| 156 | undef, # REQ_CONNECTION | ||
| 157 | $http_request, # REQ_HTTP_REQUEST | ||
| 158 | RS_CONNECT, # REQ_STATE | ||
| 159 | undef, # REQ_RESPONSE | ||
| 160 | '', # REQ_BUFFER | ||
| 161 | undef, # unused | ||
| 162 | 0, # REQ_OCTETS_GOT | ||
| 163 | undef, # REQ_TIMER | ||
| 164 | $progress, # REQ_PROG_POSTBACK | ||
| 165 | $using_proxy, # REQ_USING_PROXY | ||
| 166 | $host, # REQ_HOST | ||
| 167 | $port, # REQ_PORT | ||
| 168 | undef, # REQ_HISTORY | ||
| 169 | time(), # REQ_START_TIME | ||
| 170 | $factory, # REQ_FACTORY | ||
| 171 | undef, # REQ_CONN_ID | ||
| 172 | undef, # REQ_PEERNAME | ||
| 173 | ]; | ||
| 174 | return bless $self, $class; | ||
| 175 | } | ||
| 176 | |||
| 177 | |||
| 178 | sub return_response { | ||
| 179 | my ($self) = @_; | ||
| 180 | |||
| 181 | DEBUG and warn "in return_response ", sprintf ("0x%02X", $self->[REQ_STATE]); | ||
| 182 | return if ($self->[REQ_STATE] & RS_POSTED); | ||
| 183 | my $response = $self->[REQ_RESPONSE]; | ||
| 184 | |||
| 185 | # If we have a cookie jar, have it frob our headers. LWP rocks! | ||
| 186 | $self->[REQ_FACTORY]->frob_cookies ($response); | ||
| 187 | |||
| 188 | # If we're done, send back the HTTP::Response object, which | ||
| 189 | # is filled with content if we aren't streaming, or empty | ||
| 190 | # if we are. that there's no ARG1 lets the client know we're done | ||
| 191 | # with the content in the latter case | ||
| 192 | if ($self->[REQ_STATE] & RS_DONE) { | ||
| 193 | DEBUG and warn "done; returning $response for ", $self->[REQ_ID]; | ||
| 194 | $self->[REQ_POSTBACK]->($self->[REQ_RESPONSE]); | ||
| 195 | $self->[REQ_STATE] |= RS_POSTED; | ||
| 196 | #warn "state is now ", $self->[REQ_STATE]; | ||
| 197 | } | ||
| 198 | elsif ($self->[REQ_STATE] & RS_IN_CONTENT) { | ||
| 199 | # If we are streaming, send the chunk back to the client session. | ||
| 200 | # Otherwise add the new octets to the response's content. | ||
| 201 | # This should only add up to content-length octets total! | ||
| 202 | if ($self->[REQ_FACTORY]->is_streaming) { | ||
| 203 | DEBUG and warn "returning partial $response"; | ||
| 204 | $self->[REQ_POSTBACK]->($self->[REQ_RESPONSE], $self->[REQ_BUFFER]); | ||
| 205 | } | ||
| 206 | else { | ||
| 207 | DEBUG and warn "adding to $response"; | ||
| 208 | $self->[REQ_RESPONSE]->add_content($self->[REQ_BUFFER]); | ||
| 209 | } | ||
| 210 | } | ||
| 211 | $self->[REQ_BUFFER] = ''; | ||
| 212 | } | ||
| 213 | |||
| 214 | |||
| 215 | sub add_eof { | ||
| 216 | my ($self) = @_; | ||
| 217 | |||
| 218 | return if ($self->[REQ_STATE] & RS_POSTED); | ||
| 219 | |||
| 220 | unless (defined $self->[REQ_RESPONSE]) { | ||
| 221 | # XXX I don't know if this is actually used | ||
| 222 | $self->error(400, "incomplete response a " . $self->[REQ_ID]); | ||
| 223 | return; | ||
| 224 | } | ||
| 225 | |||
| 226 | # RFC 2616: "If a message is received with both a Transfer-Encoding | ||
| 227 | # header field and a Content-Length header field, the latter MUST be | ||
| 228 | # ignored." | ||
| 229 | # | ||
| 230 | # Google returns a Content-Length header with its HEAD request, | ||
| 231 | # generating "incomplete response" errors. Added a special case to | ||
| 232 | # ignore content for HEAD requests. This may thwart keep-alive, | ||
| 233 | # however. | ||
| 234 | |||
| 235 | if ( | ||
| 236 | $self->[REQ_HTTP_REQUEST]->method() ne "HEAD" and | ||
| 237 | defined $self->[REQ_RESPONSE]->content_length and | ||
| 238 | not defined $self->[REQ_RESPONSE]->header("Transfer-Encoding") and | ||
| 239 | $self->[REQ_OCTETS_GOT] < $self->[REQ_RESPONSE]->content_length | ||
| 240 | ) { | ||
| 241 | DEBUG and warn( | ||
| 242 | "got " . $self->[REQ_OCTETS_GOT] . " of " . | ||
| 243 | $self->[REQ_RESPONSE]->content_length | ||
| 244 | ); | ||
| 245 | |||
| 246 | $self->error( | ||
| 247 | 400, | ||
| 248 | "incomplete response b " . $self->[REQ_ID] . ". Wanted " . | ||
| 249 | $self->[REQ_RESPONSE]->content_length() . " octets. Got " . | ||
| 250 | $self->[REQ_OCTETS_GOT] . "." | ||
| 251 | ); | ||
| 252 | } | ||
| 253 | else { | ||
| 254 | $self->[REQ_STATE] |= RS_DONE; | ||
| 255 | $self->return_response(); | ||
| 256 | } | ||
| 257 | } | ||
| 258 | |||
| 259 | |||
| 260 | sub add_content { | ||
| 261 | my ($self, $data) = @_; | ||
| 262 | |||
| 263 | if (ref $data) { | ||
| 264 | $self->[REQ_STATE] = RS_DONE; | ||
| 265 | $data->scan (sub {$self->[REQ_RESPONSE]->header (@_) }); | ||
| 266 | return 1; | ||
| 267 | } | ||
| 268 | |||
| 269 | $self->[REQ_BUFFER] .= $data; | ||
| 270 | |||
| 271 | # Count how many octets we've received. -><- This may fail on | ||
| 272 | # perl 5.8 if the input has been identified as Unicode. Then | ||
| 273 | # again, the C<use bytes> in Driver::SysRW may have untainted the | ||
| 274 | # data... or it may have just changed the semantics of length() | ||
| 275 | # therein. If it's done the former, then we're safe. Otherwise | ||
| 276 | # we also need to C<use bytes>. | ||
| 277 | # TODO: write test(s) for this. | ||
| 278 | |||
| 279 | my $this_chunk_length = length($self->[REQ_BUFFER]); | ||
| 280 | $self->[REQ_OCTETS_GOT] += $this_chunk_length; | ||
| 281 | |||
| 282 | my $max = $self->[REQ_FACTORY]->max_response_size(); | ||
| 283 | |||
| 284 | DEBUG and warn( | ||
| 285 | "REQ: request ", $self->ID, | ||
| 286 | " received $self->[REQ_OCTETS_GOT] bytes; maximum is $max" | ||
| 287 | ); | ||
| 288 | |||
| 289 | # Fail if we've gone over the maximum content size to return. | ||
| 290 | if (defined $max and $self->[REQ_OCTETS_GOT] > $max) { | ||
| 291 | $self->error( | ||
| 292 | 406, | ||
| 293 | "Response content is longer than specified MaxSize of $max. " . | ||
| 294 | "Use range requests to retrieve specific amounts of content." | ||
| 295 | ); | ||
| 296 | |||
| 297 | $self->[REQ_STATE] |= RS_DONE; | ||
| 298 | $self->[REQ_STATE] &= ~RS_IN_CONTENT; | ||
| 299 | return 1; | ||
| 300 | } | ||
| 301 | |||
| 302 | # keep this for the progress callback (it gets cleared in return_response | ||
| 303 | # as I say below, this needs to go away. | ||
| 304 | my $buffer = $self->[REQ_BUFFER]; | ||
| 305 | |||
| 306 | $self->return_response; | ||
| 307 | DEBUG and do { | ||
| 308 | warn( | ||
| 309 | "REQ: request ", $self->ID, | ||
| 310 | " got $this_chunk_length octets of content..." | ||
| 311 | ); | ||
| 312 | |||
| 313 | warn( | ||
| 314 | "REQ: request ", $self->ID, " has $self->[REQ_OCTETS_GOT]", | ||
| 315 | ( | ||
| 316 | $self->[REQ_RESPONSE]->content_length() | ||
| 317 | ? ( " out of " . $self->[REQ_RESPONSE]->content_length() ) | ||
| 318 | : "" | ||
| 319 | ), | ||
| 320 | " octets" | ||
| 321 | ); | ||
| 322 | }; | ||
| 323 | |||
| 324 | if ($self->[REQ_RESPONSE]->content_length) { | ||
| 325 | |||
| 326 | # Report back progress | ||
| 327 | $self->[REQ_PROG_POSTBACK]->( | ||
| 328 | $self->[REQ_OCTETS_GOT], | ||
| 329 | $self->[REQ_RESPONSE]->content_length, | ||
| 330 | #TODO: ugh. this is stupid. Must remove/deprecate! | ||
| 331 | $buffer, | ||
| 332 | ) if ($self->[REQ_PROG_POSTBACK]); | ||
| 333 | |||
| 334 | # Stop reading when we have enough content. -><- Should never be | ||
| 335 | # greater than our content length. | ||
| 336 | if ($self->[REQ_OCTETS_GOT] >= $self->[REQ_RESPONSE]->content_length) { | ||
| 337 | DEBUG and warn( | ||
| 338 | "REQ: request ", $self->ID, " has a full response... moving to done." | ||
| 339 | ); | ||
| 340 | $self->[REQ_STATE] |= RS_DONE; | ||
| 341 | $self->[REQ_STATE] &= ~RS_IN_CONTENT; | ||
| 342 | return 1; | ||
| 343 | } | ||
| 344 | } | ||
| 345 | |||
| 346 | return 0; | ||
| 347 | } | ||
| 348 | |||
| 349 | |||
| 350 | ### Methods to manage the request's timer. | ||
| 351 | |||
| 352 | |||
| 353 | sub timer { | ||
| 354 | my ($self, $timer) = @_; | ||
| 355 | |||
| 356 | # do it this way so we can set REQ_TIMER to undef | ||
| 357 | if (@_ == 2) { | ||
| 358 | die "overwriting timer $self->[REQ_TIMER]" if $self->[REQ_TIMER]; | ||
| 359 | $self->[REQ_TIMER] = $timer; | ||
| 360 | } | ||
| 361 | return $self->[REQ_TIMER]; | ||
| 362 | } | ||
| 363 | |||
| 364 | |||
| 365 | sub create_timer { | ||
| 366 | my ($self, $timeout) = @_; | ||
| 367 | |||
| 368 | # remove old timeout first | ||
| 369 | my $kernel = $POE::Kernel::poe_kernel; | ||
| 370 | |||
| 371 | my $seconds = $timeout - (time() - $self->[REQ_START_TIME]); | ||
| 372 | $self->[REQ_TIMER] = $kernel->delay_set( | ||
| 373 | got_timeout => $seconds, $self->ID | ||
| 374 | ); | ||
| 375 | DEBUG and warn( | ||
| 376 | "TKO: request ", $self->ID, | ||
| 377 | " has timer $self->[REQ_TIMER] going off in $seconds seconds\n" | ||
| 378 | ); | ||
| 379 | } | ||
| 380 | |||
| 381 | |||
| 382 | sub remove_timeout { | ||
| 383 | my ($self) = @_; | ||
| 384 | |||
| 385 | my $alarm_id = $self->[REQ_TIMER]; | ||
| 386 | if (defined $alarm_id) { | ||
| 387 | my $kernel = $POE::Kernel::poe_kernel; | ||
| 388 | DEBUG and warn "REQ: Removing timer $alarm_id"; | ||
| 389 | $kernel->alarm_remove($alarm_id); | ||
| 390 | $self->[REQ_TIMER] = undef; | ||
| 391 | } | ||
| 392 | } | ||
| 393 | |||
| 394 | |||
| 395 | sub postback { | ||
| 396 | my ($self, $postback) = @_; | ||
| 397 | |||
| 398 | if (defined $postback) { | ||
| 399 | DEBUG and warn "REQ: modifying postback"; | ||
| 400 | $self->[REQ_POSTBACK] = $postback; | ||
| 401 | } | ||
| 402 | return $self->[REQ_POSTBACK]; | ||
| 403 | } | ||
| 404 | |||
| 405 | |||
| 406 | sub _set_host_header { | ||
| 407 | my ($request) = @_; | ||
| 408 | my $uri = $request->uri; | ||
| 409 | |||
| 410 | my ($new_host, $new_port); | ||
| 411 | eval { | ||
| 412 | $new_host = $uri->host(); | ||
| 413 | $new_port = $uri->port(); | ||
| 414 | # Only include the port if it's nonstandard. | ||
| 415 | if ($new_port == 80 || $new_port == 443) { | ||
| 416 | $request->header( Host => $new_host ); | ||
| 417 | } | ||
| 418 | else { | ||
| 419 | $request->header( Host => "$new_host:$new_port" ); | ||
| 420 | } | ||
| 421 | }; | ||
| 422 | |||
| 423 | # Return Boolean state of the eval. | ||
| 424 | return $@; | ||
| 425 | } | ||
| 426 | |||
| 427 | |||
| 428 | sub does_redirect { | ||
| 429 | my ($self, $last) = @_; | ||
| 430 | |||
| 431 | if (defined $last) { | ||
| 432 | $self->[REQ_HISTORY] = $last; | ||
| 433 | # delete OLD timeout | ||
| 434 | #my $alarm_id = $last->[REQ_TIMEOUT]; | ||
| 435 | #DEBUG and warn "RED: Removing old timeout $alarm_id\n"; | ||
| 436 | #$POE::Kernel::poe_kernel->alarm_remove ($alarm_id); | ||
| 437 | } | ||
| 438 | |||
| 439 | return defined $self->[REQ_HISTORY]; | ||
| 440 | } | ||
| 441 | |||
| 442 | |||
| 443 | sub check_redirect { | ||
| 444 | my ($self) = @_; | ||
| 445 | |||
| 446 | my $max = $self->[REQ_FACTORY]->max_redirect_count; | ||
| 447 | |||
| 448 | if (defined $self->[REQ_HISTORY]) { | ||
| 449 | $self->[REQ_RESPONSE]->previous($self->[REQ_HISTORY]->[REQ_RESPONSE]); | ||
| 450 | } | ||
| 451 | |||
| 452 | return undef unless ($self->[REQ_RESPONSE]->is_redirect); | ||
| 453 | |||
| 454 | # Make sure to frob any cookies set. Redirect cookies are cookies, too! | ||
| 455 | $self->[REQ_FACTORY]->frob_cookies($self->[REQ_RESPONSE]); | ||
| 456 | |||
| 457 | my $location_uri = $self->[REQ_RESPONSE]->header('Location'); | ||
| 458 | |||
| 459 | DEBUG and warn "REQ: Preparing redirect to $location_uri"; | ||
| 460 | my $base = $self->[REQ_RESPONSE]->base(); | ||
| 461 | $location_uri = URI->new($location_uri, $base)->abs($base); | ||
| 462 | DEBUG and warn "RED: Actual redirect uri is $location_uri"; | ||
| 463 | |||
| 464 | my $prev = $self; | ||
| 465 | my $history = 0; | ||
| 466 | while ($prev = $prev->[REQ_HISTORY]) { | ||
| 467 | last if ++$history > $max; | ||
| 468 | } | ||
| 469 | |||
| 470 | if ($history >= $max) { | ||
| 471 | #$self->[REQ_STATE] |= RS_DONE; | ||
| 472 | DEBUG and warn "RED: Too much redirection"; | ||
| 473 | } | ||
| 474 | else { # All fine, yield new request and mark this disabled. | ||
| 475 | my $newrequest = $self->[REQ_HTTP_REQUEST]->clone(); | ||
| 476 | |||
| 477 | # Sanitize new request per rt #30400. | ||
| 478 | # TODO - What other headers are security risks? | ||
| 479 | $newrequest->remove_header('Cookie'); | ||
| 480 | |||
| 481 | DEBUG and warn "RED: new request $newrequest"; | ||
| 482 | $newrequest->uri($location_uri); | ||
| 483 | |||
| 484 | # Don't change the Host header on a relative redirect. This | ||
| 485 | # allows the HTTP::Request's Host to remain intact, per | ||
| 486 | # rt.cpan.org #63990. | ||
| 487 | if (defined $location_uri->scheme()) { | ||
| 488 | DEBUG and warn "RED: redirecting to absolute location $location_uri"; | ||
| 489 | _set_host_header($newrequest); | ||
| 490 | } | ||
| 491 | else { | ||
| 492 | DEBUG and warn "RED: no new Host for relative redirect to $location_uri"; | ||
| 493 | } | ||
| 494 | |||
| 495 | $self->[REQ_STATE] = RS_REDIRECTED; | ||
| 496 | DEBUG and warn "RED: new request $newrequest"; | ||
| 497 | return $newrequest; | ||
| 498 | } | ||
| 499 | return undef; | ||
| 500 | } | ||
| 501 | |||
| 502 | |||
| 503 | sub send_to_wheel { | ||
| 504 | my ($self) = @_; | ||
| 505 | |||
| 506 | $self->[REQ_STATE] = RS_SENDING; | ||
| 507 | |||
| 508 | my $http_request = $self->[REQ_HTTP_REQUEST]; | ||
| 509 | |||
| 510 | # MEXNIX 2002-06-01: Check for proxy. Request query is a bit | ||
| 511 | # different... | ||
| 512 | |||
| 513 | my $request_uri; | ||
| 514 | if ($self->[REQ_USING_PROXY]) { | ||
| 515 | $request_uri = $http_request->uri()->canonical(); | ||
| 516 | } | ||
| 517 | else { | ||
| 518 | $request_uri = $http_request->uri()->canonical()->path_query(); | ||
| 519 | } | ||
| 520 | |||
| 521 | my $request_string = ( | ||
| 522 | $http_request->method() . ' ' . | ||
| 523 | $request_uri . ' ' . | ||
| 524 | $http_request->protocol() . "\x0D\x0A" . | ||
| 525 | $http_request->headers_as_string("\x0D\x0A") . "\x0D\x0A" | ||
| 526 | ); | ||
| 527 | |||
| 528 | if ( !ref $http_request->content() ) { | ||
| 529 | $request_string .= $http_request->content(); # . "\x0D\x0A" | ||
| 530 | } | ||
| 531 | |||
| 532 | DEBUG and do { | ||
| 533 | my $formatted_request_string = $request_string; | ||
| 534 | $formatted_request_string =~ s/([^\n])$/$1\n/; | ||
| 535 | $formatted_request_string =~ s/^/| /mg; | ||
| 536 | warn ",----- SENDING REQUEST ", '-' x 56, "\n"; | ||
| 537 | warn $formatted_request_string; | ||
| 538 | warn "`", '-' x 78, "\n"; | ||
| 539 | }; | ||
| 540 | |||
| 541 | $self->[REQ_CONNECTION]->wheel->put ($request_string); | ||
| 542 | } | ||
| 543 | |||
| 544 | |||
| 545 | sub wheel { | ||
| 546 | my ($self) = @_; | ||
| 547 | |||
| 548 | # FIXME - We don't support older versions of POE. Remove this chunk | ||
| 549 | # of code when we're not fixing something else. | ||
| 550 | # | ||
| 551 | #if (defined $new_wheel) { | ||
| 552 | # Switch wheels. This is cumbersome, but it works around a bug in | ||
| 553 | # older versions of POE. | ||
| 554 | # $self->[REQ_WHEEL] = undef; | ||
| 555 | # $self->[REQ_WHEEL] = $new_wheel; | ||
| 556 | #} | ||
| 557 | |||
| 558 | return unless $self->[REQ_CONNECTION]; | ||
| 559 | return $self->[REQ_CONNECTION]->wheel; | ||
| 560 | } | ||
| 561 | |||
| 562 | |||
| 563 | sub error { | ||
| 564 | my ($self, $code, $message) = @_; | ||
| 565 | |||
| 566 | my $nl = "\n"; | ||
| 567 | |||
| 568 | my $http_msg = status_message($code); | ||
| 569 | my $r = HTTP::Response->new($code, $http_msg, [ 'X-PCCH-Errmsg', $message ]); | ||
| 570 | my $m = ( | ||
| 571 | "<html>$nl" | ||
| 572 | . "<HEAD><TITLE>Error: $http_msg</TITLE></HEAD>$nl" | ||
| 573 | . "<BODY>$nl" | ||
| 574 | . "<H1>Error: $http_msg</H1>$nl" | ||
| 575 | . "$message$nl" | ||
| 576 | . "<small>This is a client error, not a server error.</small>$nl" | ||
| 577 | . "</BODY>$nl" | ||
| 578 | . "</HTML>$nl" | ||
| 579 | ); | ||
| 580 | |||
| 581 | $r->content($m); | ||
| 582 | $r->request($self->[REQ_HTTP_REQUEST]); | ||
| 583 | $self->[REQ_POSTBACK]->($r); | ||
| 584 | $self->[REQ_STATE] |= RS_POSTED; | ||
| 585 | } | ||
| 586 | |||
| 587 | |||
| 588 | sub connect_error { | ||
| 589 | my ($self, $operation, $errnum, $errstr) = @_; | ||
| 590 | |||
| 591 | my $host = $self->[REQ_HOST]; | ||
| 592 | my $port = $self->[REQ_PORT]; | ||
| 593 | |||
| 594 | if ($operation eq "connect" and $errnum == ETIMEDOUT) { | ||
| 595 | $self->error(408, "Connection to $host:$port failed: timeout"); | ||
| 596 | } | ||
| 597 | else { | ||
| 598 | $self->error( | ||
| 599 | RC_INTERNAL_SERVER_ERROR, | ||
| 600 | "Connection to $host:$port failed: $operation error $errnum: $errstr" | ||
| 601 | ); | ||
| 602 | } | ||
| 603 | |||
| 604 | return; | ||
| 605 | } | ||
| 606 | |||
| 607 | |||
| 608 | sub host { $_[0][REQ_HOST] } | ||
| 609 | |||
| 610 | |||
| 611 | sub port { $_[0][REQ_PORT] } | ||
| 612 | |||
| 613 | |||
| 614 | sub close_connection { | ||
| 615 | my ($self) = @_; | ||
| 616 | return unless defined $self->[REQ_CONNECTION]; | ||
| 617 | $self->[REQ_CONNECTION]->close(); | ||
| 618 | $self->[REQ_CONNECTION] = undef; | ||
| 619 | } | ||
| 620 | |||
| 621 | |||
| 622 | sub scheme { | ||
| 623 | my $self = shift; | ||
| 624 | |||
| 625 | $self->[REQ_USING_PROXY] ? 'http' : $self->[REQ_HTTP_REQUEST]->uri->scheme; | ||
| 626 | } | ||
| 627 | |||
| 628 | |||
| 629 | sub DESTROY { | ||
| 630 | my ($self) = @_; | ||
| 631 | |||
| 632 | delete $self->[REQ_CONNECTION]; | ||
| 633 | delete $self->[REQ_FACTORY]; | ||
| 634 | } | ||
| 635 | |||
| 636 | 1; | ||
| 637 | |||
| 638 | __END__ |