| 1 | package WWW::Curl::UserAgent::Request; | ||
| 2 | $WWW::Curl::UserAgent::Request::VERSION = '0.9.8'; | ||
| 3 | 1 | ▶ | use Moose; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 0 | ▶ | use WWW::Curl::Easy; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 5 | |||
| 6 | has http_request => ( | ||
| 7 | is => 'ro', | ||
| 8 | isa => 'HTTP::Request', | ||
| 9 | required => 1, | ||
| 10 | ); | ||
| 11 | |||
| 12 | has connect_timeout => ( | ||
| 13 | is => 'ro', | ||
| 14 | isa => 'Int', | ||
| 15 | required => 1, | ||
| 16 | ); | ||
| 17 | |||
| 18 | has timeout => ( | ||
| 19 | is => 'ro', | ||
| 20 | isa => 'Int', | ||
| 21 | required => 1, | ||
| 22 | ); | ||
| 23 | |||
| 24 | has keep_alive => ( | ||
| 25 | is => 'ro', | ||
| 26 | isa => 'Bool', | ||
| 27 | required => 1, | ||
| 28 | ); | ||
| 29 | |||
| 30 | has followlocation => ( | ||
| 31 | is => 'ro', | ||
| 32 | isa => 'Bool', | ||
| 33 | default => 0, | ||
| 34 | ); | ||
| 35 | |||
| 36 | has max_redirects => ( | ||
| 37 | is => 'ro', | ||
| 38 | isa => 'Int', | ||
| 39 | default => -1, | ||
| 40 | ); | ||
| 41 | |||
| 42 | has curl_easy => ( | ||
| 43 | is => 'ro', | ||
| 44 | isa => 'WWW::Curl::Easy', | ||
| 45 | lazy_build => 1, | ||
| 46 | ); | ||
| 47 | |||
| 48 | has header_ref => ( | ||
| 49 | is => 'ro', | ||
| 50 | isa => 'Ref', | ||
| 51 | default => sub { my $header; \$header }, | ||
| 52 | ); | ||
| 53 | |||
| 54 | has content_ref => ( | ||
| 55 | is => 'ro', | ||
| 56 | isa => 'Ref', | ||
| 57 | default => sub { my $content; \$content }, | ||
| 58 | ); | ||
| 59 | |||
| 60 | sub _build_curl_easy { | ||
| 61 | my $self = shift; | ||
| 62 | |||
| 63 | my $easy = WWW::Curl::Easy->new; | ||
| 64 | my $request = $self->http_request; | ||
| 65 | |||
| 66 | $easy->setopt( CURLOPT_CONNECTTIMEOUT_MS, $self->connect_timeout ); | ||
| 67 | $easy->setopt( CURLOPT_HEADER, 0 ); | ||
| 68 | $easy->setopt( CURLOPT_NOPROGRESS, 1 ); | ||
| 69 | $easy->setopt( CURLOPT_TIMEOUT_MS, $self->timeout ); | ||
| 70 | $easy->setopt( CURLOPT_URL, $request->uri ); | ||
| 71 | $easy->setopt( CURLOPT_WRITEHEADER, $self->header_ref ); | ||
| 72 | $easy->setopt( CURLOPT_WRITEDATA, $self->content_ref ); | ||
| 73 | $easy->setopt( CURLOPT_FORBID_REUSE, !$self->keep_alive ); | ||
| 74 | $easy->setopt( CURLOPT_FOLLOWLOCATION, $self->followlocation ); | ||
| 75 | $easy->setopt( CURLOPT_MAXREDIRS, $self->max_redirects ); | ||
| 76 | |||
| 77 | # see https://github.com/pauldix/typhoeus/blob/master/lib/typhoeus/easy.rb#L197 | ||
| 78 | if ( $request->method eq 'GET' ) { | ||
| 79 | $easy->setopt( CURLOPT_HTTPGET, 1 ); | ||
| 80 | } | ||
| 81 | elsif ( $request->method eq 'POST' ) { | ||
| 82 | use bytes; | ||
| 83 | my $content = $request->content; | ||
| 84 | $easy->setopt( CURLOPT_POST, 1 ); | ||
| 85 | $easy->setopt( CURLOPT_POSTFIELDSIZE, length $content ); | ||
| 86 | $easy->setopt( CURLOPT_COPYPOSTFIELDS, $content ); | ||
| 87 | } | ||
| 88 | elsif ( $request->method eq 'PUT' ) { | ||
| 89 | use bytes; | ||
| 90 | my $content = $request->content; | ||
| 91 | $easy->setopt( CURLOPT_UPLOAD, 1 ); | ||
| 92 | $easy->setopt( CURLOPT_INFILE, \$content ); | ||
| 93 | $easy->setopt( CURLOPT_INFILESIZE, length $content ); | ||
| 94 | $easy->setopt( CURLOPT_READFUNCTION, \&_read_callback ); | ||
| 95 | $easy->setopt( CURLOPT_WRITEFUNCTION, \&_chunk_callback ); | ||
| 96 | } | ||
| 97 | elsif ( $request->method eq 'PATCH' ) { | ||
| 98 | use bytes; | ||
| 99 | my $content = $request->content; | ||
| 100 | $easy->setopt( CURLOPT_UPLOAD, 1 ); | ||
| 101 | $easy->setopt( CURLOPT_INFILE, \$content ); | ||
| 102 | $easy->setopt( CURLOPT_INFILESIZE, length $content ); | ||
| 103 | $easy->setopt( CURLOPT_READFUNCTION, \&_read_callback ); | ||
| 104 | $easy->setopt( CURLOPT_WRITEFUNCTION, \&_chunk_callback ); | ||
| 105 | $easy->setopt( CURLOPT_CUSTOMREQUEST, uc 'PATCH' ); | ||
| 106 | } | ||
| 107 | elsif ( $request->method eq 'HEAD' ) { | ||
| 108 | $easy->setopt( CURLOPT_NOBODY, 1 ); | ||
| 109 | } | ||
| 110 | else { | ||
| 111 | $easy->setopt( CURLOPT_CUSTOMREQUEST, uc $request->method ); | ||
| 112 | } | ||
| 113 | |||
| 114 | my @headers; | ||
| 115 | foreach my $h ( +$request->headers->header_field_names ) { | ||
| 116 | push( @headers, "$h: " . $request->header($h) ); | ||
| 117 | } | ||
| 118 | push @headers, "Connection: close" unless $self->keep_alive; | ||
| 119 | $easy->setopt( CURLOPT_HTTPHEADER, \@headers ) | ||
| 120 | if scalar(@headers); | ||
| 121 | |||
| 122 | return $easy; | ||
| 123 | } | ||
| 124 | |||
| 125 | sub _read_callback { | ||
| 126 | my ( $maxlength, $pointer ) = @_; | ||
| 127 | my $data = substr( $$pointer, 0, $maxlength ); | ||
| 128 | $$pointer = | ||
| 129 | length($$pointer) > $maxlength | ||
| 130 | ? scalar substr( $$pointer, $maxlength ) | ||
| 131 | : ''; | ||
| 132 | return $data; | ||
| 133 | } | ||
| 134 | |||
| 135 | sub _chunk_callback { | ||
| 136 | my ( $data, $pointer ) = @_; | ||
| 137 | ${$pointer} .= $data; | ||
| 138 | return length($data); | ||
| 139 | } | ||
| 140 | |||
| 141 | no Moose; | ||
| 142 | __PACKAGE__->meta->make_immutable; | ||
| 143 | 1; | ||