DESCRIPTION

WWW::Curl::UserAgent is a web user agent based on libcurl. It can be used easily with HTTP::Request and HTTP::Response objects and handler callbacks. For an easier interface there is also a method to map a single request to a response.

WWW::Curl is used for the power of libcurl, which e.g. handles connection keep-alive, parallel requests, asynchronous callbacks and much more. This package was written, because WWW::Curl::Simple does not handle keep-alive correctly and also does not consider PUT, HEAD and other request methods like DELETE.

There is a simpler interface too, which just returns a HTTP::Response for a given HTTP::Request, named request(). The normal approach to use this library is to add as many requests with callbacks as your code allows to do and run perform afterwards. Then the callbacks will be executed sequentially when the responses arrive beginning with the first received response. The simple method request() does not support this of course, because there are no callbacks defined.

CONSTRUCTOR METHODS

The following constructor methods are available:

$ua = WWW::Curl::UserAgent->new( %options )

This method constructs a new WWW::Curl::UserAgent object and returns it. Key/value pair arguments may be provided to set up the initial state. The default values should be based on the default values of libcurl. The following options correspond to attribute methods described below:

ATTRIBUTES

Get/set the timeout in milliseconds waiting for the response to be received. If the response is not received within the timeout the on_failure handler is called.

$ua->timeout / $ua->timeout($timeout)

Get/set the timeout in milliseconds waiting for the response to be received. If the response is not received within the timeout the on_failure handler is called.

$ua->parallel_requests / $ua->parallel_requests($parallel_requests)

Get/set the number of the maximum of requests performed in parallel. libcurl itself may use less requests than this number but not more.

$ua->keep_alive / $ua->keep_alive($boolean)

Get/set if TCP connections should be reused with keep-alive. Therefor the TCP connection is forced to be closed after receiving the response and the corresponding header "Connection: close" is set. If keep-alive is enabled (default) libcurl will handle the connections.

$ua->followlocation / $ua->followlocation($boolean)

Get/set if curl should follow redirects. The headers of the redirect respones are thrown away while redirecting, so that the final response will be passed into the corresponding handler.

$ua->max_redirects / $ua->max_redirects($max_redirects)

Get/set the maximum amount of redirects. -1 (default) means infinite redirects. 0 means no redirects at all. If the maximum redirect is reached the on_failure handler will be called.

$ua->user_agent_string / $ua->user_agent_string($user_agent)

Get/set the user agent submitted in each request.

$ua->request_queue_size

Get the size of the not performed requests.

$ua->request( $request, %args )

Perform immediately a single HTTP::Request. Parameters can be submitted optionally, which will override the user agents settings for this single request. Possible options are:

If there is an error e.g. like a timeout the corresponding HTTP::Response object will have the statuscode 500, the short error description as message and a longer message description as content. It runs perform() internally, so queued requests will be performed, too.

$ua->add_request(%args)

Adds a request with some callback handler on receiving messages. The on_success callback will be called for every successful read response, even those containing error codes. The on_failure handler will be called when libcurl reports errors, e.g. timeouts or bad curl settings. The parameters request, on_success and on_failure are mandatory. Optional are timeout, connect_timeout, keep_alive, followlocation and max_redirects.

The callbacks provide as last parameter a WWW:Curl::Easy object which was used to perform the request. This can be used to obtain some informations like statistical data about the request.

Chaining of add_request calls is a feature of this module. If you add a request within an on_success handler it will be immediately executed when the callback is executed. This can be useful to immediately react on a response:

To have more control over the handler you can add a WWW::Curl::UserAgent::Handler by yourself. The WWW::Curl::UserAgent::Request inside of the handler needs all parameters provided to libcurl as mandatory to prevent defining duplicates of default values. Within the WWW::Curl::UserAgent::Request is the possiblity to modify the WWW::Curl::Easy object before it gets performed.

As a valued partner and proud supporter of MetaCPAN, StickerYou is
happy to offer a 10% discount on all Custom Stickers,
Business Labels, Roll Labels,
Vinyl Lettering or Custom Decals. StickerYou.com
is your one-stop shop to make your business stick.
Use code METACPAN10 at checkout to apply your discount.