Movatterモバイル変換


[0]ホーム

URL:


HTTP::Tiny
(source,CPAN)
version 0.025
You are viewing the version of this documentation from Perl 5.18.2.View the latest version

CONTENTS

#NAME

HTTP::Tiny - A small, simple, correct HTTP/1.1 client

#VERSION

version 0.025

#SYNOPSIS

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 simple HTTP/1.1 client, designed for doing simple GET requests without the overhead of a large framework likeLWP::UserAgent.

It is more correct and more complete thanHTTP::Lite. It supports proxies (currently only non-authenticating ones) and redirection. It also correctly resumes after EINTR.

#METHODS

#new

$http = HTTP::Tiny->new( %attributes );

This constructor returns a new HTTP::Tiny object. Valid attributes include:

Exceptions frommax_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.

See"SSL SUPPORT" for more on theverify_SSL andSSL_options attributes.

#get|head|put|post|delete

$response = $http->get($url);$response = $http->get($url, \%options);$response = $http->head($url);

These methods are shorthand for callingrequest() for the given method. The URL must have unsafe characters escaped and international domain names encoded. Seerequest() for valid options and a description of the response.

Thesuccess 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 aPOST request and sends the key/value pairs from a form data hash or array reference to the given URL with acontent-type ofapplication/x-www-form-urlencoded. See documentation for thewww_form_urlencode method for details on the encoding.

The URL must have unsafe characters escaped and international domain names encoded. Seerequest() for valid options and a description of the response. Anycontent-type header or content in the options hashref will be ignored.

Thesuccess 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 aGET request for the URL and saves the response body to the file name provided. The URL must have unsafe characters escaped and international domain names encoded. If the file already exists, the request will includes anIf-Modified-Since header with the modification timestamp of the file. You may specify a differentIf-Modified-Since header yourself in the$options->{headers} hash.

Thesuccess 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 formattedLast-Modified header, the file modification time will be updated accordingly.

#request

$response = $http->request($method, $url);$response = $http->request($method, $url, \%options);

Executes an HTTP request of the given method type ('GET', 'HEAD', 'POST', 'PUT', etc.) on the given URL. The URL must have unsafe characters escaped and international domain names encoded. A hashref of options may be appended to modify the request.

Valid options are:

If thecontent 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 thedata_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 thestatus orheaders received prior to the content body.)

Therequest method returns a hashref containing the response. The hashref will have the following keys:

On an exception during the execution of the request, thestatus field will contain 599, and thecontent 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 ax-www-form-urlencoded string. The keys and values from the data reference will be UTF-8 encoded and escaped per RFC 3986. If a value is an array reference, the key will be repeated with each of the values of the array reference. The key/value pairs in the resulting string will be sorted by key and value.

#SSL SUPPORT

Directhttps connections are supported only ifIO::Socket::SSL 1.56 or greater andNet::SSLeay 1.49 or greater are installed. An exception will be thrown if a new enough versions of these modules not installed or if the SSL encryption fails. There is no support forhttps connections via proxy (i.e. RFC 2817).

SSL provides two distinct capabilities:

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 (CA) trust model to validate a certificate as legitimate. This discriminates against servers with self-signed certificates or certificates signed by free, community-driven CA's such asCAcert.org.

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 theverify_SSL attribute to a true value will make HTTP::Tiny verify that an SSL connection has a valid SSL certificate corresponding to the host name of the connection and that the SSL certificate has been verified by a CA. Assuming you trust the CA, this will protect against aman-in-the-middle attack. If you are concerned about security, you should enable this option.

Certificate verification requires a file containing trusted CA certificates. If theMozilla::CA module is installed, HTTP::Tiny will use the CA file included with it as a source of trusted CA's. (This means you trust Mozilla, the author of Mozilla::CA, the CPAN mirror where you got Mozilla::CA, the toolchain used to install it, and your operating system security, right?)

If that module is not available, then HTTP::Tiny will search several system-specific default locations for a CA certificate file:

An exception will be raised ifverify_SSL is true and no CA certificate file is available.

If you desire complete control over SSL connections, theSSL_options attribute lets you provide a hash reference that will be passed through toIO::Socket::SSL::start_SSL(), overriding any options set by HTTP::Tiny. For example, to provide your own trusted CA file:

SSL_options => {    SSL_ca_file => $file_path,}

TheSSL_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 SSL connection. SeeIO::Socket::SSL documentation for details.

#LIMITATIONS

HTTP::Tiny isconditionally compliant with theHTTP/1.1 specification. It attempts to meet all "MUST" requirements of the specification, but does not implement all "SHOULD" requirements.

Some particular limitations of note include:

#SEE ALSO

#SUPPORT

#Bugs / Feature Requests

Please report any bugs or feature requests through the issue tracker athttps://rt.cpan.org/Public/Dist/Display.html?Name=HTTP-Tiny. 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.

https://github.com/dagolden/http-tiny

git clone git://github.com/dagolden/http-tiny.git

#AUTHORS

#COPYRIGHT AND LICENSE

This software is copyright (c) 2012 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.

Perldoc Browser is maintained by Dan Book (DBOOK). Please contact him via theGitHub issue tracker oremail regarding any issues with the site itself, search, or rendering of documentation.

The Perl documentation is maintained by the Perl 5 Porters in the development of Perl. Please contact them via thePerl issue tracker, themailing list, orIRC to report any issues with the contents or format of the documentation.


[8]ページ先頭

©2009-2026 Movatter.jp