| 1 | package WWW::Curl::UserAgent; | ||
| 2 | $WWW::Curl::UserAgent::VERSION = '0.9.8'; | ||
| 3 | # ABSTRACT: UserAgent based on libcurl | ||
| 4 | |||
| 5 | 2 | ▶ | use Moose; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 2 | ▶ | use v5.10; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | |||
| 8 | 0 | ▶ | use WWW::Curl::Easy; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 9 | use WWW::Curl::Multi; | ||
| 10 | use HTTP::Request; | ||
| 11 | use HTTP::Response; | ||
| 12 | use Time::HiRes; | ||
| 13 | use IO::Select; | ||
| 14 | |||
| 15 | use WWW::Curl::UserAgent::Handler; | ||
| 16 | use WWW::Curl::UserAgent::Request; | ||
| 17 | |||
| 18 | # timeout in milliseconds | ||
| 19 | has timeout => ( | ||
| 20 | is => 'rw', | ||
| 21 | isa => 'Int', | ||
| 22 | default => 0, | ||
| 23 | ); | ||
| 24 | |||
| 25 | # connection timeout in milliseconds | ||
| 26 | has connect_timeout => ( | ||
| 27 | is => 'rw', | ||
| 28 | isa => 'Int', | ||
| 29 | default => 300, | ||
| 30 | ); | ||
| 31 | |||
| 32 | # maximum of requests done in parallel | ||
| 33 | has parallel_requests => ( | ||
| 34 | is => 'rw', | ||
| 35 | isa => 'Int', | ||
| 36 | default => 5, | ||
| 37 | ); | ||
| 38 | |||
| 39 | # use connection keep-alive | ||
| 40 | has keep_alive => ( | ||
| 41 | is => 'rw', | ||
| 42 | isa => 'Bool', | ||
| 43 | default => 1, | ||
| 44 | ); | ||
| 45 | |||
| 46 | # follow redirects | ||
| 47 | has followlocation => ( | ||
| 48 | is => 'ro', | ||
| 49 | isa => 'Bool', | ||
| 50 | default => 0, | ||
| 51 | ); | ||
| 52 | |||
| 53 | # maximum number of redirects | ||
| 54 | has max_redirects => ( | ||
| 55 | is => 'ro', | ||
| 56 | isa => 'Int', | ||
| 57 | default => -1, | ||
| 58 | ); | ||
| 59 | |||
| 60 | # identifier in each request | ||
| 61 | has user_agent_string => ( | ||
| 62 | is => 'rw', | ||
| 63 | isa => 'Str', | ||
| 64 | default => sub { "www.curl.useragent/$WWW::Curl::UserAgent::VERSION" }, | ||
| 65 | ); | ||
| 66 | |||
| 67 | has _curl_multi => ( | ||
| 68 | is => 'ro', | ||
| 69 | isa => 'WWW::Curl::Multi', | ||
| 70 | default => sub { WWW::Curl::Multi->new }, | ||
| 71 | ); | ||
| 72 | |||
| 73 | has _handler_queue => ( | ||
| 74 | is => 'ro', | ||
| 75 | isa => 'ArrayRef[WWW::Curl::UserAgent::Handler]', | ||
| 76 | default => sub { [] }, | ||
| 77 | traits => ['Array'], | ||
| 78 | handles => { | ||
| 79 | add_handler => 'push', | ||
| 80 | _get_handler_from_queue => 'shift', | ||
| 81 | _has_handler_in_queue => 'count', | ||
| 82 | request_queue_size => 'count', | ||
| 83 | } | ||
| 84 | ); | ||
| 85 | |||
| 86 | has _active_handler_map => ( | ||
| 87 | is => 'ro', | ||
| 88 | isa => 'HashRef[WWW::Curl::UserAgent::Handler]', | ||
| 89 | default => sub { {} }, | ||
| 90 | traits => ['Hash'], | ||
| 91 | handles => { | ||
| 92 | _active_handlers => 'count', | ||
| 93 | _set_active_handler => 'set', | ||
| 94 | _get_active_handler => 'delete', | ||
| 95 | } | ||
| 96 | ); | ||
| 97 | |||
| 98 | has _max_private_id => ( | ||
| 99 | is => 'ro', | ||
| 100 | isa => 'Num', | ||
| 101 | default => 1, | ||
| 102 | traits => ['Counter'], | ||
| 103 | handles => { _inc_private_id => 'inc', } | ||
| 104 | ); | ||
| 105 | |||
| 106 | sub request { | ||
| 107 | my ( $self, $request, %args ) = @_; | ||
| 108 | |||
| 109 | my $timeout = $args{timeout} // $self->timeout; | ||
| 110 | my $connect_timeout = $args{connect_timeout} // $self->connect_timeout; | ||
| 111 | my $keep_alive = $args{keep_alive} // $self->keep_alive; | ||
| 112 | my $followlocation = $args{followlocation} // $self->followlocation; | ||
| 113 | my $max_redirects = $args{max_redirects} // $self->max_redirects; | ||
| 114 | |||
| 115 | my $response; | ||
| 116 | $self->add_handler( | ||
| 117 | WWW::Curl::UserAgent::Handler->new( | ||
| 118 | on_success => sub { | ||
| 119 | my ( $req, $res ) = @_; | ||
| 120 | $response = $res; | ||
| 121 | }, | ||
| 122 | on_failure => sub { | ||
| 123 | my ( $req, $msg, $desc ) = @_; | ||
| 124 | $response = HTTP::Response->new( 500, $msg, [], $desc ); | ||
| 125 | }, | ||
| 126 | request => WWW::Curl::UserAgent::Request->new( | ||
| 127 | http_request => $request, | ||
| 128 | connect_timeout => $connect_timeout, | ||
| 129 | timeout => $timeout, | ||
| 130 | keep_alive => $keep_alive, | ||
| 131 | followlocation => $followlocation, | ||
| 132 | max_redirects => $max_redirects, | ||
| 133 | ), | ||
| 134 | ) | ||
| 135 | ); | ||
| 136 | $self->perform; | ||
| 137 | |||
| 138 | return $response; | ||
| 139 | } | ||
| 140 | |||
| 141 | sub add_request { | ||
| 142 | my ( $self, %args ) = @_; | ||
| 143 | |||
| 144 | my $on_success = $args{on_success}; | ||
| 145 | my $on_failure = $args{on_failure}; | ||
| 146 | my $request = $args{request}; | ||
| 147 | my $timeout = $args{timeout} // $self->timeout; | ||
| 148 | my $connect_timeout = $args{connect_timeout} // $self->connect_timeout; | ||
| 149 | my $keep_alive = $args{keep_alive} // $self->keep_alive; | ||
| 150 | my $followlocation = $args{followlocation} // $self->followlocation; | ||
| 151 | my $max_redirects = $args{max_redirects} // $self->max_redirects; | ||
| 152 | |||
| 153 | my $handler = WWW::Curl::UserAgent::Handler->new( | ||
| 154 | on_success => $on_success, | ||
| 155 | on_failure => $on_failure, | ||
| 156 | request => WWW::Curl::UserAgent::Request->new( | ||
| 157 | http_request => $request, | ||
| 158 | connect_timeout => $connect_timeout, | ||
| 159 | timeout => $timeout, | ||
| 160 | keep_alive => $keep_alive, | ||
| 161 | followlocation => $followlocation, | ||
| 162 | max_redirects => $max_redirects, | ||
| 163 | ), | ||
| 164 | ); | ||
| 165 | $self->add_handler($handler); | ||
| 166 | |||
| 167 | return $handler; | ||
| 168 | } | ||
| 169 | |||
| 170 | sub perform { | ||
| 171 | my $self = shift; | ||
| 172 | |||
| 173 | my $active_handlers; | ||
| 174 | |||
| 175 | # activate handlers by draining the queue | ||
| 176 | while ( $active_handlers = $self->_drain_handler_queue ) { | ||
| 177 | |||
| 178 | # loop until there is a response available | ||
| 179 | $self->_wait_for_response($active_handlers); | ||
| 180 | |||
| 181 | # execute callbacks for all received responses | ||
| 182 | $self->_perform_callbacks; | ||
| 183 | } | ||
| 184 | } | ||
| 185 | |||
| 186 | sub _wait_for_response { | ||
| 187 | my $self = shift; | ||
| 188 | my $active_handlers = shift; | ||
| 189 | |||
| 190 | my $curl_multi = $self->_curl_multi; | ||
| 191 | |||
| 192 | while ( $curl_multi->perform == $active_handlers ) { | ||
| 193 | Time::HiRes::nanosleep(1); | ||
| 194 | my @select = map { | ||
| 195 | my $s = IO::Select->new; | ||
| 196 | $s->add( @{$_} ); | ||
| 197 | $s; | ||
| 198 | } ( $curl_multi->fdset ); | ||
| 199 | IO::Select->select( @select, 0.1 ); | ||
| 200 | } | ||
| 201 | } | ||
| 202 | |||
| 203 | sub _perform_callbacks { | ||
| 204 | my $self = shift; | ||
| 205 | |||
| 206 | while ( my ( $active_transfer_id, $return_code ) = $self->_curl_multi->info_read ) { | ||
| 207 | |||
| 208 | unless ($active_transfer_id) { | ||
| 209 | Time::HiRes::nanosleep(1); # do not eat the whole cpu | ||
| 210 | next; | ||
| 211 | } | ||
| 212 | |||
| 213 | my $handler = $self->_get_active_handler($active_transfer_id); | ||
| 214 | my $request = $handler->request; | ||
| 215 | my $curl_easy = $request->curl_easy; | ||
| 216 | |||
| 217 | if ( $return_code == 0 ) { | ||
| 218 | |||
| 219 | # Assume the final http request is the original one | ||
| 220 | my $final_http_request = $request->http_request(); | ||
| 221 | my $effective_url = $curl_easy->getinfo( CURLINFO_EFFECTIVE_URL ); | ||
| 222 | |||
| 223 | # Handle redirection last effective Request so | ||
| 224 | # the response is always link to the last request in effect. | ||
| 225 | if( $effective_url && ( $final_http_request->uri().'' ne $effective_url ) ){ | ||
| 226 | $final_http_request = HTTP::Request | ||
| 227 | ->new($final_http_request->method, | ||
| 228 | $effective_url, | ||
| 229 | $final_http_request->headers(), | ||
| 230 | $final_http_request->content()); | ||
| 231 | } | ||
| 232 | |||
| 233 | my $response = $self->_build_http_response( ${ $request->header_ref }, ${ $request->content_ref }, $final_http_request ); | ||
| 234 | $handler->on_success->( $request->http_request, $response, $curl_easy ); | ||
| 235 | } | ||
| 236 | else { | ||
| 237 | $handler->on_failure->( | ||
| 238 | $request->http_request, $curl_easy->strerror($return_code), | ||
| 239 | $curl_easy->errbuf, $curl_easy | ||
| 240 | ); | ||
| 241 | } | ||
| 242 | } | ||
| 243 | } | ||
| 244 | |||
| 245 | sub _drain_handler_queue { | ||
| 246 | my $self = shift; | ||
| 247 | |||
| 248 | while ( $self->_has_handler_in_queue && $self->_active_handlers < $self->parallel_requests ) { | ||
| 249 | $self->_activate_handler( $self->_get_handler_from_queue ); | ||
| 250 | } | ||
| 251 | |||
| 252 | return $self->_active_handlers; | ||
| 253 | } | ||
| 254 | |||
| 255 | sub _activate_handler { | ||
| 256 | my $self = shift; | ||
| 257 | my $handler = shift; | ||
| 258 | |||
| 259 | # set up curl easy | ||
| 260 | $self->_inc_private_id; | ||
| 261 | my $private_id = $self->_max_private_id; | ||
| 262 | my $easy = $handler->request->curl_easy; | ||
| 263 | $easy->setopt( CURLOPT_PRIVATE, $private_id ); | ||
| 264 | $easy->setopt( CURLOPT_USERAGENT, $self->user_agent_string ); | ||
| 265 | |||
| 266 | # reference the handler on its handler id (CURLOPT_PRIVATE) | ||
| 267 | $self->_set_active_handler( $private_id => $handler ); | ||
| 268 | |||
| 269 | # finally add the curl easy to curl multi | ||
| 270 | $self->_curl_multi->add_handle($easy); | ||
| 271 | } | ||
| 272 | |||
| 273 | sub _build_http_response { | ||
| 274 | my $self = shift; | ||
| 275 | my $header = shift; | ||
| 276 | my $content = shift; | ||
| 277 | my $final_http_request = shift; | ||
| 278 | |||
| 279 | # PUT requests may contain continue header | ||
| 280 | my @header = split "\r\n\r\n", $header; | ||
| 281 | |||
| 282 | my $response = HTTP::Response->parse($header[-1]); | ||
| 283 | |||
| 284 | if( $final_http_request ){ | ||
| 285 | # Inject this in the response to behave more like LWP::UserAgent | ||
| 286 | $response->request($final_http_request); | ||
| 287 | } | ||
| 288 | |||
| 289 | $response->content($content) if defined $content; | ||
| 290 | |||
| 291 | # message might include a bad char | ||
| 292 | my $message = $response->message; | ||
| 293 | $response->message($message) | ||
| 294 | if $message =~ s/\r//g; | ||
| 295 | |||
| 296 | return $response; | ||
| 297 | } | ||
| 298 | |||
| 299 | no Moose; | ||
| 300 | __PACKAGE__->meta->make_immutable; | ||
| 301 | 1; | ||
| 302 | |||
| 303 | __END__ | ||