| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Net::ACME2::HTTP_Tiny; | 
| 2 |  |  |  |  |  |  |  | 
| 3 |  |  |  |  |  |  | =encoding utf-8 | 
| 4 |  |  |  |  |  |  |  | 
| 5 |  |  |  |  |  |  | =head1 NAME | 
| 6 |  |  |  |  |  |  |  | 
| 7 |  |  |  |  |  |  | Net::ACME2::HTTP_Tiny - HTTP client for Net::ACME | 
| 8 |  |  |  |  |  |  |  | 
| 9 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | use Net::ACME2::HTTP_Tiny; | 
| 12 |  |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  | my $http = Net::ACME2::HTTP_Tiny->new(); | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  | #NOTE: Unlike HTTP::Tiny’s method, this will die() if the HTTP | 
| 16 |  |  |  |  |  |  | #session itself fails--for example, if the network connection was | 
| 17 |  |  |  |  |  |  | #interrupted. These will be Net::ACME2::X::HTTP::Network instances. | 
| 18 |  |  |  |  |  |  | # | 
| 19 |  |  |  |  |  |  | #This also fails on HTTP errors (4xx and 5xx). The errors are | 
| 20 |  |  |  |  |  |  | #instances of Net::ACME2::X::HTTP::Protocol. | 
| 21 |  |  |  |  |  |  | # | 
| 22 |  |  |  |  |  |  | my $resp_obj = $http->post_form( $the_url, \%the_form_post ); | 
| 23 |  |  |  |  |  |  |  | 
| 24 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 25 |  |  |  |  |  |  |  | 
| 26 |  |  |  |  |  |  | This module largely duplicates the work of C, just without the | 
| 27 |  |  |  |  |  |  | dependency on C (which brings in a mess of other undesirables). | 
| 28 |  |  |  |  |  |  |  | 
| 29 |  |  |  |  |  |  | The chief benefit is that C and related methods will return | 
| 30 |  |  |  |  |  |  | instances of C rather than simple hashes. | 
| 31 |  |  |  |  |  |  |  | 
| 32 |  |  |  |  |  |  | This also always verifies remote SSL connections and always Cs if | 
| 33 |  |  |  |  |  |  | either the network connection fails or the protocol indicates an error | 
| 34 |  |  |  |  |  |  | (4xx or 5xx). | 
| 35 |  |  |  |  |  |  |  | 
| 36 |  |  |  |  |  |  | =cut | 
| 37 |  |  |  |  |  |  |  | 
| 38 | 2 |  |  | 2 |  | 141103 | use strict; | 
|  | 2 |  |  |  |  | 14 |  | 
|  | 2 |  |  |  |  | 62 |  | 
| 39 | 2 |  |  | 2 |  | 10 | use warnings; | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 66 |  | 
| 40 |  |  |  |  |  |  |  | 
| 41 | 2 |  |  | 2 |  | 11 | use parent qw( HTTP::Tiny ); | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 10 |  | 
| 42 |  |  |  |  |  |  |  | 
| 43 | 2 |  |  | 2 |  | 104209 | use HTTP::Tiny::UA::Response (); | 
|  | 2 |  |  |  |  | 7999 |  | 
|  | 2 |  |  |  |  | 71 |  | 
| 44 |  |  |  |  |  |  |  | 
| 45 | 2 |  |  | 2 |  | 1024 | use Net::ACME2::X (); | 
|  | 2 |  |  |  |  | 5 |  | 
|  | 2 |  |  |  |  | 41 |  | 
| 46 |  |  |  |  |  |  |  | 
| 47 |  |  |  |  |  |  | # This circular dependency is unfortunate, but PAUSE needs to see a static | 
| 48 |  |  |  |  |  |  | # $Net::ACME2::VERSION. (Thanks to Dan Book for pointing it out.) | 
| 49 | 2 |  |  | 2 |  | 324 | use Net::ACME2 (); | 
|  | 2 |  |  |  |  | 5 |  | 
|  | 2 |  |  |  |  | 762 |  | 
| 50 |  |  |  |  |  |  |  | 
| 51 |  |  |  |  |  |  | sub VERSION { | 
| 52 |  |  |  |  |  |  |  | 
| 53 |  |  |  |  |  |  | # HTTP::Tiny gets upset if there’s anything non-numeric | 
| 54 |  |  |  |  |  |  | # (e.g., “-TRIAL1”) in VERSION(). So weed it out here. | 
| 55 | 7 |  |  | 7 | 0 | 406 | my $version = $Net::ACME2::VERSION; | 
| 56 | 7 |  |  |  |  | 19 | $version =~ s<[^0-9].].*><>; | 
| 57 |  |  |  |  |  |  |  | 
| 58 | 7 |  |  |  |  | 50 | return $version; | 
| 59 |  |  |  |  |  |  | } | 
| 60 |  |  |  |  |  |  |  | 
| 61 |  |  |  |  |  |  | #Use this to tweak SSL config, e.g., if you want to cache PublicSuffix. | 
| 62 |  |  |  |  |  |  | our @SSL_OPTIONS; | 
| 63 |  |  |  |  |  |  |  | 
| 64 |  |  |  |  |  |  | sub new { | 
| 65 | 7 |  |  | 7 | 1 | 31 | my ( $class, %args ) = @_; | 
| 66 |  |  |  |  |  |  |  | 
| 67 |  |  |  |  |  |  | $args{'SSL_options'} = { | 
| 68 | 7 | 50 |  |  |  | 42 | ( $args{'SSL_options'} ? (%{ $args{'SSL_options'} }) : () ), | 
|  | 0 |  |  |  |  | 0 |  | 
| 69 |  |  |  |  |  |  | @SSL_OPTIONS, | 
| 70 |  |  |  |  |  |  | }; | 
| 71 |  |  |  |  |  |  |  | 
| 72 | 7 |  |  |  |  | 69 | my $self = $class->SUPER::new( | 
| 73 |  |  |  |  |  |  | verify_SSL => 1, | 
| 74 |  |  |  |  |  |  | %args, | 
| 75 |  |  |  |  |  |  | ); | 
| 76 |  |  |  |  |  |  |  | 
| 77 | 7 |  |  |  |  | 21 | return $self; | 
| 78 |  |  |  |  |  |  | } | 
| 79 |  |  |  |  |  |  |  | 
| 80 |  |  |  |  |  |  | #mocked in tests | 
| 81 |  |  |  |  |  |  | *_base_request = HTTP::Tiny->can('request'); | 
| 82 |  |  |  |  |  |  |  | 
| 83 |  |  |  |  |  |  | sub request { | 
| 84 | 24 |  |  | 24 | 1 | 56 | my ( $self, $method, $url, $args_hr ) = @_; | 
| 85 |  |  |  |  |  |  |  | 
| 86 |  |  |  |  |  |  | #HTTP::Tiny clobbers $@. The clobbering is useless since the | 
| 87 |  |  |  |  |  |  | #error is in the $resp variable already. Clobbering also risks | 
| 88 |  |  |  |  |  |  | #action-at-a-distance problems, so prevent it here. | 
| 89 |  |  |  |  |  |  |  | 
| 90 |  |  |  |  |  |  | #cf. eval_bug.readme | 
| 91 | 24 |  |  |  |  | 31 | my $eval_err = $@; | 
| 92 |  |  |  |  |  |  |  | 
| 93 | 24 |  | 66 |  |  | 180 | my $resp = _base_request( $self, $method, $url, $args_hr || () ); | 
| 94 |  |  |  |  |  |  |  | 
| 95 | 24 |  |  |  |  | 145098189 | $@ = $eval_err; | 
| 96 |  |  |  |  |  |  |  | 
| 97 | 24 |  |  |  |  | 180 | my $resp_obj = HTTP::Tiny::UA::Response->new($resp); | 
| 98 |  |  |  |  |  |  |  | 
| 99 |  |  |  |  |  |  | #cf. HTTP::Tiny docs | 
| 100 | 24 | 50 |  |  |  | 2009 | if ( $resp_obj->status() == 599 ) { | 
| 101 |  |  |  |  |  |  | die Net::ACME2::X->create( | 
| 102 |  |  |  |  |  |  | 'HTTP::Network', | 
| 103 |  |  |  |  |  |  | { | 
| 104 |  |  |  |  |  |  | method    => $method, | 
| 105 |  |  |  |  |  |  | url       => $url, | 
| 106 |  |  |  |  |  |  | error     => $resp_obj->content(), | 
| 107 | 0 |  |  |  |  | 0 | redirects => $resp->{'redirects'}, | 
| 108 |  |  |  |  |  |  | } | 
| 109 |  |  |  |  |  |  | ); | 
| 110 |  |  |  |  |  |  | } | 
| 111 |  |  |  |  |  |  |  | 
| 112 | 24 | 50 |  |  |  | 214 | if ( $resp->{'status'} >= 400 ) { | 
| 113 |  |  |  |  |  |  | die Net::ACME2::X->create( | 
| 114 |  |  |  |  |  |  | 'HTTP::Protocol', | 
| 115 |  |  |  |  |  |  | { | 
| 116 |  |  |  |  |  |  | method    => $method, | 
| 117 |  |  |  |  |  |  | redirects => $resp->{'redirects'}, | 
| 118 | 0 |  |  |  |  | 0 | ( map { ( $_ => $resp_obj->$_() ) } qw( content status reason url headers ) ), | 
|  | 0 |  |  |  |  | 0 |  | 
| 119 |  |  |  |  |  |  | }, | 
| 120 |  |  |  |  |  |  | ); | 
| 121 |  |  |  |  |  |  | } | 
| 122 |  |  |  |  |  |  |  | 
| 123 | 24 |  |  |  |  | 132 | return $resp_obj; | 
| 124 |  |  |  |  |  |  | } | 
| 125 |  |  |  |  |  |  |  | 
| 126 |  |  |  |  |  |  | 1; | 
| 127 |  |  |  |  |  |  |  |