<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;"># vim: ts=4 sts=4 sw=4 et:
#
# This file is part of HTTP-Tiny
#
# This software is copyright (c) 2011 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.
#
package HTTP::Tiny;
BEGIN {
  $HTTP::Tiny::VERSION = '0.012';
}
use strict;
use warnings;
# ABSTRACT: A small, simple, correct HTTP/1.1 client

use Carp ();


my @attributes;
BEGIN {
    @attributes = qw(agent default_headers max_redirect max_size proxy timeout);
    no strict 'refs';
    for my $accessor ( @attributes ) {
        *{$accessor} = sub {
            @_ &gt; 1 ? $_[0]-&gt;{$accessor} = $_[1] : $_[0]-&gt;{$accessor};
        };
    }
}

sub new {
    my($class, %args) = @_;
    (my $agent = $class) =~ s{::}{-}g;
    my $self = {
        agent        =&gt; $agent . "/" . ($class-&gt;VERSION || 0),
        max_redirect =&gt; 5,
        timeout      =&gt; 60,
    };
    for my $key ( @attributes ) {
        $self-&gt;{$key} = $args{$key} if exists $args{$key}
    }
    return bless $self, $class;
}


sub get {
    my ($self, $url, $args) = @_;
    @_ == 2 || (@_ == 3 &amp;&amp; ref $args eq 'HASH')
      or Carp::croak(q/Usage: $http-&gt;get(URL, [HASHREF])/);
    return $self-&gt;request('GET', $url, $args || {});
}


sub mirror {
    my ($self, $url, $file, $args) = @_;
    @_ == 3 || (@_ == 4 &amp;&amp; ref $args eq 'HASH')
      or Carp::croak(q/Usage: $http-&gt;mirror(URL, FILE, [HASHREF])/);
    if ( -e $file and my $mtime = (stat($file))[9] ) {
        $args-&gt;{headers}{'if-modified-since'} ||= $self-&gt;_http_date($mtime);
    }
    my $tempfile = $file . int(rand(2**31));
    open my $fh, "&gt;", $tempfile
        or Carp::croak(qq/Error: Could not open temporary file $tempfile for downloading: $!/);
    binmode $fh;
    $args-&gt;{data_callback} = sub { print {$fh} $_[0] };
    my $response = $self-&gt;request('GET', $url, $args);
    close $fh
        or Carp::croak(qq/Error: Could not close temporary file $tempfile: $!/);
    if ( $response-&gt;{success} ) {
        rename $tempfile, $file
            or Carp::croak "Error replacing $file with $tempfile: $!\n";
        my $lm = $response-&gt;{headers}{'last-modified'};
        if ( $lm and my $mtime = $self-&gt;_parse_http_date($lm) ) {
            utime $mtime, $mtime, $file;
        }
    }
    $response-&gt;{success} ||= $response-&gt;{status} eq '304';
    unlink $tempfile;
    return $response;
}


my %idempotent = map { $_ =&gt; 1 } qw/GET HEAD PUT DELETE OPTIONS TRACE/;

sub request {
    my ($self, $method, $url, $args) = @_;
    @_ == 3 || (@_ == 4 &amp;&amp; ref $args eq 'HASH')
      or Carp::croak(q/Usage: $http-&gt;request(METHOD, URL, [HASHREF])/);
    $args ||= {}; # we keep some state in this during _request

    # RFC 2616 Section 8.1.4 mandates a single retry on broken socket
    my $response;
    for ( 0 .. 1 ) {
        $response = eval { $self-&gt;_request($method, $url, $args) };
        last unless $@ &amp;&amp; $idempotent{$method}
            &amp;&amp; $@ =~ m{^(?:Socket closed|Unexpected end)};
    }

    if (my $e = "$@") {
        $response = {
            success =&gt; q{},
            status  =&gt; 599,
            reason  =&gt; 'Internal Exception',
            content =&gt; $e,
            headers =&gt; {
                'content-type'   =&gt; 'text/plain',
                'content-length' =&gt; length $e,
            }
        };
    }
    return $response;
}

my %DefaultPort = (
    http =&gt; 80,
    https =&gt; 443,
);

sub _request {
    my ($self, $method, $url, $args) = @_;

    my ($scheme, $host, $port, $path_query) = $self-&gt;_split_url($url);

    my $request = {
        method    =&gt; $method,
        scheme    =&gt; $scheme,
        host_port =&gt; ($port == $DefaultPort{$scheme} ? $host : "$host:$port"),
        uri       =&gt; $path_query,
        headers   =&gt; {},
    };

    my $handle  = HTTP::Tiny::Handle-&gt;new(timeout =&gt; $self-&gt;{timeout});

    if ($self-&gt;{proxy}) {
        $request-&gt;{uri} = "$scheme://$request-&gt;{host_port}$path_query";
        croak(qq/HTTPS via proxy is not supported/)
            if $request-&gt;{scheme} eq 'https';
        $handle-&gt;connect(($self-&gt;_split_url($self-&gt;{proxy}))[0..2]);
    }
    else {
        $handle-&gt;connect($scheme, $host, $port);
    }

    $self-&gt;_prepare_headers_and_cb($request, $args);
    $handle-&gt;write_request($request);

    my $response;
    do { $response = $handle-&gt;read_response_header }
        until (substr($response-&gt;{status},0,1) ne '1');

    if ( my @redir_args = $self-&gt;_maybe_redirect($request, $response, $args) ) {
        $handle-&gt;close;
        return $self-&gt;_request(@redir_args, $args);
    }

    if ($method eq 'HEAD' || $response-&gt;{status} =~ /^[23]04/) {
        # response has no message body
    }
    else {
        my $data_cb = $self-&gt;_prepare_data_cb($response, $args);
        $handle-&gt;read_body($data_cb, $response);
    }

    $handle-&gt;close;
    $response-&gt;{success} = substr($response-&gt;{status},0,1) eq '2';
    return $response;
}

sub _prepare_headers_and_cb {
    my ($self, $request, $args) = @_;

    for ($self-&gt;{default_headers}, $args-&gt;{headers}) {
        next unless defined;
        while (my ($k, $v) = each %$_) {
            $request-&gt;{headers}{lc $k} = $v;
        }
    }
    $request-&gt;{headers}{'host'}         = $request-&gt;{host_port};
    $request-&gt;{headers}{'connection'}   = "close";
    $request-&gt;{headers}{'user-agent'} ||= $self-&gt;{agent};

    if (defined $args-&gt;{content}) {
        $request-&gt;{headers}{'content-type'} ||= "application/octet-stream";
        if (ref $args-&gt;{content} eq 'CODE') {
            $request-&gt;{headers}{'transfer-encoding'} = 'chunked'
              unless $request-&gt;{headers}{'content-length'}
                  || $request-&gt;{headers}{'transfer-encoding'};
            $request-&gt;{cb} = $args-&gt;{content};
        }
        else {
            my $content = $args-&gt;{content};
            if ( $] ge '5.008' ) {
                utf8::downgrade($content, 1)
                    or Carp::croak(q/Wide character in request message body/);
            }
            $request-&gt;{headers}{'content-length'} = length $content
              unless $request-&gt;{headers}{'content-length'}
                  || $request-&gt;{headers}{'transfer-encoding'};
            $request-&gt;{cb} = sub { substr $content, 0, length $content, '' };
        }
        $request-&gt;{trailer_cb} = $args-&gt;{trailer_callback}
            if ref $args-&gt;{trailer_callback} eq 'CODE';
    }
    return;
}

sub _prepare_data_cb {
    my ($self, $response, $args) = @_;
    my $data_cb = $args-&gt;{data_callback};
    $response-&gt;{content} = '';

    if (!$data_cb || $response-&gt;{status} !~ /^2/) {
        if (defined $self-&gt;{max_size}) {
            $data_cb = sub {
                $_[1]-&gt;{content} .= $_[0];
                die(qq/Size of response body exceeds the maximum allowed of $self-&gt;{max_size}\n/)
                  if length $_[1]-&gt;{content} &gt; $self-&gt;{max_size};
            };
        }
        else {
            $data_cb = sub { $_[1]-&gt;{content} .= $_[0] };
        }
    }
    return $data_cb;
}

sub _maybe_redirect {
    my ($self, $request, $response, $args) = @_;
    my $headers = $response-&gt;{headers};
    my ($status, $method) = ($response-&gt;{status}, $request-&gt;{method});
    if (($status eq '303' or ($status =~ /^30[127]/ &amp;&amp; $method =~ /^GET|HEAD$/))
        and $headers-&gt;{location}
        and ++$args-&gt;{redirects} &lt;= $self-&gt;{max_redirect}
    ) {
        my $location = ($headers-&gt;{location} =~ /^\//)
            ? "$request-&gt;{scheme}://$request-&gt;{host_port}$headers-&gt;{location}"
            : $headers-&gt;{location} ;
        return (($status eq '303' ? 'GET' : $method), $location);
    }
    return;
}

sub _split_url {
    my $url = pop;

    # URI regex adapted from the URI module
    my ($scheme, $authority, $path_query) = $url =~ m&lt;\A([^:/?#]+)://([^/?#]*)([^#]*)&gt;
      or Carp::croak(qq/Cannot parse URL: '$url'/);

    $scheme     = lc $scheme;
    $path_query = "/$path_query" unless $path_query =~ m&lt;\A/&gt;;

    my $host = (length($authority)) ? lc $authority : 'localhost';
       $host =~ s/\A[^@]*@//;   # userinfo
    my $port = do {
       $host =~ s/:([0-9]*)\z// &amp;&amp; length $1
         ? $1
         : ($scheme eq 'http' ? 80 : $scheme eq 'https' ? 443 : undef);
    };

    return ($scheme, $host, $port, $path_query);
}

# Date conversions adapted from HTTP::Date
my $DoW = "Sun|Mon|Tue|Wed|Thu|Fri|Sat";
my $MoY = "Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec";
sub _http_date {
    my ($sec, $min, $hour, $mday, $mon, $year, $wday) = gmtime($_[1]);
    return sprintf("%s, %02d %s %04d %02d:%02d:%02d GMT",
        substr($DoW,$wday*4,3),
        $mday, substr($MoY,$mon*4,3), $year+1900,
        $hour, $min, $sec
    );
}

sub _parse_http_date {
    my ($self, $str) = @_;
    require Time::Local;
    my @tl_parts;
    if ($str =~ /^[SMTWF][a-z]+, +(\d{1,2}) ($MoY) +(\d\d\d\d) +(\d\d):(\d\d):(\d\d) +GMT$/) {
        @tl_parts = ($6, $5, $4, $1, (index($MoY,$2)/4), $3);
    }
    elsif ($str =~ /^[SMTWF][a-z]+, +(\d\d)-($MoY)-(\d{2,4}) +(\d\d):(\d\d):(\d\d) +GMT$/ ) {
        @tl_parts = ($6, $5, $4, $1, (index($MoY,$2)/4), $3);
    }
    elsif ($str =~ /^[SMTWF][a-z]+ +($MoY) +(\d{1,2}) +(\d\d):(\d\d):(\d\d) +(?:[^0-9]+ +)?(\d\d\d\d)$/ ) {
        @tl_parts = ($5, $4, $3, $2, (index($MoY,$1)/4), $6);
    }
    return eval {
        my $t = @tl_parts ? Time::Local::timegm(@tl_parts) : -1;
        $t &lt; 0 ? undef : $t;
    };
}

package
    HTTP::Tiny::Handle; # hide from PAUSE/indexers
use strict;
use warnings;

use Carp       qw[croak];
use Errno      qw[EINTR EPIPE];
use IO::Socket qw[SOCK_STREAM];

sub BUFSIZE () { 32768 }

my $Printable = sub {
    local $_ = shift;
    s/\r/\\r/g;
    s/\n/\\n/g;
    s/\t/\\t/g;
    s/([^\x20-\x7E])/sprintf('\\x%.2X', ord($1))/ge;
    $_;
};

my $Token = qr/[\x21\x23-\x27\x2A\x2B\x2D\x2E\x30-\x39\x41-\x5A\x5E-\x7A\x7C\x7E]/;

sub new {
    my ($class, %args) = @_;
    return bless {
        rbuf             =&gt; '',
        timeout          =&gt; 60,
        max_line_size    =&gt; 16384,
        max_header_lines =&gt; 64,
        %args
    }, $class;
}

my $ssl_verify_args = {
    check_cn =&gt; "when_only",
    wildcards_in_alt =&gt; "anywhere",
    wildcards_in_cn =&gt; "anywhere"
};

sub connect {
    @_ == 4 || croak(q/Usage: $handle-&gt;connect(scheme, host, port)/);
    my ($self, $scheme, $host, $port) = @_;

    if ( $scheme eq 'https' ) {
        eval "require IO::Socket::SSL"
            unless exists $INC{'IO/Socket/SSL.pm'};
        croak(qq/IO::Socket::SSL must be installed for https support\n/)
            unless $INC{'IO/Socket/SSL.pm'};
    }
    elsif ( $scheme ne 'http' ) {
      croak(qq/Unsupported URL scheme '$scheme'/);
    }

    $self-&gt;{fh} = 'IO::Socket::INET'-&gt;new(
        PeerHost  =&gt; $host,
        PeerPort  =&gt; $port,
        Proto     =&gt; 'tcp',
        Type      =&gt; SOCK_STREAM,
        Timeout   =&gt; $self-&gt;{timeout}
    ) or croak(qq/Could not connect to '$host:$port': $@/);

    binmode($self-&gt;{fh})
      or croak(qq/Could not binmode() socket: '$!'/);

    if ( $scheme eq 'https') {
        IO::Socket::SSL-&gt;start_SSL($self-&gt;{fh});
        ref($self-&gt;{fh}) eq 'IO::Socket::SSL'
            or die(qq/SSL connection failed for $host\n/);
        $self-&gt;{fh}-&gt;verify_hostname( $host, $ssl_verify_args )
            or die(qq/SSL certificate not valid for $host\n/);
    }

    $self-&gt;{host} = $host;
    $self-&gt;{port} = $port;

    return $self;
}

sub close {
    @_ == 1 || croak(q/Usage: $handle-&gt;close()/);
    my ($self) = @_;
    CORE::close($self-&gt;{fh})
      or croak(qq/Could not close socket: '$!'/);
}

sub write {
    @_ == 2 || croak(q/Usage: $handle-&gt;write(buf)/);
    my ($self, $buf) = @_;

    if ( $] ge '5.008' ) {
        utf8::downgrade($buf, 1)
            or croak(q/Wide character in write()/);
    }

    my $len = length $buf;
    my $off = 0;

    local $SIG{PIPE} = 'IGNORE';

    while () {
        $self-&gt;can_write
          or croak(q/Timed out while waiting for socket to become ready for writing/);
        my $r = syswrite($self-&gt;{fh}, $buf, $len, $off);
        if (defined $r) {
            $len -= $r;
            $off += $r;
            last unless $len &gt; 0;
        }
        elsif ($! == EPIPE) {
            croak(qq/Socket closed by remote server: $!/);
        }
        elsif ($! != EINTR) {
            croak(qq/Could not write to socket: '$!'/);
        }
    }
    return $off;
}

sub read {
    @_ == 2 || @_ == 3 || croak(q/Usage: $handle-&gt;read(len [, allow_partial])/);
    my ($self, $len, $allow_partial) = @_;

    my $buf  = '';
    my $got = length $self-&gt;{rbuf};

    if ($got) {
        my $take = ($got &lt; $len) ? $got : $len;
        $buf  = substr($self-&gt;{rbuf}, 0, $take, '');
        $len -= $take;
    }

    while ($len &gt; 0) {
        $self-&gt;can_read
          or croak(q/Timed out while waiting for socket to become ready for reading/);
        my $r = sysread($self-&gt;{fh}, $buf, $len, length $buf);
        if (defined $r) {
            last unless $r;
            $len -= $r;
        }
        elsif ($! != EINTR) {
            croak(qq/Could not read from socket: '$!'/);
        }
    }
    if ($len &amp;&amp; !$allow_partial) {
        croak(q/Unexpected end of stream/);
    }
    return $buf;
}

sub readline {
    @_ == 1 || croak(q/Usage: $handle-&gt;readline()/);
    my ($self) = @_;

    while () {
        if ($self-&gt;{rbuf} =~ s/\A ([^\x0D\x0A]* \x0D?\x0A)//x) {
            return $1;
        }
        if (length $self-&gt;{rbuf} &gt;= $self-&gt;{max_line_size}) {
            croak(qq/Line size exceeds the maximum allowed size of $self-&gt;{max_line_size}/);
        }
        $self-&gt;can_read
          or croak(q/Timed out while waiting for socket to become ready for reading/);
        my $r = sysread($self-&gt;{fh}, $self-&gt;{rbuf}, BUFSIZE, length $self-&gt;{rbuf});
        if (defined $r) {
            last unless $r;
        }
        elsif ($! != EINTR) {
            croak(qq/Could not read from socket: '$!'/);
        }
    }
    croak(q/Unexpected end of stream while looking for line/);
}

sub read_header_lines {
    @_ == 1 || @_ == 2 || croak(q/Usage: $handle-&gt;read_header_lines([headers])/);
    my ($self, $headers) = @_;
    $headers ||= {};
    my $lines   = 0;
    my $val;

    while () {
         my $line = $self-&gt;readline;

         if (++$lines &gt;= $self-&gt;{max_header_lines}) {
             croak(qq/Header lines exceeds maximum number allowed of $self-&gt;{max_header_lines}/);
         }
         elsif ($line =~ /\A ([^\x00-\x1F\x7F:]+) : [\x09\x20]* ([^\x0D\x0A]*)/x) {
             my ($field_name) = lc $1;
             if (exists $headers-&gt;{$field_name}) {
                 for ($headers-&gt;{$field_name}) {
                     $_ = [$_] unless ref $_ eq "ARRAY";
                     push @$_, $2;
                     $val = \$_-&gt;[-1];
                 }
             }
             else {
                 $val = \($headers-&gt;{$field_name} = $2);
             }
         }
         elsif ($line =~ /\A [\x09\x20]+ ([^\x0D\x0A]*)/x) {
             $val
               or croak(q/Unexpected header continuation line/);
             next unless length $1;
             $$val .= ' ' if length $$val;
             $$val .= $1;
         }
         elsif ($line =~ /\A \x0D?\x0A \z/x) {
            last;
         }
         else {
            croak(q/Malformed header line: / . $Printable-&gt;($line));
         }
    }
    return $headers;
}

sub write_request {
    @_ == 2 || croak(q/Usage: $handle-&gt;write_request(request)/);
    my($self, $request) = @_;
    $self-&gt;write_request_header(@{$request}{qw/method uri headers/});
    $self-&gt;write_body($request) if $request-&gt;{cb};
    return;
}

my %HeaderCase = (
    'content-md5'      =&gt; 'Content-MD5',
    'etag'             =&gt; 'ETag',
    'te'               =&gt; 'TE',
    'www-authenticate' =&gt; 'WWW-Authenticate',
    'x-xss-protection' =&gt; 'X-XSS-Protection',
);

sub write_header_lines {
    (@_ == 2 &amp;&amp; ref $_[1] eq 'HASH') || croak(q/Usage: $handle-&gt;write_header_lines(headers)/);
    my($self, $headers) = @_;

    my $buf = '';
    while (my ($k, $v) = each %$headers) {
        my $field_name = lc $k;
        if (exists $HeaderCase{$field_name}) {
            $field_name = $HeaderCase{$field_name};
        }
        else {
            $field_name =~ /\A $Token+ \z/xo
              or croak(q/Invalid HTTP header field name: / . $Printable-&gt;($field_name));
            $field_name =~ s/\b(\w)/\u$1/g;
            $HeaderCase{lc $field_name} = $field_name;
        }
        for (ref $v eq 'ARRAY' ? @$v : $v) {
            /[^\x0D\x0A]/
              or croak(qq/Invalid HTTP header field value ($field_name): / . $Printable-&gt;($_));
            $buf .= "$field_name: $_\x0D\x0A";
        }
    }
    $buf .= "\x0D\x0A";
    return $self-&gt;write($buf);
}

sub read_body {
    @_ == 3 || croak(q/Usage: $handle-&gt;read_body(callback, response)/);
    my ($self, $cb, $response) = @_;
    my $te = $response-&gt;{headers}{'transfer-encoding'} || '';
    if ( grep { /chunked/i } ( ref $te eq 'ARRAY' ? @$te : $te ) ) {
        $self-&gt;read_chunked_body($cb, $response);
    }
    else {
        $self-&gt;read_content_body($cb, $response);
    }
    return;
}

sub write_body {
    @_ == 2 || croak(q/Usage: $handle-&gt;write_body(request)/);
    my ($self, $request) = @_;
    if ($request-&gt;{headers}{'content-length'}) {
        return $self-&gt;write_content_body($request);
    }
    else {
        return $self-&gt;write_chunked_body($request);
    }
}

sub read_content_body {
    @_ == 3 || @_ == 4 || croak(q/Usage: $handle-&gt;read_content_body(callback, response, [read_length])/);
    my ($self, $cb, $response, $content_length) = @_;
    $content_length ||= $response-&gt;{headers}{'content-length'};

    if ( $content_length ) {
        my $len = $content_length;
        while ($len &gt; 0) {
            my $read = ($len &gt; BUFSIZE) ? BUFSIZE : $len;
            $cb-&gt;($self-&gt;read($read, 0), $response);
            $len -= $read;
        }
    }
    else {
        my $chunk;
        $cb-&gt;($chunk, $response) while length( $chunk = $self-&gt;read(BUFSIZE, 1) );
    }

    return;
}

sub write_content_body {
    @_ == 2 || croak(q/Usage: $handle-&gt;write_content_body(request)/);
    my ($self, $request) = @_;

    my ($len, $content_length) = (0, $request-&gt;{headers}{'content-length'});
    while () {
        my $data = $request-&gt;{cb}-&gt;();

        defined $data &amp;&amp; length $data
          or last;

        if ( $] ge '5.008' ) {
            utf8::downgrade($data, 1)
                or croak(q/Wide character in write_content()/);
        }

        $len += $self-&gt;write($data);
    }

    $len == $content_length
      or croak(qq/Content-Length missmatch (got: $len expected: $content_length)/);

    return $len;
}

sub read_chunked_body {
    @_ == 3 || croak(q/Usage: $handle-&gt;read_chunked_body(callback, $response)/);
    my ($self, $cb, $response) = @_;

    while () {
        my $head = $self-&gt;readline;

        $head =~ /\A ([A-Fa-f0-9]+)/x
          or croak(q/Malformed chunk head: / . $Printable-&gt;($head));

        my $len = hex($1)
          or last;

        $self-&gt;read_content_body($cb, $response, $len);

        $self-&gt;read(2) eq "\x0D\x0A"
          or croak(q/Malformed chunk: missing CRLF after chunk data/);
    }
    $self-&gt;read_header_lines($response-&gt;{headers});
    return;
}

sub write_chunked_body {
    @_ == 2 || croak(q/Usage: $handle-&gt;write_chunked_body(request)/);
    my ($self, $request) = @_;

    my $len = 0;
    while () {
        my $data = $request-&gt;{cb}-&gt;();

        defined $data &amp;&amp; length $data
          or last;

        if ( $] ge '5.008' ) {
            utf8::downgrade($data, 1)
                or croak(q/Wide character in write_chunked_body()/);
        }

        $len += length $data;

        my $chunk  = sprintf '%X', length $data;
           $chunk .= "\x0D\x0A";
           $chunk .= $data;
           $chunk .= "\x0D\x0A";

        $self-&gt;write($chunk);
    }
    $self-&gt;write("0\x0D\x0A");
    $self-&gt;write_header_lines($request-&gt;{trailer_cb}-&gt;())
        if ref $request-&gt;{trailer_cb} eq 'CODE';
    return $len;
}

sub read_response_header {
    @_ == 1 || croak(q/Usage: $handle-&gt;read_response_header()/);
    my ($self) = @_;

    my $line = $self-&gt;readline;

    $line =~ /\A (HTTP\/(0*\d+\.0*\d+)) [\x09\x20]+ ([0-9]{3}) [\x09\x20]+ ([^\x0D\x0A]*) \x0D?\x0A/x
      or croak(q/Malformed Status-Line: / . $Printable-&gt;($line));

    my ($protocol, $version, $status, $reason) = ($1, $2, $3, $4);

    croak (qq/Unsupported HTTP protocol: $protocol/)
        unless $version =~ /0*1\.0*[01]/;

    return {
        status   =&gt; $status,
        reason   =&gt; $reason,
        headers  =&gt; $self-&gt;read_header_lines,
        protocol =&gt; $protocol,
    };
}

sub write_request_header {
    @_ == 4 || croak(q/Usage: $handle-&gt;write_request_header(method, request_uri, headers)/);
    my ($self, $method, $request_uri, $headers) = @_;

    return $self-&gt;write("$method $request_uri HTTP/1.1\x0D\x0A")
         + $self-&gt;write_header_lines($headers);
}

sub _do_timeout {
    my ($self, $type, $timeout) = @_;
    $timeout = $self-&gt;{timeout}
        unless defined $timeout &amp;&amp; $timeout &gt;= 0;

    my $fd = fileno $self-&gt;{fh};
    defined $fd &amp;&amp; $fd &gt;= 0
      or croak(q/select(2): 'Bad file descriptor'/);

    my $initial = time;
    my $pending = $timeout;
    my $nfound;

    vec(my $fdset = '', $fd, 1) = 1;

    while () {
        $nfound = ($type eq 'read')
            ? select($fdset, undef, undef, $pending)
            : select(undef, $fdset, undef, $pending) ;
        if ($nfound == -1) {
            $! == EINTR
              or croak(qq/select(2): '$!'/);
            redo if !$timeout || ($pending = $timeout - (time - $initial)) &gt; 0;
            $nfound = 0;
        }
        last;
    }
    $! = 0;
    return $nfound;
}

sub can_read {
    @_ == 1 || @_ == 2 || croak(q/Usage: $handle-&gt;can_read([timeout])/);
    my $self = shift;
    return $self-&gt;_do_timeout('read', @_)
}

sub can_write {
    @_ == 1 || @_ == 2 || croak(q/Usage: $handle-&gt;can_write([timeout])/);
    my $self = shift;
    return $self-&gt;_do_timeout('write', @_)
}

1;



__END__
=pod

=head1 NAME

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

=head1 VERSION

version 0.012

=head1 SYNOPSIS

    use HTTP::Tiny;

    my $response = HTTP::Tiny-&gt;new-&gt;get('http://example.com/');

    die "Failed!\n" unless $response-&gt;{success};

    print "$response-&gt;{status} $response-&gt;{reason}\n";

    while (my ($k, $v) = each %{$response-&gt;{headers}}) {
        for (ref $v eq 'ARRAY' ? @$v : $v) {
            print "$k: $_\n";
        }
    }

    print $response-&gt;{content} if length $response-&gt;{content};

=head1 DESCRIPTION

This is a very simple HTTP/1.1 client, designed primarily for doing simple GET
requests without the overhead of a large framework like L&lt;LWP::UserAgent&gt;.

It is more correct and more complete than L&lt;HTTP::Lite&gt;.  It supports
proxies (currently only non-authenticating ones) and redirection.  It
also correctly resumes after EINTR.

=head1 METHODS

=head2 new

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

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

=over 4

=item *

agent

A user-agent string (defaults to 'HTTP::Tiny/$VERSION')

=item *

default_headers

A hashref of default headers to apply to requests

=item *

max_redirect

Maximum number of redirects allowed (defaults to 5)

=item *

max_size

Maximum response size (only when not using a data callback).  If defined,
responses larger than this will die with an error message

=item *

proxy

URL of a proxy server to use.

=item *

timeout

Request timeout in seconds (default is 60)

=back

=head2 get

    $response = $http-&gt;get($url);
    $response = $http-&gt;get($url, \%options);

Executes a C&lt;GET&gt; request for the given URL.  The URL must have unsafe
characters escaped and international domain names encoded.  Internally, it just
calls C&lt;request()&gt; with 'GET' as the method.  See C&lt;request()&gt; for valid
options and a description of the response.

=head2 mirror

    $response = $http-&gt;mirror($url, $file, \%options)
    if ( $response-&gt;{success} ) {
        print "$file is up to date\n";
    }

Executes a C&lt;GET&gt; 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 an
C&lt;If-Modified-Since&gt; header with the modification timestamp of the file.  You
may specificy a different C&lt;If-Modified-Since&gt; header yourself in the C&lt;&lt;
$options-&gt;{headers} &gt;&gt; hash.

The C&lt;success&gt; field of the response will be true if the status code is 2XX
or 304 (unmodified).

If the file was modified and the server response includes a properly
formatted C&lt;Last-Modified&gt; header, the file modification time will
be updated accordingly.

=head2 request

    $response = $http-&gt;request($method, $url);
    $response = $http-&gt;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:

=over 4

=item *

headers

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.

=item *

content

A scalar to include as the body of the request OR a code reference
that will be called iteratively to produce the body of the response

=item *

trailer_callback

A code reference that will be called if it exists to provide a hashref
of trailing headers (only used with chunked transfer-encoding)

=item *

data_callback

A code reference that will be called for each chunks of the response
body received.

=back

If the C&lt;content&gt; 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 C&lt;data_callback&gt; 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 C&lt;status&gt; or C&lt;headers&gt;
received prior to the content body.)

The C&lt;request&gt; method returns a hashref containing the response.  The hashref
will have the following keys:

=over 4

=item *

success

Boolean indicating whether the operation returned a 2XX status code

=item *

status

The HTTP status code of the response

=item *

reason

The response phrase returned by the server

=item *

content

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

=item *

headers

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

=back

On an exception during the execution of the request, the C&lt;status&gt; field will
contain 599, and the C&lt;content&gt; field will contain the text of the exception.

=for Pod::Coverage agent
default_headers
max_redirect
max_size
proxy
timeout

=head1 LIMITATIONS

HTTP::Tiny is I&lt;conditionally compliant&gt; with the
L&lt;HTTP/1.1 specification|http://www.w3.org/Protocols/rfc2616/rfc2616.html&gt;.
It attempts to meet all "MUST" requirements of the specification, but does not
implement all "SHOULD" requirements.

Some particular limitations of note include:

=over

=item *

HTTP::Tiny focuses on correct transport.  Users are responsible for ensuring
that user-defined headers and content are compliant with the HTTP/1.1
specification.

=item *

Users must ensure that URLs are properly escaped for unsafe characters and that
international domain names are properly encoded to ASCII. See L&lt;URI::Escape&gt;,
L&lt;URI::_punycode&gt; and L&lt;Net::IDN::Encode&gt;.

=item *

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.

=item *

Persistant connections are not supported.  The C&lt;Connection&gt; header will
always be set to C&lt;close&gt;.

=item *

Direct C&lt;https&gt; connections are supported only if L&lt;IO::Socket::SSL&gt; is
installed.  There is no support for C&lt;https&gt; connections via proxy.
Any SSL certificate that matches the host is accepted -- SSL certificates
are not verified against certificate authorities.

=item *

Cookies are not directly supported.  Users that set a C&lt;Cookie&gt; header
should also set C&lt;max_redirect&gt; to zero to ensure cookies are not
inappropriately re-transmitted.

=item *

Proxy environment variables are not supported.

=item *

There is no provision for delaying a request body using an C&lt;Expect&gt; header.
Unexpected C&lt;1XX&gt; responses are silently ignored as per the specification.

=item *

Only 'chunked' C&lt;Transfer-Encoding&gt; is supported.

=item *

There is no support for a Request-URI of '*' for the 'OPTIONS' request.

=back

=head1 SEE ALSO

=over 4

=item *

L&lt;LWP::UserAgent&gt;

=back

=for :stopwords cpan testmatrix url annocpan anno bugtracker rt cpants kwalitee diff irc mailto metadata placeholders

=head1 SUPPORT

=head2 Bugs / Feature Requests

Please report any bugs or feature requests by email to C&lt;bug-http-tiny at rt.cpan.org&gt;, or through
the web interface at L&lt;http://rt.cpan.org/Public/Dist/Display.html?Name=HTTP-Tiny&gt;. You will be automatically notified of any
progress on the request by the system.

=head2 Source Code

This is open source software.  The code repository is available for
public review and contribution under the terms of the license.

L&lt;http://github.com/dagolden/p5-http-tiny/tree&gt;

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

=head1 AUTHORS

=over 4

=item *

Christian Hansen &lt;chansen@cpan.org&gt;

=item *

David Golden &lt;dagolden@cpan.org&gt;

=back

=head1 COPYRIGHT AND LICENSE

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

=cut

</pre></body></html>