File Coverage

blib/lib/Net/ACME2/HTTP_Tiny.pm
Criterion Covered Total %
statement 33 37 89.1
branch 3 6 50.0
condition 2 3 66.6
subroutine 9 9 100.0
pod 2 3 66.6
total 49 58 84.4


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 1     1   100339 use strict;
  1         16  
  1         23  
39 1     1   4 use warnings;
  1         2  
  1         41  
40              
41 1     1   5 use parent qw( HTTP::Tiny );
  1         1  
  1         4  
42              
43 1     1   41496 use HTTP::Tiny::UA::Response ();
  1         3417  
  1         18  
44              
45 1     1   361 use Net::ACME2::Constants ();
  1         2  
  1         15  
46 1     1   331 use Net::ACME2::X ();
  1         2  
  1         255  
47              
48             our $VERSION;
49              
50             sub VERSION {
51              
52             # HTTP::Tiny gets upset if there’s anything non-numeric
53             # (e.g., “-TRIAL1”) in VERSION(). So weed it out here.
54 1     1 0 61 my $version = $Net::ACME2::Constants::VERSION;
55 1         3 $version =~ s<[^0-9].].*><>;
56              
57 1         8 return $version;
58             }
59              
60             #Use this to tweak SSL config, e.g., if you want to cache PublicSuffix.
61             our @SSL_OPTIONS;
62              
63             sub new {
64 1     1 1 5 my ( $class, %args ) = @_;
65              
66             $args{'SSL_options'} = {
67 1 50       7 ( $args{'SSL_options'} ? (%{ $args{'SSL_options'} }) : () ),
  0         0  
68             @SSL_OPTIONS,
69             };
70              
71 1         20 my $self = $class->SUPER::new(
72             verify_SSL => 1,
73             %args,
74             );
75              
76 1         4 return $self;
77             }
78              
79             #mocked in tests
80             *_base_request = HTTP::Tiny->can('request');
81              
82             sub request {
83 4     4 1 18 my ( $self, $method, $url, $args_hr ) = @_;
84              
85             #HTTP::Tiny clobbers $@. The clobbering is useless since the
86             #error is in the $resp variable already. Clobbering also risks
87             #action-at-a-distance problems, so prevent it here.
88              
89             #cf. eval_bug.readme
90 4         12 my $eval_err = $@;
91              
92 4   66     48 my $resp = _base_request( $self, $method, $url, $args_hr || () );
93              
94 4         1081609 $@ = $eval_err;
95              
96 4         47 my $resp_obj = HTTP::Tiny::UA::Response->new($resp);
97              
98             #cf. HTTP::Tiny docs
99 4 50       415 if ( $resp_obj->status() == 599 ) {
100             die Net::ACME2::X->create(
101             'HTTP::Network',
102             {
103             method => $method,
104             url => $url,
105             error => $resp_obj->content(),
106 0         0 redirects => $resp->{'redirects'},
107             }
108             );
109             }
110              
111 4 50       84 if ( $resp->{'status'} >= 400 ) {
112             die Net::ACME2::X->create(
113             'HTTP::Protocol',
114             {
115             method => $method,
116             redirects => $resp->{'redirects'},
117 0         0 ( map { ( $_ => $resp_obj->$_() ) } qw( content status reason url headers ) ),
  0         0  
118             },
119             );
120             }
121              
122 4         34 return $resp_obj;
123             }
124              
125             1;
126