| 1 | package HTTP::Tinyish::LWP; | ||
| 2 | use strict; | ||
| 3 | use parent qw(HTTP::Tinyish::Base); | ||
| 4 | |||
| 5 | use LWP 5.802; | ||
| 6 | use LWP::UserAgent; | ||
| 7 | |||
| 8 | my %supports = (http => 1); | ||
| 9 | |||
| 10 | sub configure { | ||
| 11 | my %meta = ( | ||
| 12 | LWP => $LWP::VERSION, | ||
| 13 | ); | ||
| 14 | |||
| 15 | if (eval { require LWP::Protocol::https; require Mozilla::CA; 1 }) { | ||
| 16 | $supports{https} = 1; | ||
| 17 | $meta{"LWP::Protocol::https"} = $LWP::Protocol::https::VERSION; | ||
| 18 | } | ||
| 19 | |||
| 20 | \%meta; | ||
| 21 | } | ||
| 22 | |||
| 23 | sub supports { | ||
| 24 | $supports{$_[1]}; | ||
| 25 | } | ||
| 26 | |||
| 27 | sub new { | ||
| 28 | my($class, %attr) = @_; | ||
| 29 | |||
| 30 | my $ua = LWP::UserAgent->new; | ||
| 31 | |||
| 32 | bless { | ||
| 33 | ua => $class->translate_lwp($ua, %attr), | ||
| 34 | }, $class; | ||
| 35 | } | ||
| 36 | |||
| 37 | sub _headers_to_hashref { | ||
| 38 | my($self, $hdrs) = @_; | ||
| 39 | |||
| 40 | my %headers; | ||
| 41 | for my $field ($hdrs->header_field_names) { | ||
| 42 | $headers{lc $field} = $hdrs->header($field); # could be an array ref | ||
| 43 | } | ||
| 44 | |||
| 45 | \%headers; | ||
| 46 | } | ||
| 47 | |||
| 48 | sub request { | ||
| 49 | my($self, $method, $url, $opts) = @_; | ||
| 50 | $opts ||= {}; | ||
| 51 | |||
| 52 | my $req = HTTP::Request->new($method => $url); | ||
| 53 | |||
| 54 | if ($opts->{headers}) { | ||
| 55 | $req->header(%{$opts->{headers}}); | ||
| 56 | } | ||
| 57 | |||
| 58 | if ($opts->{content}) { | ||
| 59 | $req->content($opts->{content}); | ||
| 60 | } | ||
| 61 | |||
| 62 | my $res = $self->{ua}->request($req); | ||
| 63 | |||
| 64 | if ($self->is_internal_response($res)) { | ||
| 65 | return $self->internal_error($url, $res->content); | ||
| 66 | } | ||
| 67 | |||
| 68 | return { | ||
| 69 | url => $url, | ||
| 70 | content => $res->decoded_content(charset => 'none'), | ||
| 71 | success => $res->is_success, | ||
| 72 | status => $res->code, | ||
| 73 | reason => $res->message, | ||
| 74 | headers => $self->_headers_to_hashref($res->headers), | ||
| 75 | protocol => $res->protocol, | ||
| 76 | }; | ||
| 77 | } | ||
| 78 | |||
| 79 | sub mirror { | ||
| 80 | my($self, $url, $file) = @_; | ||
| 81 | |||
| 82 | # TODO support optional headers | ||
| 83 | my $res = $self->{ua}->mirror($url, $file); | ||
| 84 | |||
| 85 | if ($self->is_internal_response($res)) { | ||
| 86 | return $self->internal_error($url, $res->content); | ||
| 87 | } | ||
| 88 | |||
| 89 | return { | ||
| 90 | url => $url, | ||
| 91 | content => $res->decoded_content, | ||
| 92 | success => $res->is_success || $res->code == 304, | ||
| 93 | status => $res->code, | ||
| 94 | reason => $res->message, | ||
| 95 | headers => $self->_headers_to_hashref($res->headers), | ||
| 96 | protocol => $res->protocol, | ||
| 97 | }; | ||
| 98 | } | ||
| 99 | |||
| 100 | sub translate_lwp { | ||
| 101 | my($class, $agent, %attr) = @_; | ||
| 102 | |||
| 103 | $agent->parse_head(0); | ||
| 104 | $agent->env_proxy; | ||
| 105 | $agent->timeout(delete $attr{timeout} || 60); | ||
| 106 | $agent->max_redirect(exists $attr{max_redirect} ? $attr{max_redirect} : 5); | ||
| 107 | $agent->agent(delete $attr{agent} || "HTTP-Tinyish/$HTTP::Tinyish::VERSION"); | ||
| 108 | |||
| 109 | # LWP default is to verify, HTTP::Tiny isn't | ||
| 110 | unless ($attr{verify_SSL}) { | ||
| 111 | if ($agent->can("ssl_opts")) { | ||
| 112 | $agent->ssl_opts(verify_hostname => 0); | ||
| 113 | } | ||
| 114 | } | ||
| 115 | |||
| 116 | if ($attr{default_headers}) { | ||
| 117 | $agent->default_headers( HTTP::Headers->new(%{$attr{default_headers}}) ); | ||
| 118 | } | ||
| 119 | |||
| 120 | $agent; | ||
| 121 | } | ||
| 122 | |||
| 123 | sub is_internal_response { | ||
| 124 | my($self, $res) = @_; | ||
| 125 | |||
| 126 | $res->code == 500 && | ||
| 127 | ( $res->header('Client-Warning') || '' ) eq 'Internal response'; | ||
| 128 | } | ||
| 129 | |||
| 130 | 1; |