Movatterモバイル変換


[0]ホーム

URL:


HTTP::Tiny
(source,CPAN)
version 0.090

CONTENTS

#NAME

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

#VERSION

version 0.090

#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 requests without the overhead of a large framework likeLWP::UserAgent.

It is more correct and more complete thanHTTP::Lite. It supports proxies and redirection. It also correctly resumes after EINTR.

IfIO::Socket::IP 0.25 or later is installed, HTTP::Tiny will use it instead ofIO::Socket::INET for transparent support for both IPv4 and IPv6.

Cookie support requiresHTTP::CookieJar or an equivalent class.

#METHODS

#new

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

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

An accessor/mutator method exists for each attribute.

Passing an explicitundef forproxy,http_proxy orhttps_proxy will prevent getting the corresponding proxies from the environment.

Errors during request execution 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 error.

Thekeep_alive parameter enables a persistent connection, but only to a single destination scheme, host and port. If any connection-relevant attributes are modified via accessor, or if the process ID or thread ID change, the persistent connection will be dropped. If you want persistent connections across multiple destinations, use multiple HTTP::Tiny objects.

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

#get|head|put|post|patch|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. If data is provided as an array reference, the order is preserved; if provided as a hash reference, the terms are sorted by key for consistency. 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 include 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.

NOTE: Method names arecase-sensitive per the HTTP/1.1 specification. Don't useget when you really wantGET. SeeLIMITATIONS for how this applies to redirection.

If the URL includes a "user:password" stanza, they will be used for Basic-style authorization headers. (Authorization headers will not be included in a redirected request.) For example:

$http->request('GET', 'http://Aladdin:open sesame@example.com/');

If the "user:password" stanza contains reserved characters, they must be percent-escaped:

$http->request('GET', 'http://john%40example.com:password@example.com/');

A hashref of options may be appended to modify the request.

Valid options are:

TheHost header is generated from the URL in accordance with RFC 2616. It is a fatal error to specifyHost in theheaders option. Other headers may be ignored or overwritten if necessary for transport compliance.

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 thecontent option is the empty string, nocontent-type orcontent-length headers will be generated.

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

Content data in the request/response is handled as "raw bytes". Any encoding/decoding (with associated headers) are the responsibility of the caller.

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

On an error during the execution of the request, thestatus field will contain 599, and thecontent field will contain the text of the error.

#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. If data is provided as a hash reference, the key/value pairs in the resulting string will be sorted by key and value for consistent ordering.

#can_ssl

$ok         = HTTP::Tiny->can_ssl;($ok, $why) = HTTP::Tiny->can_ssl;($ok, $why) = $http->can_ssl;

Indicates if SSL support is available. When called as a class object, it checks for the correct version ofNet::SSLeay andIO::Socket::SSL. When called as an object methods, ifSSL_verify is true or ifSSL_verify_mode is set inSSL_options, it checks that a CA file is available.

In scalar context, returns a boolean indicating if SSL is available. In list context, returns the boolean and a (possibly multi-line) string of errors indicating why SSL isn't available.

#connected

$host = $http->connected;($host, $port) = $http->connected;

Indicates if a connection to a peer is being kept alive, per thekeep_alive option.

In scalar context, returns the peer host and port, joined with a colon, orundef (if no peer is connected). In list context, returns the peer host and port or an empty list (if no peer is connected).

Note: This method cannot reliably be used to discover whether the remote host has closed its end of the socket.

#TLS/SSL SUPPORT

Directhttps connections are supported only ifIO::Socket::SSL 1.56 or greater andNet::SSLeay 1.49 or greater are installed. An error will occur if new enough versions of these modules are not installed or if the TLS encryption fails. You can also useHTTP::Tiny::can_ssl() utility function that returns boolean to see if the required modules are installed.

Anhttps connection may be made via anhttp proxy that supports the CONNECT command (i.e. RFC 2817). You may not proxyhttps via a proxy that itself requireshttps to communicate.

TLS/SSL provides two distinct capabilities:

By default, HTTP::Tiny verifies server identity.

This was changed in version 0.083 due to security concerns. The previous default behavior can be enabled by setting$ENV{PERL_HTTP_TINY_SSL_INSECURE_BY_DEFAULT} to 1.

Verification is done by checking that that the TLS/SSL connection has a valid certificate corresponding to the host name of the connection and that the certificate has been verified by a CA. Assuming you trust the CA, this will protect againstmachine-in-the-middle attacks.

Certificate verification requires a file or directory containing trusted CA certificates.

IO::Socket::SSL::default_ca() is called to detect the default location of your CA certificates. This also supports the environment variablesSSL_CERT_FILE andSSL_CERT_DIR, and will fail over toMozilla::CA if no certs are found.

IfIO::Socket::SSL::default_ca() is not able to find usable CA certificates, HTTP::Tiny will search several well-known system-specific default locations for a CA certificate file as a last resort:

An error will be occur ifverify_SSL is true and no CA certificate file is available.

If you desire complete control over TLS/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 TLS/SSL connection. SeeIO::Socket::SSL documentation for details.

#PROXY SUPPORT

HTTP::Tiny can proxy bothhttp andhttps requests. Only Basic proxy authorization is supported and it must be provided as part of the proxy URL:http://user:pass@proxy.example.com/.

HTTP::Tiny supports the following proxy environment variables:

If theREQUEST_METHOD environment variable is set, then this might be a CGI process andHTTP_PROXY would be set from theProxy: header, which is a security risk. IfREQUEST_METHOD is set,HTTP_PROXY (the upper case variant only) is ignored, butCGI_HTTP_PROXY is considered instead.

Tunnellinghttps over anhttp proxy using the CONNECT method is supported. If your proxy useshttps itself, you can not tunnelhttps over it.

Be warned that proxying anhttps connection opens you to the risk of a man-in-the-middle attack by the proxy server.

Theno_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 tonew will override their corresponding environment variables.

#LIMITATIONS

HTTP::Tiny isconditionally compliant with theHTTP/1.1 specifications:

It attempts to meet all "MUST" requirements of the specification, but does not implement all "SHOULD" requirements. (Note: it was developed against the earlier RFC 2616 specification and may not yet meet the revised RFC 7230-7235 spec.) Additionally, HTTP::Tiny supports thePATCH method of RFC 5789.

Some particular limitations of note include:

Despite the limitations listed above, HTTP::Tiny is considered feature-complete. New feature requests should be directed toHTTP::Tiny::UA.

#SEE ALSO

#SUPPORT

#Bugs / Feature Requests

Please report any bugs or feature requests through the issue tracker athttps://github.com/Perl-Toolchain-Gang/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.

https://github.com/Perl-Toolchain-Gang/HTTP-Tiny

git clone https://github.com/Perl-Toolchain-Gang/HTTP-Tiny.git

#AUTHORS

#CONTRIBUTORS

#COPYRIGHT AND LICENSE

This software is copyright (c) 2024 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-2025 Movatter.jp