| 1 | package POE::Component::Client::HTTP::RequestFactory; | ||
| 2 | # vim: ts=2 sw=2 expandtab | ||
| 3 | $POE::Component::Client::HTTP::RequestFactory::VERSION = '0.949'; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | |||
| 7 | use Carp; | ||
| 8 | use POE::Component::Client::HTTP::Request; | ||
| 9 | use POE::Component::Client::HTTP; | ||
| 10 | |||
| 11 | use constant { | ||
| 12 | FCT_AGENT => 0, | ||
| 13 | FCT_STREAMING => 1, | ||
| 14 | FCT_MAXSIZE => 2, | ||
| 15 | FCT_PROTOCOL => 3, | ||
| 16 | FCT_COOKIEJAR => 4, | ||
| 17 | FCT_FROM => 5, | ||
| 18 | FCT_NOPROXY => 6, | ||
| 19 | FCT_HTTP_PROXY => 7, | ||
| 20 | FCT_FOLLOWREDIRECTS => 8, | ||
| 21 | FCT_TIMEOUT => 9, | ||
| 22 | }; | ||
| 23 | |||
| 24 | use constant DEBUG => 0; | ||
| 25 | |||
| 26 | use constant DEFAULT_BLOCK_SIZE => 4096; | ||
| 27 | |||
| 28 | |||
| 29 | =head1 NAME | ||
| 30 | |||
| 31 | POE::Component::Client::HTTP::RequestFactory - an HTTP request factory object | ||
| 32 | |||
| 33 | =head1 VERSION | ||
| 34 | |||
| 35 | version 0.949 | ||
| 36 | |||
| 37 | =head1 SYNOPSIS | ||
| 38 | |||
| 39 | # Used internally by POE::Component::Client::HTTP | ||
| 40 | |||
| 41 | =head1 CONSTRUCTOR | ||
| 42 | |||
| 43 | =head2 new | ||
| 44 | |||
| 45 | Create a new request factory object. It expects its parameters in a | ||
| 46 | hashref. | ||
| 47 | |||
| 48 | The following parameters are accepted. They are explained in detail | ||
| 49 | in L<POE::Component::Client::HTTP>. | ||
| 50 | |||
| 51 | =over 4 | ||
| 52 | |||
| 53 | =item | ||
| 54 | |||
| 55 | Agent | ||
| 56 | |||
| 57 | =item | ||
| 58 | |||
| 59 | MaxSize | ||
| 60 | |||
| 61 | =item | ||
| 62 | |||
| 63 | Streaming | ||
| 64 | |||
| 65 | =item | ||
| 66 | |||
| 67 | Protocol | ||
| 68 | |||
| 69 | =item | ||
| 70 | |||
| 71 | From | ||
| 72 | |||
| 73 | =item | ||
| 74 | |||
| 75 | CookieJar | ||
| 76 | |||
| 77 | =item | ||
| 78 | |||
| 79 | NoProxy | ||
| 80 | |||
| 81 | =item | ||
| 82 | |||
| 83 | Proxy | ||
| 84 | |||
| 85 | =item | ||
| 86 | |||
| 87 | FollowRedirects | ||
| 88 | |||
| 89 | =item | ||
| 90 | |||
| 91 | Timeout | ||
| 92 | |||
| 93 | =back | ||
| 94 | |||
| 95 | =cut | ||
| 96 | |||
| 97 | |||
| 98 | sub new { | ||
| 99 | my ($class, $params) = @_; | ||
| 100 | |||
| 101 | croak __PACKAGE__ . "expects its arguments in a hashref" | ||
| 102 | unless (!defined ($params) or ref($params) eq 'HASH'); | ||
| 103 | |||
| 104 | # Accept an agent, or a reference to a list of agents. | ||
| 105 | my $agent = delete $params->{Agent}; | ||
| 106 | $agent = [] unless defined $agent; | ||
| 107 | $agent = [ $agent ] unless ref($agent); | ||
| 108 | unless (ref($agent) eq "ARRAY") { | ||
| 109 | croak "Agent must be a scalar or a reference to a list of agent strings"; | ||
| 110 | } | ||
| 111 | |||
| 112 | my $v = $POE::Component::Client::HTTP::VERSION; | ||
| 113 | $v = "0.000" unless defined $v; | ||
| 114 | |||
| 115 | push( | ||
| 116 | @$agent, | ||
| 117 | sprintf( | ||
| 118 | 'POE-Component-Client-HTTP/%s (perl; N; POE; en; rv:%f)', | ||
| 119 | $v, $v | ||
| 120 | ) | ||
| 121 | ) unless @$agent; | ||
| 122 | |||
| 123 | my $max_size = delete $params->{MaxSize}; | ||
| 124 | |||
| 125 | my $streaming = delete $params->{Streaming}; | ||
| 126 | |||
| 127 | my $protocol = delete $params->{Protocol}; | ||
| 128 | $protocol = 'HTTP/1.1' unless defined $protocol and length $protocol; | ||
| 129 | |||
| 130 | my $cookie_jar = delete $params->{CookieJar}; | ||
| 131 | my $from = delete $params->{From}; | ||
| 132 | my $no_proxy = delete $params->{NoProxy}; | ||
| 133 | my $proxy = delete $params->{Proxy}; | ||
| 134 | my $follow_redirects = delete $params->{FollowRedirects} || 0; | ||
| 135 | my $timeout = delete $params->{Timeout}; | ||
| 136 | |||
| 137 | # Process HTTP_PROXY and NO_PROXY environment variables. | ||
| 138 | |||
| 139 | $proxy = $ENV{HTTP_PROXY} || $ENV{http_proxy} unless defined $proxy; | ||
| 140 | $no_proxy = $ENV{NO_PROXY} || $ENV{no_proxy} unless defined $no_proxy; | ||
| 141 | |||
| 142 | # Translate environment variable formats into internal versions. | ||
| 143 | |||
| 144 | $class->parse_proxy($proxy) if defined $proxy; | ||
| 145 | |||
| 146 | if (defined $no_proxy) { | ||
| 147 | unless (ref($no_proxy) eq 'ARRAY') { | ||
| 148 | $no_proxy = [ split(/\s*\,\s*/, $no_proxy) ]; | ||
| 149 | } | ||
| 150 | } | ||
| 151 | |||
| 152 | $timeout = 180 unless (defined $timeout and $timeout > 0); | ||
| 153 | |||
| 154 | my $self = [ | ||
| 155 | $agent, # FCT_AGENT | ||
| 156 | $streaming, # FCT_STREAMING | ||
| 157 | $max_size, # FCT_MAXSIZE | ||
| 158 | $protocol, # FCT_PROTOCOL | ||
| 159 | $cookie_jar, # FCT_COOKIEJAR | ||
| 160 | $from, # FCT_FROM | ||
| 161 | $no_proxy, # FCT_NOPROXY | ||
| 162 | $proxy, # FCT_HTTP_PROXY | ||
| 163 | $follow_redirects, # FCT_FOLLOWREDIRECTS | ||
| 164 | $timeout, # FCT_TIMEOUT | ||
| 165 | ]; | ||
| 166 | |||
| 167 | return bless $self, $class; | ||
| 168 | } | ||
| 169 | |||
| 170 | |||
| 171 | =head1 METHODS | ||
| 172 | |||
| 173 | =head2 timeout [$timeout] | ||
| 174 | |||
| 175 | Method that lets you query and/or change the timeout value for requests | ||
| 176 | created by this factory. | ||
| 177 | |||
| 178 | =cut | ||
| 179 | |||
| 180 | |||
| 181 | sub timeout { | ||
| 182 | my ($self, $timeout) = @_; | ||
| 183 | |||
| 184 | if (defined $timeout) { | ||
| 185 | $self->[FCT_TIMEOUT] = $timeout; | ||
| 186 | } | ||
| 187 | return $self->[FCT_TIMEOUT]; | ||
| 188 | } | ||
| 189 | |||
| 190 | |||
| 191 | =head2 is_streaming | ||
| 192 | |||
| 193 | Accessor for the Streaming parameter | ||
| 194 | |||
| 195 | =cut | ||
| 196 | |||
| 197 | |||
| 198 | sub is_streaming { | ||
| 199 | my ($self) = @_; | ||
| 200 | |||
| 201 | DEBUG and warn( | ||
| 202 | "FCT: this is " | ||
| 203 | . ($self->[FCT_STREAMING] ? "" : "not ") | ||
| 204 | . "streaming" | ||
| 205 | ); | ||
| 206 | return $self->[FCT_STREAMING]; | ||
| 207 | } | ||
| 208 | |||
| 209 | |||
| 210 | =head2 agent | ||
| 211 | |||
| 212 | Accessor to the Agent parameter | ||
| 213 | |||
| 214 | =cut | ||
| 215 | |||
| 216 | |||
| 217 | sub agent { | ||
| 218 | my ($self) = @_; | ||
| 219 | |||
| 220 | return $self->[FCT_AGENT]->[rand @{$self->[FCT_AGENT]}]; | ||
| 221 | } | ||
| 222 | |||
| 223 | |||
| 224 | =head2 from | ||
| 225 | |||
| 226 | getter/setter for the From parameter | ||
| 227 | |||
| 228 | =cut | ||
| 229 | |||
| 230 | |||
| 231 | sub from { | ||
| 232 | my ($self) = @_; | ||
| 233 | |||
| 234 | if (defined $self->[FCT_FROM] and length $self->[FCT_FROM]) { | ||
| 235 | return $self->[FCT_FROM]; | ||
| 236 | } | ||
| 237 | return undef; | ||
| 238 | } | ||
| 239 | |||
| 240 | |||
| 241 | =head2 create_request | ||
| 242 | |||
| 243 | Creates a new L<POE::Component::Client::HTTP::Request> | ||
| 244 | |||
| 245 | =cut | ||
| 246 | |||
| 247 | |||
| 248 | sub create_request { | ||
| 249 | my ($self, $http_request, $response_event, $tag, | ||
| 250 | $progress_event, $proxy_override, $sender) = @_; | ||
| 251 | |||
| 252 | # Add a protocol if one isn't included. | ||
| 253 | $http_request->protocol( $self->[FCT_PROTOCOL] ) unless ( | ||
| 254 | defined $http_request->protocol() | ||
| 255 | and length $http_request->protocol() | ||
| 256 | ); | ||
| 257 | |||
| 258 | # Add the User-Agent: header if one isn't included. | ||
| 259 | unless (defined $http_request->user_agent()) { | ||
| 260 | $http_request->user_agent($self->agent); | ||
| 261 | } | ||
| 262 | |||
| 263 | # Add a From: header if one isn't included. | ||
| 264 | if (defined $self->from) { | ||
| 265 | my $req_from = $http_request->from(); | ||
| 266 | unless (defined $req_from and length $req_from) { | ||
| 267 | $http_request->from( $self->from ); | ||
| 268 | } | ||
| 269 | } | ||
| 270 | |||
| 271 | # Add a Content-Length header if this request has content but | ||
| 272 | # doesn't have a Content-Length header already. Also, don't do it | ||
| 273 | # if the content is a reference, as this means we're streaming via | ||
| 274 | # callback. | ||
| 275 | if ( | ||
| 276 | length($http_request->content()) and | ||
| 277 | !ref($http_request->content()) and | ||
| 278 | !$http_request->content_length() | ||
| 279 | ) { | ||
| 280 | use bytes; | ||
| 281 | $http_request->content_length(length($http_request->content())); | ||
| 282 | } | ||
| 283 | |||
| 284 | my ($last_request, $postback); | ||
| 285 | if (ref($response_event) eq 'POE::Component::Client::HTTP::Request') { | ||
| 286 | $last_request = $response_event; | ||
| 287 | $postback = $last_request->postback; | ||
| 288 | } | ||
| 289 | else { | ||
| 290 | $postback = $sender->postback( $response_event, $http_request, $tag ); | ||
| 291 | } | ||
| 292 | # Create a progress postback if requested. | ||
| 293 | my $progress_postback; | ||
| 294 | if (defined $progress_event) { | ||
| 295 | if (ref $progress_event) { | ||
| 296 | # The given progress event appears to already | ||
| 297 | # be a postback, so use it. This is needed to | ||
| 298 | # propagate the postback through redirects. | ||
| 299 | $progress_postback = $progress_event; | ||
| 300 | } | ||
| 301 | else { | ||
| 302 | $progress_postback = $sender->postback( | ||
| 303 | $progress_event, | ||
| 304 | $http_request, | ||
| 305 | $tag | ||
| 306 | ); | ||
| 307 | } | ||
| 308 | } | ||
| 309 | |||
| 310 | # If we have a cookie jar, have it add the appropriate headers. | ||
| 311 | # LWP rocks! | ||
| 312 | |||
| 313 | if (defined $self->[FCT_COOKIEJAR]) { | ||
| 314 | $self->[FCT_COOKIEJAR]->add_cookie_header($http_request); | ||
| 315 | } | ||
| 316 | |||
| 317 | # MEXNIX 2002-06-01: If we have a proxy set, and the request URI is | ||
| 318 | # not in our no_proxy, then use the proxy. Otherwise use the | ||
| 319 | # request URI. | ||
| 320 | # | ||
| 321 | # RCAPUTO 2006-03-23: We only support http proxying right now. | ||
| 322 | # Avoid proxying if this isn't an http request. | ||
| 323 | |||
| 324 | # TODO CONNECT - Create a PCCH::Request object in https-CONNECT mode | ||
| 325 | # if we're using https and there's an appropriate proxy. | ||
| 326 | |||
| 327 | my $proxy = $proxy_override; | ||
| 328 | if ($http_request->uri->scheme() eq "http") { | ||
| 329 | $proxy ||= $self->[FCT_HTTP_PROXY]; | ||
| 330 | } | ||
| 331 | |||
| 332 | if (defined $proxy) { | ||
| 333 | # This request qualifies for proxying. Replace the host and port | ||
| 334 | # with the proxy's host and port. This comes after the Host: | ||
| 335 | # header is set, so it doesn't break the request object. | ||
| 336 | my $host = $http_request->uri->host; | ||
| 337 | |||
| 338 | undef $proxy if ( | ||
| 339 | !defined($host) or | ||
| 340 | _in_no_proxy ($host, $self->[FCT_NOPROXY]) | ||
| 341 | ); | ||
| 342 | } | ||
| 343 | |||
| 344 | my $request = POE::Component::Client::HTTP::Request->new ( | ||
| 345 | Request => $http_request, | ||
| 346 | Proxy => $proxy, | ||
| 347 | Postback => $postback, | ||
| 348 | #Tag => $tag, # TODO - Is this needed for anything? | ||
| 349 | Progress => $progress_postback, | ||
| 350 | Factory => $self, | ||
| 351 | ); | ||
| 352 | |||
| 353 | if (defined $last_request) { | ||
| 354 | $request->does_redirect($last_request); | ||
| 355 | } | ||
| 356 | return $request; | ||
| 357 | } | ||
| 358 | |||
| 359 | |||
| 360 | # Determine whether a host is in a no-proxy list. | ||
| 361 | |||
| 362 | sub _in_no_proxy { | ||
| 363 | my ($host, $no_proxy) = @_; | ||
| 364 | |||
| 365 | foreach my $no_proxy_domain (@$no_proxy) { | ||
| 366 | return 1 if $host =~ /\Q$no_proxy_domain\E$/i; | ||
| 367 | } | ||
| 368 | return 0; | ||
| 369 | } | ||
| 370 | |||
| 371 | |||
| 372 | =head2 max_response_size | ||
| 373 | |||
| 374 | Method to retrieve the maximum size of a response, as set by the | ||
| 375 | C<MaxSize> parameter to L<Client::HTTP>'s C<spawn()> method. | ||
| 376 | |||
| 377 | =cut | ||
| 378 | |||
| 379 | |||
| 380 | sub max_response_size { | ||
| 381 | my ($self) = @_; | ||
| 382 | |||
| 383 | return $self->[FCT_MAXSIZE]; | ||
| 384 | } | ||
| 385 | |||
| 386 | |||
| 387 | =head2 block_size | ||
| 388 | |||
| 389 | Accessor for the Streaming parameter | ||
| 390 | |||
| 391 | =cut | ||
| 392 | |||
| 393 | |||
| 394 | sub block_size { | ||
| 395 | my ($self) = @_; | ||
| 396 | |||
| 397 | my $block_size = $self->[FCT_STREAMING] || DEFAULT_BLOCK_SIZE; | ||
| 398 | $block_size = DEFAULT_BLOCK_SIZE if $block_size < 1; | ||
| 399 | |||
| 400 | return $block_size; | ||
| 401 | } | ||
| 402 | |||
| 403 | |||
| 404 | =head2 frob_cookies $response | ||
| 405 | |||
| 406 | Store the cookies from the L<HTTP::Response> parameter passed into | ||
| 407 | our cookie jar | ||
| 408 | |||
| 409 | =cut | ||
| 410 | |||
| 411 | |||
| 412 | sub frob_cookies { | ||
| 413 | my ($self, $response) = @_; | ||
| 414 | |||
| 415 | if (defined $self->[FCT_COOKIEJAR]) { | ||
| 416 | $self->[FCT_COOKIEJAR] ->extract_cookies($response); | ||
| 417 | } | ||
| 418 | } | ||
| 419 | |||
| 420 | |||
| 421 | =head2 max_redirect_count [$count] | ||
| 422 | |||
| 423 | Function to get/set the maximum number of redirects to follow | ||
| 424 | automatically. This allows you to retrieve or modify the value | ||
| 425 | you passed with the FollowRedirects parameter to L<Client::HTTP>'s | ||
| 426 | C<spawn> method. | ||
| 427 | |||
| 428 | =cut | ||
| 429 | |||
| 430 | |||
| 431 | sub max_redirect_count { | ||
| 432 | my ($self, $count) = @_; | ||
| 433 | |||
| 434 | if (defined $count) { | ||
| 435 | $self->[FCT_FOLLOWREDIRECTS] = $count; | ||
| 436 | } | ||
| 437 | return $self->[FCT_FOLLOWREDIRECTS]; | ||
| 438 | } | ||
| 439 | |||
| 440 | |||
| 441 | =head2 parse_proxy $proxy | ||
| 442 | |||
| 443 | This static method is used for parsing proxies. The $proxy can be | ||
| 444 | array reference like [host, port] or comma separated string like | ||
| 445 | "http://1.2.3.4:80/,http://2.3.4.5:80/". | ||
| 446 | |||
| 447 | parse_proxy() returns an array reference of two-element tuples (also | ||
| 448 | array ferences), each containing a host and a port: | ||
| 449 | |||
| 450 | [ [ host1, port1 ], | ||
| 451 | [ host2, port2 ], | ||
| 452 | ... | ||
| 453 | ] | ||
| 454 | |||
| 455 | =cut | ||
| 456 | |||
| 457 | |||
| 458 | sub parse_proxy { | ||
| 459 | my $proxy = $_[1]; | ||
| 460 | |||
| 461 | if (ref($proxy) eq 'ARRAY') { | ||
| 462 | croak "Proxy must contain [HOST,PORT]" unless @$proxy == 2; | ||
| 463 | $proxy = [ $proxy ]; | ||
| 464 | } else { | ||
| 465 | my @proxies = split /\s*\,\s*/, $proxy; | ||
| 466 | foreach (@proxies) { | ||
| 467 | s/^http:\/+//; | ||
| 468 | s/\/+$//; | ||
| 469 | croak "Proxy must contain host:port" unless /^(.+):(\d+)$/; | ||
| 470 | $_ = [ $1, $2 ]; | ||
| 471 | } | ||
| 472 | if (@proxies) { | ||
| 473 | $proxy = \@proxies; | ||
| 474 | } else { | ||
| 475 | undef $proxy; # Empty proxy list means not to use proxy | ||
| 476 | } | ||
| 477 | } | ||
| 478 | |||
| 479 | $_[1] = $proxy; | ||
| 480 | } | ||
| 481 | |||
| 482 | 1; |