|
NAMEPOE::Component::Client::HTTP - a HTTP user-agent componentVERSIONversion 0.949SYNOPSISuse POE qw(Component::Client::HTTP); POE::Component::Client::HTTP->spawn( Agent => 'SpiffCrawler/0.90', # defaults to something long Alias => 'ua', # defaults to 'weeble' From => 'spiffster@perl.org', # defaults to undef (no header) Protocol => 'HTTP/0.9', # defaults to 'HTTP/1.1' Timeout => 60, # defaults to 180 seconds MaxSize => 16384, # defaults to entire response Streaming => 4096, # defaults to 0 (off) FollowRedirects => 2, # defaults to 0 (off) Proxy => "http://localhost:80", # defaults to HTTP_PROXY env. variable NoProxy => [ "localhost", "127.0.0.1" ], # defs to NO_PROXY env. variable BindAddr => "12.34.56.78", # defaults to INADDR_ANY ); $kernel->post( 'ua', # posts to the 'ua' alias 'request', # posts to ua's 'request' state 'response', # which of our states will receive the response $request, # an HTTP::Request object ); # This is the sub which is called when the session receives a # 'response' event. sub response_handler { my ($request_packet, $response_packet) = @_[ARG0, ARG1]; # HTTP::Request my $request_object = $request_packet->[0]; # HTTP::Response my $response_object = $response_packet->[0]; my $stream_chunk; if (! defined($response_object->content)) { $stream_chunk = $response_packet->[1]; } print( "*" x 78, "\n", "*** my request:\n", "-" x 78, "\n", $request_object->as_string(), "*" x 78, "\n", "*** their response:\n", "-" x 78, "\n", $response_object->as_string(), ); if (defined $stream_chunk) { print "-" x 40, "\n", $stream_chunk, "\n"; } print "*" x 78, "\n"; } DESCRIPTIONPOE::Component::Client::HTTP is an HTTP user-agent for POE. It lets other sessions run while HTTP transactions are being processed, and it lets several HTTP transactions be processed in parallel.It supports keep-alive through POE::Component::Client::Keepalive, which in turn uses POE::Component::Resolver for asynchronous IPv4 and IPv6 name resolution. HTTP client components are not proper objects. Instead of being created, as most objects are, they are "spawned" as separate sessions. To avoid confusion (and hopefully not cause other confusion), they must be spawned with a "spawn" method, not created anew with a "new" one. CONSTRUCTORspawnPoCo::Client::HTTP's "spawn" method takes a few named parameters:
ACCEPTED EVENTSSessions communicate asynchronously with PoCo::Client::HTTP. They post requests to it, and it posts responses back.requestRequests are posted to the component's "request" state. They include an HTTP::Request object which defines the request. For example:$kernel->post( 'ua', 'request', # http session alias & state 'response', # my state to receive responses GET('http://poe.perl.org'), # a simple HTTP request 'unique id', # a tag to identify the request 'progress', # an event to indicate progress 'http://1.2.3.4:80/' # proxy to use for this request ); Requests include the state to which responses will be posted. In the previous example, the handler for a 'response' state will be called with each HTTP response. The "progress" handler is optional and if installed, the component will provide progress metrics (see sample handler below). The "proxy" parameter is optional and if not defined, a default proxy will be used if configured. No proxy will be used if neither a default one nor a "proxy" parameter is defined. pending_requests_countThere's also a pending_requests_count state that returns the number of requests currently being processed. To receive the return value, it must be invoked with $kernel->call().my $count = $kernel->call('ua' => 'pending_requests_count'); NOTE: Sometimes the count might not be what you expected, because responses are currently in POE's queue and you haven't processed them. This could happen if you configure the "ConnectionManager"'s concurrency to a high enough value. cancelCancel a specific HTTP request. Requires a reference to the original request (blessed or stringified) so it knows which one to cancel. See "progress handler" below for notes on canceling streaming requests.To cancel a request based on its blessed HTTP::Request object: $kernel->post( component => cancel => $http_request ); To cancel a request based on its stringified HTTP::Request object: $kernel->post( component => cancel => "$http_request" ); shutdownResponds to all pending requests with 408 (request timeout), and then shuts down the component and all subcomponents.SENT EVENTSresponse handlerIn addition to all the usual POE parameters, HTTP responses come with two list references:my ($request_packet, $response_packet) = @_[ARG0, ARG1]; $request_packet contains a reference to the original HTTP::Request object. This is useful for matching responses back to the requests that generated them. my $http_request_object = $request_packet->[0]; my $http_request_tag = $request_packet->[1]; # from the 'request' post $response_packet contains a reference to the resulting HTTP::Response object. my $http_response_object = $response_packet->[0]; Please see the HTTP::Request and HTTP::Response manpages for more information. progress handlerThe example progress handler shows how to calculate a percentage of download completion.sub progress_handler { my $gen_args = $_[ARG0]; # args passed to all calls my $call_args = $_[ARG1]; # args specific to the call my $req = $gen_args->[0]; # HTTP::Request object being serviced my $tag = $gen_args->[1]; # Request ID tag from. my $got = $call_args->[0]; # Number of bytes retrieved so far. my $tot = $call_args->[1]; # Total bytes to be retrieved. my $oct = $call_args->[2]; # Chunk of raw octets received this time. my $percent = $got / $tot * 100; printf( "-- %.0f%% [%d/%d]: %s\n", $percent, $got, $tot, $req->uri() ); # To cancel the request: # $_[KERNEL]->post( component => cancel => $req ); } DEPRECATION WARNING The third return argument (the raw octets received) has been deprecated. Instead of it, use the Streaming parameter to get chunks of content in the response handler. REQUEST CALLBACKSThe HTTP::Request object passed to the request event can contain a CODE reference as "content". This allows for sending large files without wasting memory. Your callback should return a chunk of data each time it is called, and an empty string when done. Don't forget to set the Content-Length header correctly. Example:my $request = HTTP::Request->new( PUT => 'http://...' ); my $file = '/path/to/large_file'; open my $fh, '<', $file; my $upload_cb = sub { if ( sysread $fh, my $buf, 4096 ) { return $buf; } else { close $fh; return ''; } }; $request->content_length( -s $file ); $request->content( $upload_cb ); $kernel->post( ua => request, 'response', $request ); CONTENT ENCODING AND COMPRESSIONTransparent content decoding has been disabled as of version 0.84. This also removes support for transparent gzip requesting and decompression.To re-enable gzip compression, specify the gzip Content-Encoding and use HTTP::Response's decoded_content() method rather than content(): my $request = HTTP::Request->new( GET => "http://www.yahoo.com/", [ 'Accept-Encoding' => 'gzip' ] ); # ... time passes ... my $content = $response->decoded_content(); The change in POE::Component::Client::HTTP behavior was prompted by changes in HTTP::Response that surfaced a bug in the component's transparent gzip handling. Allowing the application to specify and handle content encodings seems to be the most reliable and flexible resolution. For more information about the problem and discussions regarding the solution, see: <http://www.perlmonks.org/?node_id=683833> and <http://rt.cpan.org/Ticket/Display.html?id=35538> CLIENT HEADERSPOE::Component::Client::HTTP sets its own response headers with additional information. All of its headers begin with "X-PCCH".X-PCCH-ErrmsgPOE::Component::Client::HTTP may fail because of an internal client error rather than an HTTP protocol error. X-PCCH-Errmsg will contain a human readable reason for client failures, should they occur.The text of X-PCCH-Errmsg may also be repeated in the response's content. X-PCCH-PeerX-PCCH-Peer contains the remote IPv4 address and port, separated by a period. For example, "127.0.0.1.8675" represents port 8675 on localhost.Proxying will render X-PCCH-Peer nearly useless, since the socket will be connected to a proxy rather than the server itself. This feature was added at Doreen Grey's request. Doreen wanted a means to find the remote server's address without having to make an additional request. ENVIRONMENTPOE::Component::Client::HTTP uses two standard environment variables: HTTP_PROXY and NO_PROXY.HTTP_PROXY sets the proxy server that Client::HTTP will forward requests through. NO_PROXY sets a list of hosts that will not be forwarded through a proxy. See the Proxy and NoProxy constructor parameters for more information about these variables. SEE ALSOThis component is built upon HTTP::Request, HTTP::Response, and POE. Please see its source code and the documentation for its foundation modules to learn more. If you want to use cookies, you'll need to read about HTTP::Cookies as well.Also see the test program, t/01_request.t, in the PoCo::Client::HTTP distribution. BUGSThere is no support for CGI_PROXY or CgiProxy.Secure HTTP (https) proxying is not supported at this time. There is no object oriented interface. See POE::Component::Client::Keepalive and POE::Component::Resolver for examples of a decent OO interface. AUTHOR, COPYRIGHT, & LICENSEPOE::Component::Client::HTTP is
All rights are reserved. POE::Component::Client::HTTP is free software; you may redistribute it and/or modify it under the same terms as Perl itself. CONTRIBUTORSJoel Bernstein solved some nasty race conditions. Portugal Telecom <http://www.sapo.pt/> was kind enough to support his contributions.Jeff Bisbee added POD tests and documentation to pass several of them to version 0.79. He's a kwalitee-increasing machine! BUG TRACKERhttps://rt.cpan.org/Dist/Display.html?Queue=POE-Component-Client-HTTPREPOSITORYGithub: <http://github.com/rcaputo/poe-component-client-http> .Gitorious: <http://gitorious.org/poe-component-client-http> . OTHER RESOURCES<http://search.cpan.org/dist/POE-Component-Client-HTTP/>
Visit the GSP FreeBSD Man Page Interface. |