POE-Component-Client-HTTP
view release on metacpan or search on metacpan
lib/POE/Component/Client/HTTP.pm view on Meta::CPAN
# connection request.
$heap->{cm}->deallocate($request->[REQ_CONN_ID]);
}
unless ($request->[REQ_STATE] & RS_POSTED) {
$request->error($code, $message);
}
}
# Shut down the entire component.
sub _poco_weeble_shutdown {
my ($kernel, $heap) = @_[KERNEL, HEAP];
$heap->{is_shut_down} = 1;
my @request_ids = keys %{$heap->{request}};
foreach my $request_id (@request_ids) {
_internal_cancel(
$heap, $request_id, 408, "Request timed out (component shut down)"
);
}
# Shut down the connection manager subcomponent.
if (defined $heap->{cm}) {
DEBUG and warn "CXL: Client::HTTP shutting down Client::Keepalive";
$heap->{cm}->shutdown();
delete $heap->{cm};
}
# Final cleanup of this component.
$kernel->alias_remove($heap->{alias});
}
1;
__END__
=head1 NAME
POE::Component::Client::HTTP - a HTTP user-agent component
=head1 VERSION
version 0.949
=head1 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.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";
}
=head1 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.
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 C<spawn> method, not created anew with a C<new>
one.
lib/POE/Component/Client/HTTP.pm view on Meta::CPAN
);
See L<POE::Component::Client::Keepalive> for more information,
including how to alter the connection manager's resolver
configuration (for example, to force IPv6 or prefer it before IPv4).
=item CookieJar => $cookie_jar
C<CookieJar> sets the component's cookie jar. It expects the cookie
jar to be a reference to a HTTP::Cookies object.
=item From => $admin_address
C<From> 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.
=item MaxSize => OCTETS
C<MaxSize> 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>.
=item NoProxy => [ $host_1, $host_2, ..., $host_N ]
=item NoProxy => "host1,host2,hostN"
C<NoProxy> 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",
=item BindAddr => $local_ip
Specify C<BindAddr> to bind all client sockets to a particular local
address. The value of BindAddr will be passed through
POE::Component::Client::Keepalive to POE::Wheel::SocketFactory (as
C<bind_address>). See that module's documentation for implementation
details.
BindAddr => "12.34.56.78"
=item Protocol => $http_protocol_string
C<Protocol> advertises the protocol that the client wishes to see.
Under normal circumstances, it should be left to its default value:
"HTTP/1.1".
=item Proxy => [ $proxy_host, $proxy_port ]
=item Proxy => $proxy_url
=item Proxy => $proxy_url,$proxy_url,...
C<Proxy> 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/",
C<Proxy> 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/",
=item Streaming => OCTETS
C<Streaming> 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 C<Streaming> 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;
}
=item FollowRedirects => $number_of_hops_to_follow
C<FollowRedirects> 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 $response_object->previous(). See HTTP::Response
for details here.
=item Timeout => $query_timeout
C<Timeout> sets how long POE::Component::Client::HTTP has to process
an application's request, in seconds. C<Timeout> defaults to 180
(three minutes) if not specified.
It's important to note that the timeout begins when the component
receives an application's request, not when it attempts to connect to
the web server.
Timeouts may result from sending the component too many requests at
lib/POE/Component/Client/HTTP.pm view on Meta::CPAN
Transparent 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:
L<http://www.perlmonks.org/?node_id=683833> and
L<http://rt.cpan.org/Ticket/Display.html?id=35538>
=head1 CLIENT HEADERS
POE::Component::Client::HTTP sets its own response headers with
additional information. All of its headers begin with "X-PCCH".
=head2 X-PCCH-Errmsg
POE::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.
=head2 X-PCCH-Peer
X-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.
=head1 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.
=head1 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.
=head1 BUGS
There 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
L<POE::Component::Client::Keepalive> and
L<POE::Component::Resolver> for examples of a decent OO interface.
=head1 AUTHOR, COPYRIGHT, & LICENSE
POE::Component::Client::HTTP is
=over 2
=item
Copyright 1999-2009 Rocco Caputo
=item
Copyright 2004 Rob Bloodgood
=item
Copyright 2004-2005 Martijn van Beers
=back
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.
=head1 CONTRIBUTORS
Joel Bernstein solved some nasty race conditions. Portugal Telecom
L<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!
=head1 BUG TRACKER
( run in 0.551 second using v1.01-cache-2.11-cpan-71847e10f99 )