AnyEvent-HTTP-LWP-UserAgent-0.10/000075500000000000000000000000001222617602500162745ustar00rootroot00000000000000AnyEvent-HTTP-LWP-UserAgent-0.10/Changes000064400000000000000000000022061222617602500175670ustar00rootroot00000000000000Revision history for AnyEvent-HTTP-LWP-UserAgent 0.10 Mon Dec 10, 2012 Add guard for length(undef) to avoid warnings in older perl. t/invalid_host.t checks if not-died instead of not-success. 0.09 Sat Dec 08, 2012 Add minimum support for request content by code reference, response content by filename or code reference 0.08 Fri Oct 05, 2012 Fair support of pure AnyEvent without Coro. Thanks to Yasutaka Atarashi. 0.07 Mon Mar 14, 2011 Use new version of AnyEvent::HTTP with persistent connections. Initial support of LWP's conn_cache for it. 0.06 Wed Mar 09, 2011 Don't overload Coro stack when we have a really big url. 0.05 Thu Mar 03, 2011 Increase version. 0.04 Thu Mar 03, 2011 More compatible with LWP::UserAgent using handlers. Thanks motetem (Masatoshi Fujimura) for patch. 0.03 Thu Dec 09, 2010 Support cookies. Fix headers that exists many times in response. 0.02 Tue Dec 07, 2010 Fix SYNOPSIS in POD, thanks to Ryan Perry. 0.01 Fri Nov 26, 2010 First prototype AnyEvent-HTTP-LWP-UserAgent-0.10/LICENSE000064400000000000000000000436531222617602500173140ustar00rootroot00000000000000This software is copyright (c) 2012 by Yury Zavarin. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. Terms of the Perl programming language system itself a) the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version, or b) the "Artistic License" --- The GNU General Public License, Version 1, February 1989 --- This software is Copyright (c) 2012 by Yury Zavarin. This is free software, licensed under: The GNU General Public License, Version 1, February 1989 GNU GENERAL PUBLIC LICENSE Version 1, February 1989 Copyright (C) 1989 Free Software Foundation, Inc. 51 Franklin St, Suite 500, Boston, MA 02110-1335 USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The license agreements of most software companies try to keep users at the mercy of those companies. By contrast, our General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. The General Public License applies to the Free Software Foundation's software and to any other program whose authors commit to using it. You can use it for your programs, too. When we speak of free software, we are referring to freedom, not price. Specifically, the General Public License is designed to make sure that you have the freedom to give away or sell copies of free software, that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs; and that you know you can do these things. To protect your rights, we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights. These restrictions translate to certain responsibilities for you if you distribute copies of the software, or if you modify it. For example, if you distribute copies of a such a program, whether gratis or for a fee, you must give the recipients all the rights that you have. You must make sure that they, too, receive or can get the source code. And you must tell them their rights. We protect your rights with two steps: (1) copyright the software, and (2) offer you this license which gives you legal permission to copy, distribute and/or modify the software. Also, for each author's protection and ours, we want to make certain that everyone understands that there is no warranty for this free software. If the software is modified by someone else and passed on, we want its recipients to know that what they have is not the original, so that any problems introduced by others will not reflect on the original authors' reputations. The precise terms and conditions for copying, distribution and modification follow. GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License Agreement applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License. The "Program", below, refers to any such program or work, and a "work based on the Program" means either the Program or any work containing the Program or a portion of it, either verbatim or with modifications. Each licensee is addressed as "you". 1. You may copy and distribute verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this General Public License and to the absence of any warranty; and give any other recipients of the Program a copy of this General Public License along with the Program. You may charge a fee for the physical act of transferring a copy. 2. You may modify your copy or copies of the Program or any portion of it, and copy and distribute such modifications under the terms of Paragraph 1 above, provided that you also do the following: a) cause the modified files to carry prominent notices stating that you changed the files and the date of any change; and b) cause the whole of any work that you distribute or publish, that in whole or in part contains the Program or any part thereof, either with or without modifications, to be licensed at no charge to all third parties under the terms of this General Public License (except that you may choose to grant warranty protection to some or all third parties, at your option). c) If the modified program normally reads commands interactively when run, you must cause it, when started running for such interactive use in the simplest and most usual way, to print or display an announcement including an appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a warranty) and that users may redistribute the program under these conditions, and telling the user how to view a copy of this General Public License. d) You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. Mere aggregation of another independent work with the Program (or its derivative) on a volume of a storage or distribution medium does not bring the other work under the scope of these terms. 3. You may copy and distribute the Program (or a portion or derivative of it, under Paragraph 2) in object code or executable form under the terms of Paragraphs 1 and 2 above provided that you also do one of the following: a) accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Paragraphs 1 and 2 above; or, b) accompany it with a written offer, valid for at least three years, to give any third party free (except for a nominal charge for the cost of distribution) a complete machine-readable copy of the corresponding source code, to be distributed under the terms of Paragraphs 1 and 2 above; or, c) accompany it with the information you received as to where the corresponding source code may be obtained. (This alternative is allowed only for noncommercial distribution and only if you received the program in object code or executable form alone.) Source code for a work means the preferred form of the work for making modifications to it. For an executable file, complete source code means all the source code for all modules it contains; but, as a special exception, it need not include source code for modules which are standard libraries that accompany the operating system on which the executable file runs, or for standard header files or definitions files that accompany that operating system. 4. You may not copy, modify, sublicense, distribute or transfer the Program except as expressly provided under this General Public License. Any attempt otherwise to copy, modify, sublicense, distribute or transfer the Program is void, and will automatically terminate your rights to use the Program under this License. However, parties who have received copies, or rights to use copies, from you under this General Public License will not have their licenses terminated so long as such parties remain in full compliance. 5. By copying, distributing or modifying the Program (or any work based on the Program) you indicate your acceptance of this license to do so, and all its terms and conditions. 6. Each time you redistribute the Program (or any work based on the Program), the recipient automatically receives a license from the original licensor to copy, distribute or modify the Program subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. 7. The Free Software Foundation may publish revised and/or new versions of the General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies a version number of the license which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of the license, you may choose any version ever published by the Free Software Foundation. 8. If you wish to incorporate parts of the Program into other free programs whose distribution conditions are different, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. NO WARRANTY 9. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 10. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. END OF TERMS AND CONDITIONS Appendix: How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to humanity, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively convey the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) 19yy This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA Also add information on how to contact you by electronic and paper mail. If the program is interactive, make it output a short notice like this when it starts in an interactive mode: Gnomovision version 69, Copyright (C) 19xx name of author Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, the commands you use may be called something other than `show w' and `show c'; they could even be mouse-clicks or menu items--whatever suits your program. You should also get your employer (if you work as a programmer) or your school, if any, to sign a "copyright disclaimer" for the program, if necessary. Here a sample; alter the names: Yoyodyne, Inc., hereby disclaims all copyright interest in the program `Gnomovision' (a program to direct compilers to make passes at assemblers) written by James Hacker. , 1 April 1989 Ty Coon, President of Vice That's all there is to it! --- The Artistic License 1.0 --- This software is Copyright (c) 2012 by Yury Zavarin. This is free software, licensed under: The Artistic License 1.0 The Artistic License Preamble The intent of this document is to state the conditions under which a Package may be copied, such that the Copyright Holder maintains some semblance of artistic control over the development of the package, while giving the users of the package the right to use and distribute the Package in a more-or-less customary fashion, plus the right to make reasonable modifications. Definitions: - "Package" refers to the collection of files distributed by the Copyright Holder, and derivatives of that collection of files created through textual modification. - "Standard Version" refers to such a Package if it has not been modified, or has been modified in accordance with the wishes of the Copyright Holder. - "Copyright Holder" is whoever is named in the copyright or copyrights for the package. - "You" is you, if you're thinking about copying or distributing this Package. - "Reasonable copying fee" is whatever you can justify on the basis of media cost, duplication charges, time of people involved, and so on. (You will not be required to justify it to the Copyright Holder, but only to the computing community at large as a market that must bear the fee.) - "Freely Available" means that no fee is charged for the item itself, though there may be fees involved in handling the item. It also means that recipients of the item may redistribute it under the same conditions they received it. 1. You may make and give away verbatim copies of the source form of the Standard Version of this Package without restriction, provided that you duplicate all of the original copyright notices and associated disclaimers. 2. You may apply bug fixes, portability fixes and other modifications derived from the Public Domain or from the Copyright Holder. A Package modified in such a way shall still be considered the Standard Version. 3. You may otherwise modify your copy of this Package in any way, provided that you insert a prominent notice in each changed file stating how and when you changed that file, and provided that you do at least ONE of the following: a) place your modifications in the Public Domain or otherwise make them Freely Available, such as by posting said modifications to Usenet or an equivalent medium, or placing the modifications on a major archive site such as ftp.uu.net, or by allowing the Copyright Holder to include your modifications in the Standard Version of the Package. b) use the modified Package only within your corporation or organization. c) rename any non-standard executables so the names do not conflict with standard executables, which must also be provided, and provide a separate manual page for each non-standard executable that clearly documents how it differs from the Standard Version. d) make other distribution arrangements with the Copyright Holder. 4. You may distribute the programs of this Package in object code or executable form, provided that you do at least ONE of the following: a) distribute a Standard Version of the executables and library files, together with instructions (in the manual page or equivalent) on where to get the Standard Version. b) accompany the distribution with the machine-readable source of the Package with your modifications. c) accompany any non-standard executables with their corresponding Standard Version executables, giving the non-standard executables non-standard names, and clearly documenting the differences in manual pages (or equivalent), together with instructions on where to get the Standard Version. d) make other distribution arrangements with the Copyright Holder. 5. You may charge a reasonable copying fee for any distribution of this Package. You may charge any fee you choose for support of this Package. You may not charge a fee for this Package itself. However, you may distribute this Package in aggregate with other (possibly commercial) programs as part of a larger (possibly commercial) software distribution provided that you do not advertise this Package as a product of your own. 6. The scripts and library files supplied as input to or produced as output from the programs of this Package do not automatically fall under the copyright of this Package, but belong to whomever generated them, and may be sold commercially, and may be aggregated with this Package. 7. C or perl subroutines supplied by you and linked into this Package shall not be considered part of this Package. 8. The name of the Copyright Holder may not be used to endorse or promote products derived from this software without specific prior written permission. 9. THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. The End AnyEvent-HTTP-LWP-UserAgent-0.10/MANIFEST000064400000000000000000000003641222617602500174300ustar00rootroot00000000000000Changes LICENSE MANIFEST META.yml Makefile.PL README dist.ini lib/AnyEvent/HTTP/LWP/UserAgent.pm t/async.t t/callbacks.t t/invalid_host.t t/main.t t/release-pod-syntax.t t/request_content_ref.t t/response_content_cb.t t/response_content_file.t AnyEvent-HTTP-LWP-UserAgent-0.10/META.yml000064400000000000000000000012161222617602500175450ustar00rootroot00000000000000--- abstract: 'LWP::UserAgent interface but works using AnyEvent::HTTP' author: - 'Yury Zavarin ' build_requires: File::Temp: 0 Test::More: 0 configure_requires: ExtUtils::MakeMaker: 6.30 dynamic_config: 0 generated_by: 'Dist::Zilla version 4.300028, CPAN::Meta::Converter version 2.120921' license: perl meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: 1.4 name: AnyEvent-HTTP-LWP-UserAgent requires: AnyEvent: 5 AnyEvent::HTTP: 2.1 HTTP::Headers::Util: 0 HTTP::Request::Common: 0 HTTP::Response: 0 LWP::UserAgent: 5.815 parent: 0 strict: 0 warnings: 0 version: 0.10 AnyEvent-HTTP-LWP-UserAgent-0.10/Makefile.PL000064400000000000000000000024341222617602500202510ustar00rootroot00000000000000 use strict; use warnings; use ExtUtils::MakeMaker 6.30; my %WriteMakefileArgs = ( "ABSTRACT" => "LWP::UserAgent interface but works using AnyEvent::HTTP", "AUTHOR" => "Yury Zavarin ", "BUILD_REQUIRES" => { "File::Temp" => 0, "Test::More" => 0 }, "CONFIGURE_REQUIRES" => { "ExtUtils::MakeMaker" => "6.30" }, "DISTNAME" => "AnyEvent-HTTP-LWP-UserAgent", "EXE_FILES" => [], "LICENSE" => "perl", "NAME" => "AnyEvent::HTTP::LWP::UserAgent", "PREREQ_PM" => { "AnyEvent" => 5, "AnyEvent::HTTP" => "2.1", "HTTP::Headers::Util" => 0, "HTTP::Request::Common" => 0, "HTTP::Response" => 0, "LWP::UserAgent" => "5.815", "parent" => 0, "strict" => 0, "warnings" => 0 }, "VERSION" => "0.10", "test" => { "TESTS" => "t/*.t" } ); unless ( eval { ExtUtils::MakeMaker->VERSION(6.56) } ) { my $br = delete $WriteMakefileArgs{BUILD_REQUIRES}; my $pp = $WriteMakefileArgs{PREREQ_PM}; for my $mod ( keys %$br ) { if ( exists $pp->{$mod} ) { $pp->{$mod} = $br->{$mod} if $br->{$mod} > $pp->{$mod}; } else { $pp->{$mod} = $br->{$mod}; } } } delete $WriteMakefileArgs{CONFIGURE_REQUIRES} unless eval { ExtUtils::MakeMaker->VERSION(6.52) }; WriteMakefile(%WriteMakefileArgs); AnyEvent-HTTP-LWP-UserAgent-0.10/README000064400000000000000000000005171222617602500171570ustar00rootroot00000000000000 This archive contains the distribution AnyEvent-HTTP-LWP-UserAgent, version 0.10: LWP::UserAgent interface but works using AnyEvent::HTTP This software is copyright (c) 2012 by Yury Zavarin. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. AnyEvent-HTTP-LWP-UserAgent-0.10/dist.ini000064400000000000000000000005341222617602500177420ustar00rootroot00000000000000name = AnyEvent-HTTP-LWP-UserAgent version = 0.10 author = Yury Zavarin license = Perl_5 copyright_holder = Yury Zavarin [PodWeaver] [@Filter] bundle = @Classic remove = PodCoverageTests [PruneFiles] match = .*~$ match = ^AnyEvent-HTTP-LWP-UserAgent* [AutoPrereqs] [NextRelease] format = %-7v %{eee MMM dd, yyyy}d AnyEvent-HTTP-LWP-UserAgent-0.10/lib/000075500000000000000000000000001222617602500170425ustar00rootroot00000000000000AnyEvent-HTTP-LWP-UserAgent-0.10/lib/AnyEvent/000075500000000000000000000000001222617602500205735ustar00rootroot00000000000000AnyEvent-HTTP-LWP-UserAgent-0.10/lib/AnyEvent/HTTP/000075500000000000000000000000001222617602500213525ustar00rootroot00000000000000AnyEvent-HTTP-LWP-UserAgent-0.10/lib/AnyEvent/HTTP/LWP/000075500000000000000000000000001222617602500220145ustar00rootroot00000000000000AnyEvent-HTTP-LWP-UserAgent-0.10/lib/AnyEvent/HTTP/LWP/UserAgent.pm000064400000000000000000000404511222617602500242530ustar00rootroot00000000000000package AnyEvent::HTTP::LWP::UserAgent; { $AnyEvent::HTTP::LWP::UserAgent::VERSION = '0.10'; } use strict; use warnings; #ABSTRACT: LWP::UserAgent interface but works using AnyEvent::HTTP use parent qw(LWP::UserAgent); use AnyEvent 5; # AE syntax use AnyEvent::HTTP 2.1; # http(s)/1.1 use HTTP::Response; use LWP::UserAgent 5.815; # first version with handlers sub conn_cache { my $self = shift; my $res = $self->SUPER::conn_cache(@_); my $cache = $self->SUPER::conn_cache; if ($cache) { my $total_capacity = $cache->total_capacity; $total_capacity = 100_000 unless(defined($total_capacity)); $AnyEvent::HTTP::ACTIVE = $total_capacity; } return $res; } sub simple_request_async { my ($self, $in_req, $arg, $size) = @_; my ($method, $uri_ref, $args) = $self->lwp_request2anyevent_request($in_req); my $cv = AE::cv; my $out_req; my $content = ''; my $fh; if(!ref($arg) && defined($arg) && length($arg)) { open $fh, '>', $arg or $cv->croak("Can't write to '$arg': $!"); binmode $fh; $args->{on_body} = sub { my ($d, $h) = @_; if($out_req->code < 200 || 300 <= $out_req->code) { # not success $content .= $d; } else { print $fh $d or $cv->croak("Can't write to '$arg': $!"); } return 1; }; } elsif(ref($arg) eq 'CODE') { $args->{on_body} = sub { my ($d, $h) = @_; if($out_req->code < 200 || 300 <= $out_req->code) { # not success $content .= $d; } else { eval { $arg->($d, $out_req, undef) }; my $err = $@; if($err) { chomp $err; $out_req->header('X-Died' => $err); $out_req->header('Client-Aborted' => 'die'); return 0; } } return 1; }; } my $header_init = sub { my ($d, $h) = @_; # special AnyEvent::HTTP's headers my $code = delete $h->{Status}; my $message = delete $h->{Reason}; # Now we don't use in any place this AnyEvent::HTTP pseudo-headers, so # just delete it for (qw/HTTPVersion OrigStatus OrigReason Redirect URL/) { delete $h->{$_}; } # AnyEvent::HTTP join headers by comma # in this header exists many times in response. # It is some trie to split such headers, I need # to read RFCs more carefully. my $headers = HTTP::Headers->new; while (my ($header, $value) = each %$h) { # In previous versions it was a place where heavily used # Coro stack (if Coro used) when you had pseudo-header URL # and URL was really big. # Now it's not such a big problem, we delete URL pseudo-header # and haven't sudden gigantous headers (I hope). my @v = $value =~ /^([^ ].*?[^ ],)*([^ ].*?[^ ])$/; @v = grep { defined($_) } @v; if (scalar(@v) > 1) { @v = map { s/,$//; $_ } @v; $value = \@v; } $headers->header($header => $value); } # special AnyEvent::HTTP codes if ($code >= 590 && $code <= 599) { # make LWP-compatible error in the case of timeout if ($message =~ /timed/ && $code == 599) { $d = '500 read timeout'; $code = 500; } elsif (!defined($d) || $d =~ /^\s*$/) { $d = $message; } } $out_req = HTTP::Response->new($code, $message, $headers, $d); $self->run_handlers(response_header => $out_req); return 1; }; $args->{on_header} = sub { my ($h) = @_; $header_init->(undef, $h); }; http_request $method => $$uri_ref, %$args, sub { my ($d, $h) = @_; $d = $content if $content ne ''; $header_init->($d, $h) if ! defined $out_req; $out_req->content($d) if defined $d; close($fh) or $cv->croak("Can't write to '$arg': $!") if defined ($fh); if(defined($d) && length($d)) { # from LWP::Protocol my %skip_h; for my $h ($self->handlers('response_data', $out_req)) { next if $skip_h{$h}; unless ($h->{callback}->($out_req, $self, $h, $d)) { # XXX remove from $response->{handlers}{response_data} if present $skip_h{$h}++; } } } $out_req->request($in_req); # cookie_jar will be set by the handler $self->run_handlers(response_done => $out_req); $cv->send($out_req); }; return $cv; } sub simple_request { return shift->simple_request_async(@_)->recv; } sub get_async { require HTTP::Request::Common; my($self, @parameters) = @_; my @suff = $self->_process_colonic_headers(\@parameters,1); return $self->request_async( HTTP::Request::Common::GET( @parameters ), @suff ); } sub post_async { require HTTP::Request::Common; my($self, @parameters) = @_; my @suff = $self->_process_colonic_headers(\@parameters, (ref($parameters[1]) ? 2 : 1)); return $self->request_async( HTTP::Request::Common::POST( @parameters ), @suff ); } sub head_async { require HTTP::Request::Common; my($self, @parameters) = @_; my @suff = $self->_process_colonic_headers(\@parameters,1); return $self->request_async( HTTP::Request::Common::HEAD( @parameters ), @suff ); } sub put_async { require HTTP::Request::Common; my($self, @parameters) = @_; my @suff = $self->_process_colonic_headers(\@parameters, (ref($parameters[1]) ? 2 : 1)); return $self->request_async( HTTP::Request::Common::PUT( @parameters ), @suff ); } sub delete_async { require HTTP::Request::Common; my($self, @parameters) = @_; my @suff = $self->_process_colonic_headers(\@parameters,1); return $self->request_async( HTTP::Request::Common::DELETE( @parameters ), @suff ); } sub get { return shift->get_async(@_)->recv; } sub post { return shift->post_async(@_)->recv; } sub head { return shift->head_async(@_)->recv; } sub put { return shift->put_async(@_)->recv; } sub delete { return shift->delete_async(@_)->recv; } sub lwp_request2anyevent_request { my ($self, $in_req) = @_; my $method = $in_req->method; my $uri = $in_req->uri->as_string; if ($self->cookie_jar) { $self->cookie_jar->add_cookie_header($in_req); } my $in_headers = $in_req->headers; my $out_headers = {}; $in_headers->scan( sub { my ($header, $value) = @_; $out_headers->{$header} = $value; } ); # if we will use some code like # local $AnyEvent::HTTP::USERAGENT = $useragent; # in simple_request, it will not work properly in redirects $out_headers->{'User-Agent'} = $self->agent; my $body; if(ref($in_req->content) eq 'CODE') { # Minimum coderef support # TODO: Add chunked transfer but maybe necessary to modify AnyEvent::HTTP itself $body = ''; while(my $ret = $in_req->content->()) { $body .= $ret; last if $ret eq ''; } } else { $body = $in_req->content; } my %args = ( headers => $out_headers, body => $body, recurse => 0, # because LWP call simple_request as much as needed timeout => $self->timeout, ); if ($self->conn_cache) { $args{persistent} = 1; $args{keepalive} = 1; } else { # By default AnyEvent::HTTP set persistent = 1 for idempotent # requests. So just for compatibility with LWP::UserAgent we # disable this options. $args{persistent} = 0; $args{keepalive} = 0; } return ($method, \$uri, \%args); } sub request_async { my($self, $request, $arg, $size, $previous) = @_; my $cv = AE::cv; $self->simple_request_async($request, $arg, $size)->cb(sub { my $response = shift->recv; $response->previous($previous) if $previous; if ($response->redirects >= $self->{max_redirect}) { $response->header("Client-Warning" => "Redirect loop detected (max_redirect = $self->{max_redirect})"); $cv->send($response); return; } if (my $req = $self->run_handlers("response_redirect", $response)) { $self->request_async($req, $arg, $size, $response)->cb(sub { $cv->send(shift->recv) }); return; } my $code = $response->code; if ($code == &HTTP::Status::RC_MOVED_PERMANENTLY or $code == &HTTP::Status::RC_FOUND or $code == &HTTP::Status::RC_SEE_OTHER or $code == &HTTP::Status::RC_TEMPORARY_REDIRECT) { my $referral = $request->clone; # These headers should never be forwarded $referral->remove_header('Host', 'Cookie'); if ($referral->header('Referer') && $request->uri->scheme eq 'https' && $referral->uri->scheme eq 'http') { # RFC 2616, section 15.1.3. # https -> http redirect, suppressing Referer $referral->remove_header('Referer'); } if ($code == &HTTP::Status::RC_SEE_OTHER || $code == &HTTP::Status::RC_FOUND) { my $method = uc($referral->method); unless ($method eq "GET" || $method eq "HEAD") { $referral->method("GET"); $referral->content(""); $referral->remove_content_headers; } } # And then we update the URL based on the Location:-header. my $referral_uri = $response->header('Location'); { # Some servers erroneously return a relative URL for redirects, # so make it absolute if it not already is. local $URI::ABS_ALLOW_RELATIVE_SCHEME = 1; my $base = $response->base; $referral_uri = "" unless defined $referral_uri; $referral_uri = $HTTP::URI_CLASS->new($referral_uri, $base) ->abs($base); } $referral->uri($referral_uri); if($self->redirect_ok($referral, $response)) { $self->request_async($referral, $arg, $size, $response)->cb(sub{ $cv->send(shift->recv) }); return; } else { $cv->send($response); return; } } elsif ($code == &HTTP::Status::RC_UNAUTHORIZED || $code == &HTTP::Status::RC_PROXY_AUTHENTICATION_REQUIRED ) { my $proxy = ($code == &HTTP::Status::RC_PROXY_AUTHENTICATION_REQUIRED); my $ch_header = $proxy ? "Proxy-Authenticate" : "WWW-Authenticate"; my @challenge = $response->header($ch_header); unless (@challenge) { $response->header("Client-Warning" => "Missing Authenticate header"); $cv->send($response); return; } require HTTP::Headers::Util; CHALLENGE: for my $challenge (@challenge) { $challenge =~ tr/,/;/; # "," is used to separate auth-params!! ($challenge) = HTTP::Headers::Util::split_header_words($challenge); my $scheme = shift(@$challenge); shift(@$challenge); # no value $challenge = { @$challenge }; # make rest into a hash unless ($scheme =~ /^([a-z]+(?:-[a-z]+)*)$/) { $response->header("Client-Warning" => "Bad authentication scheme '$scheme'"); $cv->send($response); return; } $scheme = $1; # untainted now my $class = "LWP::Authen::\u$scheme"; $class =~ s/-/_/g; no strict 'refs'; unless (%{"$class\::"}) { # try to load it eval "require $class"; if ($@) { if ($@ =~ /^Can\'t locate/) { $response->header("Client-Warning" => "Unsupported authentication scheme '$scheme'"); } else { $response->header("Client-Warning" => $@); } next CHALLENGE; } } unless ($class->can("authenticate")) { $response->header("Client-Warning" => "Unsupported authentication scheme '$scheme'"); next CHALLENGE; } # TODO: Maybe able to be more asynchronous $cv->send($class->authenticate($self, $proxy, $challenge, $response, $request, $arg, $size)); return; } $cv->send($response); return } $cv->send($response); return; }); return $cv; } sub request { return shift->request_async(@_)->recv; } 1; __END__ =pod =head1 NAME AnyEvent::HTTP::LWP::UserAgent - LWP::UserAgent interface but works using AnyEvent::HTTP =head1 VERSION version 0.10 =head1 SYNOPSIS use AnyEvent::HTTP::LWP::UserAgent; use Coro; my $ua = AnyEvent::HTTP::LWP::UserAgent->new; my @urls = (...); my @coro = map { my $url = $_; async { my $r = $ua->get($url); print "url $url, content " . $r->content . "\n"; } } @urls; $_->join for @coro; # Or without Coro use AnyEvent::HTTP::LWP::UserAgent; use AnyEvent; my $ua = AnyEvent::HTTP::LWP::UserAgent->new; my @urls = (...); my $cv = AE::cv; $cv->begin; foreach my $url (@urls) { $cv->begin; $ua->get_async($url)->cb(sub { my $r = shift->recv; print "url $url, content " . $r->content . "\n"; $cv->end; }); } $cv->end; $cv->recv; =head1 DESCRIPTION When you use Coro you have a choice: you can use L or L (if you want to make asynchronous HTTP requests). If you use Coro::LWP, some modules may work incorrectly (for example Cache::Memcached) because of global change of IO::Socket behavior. AnyEvent::HTTP uses different programming interface, so you must change more of your old code with LWP::UserAgent (and HTTP::Request and so on), if you want to make asynchronous code. AnyEvent::HTTP::LWP::UserAgent uses AnyEvent::HTTP inside but have an interface of LWP::UserAgent. You can safely use this module in Coro environment (and possibly in AnyEvent too). In plain AnyEvent, you may use _async methods. They don't make blocking wait but return condition variable. So, you can avoid recursive blocking wait error. =head1 SOME METHODS =over =item $ua->conn_cache =item $ua->conn_cache($cache_obj) New versions of C supports HTTP(S)/1.1 persistent connection, so you can control it in C using C method. If you set C (as C object) then C makes two things. In first it sets global variable C<$AnyEvent::HTTP::ACTIVE> as you setted C for C (be careful: this have a global consequences, not local). And in the second C will create persistent connections if your C<$ua> have C (local propery of C<$ua>). But you can't use remainder methods of your C, all connections will contains in C. C<$AnyEvent::HTTP::ACTIVE> sets only when you set C for C<$ua>. If you just change C of old C it will not change anything. =back =head1 ASYNC METHODS The following methods are async version of corresponding methods w/o _async suffix. Parameters are identical as originals. However, return value becomes condition variable. You can use it in a synchronous way by blocking wait $ua->simple_request_async(@args)->recv or in an asynchronous way, also. $ua->simple_request_async(@args)->cb(sub { ... }); =over 4 =item simple_request_async =item request_async =item get_async =item post_async =item head_async =item put_async =item delete_async =back =head1 LIMITATIONS AND DETAILS Some features of LWP::UserAgent can be broken (C or something else). Precise documentation and realization of these features will come in the future. You can use some AnyEvent::HTTP global function and variables. But use C of UA instead of C<$AnyEvent::HTTP::USERAGENT> and C instead of C<$AnyEvent::HTTP::MAX_RECURSE>. Content in request can be specified by code reference. This is the same as L but there are some limitations. L uses chunked encoding if Content-Length is not specified, while this module does NOT use chunked encoding even if Content-Length is not specified. Content in response can be specified as filename or code reference. This is the same as L. =head1 SEE ALSO L L L L L =head1 ACKNOWLEDGEMENTS Yasutaka Atarashi =head1 AUTHOR Yury Zavarin =head1 COPYRIGHT AND LICENSE This software is copyright (c) 2012 by Yury Zavarin. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. =cut AnyEvent-HTTP-LWP-UserAgent-0.10/t/000075500000000000000000000000001222617602500165375ustar00rootroot00000000000000AnyEvent-HTTP-LWP-UserAgent-0.10/t/async.t000064400000000000000000000047331222617602500200500ustar00rootroot00000000000000use strict; use Test::More; use AnyEvent::HTTP::LWP::UserAgent; use AnyEvent; BEGIN { eval q{ require Test::TCP } or plan skip_all => 'Could not require Test::TCP'; eval q{ require HTTP::Server::Simple::CGI } or plan skip_all => 'Could not require HTTP::Server::Simple::CGI'; } { package HTTP::Server::Simple::Test; our @ISA = 'HTTP::Server::Simple::CGI'; sub print_banner { } sub handle_request { my ($self, $cgi) = @_; if($cgi->url(-path_info=>1) =~ m,/redirected$,) { print "HTTP/1.0 200 OK\r\n"; print "Content-Type: text/html\r\n"; print "Set-Cookie: test=abc; path=/\r\n"; print "\r\n"; print <<__HTML__; Test Web Page

blahblahblha

__HTML__ } else { print "HTTP/1.0 301 Moved Permanently\r\n"; print "Location: ",$cgi->url(-path_info=>1),"redirected\r\n"; print "\r\n"; print <<__HTML__; Test Web Page Redirected to

__HTML__ } } } plan tests => 14; my $cv = AE::cv; my %tests = ( DELETE => 'AnyEvent::HTTP::LWP::UserAgent::delete_async', GET => 'AnyEvent::HTTP::LWP::UserAgent::get_async', HEAD => 'AnyEvent::HTTP::LWP::UserAgent::head_async', POST => 'AnyEvent::HTTP::LWP::UserAgent::post_async', PUT => 'AnyEvent::HTTP::LWP::UserAgent::put_async', ); Test::TCP::test_tcp( server => sub { my $port = shift; my $server = HTTP::Server::Simple::Test->new($port); $server->run; }, client => sub { my $port = shift; $cv->begin; for my $test (keys %tests) { # We do not share $ua because of cookie_jar separation my $ua = AnyEvent::HTTP::LWP::UserAgent->new(cookie_jar => {}); $ua->requests_redirectable([$test]); $cv->begin; my $method = $tests{$test}; $ua->$method("http://localhost:$port/")->cb(sub { my $res = shift->recv; ok $res->is_success; like $ua->cookie_jar->as_string, qr/test=abc/, $test . ': $ua->cookie_jar set'; is $res->base, 'http://www.example.com/', $test . ': $res->base set' if $test ne 'HEAD'; $cv->end; }); } $cv->end; $cv->recv; }, ); AnyEvent-HTTP-LWP-UserAgent-0.10/t/callbacks.t000064400000000000000000000024171222617602500206470ustar00rootroot00000000000000use strict; use Test::More; use AnyEvent::HTTP::LWP::UserAgent; BEGIN { eval q{ require Test::TCP } or plan skip_all => 'Could not require Test::TCP'; eval q{ require HTTP::Server::Simple::CGI } or plan skip_all => 'Could not require HTTP::Server::Simple::CGI'; } { package HTTP::Server::Simple::Test; our @ISA = 'HTTP::Server::Simple::CGI'; sub print_banner { } sub handle_request { print "HTTP/1.0 200 OK\r\n"; print "Content-Type: text/html\r\n"; print "Set-Cookie: test=abc; path=/\r\n"; print "\r\n"; print <<__HTML__; Test Web Page

blahblahblha

__HTML__ } } plan tests => 3; Test::TCP::test_tcp( server => sub { my $port = shift; my $server = HTTP::Server::Simple::Test->new($port); $server->run; }, client => sub { my $port = shift; my $ua = AnyEvent::HTTP::LWP::UserAgent->new(cookie_jar => {}); my $res = $ua->get("http://localhost:$port/"); ok $res->is_success; like $ua->cookie_jar->as_string, qr/test=abc/, '$ua->cookie_jar set'; is $res->base, 'http://www.example.com/', '$res->base set'; }, ); AnyEvent-HTTP-LWP-UserAgent-0.10/t/invalid_host.t000064400000000000000000000005521222617602500214110ustar00rootroot00000000000000use strict; use Test::More tests => 1; use AnyEvent::HTTP::LWP::UserAgent; # For some error cases, AnyEvent::HTTP does not call `on_header' callback. # Invalid host name is one of the cases. # This test checks if this class does not die for the cases. my $ua = AnyEvent::HTTP::LWP::UserAgent->new; eval { my $res = $ua->get('http://www.invalid/'); }; ok ! $@; AnyEvent-HTTP-LWP-UserAgent-0.10/t/main.t000064400000000000000000000001061222617602500176450ustar00rootroot00000000000000use Test::More tests => 1; use_ok('AnyEvent::HTTP::LWP::UserAgent'); AnyEvent-HTTP-LWP-UserAgent-0.10/t/release-pod-syntax.t000064400000000000000000000004501222617602500224470ustar00rootroot00000000000000#!perl BEGIN { unless ($ENV{RELEASE_TESTING}) { require Test::More; Test::More::plan(skip_all => 'these tests are for release candidate testing'); } } use Test::More; eval "use Test::Pod 1.41"; plan skip_all => "Test::Pod 1.41 required for testing POD" if $@; all_pod_files_ok(); AnyEvent-HTTP-LWP-UserAgent-0.10/t/request_content_ref.t000064400000000000000000000037421222617602500230100ustar00rootroot00000000000000use strict; use Test::More; my $pkg; BEGIN { if(exists $ENV{USE_LWP}) { require LWP::UserAgent; $pkg = 'LWP::UserAgent'; } else { require AnyEvent::HTTP::LWP::UserAgent; $pkg = 'AnyEvent::HTTP::LWP::UserAgent'; } } BEGIN { eval q{ require Test::TCP } or plan skip_all => 'Could not require Test::TCP'; eval q{ require HTTP::Server::Simple::CGI } or plan skip_all => 'Could not require HTTP::Server::Simple::CGI'; } { package HTTP::Server::Simple::Test; our @ISA = 'HTTP::Server::Simple::CGI'; sub print_banner { } sub handle_request { my ($self, $cgi) = @_; my $data = $cgi->param('PUTDATA'); print "HTTP/1.0 200 OK\r\n"; print "Content-Type: text/html\r\n"; print "Set-Cookie: test=abc; path=/\r\n"; print "\r\n"; print <<__HTML__; Test Web Page

$data

__HTML__ } } plan tests => 4; Test::TCP::test_tcp( server => sub { my $port = shift; my $server = HTTP::Server::Simple::Test->new($port); $server->run; }, client => sub { my $port = shift; my @content = ('This', ' ', 'is', ' ', 'content.', ''); my $ua = $pkg->new(cookie_jar => {}); # For AnyEvent::HTTP::LWP::UserAgent, even if Content-Length is not specified, chunked encoding is NOT used. # For LWP::UserAgent, if Content-Length is not specified, chunked encoding is used. my $req = HTTP::Request->new('PUT', "http://localhost:$port/", ['Content-Type' => 'text/plain', 'Content-Length' => 16], sub { return shift @content }); my $res = $ua->request($req); ok $res->is_success; like $ua->cookie_jar->as_string, qr/test=abc/, '$ua->cookie_jar set'; is $res->base, 'http://www.example.com/', '$res->base set'; like($res->content, qr{

This is content\.

}, 'content'); }, ); AnyEvent-HTTP-LWP-UserAgent-0.10/t/response_content_cb.t000064400000000000000000000055201222617602500227620ustar00rootroot00000000000000use strict; use Test::More; my $pkg; BEGIN { if(exists $ENV{USE_LWP}) { require LWP::UserAgent; $pkg = 'LWP::UserAgent'; } else { require AnyEvent::HTTP::LWP::UserAgent; $pkg = 'AnyEvent::HTTP::LWP::UserAgent'; } } BEGIN { eval q{ require Test::TCP } or plan skip_all => 'Could not require Test::TCP'; eval q{ require HTTP::Server::Simple::CGI } or plan skip_all => 'Could not require HTTP::Server::Simple::CGI'; } { package HTTP::Server::Simple::Test; our @ISA = 'HTTP::Server::Simple::CGI'; sub print_banner { } sub handle_request { my ($self, $cgi) = @_; if($cgi->url(-path_info=>1) =~ m,/error$,) { print "HTTP/1.0 404 Not found\r\n"; print "Content-Type: text/plain\r\n"; print "\r\n"; print "404 Not found"; return; } print "HTTP/1.0 200 OK\r\n"; print "Content-Type: text/html\r\n"; print "Set-Cookie: test=abc; path=/\r\n"; print "\r\n"; print <<__HTML__; Test Web Page

blahblahblha

__HTML__ } } plan tests => 12; Test::TCP::test_tcp( server => sub { my $port = shift; my $server = HTTP::Server::Simple::Test->new($port); $server->run; }, client => sub { my $port = shift; { my $ua = $pkg->new(cookie_jar => {}); my $content = ''; my $res = $ua->get("http://localhost:$port/", ':content_cb' => sub { $content .= $_[0] }); ok $res->is_success, 'is_success'; like $ua->cookie_jar->as_string, qr/test=abc/, '$ua->cookie_jar set'; is $res->content, '', 'empty content'; like $content, qr{

blahblahblha

}, 'valid callback'; } { my $ua = $pkg->new(cookie_jar => {}); my $res = $ua->get("http://localhost:$port/", ':content_cb' => sub { die 'Died by client'; }); ok $res->is_success, 'is_success when client died'; like $ua->cookie_jar->as_string, qr/test=abc/, '$ua->cookie_jar set when client died'; is $res->content, '', 'empty content when client died'; like $res->header('X-Died'), qr/Died by client/, 'X-Died: when client died'; like $res->header('Client-Aborted'), qr/die/, 'Client-Aborted: when client died'; } { my $ua = $pkg->new(cookie_jar => {}); my $content = ''; my $res = $ua->get("http://localhost:$port/error", ':content_cb' => sub { $content .= $_[0] }); ok !$res->is_success, '!is_success when error'; is $content, '', 'callback when error'; is $res->content, '404 Not found', 'content when error'; } }, ); AnyEvent-HTTP-LWP-UserAgent-0.10/t/response_content_file.t000064400000000000000000000050021222617602500233100ustar00rootroot00000000000000use strict; use Test::More; my $pkg; BEGIN { if(exists $ENV{USE_LWP}) { require LWP::UserAgent; $pkg = 'LWP::UserAgent'; } else { require AnyEvent::HTTP::LWP::UserAgent; $pkg = 'AnyEvent::HTTP::LWP::UserAgent'; } } use File::Temp; BEGIN { eval q{ require Test::TCP } or plan skip_all => 'Could not require Test::TCP'; eval q{ require HTTP::Server::Simple::CGI } or plan skip_all => 'Could not require HTTP::Server::Simple::CGI'; } { package HTTP::Server::Simple::Test; our @ISA = 'HTTP::Server::Simple::CGI'; sub print_banner { } sub handle_request { my ($self, $cgi) = @_; if($cgi->url(-path_info=>1) =~ m,/error$,) { print "HTTP/1.0 404 Not found\r\n"; print "Content-Type: text/plain\r\n"; print "\r\n"; print "404 Not found"; return; } print "HTTP/1.0 200 OK\r\n"; print "Content-Type: text/html\r\n"; print "Set-Cookie: test=abc; path=/\r\n"; print "\r\n"; print <<__HTML__; Test Web Page

blahblahblha

__HTML__ } } plan tests => 7; Test::TCP::test_tcp( server => sub { my $port = shift; my $server = HTTP::Server::Simple::Test->new($port); $server->run; }, client => sub { my $port = shift; { my $ua = $pkg->new(cookie_jar => {}); my $temp = File::Temp->new; my $res = $ua->get("http://localhost:$port/", ':content_file' => $temp->filename); ok $res->is_success, 'is_success'; like $ua->cookie_jar->as_string, qr/test=abc/, '$ua->cookie_jar set'; is $res->content, '', 'empty content'; { local $/; open my $fh, '<', $temp; like <$fh>, qr{

blahblahblha

}, 'valid file'; close $fh; } } { my $ua = $pkg->new(cookie_jar => {}); my $temp = File::Temp->new; my $res = $ua->get("http://localhost:$port/error", ':content_file' => $temp->filename); ok !$res->is_success, '!is_success when error'; is $res->content, '404 Not found', 'content when error'; { local $/; open my $fh, '<', $temp; is <$fh>, '', 'file when error'; close $fh; } } }, );