man POE::Component::Client::HTTP () - a HTTP user-agent component

NAME

POE::Component::Client::HTTP - a HTTP user-agent component

SYNOPSIS

  use 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.0'
    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
  );

  $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";
  }

DESCRIPTION

POE::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.

If POE::Component::Client::DNS is also installed, Client::HTTP will use it to resolve hosts without blocking. Otherwise it will use gethostbyname(), which may have performance problems.

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 CWspawn method, not created anew with a CWnew one.

PoCo::Client::HTTP's CWspawn method takes a few named parameters:

Agent => \@list_of_agents
If a UserAgent header is not present in the HTTP::Request, a random one will be used from those specified by the CWAgent parameter. If none are supplied, POE::Component::Client::HTTP will advertise itself to the server. CWAgent may contain a reference to a list of user agents. If this is the case, PoCo::Client::HTTP will choose one of them at random for each request. CWAlias sets the name by which the session will be known. If no alias is given, the component defaults to weeble. The alias lets several sessions interact with HTTP components without keeping (or even knowing) hard references to them. It's possible to spawn several HTTP components with different names. CWCookieJar sets the component's cookie jar. It expects the cookie jar to be a reference to a HTTP::Cookies object. CWFrom holds an e-mail address where the client's administrator and/or maintainer may be reached. It defaults to undef, which means no From header will be included in requests.
MaxSize => OCTETS
CWMaxSize specifies the largest response to accept from a server. The content of larger responses will be truncated to OCTET octets. This has been used to return the <head></head> section of web pages without the need to wade through <body></body>. CWNoProxy specifies a list of server hosts that will not be proxied. It is useful for local hosts and hosts that do not properly support proxying. If NoProxy is not specified, a list will be taken from the NO_PROXY environment variable.
  NoProxy => [ "localhost", "127.0.0.1" ],
  NoProxy => "localhost,127.0.0.1",
CWProtocol advertises the protocol that the client wishes to see. Under normal circumstances, it should be left to its default value: HTTP/1.0. CWProxy specifies one or more proxy hosts that requests will be passed through. If not specified, proxy servers will be taken from the HTTP_PROXY (or http_proxy) environment variable. No proxying will occur unless Proxy is set or one of the environment variables exists. The proxy can be specified either as a host and port, or as one or more URLs. Proxy URLs must specify the proxy port, even if it is 80.
  Proxy => [ "127.0.0.1", 80 ],
  Proxy => "http://127.0.0.1:80/",
CWProxy may specify multiple proxies separated by commas. PoCo::Client::HTTP will choose proxies from this list at random. This is useful for load balancing requests through multiple gateways.
  Proxy => "http://127.0.0.1:80/,http://127.0.0.1:81/",
Streaming => OCTETS
CWStreaming changes allows Client::HTTP to return large content in chunks (of OCTETS octets each) rather than combine the entire content into a single HTTP::Response object. By default, Client::HTTP reads the entire content for a response into memory before returning an HTTP::Response object. This is obviously bad for applications like streaming MP3 clients, because they often fetch songs that never end. Yes, they go on and on, my friend. When CWStreaming is set to nonzero, however, the response handler receives chunks of up to OCTETS octets apiece. The response handler accepts slightly different parameters in this case. ARG0 is also an HTTP::Response object but it does not contain response content, and ARG1 contains a a chunk of raw response content, or undef if the stream has ended.
  sub streaming_response_handler {
    my $response_packet = $_[ARG1];
    my ($response, $data) = @$response_packet;
    print SAVED_STREAM $data if defined $data;
  }
CWFollowRedirects specifies how many redirects (e.g. 302 Moved) to follow. If not specified defaults to 0, and thus no redirection is followed. This maintains compatibility with the previous behavior, which was not to follow redirects at all. If redirects are followed, a response chain should be built, and can be accessed through CW$response_object->previous(). See HTTP::Response for details here. CWTimeout specifies the amount of time a HTTP request will wait for an answer. This defaults to 180 seconds (three minutes).

Sessions communicate asynchronously with PoCo::Client::HTTP. They post requests to it, and it posts responses back.

Requests 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
               );

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).

In addition to all the usual POE parameters, HTTP responses come with two list references:

  my ($request_packet, $response_packet) = @_[ARG0, ARG1];

CW$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

CW$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.

There'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 CW$kernel->call().

  my $count = $kernel->call('ua' => 'pending_requests_count');

The 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()
    );
  }

ENVIRONMENT

POE::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 ALSO

This 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.

BUGS

HTTP/1.1 requests are not supported.

The following spawn() parameters are accepted but not yet implemented: Timeout.

There is no support for CGI_PROXY or CgiProxy.

AUTHOR & COPYRIGHTS

POE::Component::Client::HTTP is Copyright 1999-2002 by Rocco Caputo. 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.

Rocco may be contacted by e-mail via rcaputo@cpan.org.