PageRenderTime 36ms CodeModel.GetById 3ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Mojo/UserAgent.pm

http://github.com/jamadam/mojo-legacy
Perl | 873 lines | 607 code | 205 blank | 61 comment | 74 complexity | f072b38b3bdef10d56f34254be9f78b3 MD5 | raw file
Possible License(s): AGPL-3.0
  1. package Mojo::UserAgent;
  2. use Mojo::Base 'Mojo::EventEmitter';
  3. # "Fry: Since when is the Internet about robbing people of their privacy?
  4. # Bender: August 6, 1991."
  5. use Mojo::IOLoop;
  6. use Mojo::Util 'monkey_patch';
  7. use Mojo::UserAgent::CookieJar;
  8. use Mojo::UserAgent::Proxy;
  9. use Mojo::UserAgent::Server;
  10. use Mojo::UserAgent::Transactor;
  11. use Scalar::Util 'weaken';
  12. use constant DEBUG => $ENV{MOJO_USERAGENT_DEBUG} || 0;
  13. has ca => sub { $ENV{MOJO_CA_FILE} };
  14. has cert => sub { $ENV{MOJO_CERT_FILE} };
  15. has connect_timeout => sub { $ENV{MOJO_CONNECT_TIMEOUT} || 10 };
  16. has cookie_jar => sub { Mojo::UserAgent::CookieJar->new };
  17. has 'local_address';
  18. has inactivity_timeout => sub { defined $ENV{MOJO_INACTIVITY_TIMEOUT} ? $ENV{MOJO_INACTIVITY_TIMEOUT} : 20 };
  19. has ioloop => sub { Mojo::IOLoop->new };
  20. has key => sub { $ENV{MOJO_KEY_FILE} };
  21. has max_connections => 5;
  22. has max_redirects => sub { $ENV{MOJO_MAX_REDIRECTS} || 0 };
  23. has proxy => sub { Mojo::UserAgent::Proxy->new };
  24. has request_timeout => sub { defined $ENV{MOJO_REQUEST_TIMEOUT} ? $ENV{MOJO_REQUEST_TIMEOUT} : 0 };
  25. has server => sub { Mojo::UserAgent::Server->new(ioloop => shift->ioloop) };
  26. has transactor => sub { Mojo::UserAgent::Transactor->new };
  27. # Common HTTP methods
  28. for my $name (qw(DELETE GET HEAD OPTIONS PATCH POST PUT)) {
  29. monkey_patch __PACKAGE__, lc $name, sub {
  30. my $self = shift;
  31. my $cb = ref $_[-1] eq 'CODE' ? pop : undef;
  32. return $self->start($self->build_tx($name, @_), $cb);
  33. };
  34. }
  35. sub DESTROY { shift->_cleanup }
  36. sub build_tx { shift->transactor->tx(@_) }
  37. sub build_websocket_tx { shift->transactor->websocket(@_) }
  38. sub start {
  39. my ($self, $tx, $cb) = @_;
  40. # Fork safety
  41. $self->_cleanup->server->restart unless ($self->{pid} = defined $self->{pid} ? $self->{pid} : $$) eq $$;
  42. # Non-blocking
  43. if ($cb) {
  44. warn "-- Non-blocking request (@{[$tx->req->url->to_abs]})\n" if DEBUG;
  45. return $self->_start(1, $tx, $cb);
  46. }
  47. # Blocking
  48. warn "-- Blocking request (@{[$tx->req->url->to_abs]})\n" if DEBUG;
  49. $self->_start(0, $tx => sub { shift->ioloop->stop; $tx = shift });
  50. $self->ioloop->start;
  51. return $tx;
  52. }
  53. sub websocket {
  54. my $self = shift;
  55. my $cb = ref $_[-1] eq 'CODE' ? pop : undef;
  56. $self->start($self->build_websocket_tx(@_), $cb);
  57. }
  58. sub _cleanup {
  59. my $self = shift;
  60. return unless my $loop = $self->_loop(0);
  61. # Clean up active connections (by closing them)
  62. delete $self->{pid};
  63. $self->_finish($_, 1) for keys %{$self->{connections} || {}};
  64. # Clean up keep-alive connections
  65. $loop->remove($_->[1]) for @{delete $self->{queue} || []};
  66. $loop = $self->_loop(1);
  67. $loop->remove($_->[1]) for @{delete $self->{nb_queue} || []};
  68. return $self;
  69. }
  70. sub _connect {
  71. my ($self, $nb, $peer, $tx, $handle, $cb) = @_;
  72. my $t = $self->transactor;
  73. my ($proto, $host, $port) = $peer ? $t->peer($tx) : $t->endpoint($tx);
  74. my %options
  75. = (address => $host, port => $port, timeout => $self->connect_timeout);
  76. if (my $local = $self->local_address) { $options{local_address} = $local }
  77. $options{handle} = $handle if $handle;
  78. # SOCKS
  79. if ($proto eq 'socks') {
  80. @options{qw(socks_address socks_port)} = @options{qw(address port)};
  81. ($proto, @options{qw(address port)}) = $t->endpoint($tx);
  82. my $req = $tx->req;
  83. my $userinfo = $req->proxy->userinfo;
  84. $req->proxy(0);
  85. @options{qw(socks_user socks_pass)} = split ':', $userinfo if $userinfo;
  86. }
  87. # TLS
  88. map { $options{"tls_$_"} = $self->$_ } qw(ca cert key)
  89. if ($options{tls} = $proto eq 'https');
  90. weaken $self;
  91. my $id;
  92. return $id = $self->_loop($nb)->client(
  93. %options => sub {
  94. my ($loop, $err, $stream) = @_;
  95. # Connection error
  96. return unless $self;
  97. return $self->_error($id, $err) if $err;
  98. # Connection established
  99. $stream->on(timeout => sub { $self->_error($id, 'Inactivity timeout') });
  100. $stream->on(close => sub { $self && $self->_finish($id, 1) });
  101. $stream->on(error => sub { $self && $self->_error($id, pop) });
  102. $stream->on(read => sub { $self->_read($id, pop) });
  103. $self->$cb($id);
  104. }
  105. );
  106. }
  107. sub _connect_proxy {
  108. my ($self, $nb, $old, $cb) = @_;
  109. # Start CONNECT request
  110. return undef unless my $new = $self->transactor->proxy_connect($old);
  111. return $self->_start(
  112. ($nb, $new) => sub {
  113. my ($self, $tx) = @_;
  114. # CONNECT failed (connection needs to be kept alive)
  115. $old->res->error({message => 'Proxy connection failed'})
  116. and return $self->$cb($old)
  117. if $tx->error || !$tx->res->is_status_class(200) || !$tx->keep_alive;
  118. # Start real transaction
  119. $old->req->proxy(0);
  120. my $id = $tx->connection;
  121. return $self->_start($nb, $old->connection($id), $cb)
  122. unless $tx->req->url->protocol eq 'https';
  123. # TLS upgrade
  124. my $loop = $self->_loop($nb);
  125. my $handle = $loop->stream($id)->steal_handle;
  126. $loop->remove($id);
  127. $id = $self->_connect($nb, 0, $old, $handle,
  128. sub { shift->_start($nb, $old->connection($id), $cb) });
  129. $self->{connections}{$id} = {cb => $cb, nb => $nb, tx => $old};
  130. }
  131. );
  132. }
  133. sub _connected {
  134. my ($self, $id) = @_;
  135. # Inactivity timeout
  136. my $c = $self->{connections}{$id};
  137. my $stream
  138. = $self->_loop($c->{nb})->stream($id)->timeout($self->inactivity_timeout);
  139. # Store connection information in transaction
  140. my $tx = $c->{tx}->connection($id);
  141. my $handle = $stream->handle;
  142. $tx->local_address($handle->sockhost)->local_port($handle->sockport);
  143. $tx->remote_address($handle->peerhost)->remote_port($handle->peerport);
  144. # Start writing
  145. weaken $self;
  146. $tx->on(resume => sub { $self->_write($id) });
  147. $self->_write($id);
  148. }
  149. sub _connection {
  150. my ($self, $nb, $tx, $cb) = @_;
  151. # Reuse connection
  152. my $id = $tx->connection;
  153. my ($proto, $host, $port) = $self->transactor->endpoint($tx);
  154. $id ||= $self->_dequeue($nb, "$proto:$host:$port", 1);
  155. if ($id && !ref $id) {
  156. warn "-- Reusing connection ($proto:$host:$port)\n" if DEBUG;
  157. $self->{connections}{$id} = {cb => $cb, nb => $nb, tx => $tx};
  158. $tx->kept_alive(1) unless $tx->connection;
  159. $self->_connected($id);
  160. return $id;
  161. }
  162. # CONNECT request to proxy required
  163. if (my $id = $self->_connect_proxy($nb, $tx, $cb)) { return $id }
  164. # Connect
  165. warn "-- Connect ($proto:$host:$port)\n" if DEBUG;
  166. $id = $self->_connect($nb, 1, $tx, $id, \&_connected);
  167. $self->{connections}{$id} = {cb => $cb, nb => $nb, tx => $tx};
  168. return $id;
  169. }
  170. sub _dequeue {
  171. my ($self, $nb, $name, $test) = @_;
  172. my $loop = $self->_loop($nb);
  173. my $old = $self->{$nb ? 'nb_queue' : 'queue'} ||= [];
  174. my ($found, @new);
  175. for my $queued (@$old) {
  176. push @new, $queued and next if $found || !grep { $_ eq $name } @$queued;
  177. # Search for id/name and sort out corrupted connections if necessary
  178. next unless my $stream = $loop->stream($queued->[1]);
  179. $test && $stream->is_readable ? $stream->close : ($found = $queued->[1]);
  180. }
  181. @$old = @new;
  182. return $found;
  183. }
  184. sub _enqueue {
  185. my ($self, $nb, $name, $id) = @_;
  186. # Enforce connection limit
  187. my $queue = $self->{$nb ? 'nb_queue' : 'queue'} ||= [];
  188. my $max = $self->max_connections;
  189. $self->_remove(shift(@$queue)->[1]) while @$queue && @$queue >= $max;
  190. $max ? push @$queue, [$name, $id] : $self->_loop($nb)->stream($id)->close;
  191. }
  192. sub _error {
  193. my ($self, $id, $err) = @_;
  194. my $tx = $self->{connections}{$id}{tx};
  195. $tx->res->error({message => $err}) if $tx;
  196. $self->_finish($id, 1);
  197. }
  198. sub _finish {
  199. my ($self, $id, $close) = @_;
  200. # Remove request timeout
  201. return unless my $c = $self->{connections}{$id};
  202. return unless my $loop = $self->_loop($c->{nb});
  203. $loop->remove($c->{timeout}) if $c->{timeout};
  204. return $self->_remove($id, $close) unless my $old = $c->{tx};
  205. $old->client_close($close);
  206. # Finish WebSocket
  207. return $self->_remove($id, 1) if $old->is_websocket;
  208. if (my $jar = $self->cookie_jar) { $jar->extract($old) }
  209. # Upgrade connection to WebSocket
  210. if (my $new = $self->transactor->upgrade($old)) {
  211. weaken $self;
  212. $new->on(resume => sub { $self->_write($id) });
  213. $c->{cb}->($self, $c->{tx} = $new);
  214. return $new->client_read($old->res->content->leftovers);
  215. }
  216. # Finish normal connection and handle redirects
  217. $self->_remove($id, $close);
  218. $c->{cb}->($self, $old) unless $self->_redirect($c, $old);
  219. }
  220. sub _loop { $_[1] ? Mojo::IOLoop->singleton : $_[0]->ioloop }
  221. sub _read {
  222. my ($self, $id, $chunk) = @_;
  223. # Corrupted connection
  224. return unless my $c = $self->{connections}{$id};
  225. return $self->_remove($id) unless my $tx = $c->{tx};
  226. # Process incoming data
  227. warn "-- Client <<< Server (@{[$tx->req->url->to_abs]})\n$chunk\n" if DEBUG;
  228. $tx->client_read($chunk);
  229. if ($tx->is_finished) { $self->_finish($id) }
  230. elsif ($tx->is_writing) { $self->_write($id) }
  231. }
  232. sub _redirect {
  233. my ($self, $c, $old) = @_;
  234. return undef unless my $new = $self->transactor->redirect($old);
  235. return undef unless @{$old->redirects} < $self->max_redirects;
  236. return $self->_start($c->{nb}, $new, delete $c->{cb});
  237. }
  238. sub _remove {
  239. my ($self, $id, $close) = @_;
  240. # Close connection
  241. my $c = delete $self->{connections}{$id} || {};
  242. my $tx = $c->{tx};
  243. return map { $self->_dequeue($_, $id); $self->_loop($_)->remove($id) } 1, 0
  244. if $close || !$tx || !$tx->keep_alive || $tx->error;
  245. # Keep connection alive (CONNECT requests get upgraded)
  246. $self->_enqueue($c->{nb}, join(':', $self->transactor->endpoint($tx)), $id)
  247. unless uc $tx->req->method eq 'CONNECT';
  248. }
  249. sub _start {
  250. my ($self, $nb, $tx, $cb) = @_;
  251. # Application server
  252. my $url = $tx->req->url;
  253. unless ($url->is_abs) {
  254. my $base = $nb ? $self->server->nb_url : $self->server->url;
  255. $url->scheme($base->scheme)->authority($base->authority);
  256. }
  257. $_ && $_->inject($tx) for $self->proxy, $self->cookie_jar;
  258. # Connect and add request timeout if necessary
  259. my $id = $self->emit(start => $tx)->_connection($nb, $tx, $cb);
  260. if (my $timeout = $self->request_timeout) {
  261. weaken $self;
  262. $self->{connections}{$id}{timeout} = $self->_loop($nb)
  263. ->timer($timeout => sub { $self->_error($id, 'Request timeout') });
  264. }
  265. return $id;
  266. }
  267. sub _write {
  268. my ($self, $id) = @_;
  269. # Get and write chunk
  270. return unless my $c = $self->{connections}{$id};
  271. return unless my $tx = $c->{tx};
  272. return if !$tx->is_writing || $c->{writing}++;
  273. my $chunk = $tx->client_write;
  274. delete $c->{writing};
  275. warn "-- Client >>> Server (@{[$tx->req->url->to_abs]})\n$chunk\n" if DEBUG;
  276. my $stream = $self->_loop($c->{nb})->stream($id)->write($chunk);
  277. $self->_finish($id) if $tx->is_finished;
  278. # Continue writing
  279. return unless $tx->is_writing;
  280. weaken $self;
  281. $stream->write('' => sub { $self->_write($id) });
  282. }
  283. 1;
  284. =encoding utf8
  285. =head1 NAME
  286. Mojo::UserAgent - Non-blocking I/O HTTP and WebSocket user agent
  287. =head1 SYNOPSIS
  288. use Mojo::UserAgent;
  289. # Say hello to the Unicode snowman with "Do Not Track" header
  290. my $ua = Mojo::UserAgent->new;
  291. say $ua->get('www.☃.net?hello=there' => {DNT => 1})->res->body;
  292. # Form POST with exception handling
  293. my $tx = $ua->post('https://metacpan.org/search' => form => {q => 'mojo'});
  294. if (my $res = $tx->success) { say $res->body }
  295. else {
  296. my $err = $tx->error;
  297. die "$err->{code} response: $err->{message}" if $err->{code};
  298. die "Connection error: $err->{message}";
  299. }
  300. # Quick JSON API request with Basic authentication
  301. say $ua->get('https://sri:s3cret@example.com/search.json?q=perl')
  302. ->res->json('/results/0/title');
  303. # Extract data from HTML and XML resources
  304. say $ua->get('www.perl.org')->res->dom->at('title')->text;
  305. # Scrape the latest headlines from a news site with CSS selectors
  306. say $ua->get('blogs.perl.org')->res->dom->find('h2 > a')->pluck('text');
  307. # Search DuckDuckGo anonymously through Tor
  308. $ua->proxy->http('socks://127.0.0.1:9050');
  309. say $ua->get('api.3g2upl4pq6kufc4m.onion/?q=mojolicious&format=json')
  310. ->res->json('/Abstract');
  311. # IPv6 PUT request with content
  312. my $tx
  313. = $ua->put('[::1]:3000' => {'Content-Type' => 'text/plain'} => 'Hello!');
  314. # Follow redirects to grab the latest Mojolicious release :)
  315. $ua->max_redirects(5)
  316. ->get('https://www.github.com/kraih/mojo/tarball/master')
  317. ->res->content->asset->move_to('/Users/sri/mojo.tar.gz');
  318. # TLS certificate authentication and JSON POST
  319. my $tx = $ua->cert('tls.crt')->key('tls.key')
  320. ->post('https://example.com' => json => {top => 'secret'});
  321. # Non-blocking concurrent requests
  322. Mojo::IOLoop->delay(
  323. sub {
  324. my $delay = shift;
  325. $ua->get('mojolicio.us' => $delay->begin);
  326. $ua->get('cpan.org' => $delay->begin);
  327. },
  328. sub {
  329. my ($delay, $mojo, $cpan) = @_;
  330. say $mojo->res->dom->at('title')->text;
  331. say $cpan->res->dom->at('title')->text;
  332. }
  333. )->wait;
  334. # Non-blocking WebSocket connection sending and receiving JSON messages
  335. $ua->websocket('ws://example.com/echo.json' => sub {
  336. my ($ua, $tx) = @_;
  337. say 'WebSocket handshake failed!' and return unless $tx->is_websocket;
  338. $tx->on(json => sub {
  339. my ($tx, $hash) = @_;
  340. say "WebSocket message via JSON: $hash->{msg}";
  341. $tx->finish;
  342. });
  343. $tx->send({json => {msg => 'Hello World!'}});
  344. });
  345. Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
  346. =head1 DESCRIPTION
  347. L<Mojo::UserAgent> is a full featured non-blocking I/O HTTP and WebSocket user
  348. agent, with IPv6, TLS, SNI, IDNA, HTTP/SOCKS5 proxy, Comet (long polling),
  349. keep-alive, connection pooling, timeout, cookie, multipart, gzip compression
  350. and multiple event loop support.
  351. All connections will be reset automatically if a new process has been forked,
  352. this allows multiple processes to share the same L<Mojo::UserAgent> object
  353. safely.
  354. For better scalability (epoll, kqueue) and to provide IPv6, SOCKS5 as well as
  355. TLS support, the optional modules L<EV> (4.0+), L<IO::Socket::IP> (0.20+),
  356. L<IO::Socket::Socks> (0.64+) and L<IO::Socket::SSL> (1.84+) will be used
  357. automatically if they are installed. Individual features can also be disabled
  358. with the C<MOJO_NO_IPV6>, C<MOJO_NO_SOCKS> and C<MOJO_NO_TLS> environment
  359. variables.
  360. See L<Mojolicious::Guides::Cookbook/"USER AGENT"> for more.
  361. =head1 EVENTS
  362. L<Mojo::UserAgent> inherits all events from L<Mojo::EventEmitter> and can emit
  363. the following new ones.
  364. =head2 start
  365. $ua->on(start => sub {
  366. my ($ua, $tx) = @_;
  367. ...
  368. });
  369. Emitted whenever a new transaction is about to start, this includes
  370. automatically prepared proxy C<CONNECT> requests and followed redirects.
  371. $ua->on(start => sub {
  372. my ($ua, $tx) = @_;
  373. $tx->req->headers->header('X-Bender' => 'Bite my shiny metal ass!');
  374. });
  375. =head1 ATTRIBUTES
  376. L<Mojo::UserAgent> implements the following attributes.
  377. =head2 ca
  378. my $ca = $ua->ca;
  379. $ua = $ua->ca('/etc/tls/ca.crt');
  380. Path to TLS certificate authority file, defaults to the value of the
  381. C<MOJO_CA_FILE> environment variable. Also activates hostname verification.
  382. # Show certificate authorities for debugging
  383. IO::Socket::SSL::set_defaults(
  384. SSL_verify_callback => sub { say "Authority: $_[2]" and return $_[0] });
  385. =head2 cert
  386. my $cert = $ua->cert;
  387. $ua = $ua->cert('/etc/tls/client.crt');
  388. Path to TLS certificate file, defaults to the value of the C<MOJO_CERT_FILE>
  389. environment variable.
  390. =head2 connect_timeout
  391. my $timeout = $ua->connect_timeout;
  392. $ua = $ua->connect_timeout(5);
  393. Maximum amount of time in seconds establishing a connection may take before
  394. getting canceled, defaults to the value of the C<MOJO_CONNECT_TIMEOUT>
  395. environment variable or C<10>.
  396. =head2 cookie_jar
  397. my $cookie_jar = $ua->cookie_jar;
  398. $ua = $ua->cookie_jar(Mojo::UserAgent::CookieJar->new);
  399. Cookie jar to use for requests performed by this user agent, defaults to a
  400. L<Mojo::UserAgent::CookieJar> object.
  401. # Disable extraction of cookies from responses
  402. $ua->cookie_jar->extracting(0);
  403. =head2 inactivity_timeout
  404. my $timeout = $ua->inactivity_timeout;
  405. $ua = $ua->inactivity_timeout(15);
  406. Maximum amount of time in seconds a connection can be inactive before getting
  407. closed, defaults to the value of the C<MOJO_INACTIVITY_TIMEOUT> environment
  408. variable or C<20>. Setting the value to C<0> will allow connections to be
  409. inactive indefinitely.
  410. =head2 ioloop
  411. my $loop = $ua->ioloop;
  412. $ua = $ua->ioloop(Mojo::IOLoop->new);
  413. Event loop object to use for blocking I/O operations, defaults to a
  414. L<Mojo::IOLoop> object.
  415. =head2 key
  416. my $key = $ua->key;
  417. $ua = $ua->key('/etc/tls/client.crt');
  418. Path to TLS key file, defaults to the value of the C<MOJO_KEY_FILE>
  419. environment variable.
  420. =head2 local_address
  421. my $address = $ua->local_address;
  422. $ua = $ua->local_address('127.0.0.1');
  423. Local address to bind to.
  424. =head2 max_connections
  425. my $max = $ua->max_connections;
  426. $ua = $ua->max_connections(5);
  427. Maximum number of keep-alive connections that the user agent will retain
  428. before it starts closing the oldest ones, defaults to C<5>. Setting the value
  429. to C<0> will prevent any connections from being kept alive.
  430. =head2 max_redirects
  431. my $max = $ua->max_redirects;
  432. $ua = $ua->max_redirects(3);
  433. Maximum number of redirects the user agent will follow before it fails,
  434. defaults to the value of the C<MOJO_MAX_REDIRECTS> environment variable or
  435. C<0>.
  436. =head2 proxy
  437. my $proxy = $ua->proxy;
  438. $ua = $ua->proxy(Mojo::UserAgent::Proxy->new);
  439. Proxy manager, defaults to a L<Mojo::UserAgent::Proxy> object.
  440. # Detect proxy servers from environment
  441. $ua->proxy->detect;
  442. # Manually configure HTTP proxy (using CONNECT for HTTPS)
  443. $ua->proxy->http('http://127.0.0.1:8080')->https('http://127.0.0.1:8080');
  444. # Manually configure Tor (SOCKS5)
  445. $ua->proxy->http('socks://127.0.0.1:9050')->https('socks://127.0.0.1:9050');
  446. =head2 request_timeout
  447. my $timeout = $ua->request_timeout;
  448. $ua = $ua->request_timeout(5);
  449. Maximum amount of time in seconds establishing a connection, sending the
  450. request and receiving a whole response may take before getting canceled,
  451. defaults to the value of the C<MOJO_REQUEST_TIMEOUT> environment variable or
  452. C<0>. Setting the value to C<0> will allow the user agent to wait
  453. indefinitely. The timeout will reset for every followed redirect.
  454. # Total limit of 5 seconds, of which 3 seconds may be spent connecting
  455. $ua->max_redirects(0)->connect_timeout(3)->request_timeout(5);
  456. =head2 server
  457. my $server = $ua->server;
  458. $ua = $ua->server(Mojo::UserAgent::Server->new);
  459. Application server relative URLs will be processed with, defaults to a
  460. L<Mojo::UserAgent::Server> object.
  461. # Introspect
  462. say for @{$ua->server->app->secrets};
  463. # Change log level
  464. $ua->server->app->log->level('fatal');
  465. # Port currently used for processing relative URLs blocking
  466. say $ua->server->url->port;
  467. # Port currently used for processing relative URLs non-blocking
  468. say $ua->server->nb_url->port;
  469. =head2 transactor
  470. my $t = $ua->transactor;
  471. $ua = $ua->transactor(Mojo::UserAgent::Transactor->new);
  472. Transaction builder, defaults to a L<Mojo::UserAgent::Transactor> object.
  473. # Change name of user agent
  474. $ua->transactor->name('MyUA 1.0');
  475. =head1 METHODS
  476. L<Mojo::UserAgent> inherits all methods from L<Mojo::EventEmitter> and
  477. implements the following new ones.
  478. =head2 build_tx
  479. my $tx = $ua->build_tx(GET => 'example.com');
  480. my $tx = $ua->build_tx(
  481. PUT => 'http://example.com' => {Accept => '*/*'} => 'Hi!');
  482. my $tx = $ua->build_tx(
  483. PUT => 'http://example.com' => {Accept => '*/*'} => form => {a => 'b'});
  484. my $tx = $ua->build_tx(
  485. PUT => 'http://example.com' => {Accept => '*/*'} => json => {a => 'b'});
  486. Generate L<Mojo::Transaction::HTTP> object with
  487. L<Mojo::UserAgent::Transactor/"tx">.
  488. # Request with custom cookie
  489. my $tx = $ua->build_tx(GET => 'example.com');
  490. $tx->req->cookies({name => 'foo', value => 'bar'});
  491. $tx = $ua->start($tx);
  492. # Deactivate gzip compression
  493. my $tx = $ua->build_tx(GET => 'example.com');
  494. $tx->req->headers->remove('Accept-Encoding');
  495. $tx = $ua->start($tx);
  496. # Interrupt response by raising an error
  497. my $tx = $ua->build_tx(GET => 'example.com');
  498. $tx->res->on(progress => sub {
  499. my $res = shift;
  500. return unless my $server = $res->headers->server;
  501. $res->error({message => 'Oh noes, it is IIS!'}) if $server =~ /IIS/;
  502. });
  503. $tx = $ua->start($tx);
  504. =head2 build_websocket_tx
  505. my $tx = $ua->build_websocket_tx('ws://example.com');
  506. my $tx = $ua->build_websocket_tx(
  507. 'ws://example.com' => {DNT => 1} => ['v1.proto']);
  508. Generate L<Mojo::Transaction::HTTP> object with
  509. L<Mojo::UserAgent::Transactor/"websocket">.
  510. =head2 delete
  511. my $tx = $ua->delete('example.com');
  512. my $tx = $ua->delete('http://example.com' => {Accept => '*/*'} => 'Hi!');
  513. my $tx = $ua->delete(
  514. 'http://example.com' => {Accept => '*/*'} => form => {a => 'b'});
  515. my $tx = $ua->delete(
  516. 'http://example.com' => {Accept => '*/*'} => json => {a => 'b'});
  517. Perform blocking C<DELETE> request and return resulting
  518. L<Mojo::Transaction::HTTP> object, takes the same arguments as
  519. L<Mojo::UserAgent::Transactor/"tx"> (except for the C<DELETE> method, which is
  520. implied). You can also append a callback to perform requests non-blocking.
  521. $ua->delete('http://example.com' => sub {
  522. my ($ua, $tx) = @_;
  523. say $tx->res->body;
  524. });
  525. Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
  526. =head2 get
  527. my $tx = $ua->get('example.com');
  528. my $tx = $ua->get('http://example.com' => {Accept => '*/*'} => 'Hi!');
  529. my $tx = $ua->get(
  530. 'http://example.com' => {Accept => '*/*'} => form => {a => 'b'});
  531. my $tx = $ua->get(
  532. 'http://example.com' => {Accept => '*/*'} => json => {a => 'b'});
  533. Perform blocking C<GET> request and return resulting
  534. L<Mojo::Transaction::HTTP> object, takes the same arguments as
  535. L<Mojo::UserAgent::Transactor/"tx"> (except for the C<GET> method, which is
  536. implied). You can also append a callback to perform requests non-blocking.
  537. $ua->get('http://example.com' => sub {
  538. my ($ua, $tx) = @_;
  539. say $tx->res->body;
  540. });
  541. Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
  542. =head2 head
  543. my $tx = $ua->head('example.com');
  544. my $tx = $ua->head('http://example.com' => {Accept => '*/*'} => 'Hi!');
  545. my $tx = $ua->head(
  546. 'http://example.com' => {Accept => '*/*'} => form => {a => 'b'});
  547. my $tx = $ua->head(
  548. 'http://example.com' => {Accept => '*/*'} => json => {a => 'b'});
  549. Perform blocking C<HEAD> request and return resulting
  550. L<Mojo::Transaction::HTTP> object, takes the same arguments as
  551. L<Mojo::UserAgent::Transactor/"tx"> (except for the C<HEAD> method, which is
  552. implied). You can also append a callback to perform requests non-blocking.
  553. $ua->head('http://example.com' => sub {
  554. my ($ua, $tx) = @_;
  555. say $tx->res->body;
  556. });
  557. Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
  558. =head2 options
  559. my $tx = $ua->options('example.com');
  560. my $tx = $ua->options('http://example.com' => {Accept => '*/*'} => 'Hi!');
  561. my $tx = $ua->options(
  562. 'http://example.com' => {Accept => '*/*'} => form => {a => 'b'});
  563. my $tx = $ua->options(
  564. 'http://example.com' => {Accept => '*/*'} => json => {a => 'b'});
  565. Perform blocking C<OPTIONS> request and return resulting
  566. L<Mojo::Transaction::HTTP> object, takes the same arguments as
  567. L<Mojo::UserAgent::Transactor/"tx"> (except for the C<OPTIONS> method, which
  568. is implied). You can also append a callback to perform requests non-blocking.
  569. $ua->options('http://example.com' => sub {
  570. my ($ua, $tx) = @_;
  571. say $tx->res->body;
  572. });
  573. Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
  574. =head2 patch
  575. my $tx = $ua->patch('example.com');
  576. my $tx = $ua->patch('http://example.com' => {Accept => '*/*'} => 'Hi!');
  577. my $tx = $ua->patch(
  578. 'http://example.com' => {Accept => '*/*'} => form => {a => 'b'});
  579. my $tx = $ua->patch(
  580. 'http://example.com' => {Accept => '*/*'} => json => {a => 'b'});
  581. Perform blocking C<PATCH> request and return resulting
  582. L<Mojo::Transaction::HTTP> object, takes the same arguments as
  583. L<Mojo::UserAgent::Transactor/"tx"> (except for the C<PATCH> method, which is
  584. implied). You can also append a callback to perform requests non-blocking.
  585. $ua->patch('http://example.com' => sub {
  586. my ($ua, $tx) = @_;
  587. say $tx->res->body;
  588. });
  589. Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
  590. =head2 post
  591. my $tx = $ua->post('example.com');
  592. my $tx = $ua->post('http://example.com' => {Accept => '*/*'} => 'Hi!');
  593. my $tx = $ua->post(
  594. 'http://example.com' => {Accept => '*/*'} => form => {a => 'b'});
  595. my $tx = $ua->post(
  596. 'http://example.com' => {Accept => '*/*'} => json => {a => 'b'});
  597. Perform blocking C<POST> request and return resulting
  598. L<Mojo::Transaction::HTTP> object, takes the same arguments as
  599. L<Mojo::UserAgent::Transactor/"tx"> (except for the C<POST> method, which is
  600. implied). You can also append a callback to perform requests non-blocking.
  601. $ua->post('http://example.com' => sub {
  602. my ($ua, $tx) = @_;
  603. say $tx->res->body;
  604. });
  605. Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
  606. =head2 put
  607. my $tx = $ua->put('example.com');
  608. my $tx = $ua->put('http://example.com' => {Accept => '*/*'} => 'Hi!');
  609. my $tx = $ua->put(
  610. 'http://example.com' => {Accept => '*/*'} => form => {a => 'b'});
  611. my $tx = $ua->put(
  612. 'http://example.com' => {Accept => '*/*'} => json => {a => 'b'});
  613. Perform blocking C<PUT> request and return resulting
  614. L<Mojo::Transaction::HTTP> object, takes the same arguments as
  615. L<Mojo::UserAgent::Transactor/"tx"> (except for the C<PUT> method, which is
  616. implied). You can also append a callback to perform requests non-blocking.
  617. $ua->put('http://example.com' => sub {
  618. my ($ua, $tx) = @_;
  619. say $tx->res->body;
  620. });
  621. Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
  622. =head2 start
  623. my $tx = $ua->start(Mojo::Transaction::HTTP->new);
  624. Perform blocking request for a custom L<Mojo::Transaction::HTTP> object, which
  625. can be prepared manually or with L</"build_tx">. You can also append a
  626. callback to perform requests non-blocking.
  627. my $tx = $ua->build_tx(GET => 'http://example.com');
  628. $ua->start($tx => sub {
  629. my ($ua, $tx) = @_;
  630. say $tx->res->body;
  631. });
  632. Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
  633. =head2 websocket
  634. $ua->websocket('ws://example.com' => sub {...});
  635. $ua->websocket(
  636. 'ws://example.com' => {DNT => 1} => ['v1.proto'] => sub {...});
  637. Open a non-blocking WebSocket connection with transparent handshake, takes the
  638. same arguments as L<Mojo::UserAgent::Transactor/"websocket">. The callback
  639. will receive either a L<Mojo::Transaction::WebSocket> or
  640. L<Mojo::Transaction::HTTP> object, depending on if the handshake was
  641. successful.
  642. $ua->websocket('ws://example.com/echo' => sub {
  643. my ($ua, $tx) = @_;
  644. say 'WebSocket handshake failed!' and return unless $tx->is_websocket;
  645. $tx->on(finish => sub {
  646. my ($tx, $code, $reason) = @_;
  647. say "WebSocket closed with status $code.";
  648. });
  649. $tx->on(message => sub {
  650. my ($tx, $msg) = @_;
  651. say "WebSocket message: $msg";
  652. $tx->finish;
  653. });
  654. $tx->send('Hi!');
  655. });
  656. Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
  657. You can activate C<permessage-deflate> compression by setting the
  658. C<Sec-WebSocket-Extensions> header, this can result in much better
  659. performance, but also increases memory usage by up to 300KB per connection.
  660. my $headers = {'Sec-WebSocket-Extensions' => 'permessage-deflate'};
  661. $ua->websocket('ws://example.com/foo' => $headers => sub {...});
  662. =head1 DEBUGGING
  663. You can set the C<MOJO_USERAGENT_DEBUG> environment variable to get some
  664. advanced diagnostics information printed to C<STDERR>.
  665. MOJO_USERAGENT_DEBUG=1
  666. =head1 SEE ALSO
  667. L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>.
  668. =cut