HTTP::Tiny (3)
Leading comments
Automatically generated by Pod::Man 4.09 (Pod::Simple 3.35) Standard preamble: ========================================================================
NAME
HTTP::Tiny - A small, simple, correct HTTP/1.1 clientVERSION
version 0.054SYNOPSIS
use HTTP::Tiny; my $response = HTTP::Tiny->new->get('http://example.com/'); die "Failed!\n" unless $response->{success}; print "$response->{status} $response->{reason}\n"; while (my ($k, $v) = each %{$response->{headers}}) { for (ref $v eq 'ARRAY' ? @$v : $v) { print "$k: $_\n"; } } print $response->{content} if length $response->{content};
DESCRIPTION
This is a very simpleIt is more correct and more complete than HTTP::Lite. It supports proxies and redirection. It also correctly resumes after
If IO::Socket::IP 0.25 or later is installed, HTTP::Tiny will use it instead of IO::Socket::INET for transparent support for both IPv4 and IPv6.
Cookie support requires HTTP::CookieJar or an equivalent class.
METHODS
new
$http = HTTP::Tiny->new( %attributes );
This constructor returns a new HTTP::Tiny object. Valid attributes include:
- *
- "agent" X A user-agent string (defaults to 'HTTP-Tiny/$VERSION'). If "agent" X ends in a space character, the default user-agent string is appended.
- *
- "cookie_jar" X An instance of HTTP::CookieJar X or equivalent class that supports the "add" and "cookie_header" methods
- *
- "default_headers" X A hashref of default headers to apply to requests
- *
-
"local_address" X The local IPaddress to bind to
- *
- "keep_alive" X Whether to reuse the last connection (if for the same scheme, host and port) (defaults to 1)
- *
- "max_redirect" X Maximum number of redirects allowed (defaults to 5)
- *
- "max_size" X Maximum response size (only when not using a data callback). If defined, responses larger than this will return an exception.
- *
-
"http_proxy" X URLof a proxy server to use forHTTPconnections (default is $ENV{http_proxy} X if set)
- *
-
"https_proxy" X URLof a proxy server to use forHTTPSconnections (default is $ENV{https_proxy} X if set)
- *
-
"proxy" X URLof a generic proxy server for bothHTTPandHTTPSconnections (default is $ENV{all_proxy} X if set)
- *
- "no_proxy" X List of domain suffixes that should not be proxied. Must be a comma-separated string or an array reference. (default is $ENV{no_proxy} X)
- *
- "timeout" X Request timeout in seconds (default is 60)
- *
-
"verify_SSL" X A boolean that indicates whether to validate the SSLcertificate of an "https" X connection (default is false)
- *
- "SSL_options" X A hashref of "SSL_*" X options to pass through to IO::Socket::SSL
Passing an explicit "undef" for "proxy", "http_proxy" or "https_proxy" will prevent getting the corresponding proxies from the environment.
Exceptions from "max_size", "timeout" or other errors will result in a pseudo-HTTP status code of 599 and a reason of ``Internal Exception''. The content field in the response will contain the text of the exception.
The "keep_alive" parameter enables a persistent connection, but only to a single destination scheme, host and port. Also, if any connection-relevant attributes are modified, or if the process
See ``
get|head|put|post|delete
$response = $http->get($url); $response = $http->get($url, \%options); $response = $http->head($url);
These methods are shorthand for calling "request()" for the given method. The
The "success" field of the response will be true if the status code is 2XX.
post_form
$response = $http->post_form($url, $form_data); $response = $http->post_form($url, $form_data, \%options);
This method executes a "POST" request and sends the key/value pairs from a form data hash or array reference to the given
The
The "success" field of the response will be true if the status code is 2XX.
mirror
$response = $http->mirror($url, $file, \%options) if ( $response->{success} ) { print "$file is up to date\n"; }
Executes a "GET" request for the
The "success" field of the response will be true if the status code is 2XX or if the status code is 304 (unmodified).
If the file was modified and the server response includes a properly formatted "Last-Modified" header, the file modification time will be updated accordingly.
request
$response = $http->request($method, $url); $response = $http->request($method, $url, \%options);
Executes an
If the
$http->request('GET', 'Aladdin:open sesame@example.com/');
If the ``user:password'' stanza contains reserved characters, they must be percent-escaped:
$http->request('GET', 'john%40example.com:password@example.com/');
A hashref of options may be appended to modify the request.
Valid options are:
- *
- "headers" X A hashref containing headers to include with the request. If the value for a header is an array reference, the header will be output multiple times with each value in the array. These headers over-write any default headers.
- *
-
"content" X A scalar to include as the body of the request ORa code reference that will be called iteratively to produce the body of the request
- *
- "trailer_callback" X A code reference that will be called if it exists to provide a hashref of trailing headers (only used with chunked transfer-encoding)
- *
- "data_callback" X A code reference that will be called for each chunks of the response body received.
The "Host" header is generated from the
If the "content" option is a code reference, it will be called iteratively to provide the content body of the request. It should return the empty string or undef when the iterator is exhausted.
If the "content" option is the empty string, no "content-type" or "content-length" headers will be generated.
If the "data_callback" option is provided, it will be called iteratively until the entire response body is received. The first argument will be a string containing a chunk of the response body, the second argument will be the in-progress response hash reference, as described below. (This allows customizing the action of the callback based on the "status" or "headers" received prior to the content body.)
The "request" method returns a hashref containing the response. The hashref will have the following keys:
- *
- "success" X Boolean indicating whether the operation returned a 2XX status code
- *
-
"url" X URLthat provided the response. This is theURLof the request unless there were redirections, in which case it is the lastURLqueried in a redirection chain
- *
-
"status" X The HTTPstatus code of the response
- *
- "reason" X The response phrase returned by the server
- *
- "content" X The body of the response. If the response does not have any content or if a data callback is provided to consume the response body, this will be the empty string
- *
- "headers" X A hashref of header fields. All header field names will be normalized to be lower case. If a header is repeated, the value will be an arrayref; it will otherwise be a scalar string containing the value
On an exception during the execution of the request, the "status" field will contain 599, and the "content" field will contain the text of the exception.
www_form_urlencode
$params = $http->www_form_urlencode( $data ); $response = $http->get("http://example.com/query?$params");
This method converts the key/value pairs from a data hash or array reference into a "x-www-form-urlencoded" string. The keys and values from the data reference will be
SSL SUPPORT
Direct "https" connections are supported only if IO::Socket::SSL 1.56 or greater and Net::SSLeay 1.49 or greater are installed. An exception will be thrown if new enough versions of these modules are not installed or if the
- *
- Encrypted communication channel
- *
- Verification of server identity
By default, HTTP::Tiny does not verify server identity.
Server identity verification is controversial and potentially tricky because it depends on a (usually paid) third-party Certificate Authority (
By default, HTTP::Tiny does not make any assumptions about your trust model, threat level or risk tolerance. It just aims to give you an encrypted channel when you need one.
Setting the "verify_SSL" attribute to a true value will make HTTP::Tiny verify that an
Certificate verification requires a file containing trusted
If that module is not available, then HTTP::Tiny will search several system-specific default locations for a
- *
- /etc/ssl/certs/ca-certificates.crt
- *
- /etc/pki/tls/certs/ca-bundle.crt
- *
- /etc/ssl/ca-bundle.pem
An exception will be raised if "verify_SSL" is true and no
If you desire complete control over
SSL_options => { SSL_ca_file => $file_path, }
The "SSL_options" attribute could also be used for such things as providing a client certificate for authentication to a server or controlling the choice of cipher used for the
PROXY SUPPORT
HTTP::Tiny can proxy both "http" and "https" requests. Only Basic proxy authorization is supported and it must be provided as part of the proxyHTTP::Tiny supports the following proxy environment variables:
- *
-
http_proxy or HTTP_PROXY
- *
-
https_proxy or HTTPS_PROXY
- *
-
all_proxy or ALL_PROXY
If the "REQUEST_METHOD" environment variable is set, then this might be a
Tunnelling "https" over an "http" proxy using the
Be warned that proxying an "https" connection opens you to the risk of a man-in-the-middle attack by the proxy server.
The "no_proxy" environment variable is supported in the format of a comma-separated list of domain extensions proxy should not be used for.
Proxy arguments passed to "new" will override their corresponding environment variables.
LIMITATIONS
HTTP::Tiny is conditionally compliant with the- *
-
``Message Syntax and Routing'' [RFC7230]
- *
-
``Semantics and Content'' [RFC7231]
- *
-
``Conditional Requests'' [RFC7232]
- *
-
``Range Requests'' [RFC7233]
- *
-
``Caching'' [RFC7234]
- *
-
``Authentication'' [RFC7235]
It attempts to meet all ``
Some particular limitations of note include:
- *
-
HTTP::Tiny focuses on correct transport. Users are responsible for ensuring
that user-defined headers and content are compliant with the HTTP/1.1specification.
- *
-
Users must ensure that URLs are properly escaped for unsafe characters and that
international domain names are properly encoded to ASCII.See URI::Escape, URI::_punycode and Net::IDN::Encode.
- *
-
Redirection is very strict against the specification. Redirection is only
automatic for response codes 301, 302 and 307 if the request method is 'GET' or 'HEAD'. Response code 303 is always converted into a 'GET' redirection, as mandated by the specification. There is no automatic support for status 305 (``Use proxy'') redirections.
- *
- There is no provision for delaying a request body using an "Expect" header. Unexpected "1XX" responses are silently ignored as per the specification.
- *
- Only 'chunked' "Transfer-Encoding" is supported.
- *
-
There is no support for a Request-URI of '*' for the 'OPTIONS' request.
Despite the limitations listed above, HTTP::Tiny is considered feature-complete. New feature requests should be directed to HTTP::Tiny::UA.
SEE ALSO
- *
-
HTTP::Tiny::UA - Higher level UAfeatures for HTTP::Tiny
- *
- HTTP::Thin - HTTP::Tiny wrapper with HTTP::Request/HTTP::Response compatibility
- *
- HTTP::Tiny::Mech - Wrap WWW::Mechanize instance in HTTP::Tiny compatible interface
- *
- IO::Socket::IP - Required for IPv6 support
- *
-
IO::Socket::SSL - Required for SSLsupport
- *
- LWP::UserAgent - If HTTP::Tiny isn't enough for you, this is the ``standard'' way to do things
- *
-
Mozilla::CA - Required if you want to validate SSLcertificates
- *
-
Net::SSLeay - Required for SSLsupport
SUPPORT
Bugs / Feature Requests
Please report any bugs or feature requests through the issue tracker at <github.com/chansen/p5-http-tiny/issues>. You will be notified automatically of any progress on your issue.Source Code
This is open source software. The code repository is available for public review and contribution under the terms of the license.<github.com/chansen/p5-http-tiny>
git clone github.com/chansen/p5-http-tiny.git
AUTHORS
- *
- Christian Hansen <chansen@cpan.org>
- *
- David Golden <dagolden@cpan.org>
CONTRIBUTORS
- *
- Alan Gardner <gardner@pythian.com>
- *
- Alessandro Ghedini <al3xbio@gmail.com>
- *
- Brad Gilbert <bgills@cpan.org>
- *
- Chris Nehren <apeiron@cpan.org>
- *
- Chris Weyl <cweyl@alumni.drew.edu>
- *
- Claes Jakobsson <claes@surfar.nu>
- *
- Clinton Gormley <clint@traveljury.com>
- *
- Craig Berry <cberry@cpan.org>
- *
- David Mitchell <davem@iabyn.com>
- *
- Dean Pearce <pearce@pythian.com>
- *
- Edward Zborowski <ed@rubensteintech.com>
- *
- James Raspass <jraspass@gmail.com>
- *
- Jess Robinson <castaway@desert-island.me.uk>
- *
- Lukas Eklund <leklund@gmail.com>
- *
- Martin J. Evans <mjegh@ntlworld.com>
- *
- Martin-Louis Bright <mlbright@gmail.com>
- *
- Mike Doherty <doherty@cpan.org>
- *
- Olaf Alders <olaf@wundersolutions.com>
- *
- Petr PisaX <ppisar@redhat.com>
- *
- Serguei Trouchelle <stro@cpan.org>
- *
- So.ren Kornetzki <soeren.kornetzki@delti.com>
- *
-
Syohei YOSHIDA<syohex@gmail.com>
- *
- Tom Hukins <tom@eborcom.com>
- *
- Tony Cook <tony@develop-help.com>
COPYRIGHT AND LICENSE
This software is copyright (c) 2015 by Christian Hansen.This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.