From 3f4df8df0ef6ac3065c06bcb410c344c7183f976 Mon Sep 17 00:00:00 2001 From: Packit Date: Sep 16 2020 12:41:06 +0000 Subject: perl-HTTP-Tiny-0.074 base --- diff --git a/CONTRIBUTING.mkdn b/CONTRIBUTING.mkdn new file mode 100644 index 0000000..761c9db --- /dev/null +++ b/CONTRIBUTING.mkdn @@ -0,0 +1,87 @@ +## HOW TO CONTRIBUTE + +Thank you for considering contributing to this distribution. This file +contains instructions that will help you work with the source code. + +The distribution is managed with Dist::Zilla. This means than many of the +usual files you might expect are not in the repository, but are generated at +release time, as is much of the documentation. Some generated files are +kept in the repository as a convenience (e.g. Makefile.PL or cpanfile). + +Generally, **you do not need Dist::Zilla to contribute patches**. You do need +Dist::Zilla to create a tarball. See below for guidance. + +### Getting dependencies + +If you have App::cpanminus 1.6 or later installed, you can use `cpanm` to +satisfy dependencies like this: + + $ cpanm --installdeps . + +Otherwise, look for either a `Makefile.PL` or `cpanfile` file for +a list of dependencies to satisfy. + +### Running tests + +You can run tests directly using the `prove` tool: + + $ prove -l + $ prove -lv t/some_test_file.t + +For most of my distributions, `prove` is entirely sufficient for you to test any +patches you have. I use `prove` for 99% of my testing during development. + +### Code style and tidying + +Please try to match any existing coding style. If there is a `.perltidyrc` +file, please install Perl::Tidy and use perltidy before submitting patches. + +If there is a `tidyall.ini` file, you can also install Code::TidyAll and run +`tidyall` on a file or `tidyall -a` to tidy all files. + +### Patching documentation + +Much of the documentation Pod is generated at release time. Some is +generated boilerplate; other documentation is built from pseudo-POD +directives in the source like C<=method> or C<=func>. + +If you would like to submit a documentation edit, please limit yourself to +the documentation you see. + +If you see typos or documentation issues in the generated docs, please +email or open a bug ticket instead of patching. + +### Installing and using Dist::Zilla + +Dist::Zilla is a very powerful authoring tool, optimized for maintaining a +large number of distributions with a high degree of automation, but it has a +large dependency chain, a bit of a learning curve and requires a number of +author-specific plugins. + +To install it from CPAN, I recommend one of the following approaches for +the quickest installation: + + # using CPAN.pm, but bypassing non-functional pod tests + $ cpan TAP::Harness::Restricted + $ PERL_MM_USE_DEFAULT=1 HARNESS_CLASS=TAP::Harness::Restricted cpan Dist::Zilla + + # using cpanm, bypassing *all* tests + $ cpanm -n Dist::Zilla + +In either case, it's probably going to take about 10 minutes. Go for a walk, +go get a cup of your favorite beverage, take a bathroom break, or whatever. +When you get back, Dist::Zilla should be ready for you. + +Then you need to install any plugins specific to this distribution: + + $ cpan `dzil authordeps` + $ dzil authordeps | cpanm + +Once installed, here are some dzil commands you might try: + + $ dzil build + $ dzil test + $ dzil xtest + +You can learn more about Dist::Zilla at http://dzil.org/ + diff --git a/Changes b/Changes new file mode 100644 index 0000000..ebdf66b --- /dev/null +++ b/Changes @@ -0,0 +1,656 @@ +Release notes for HTTP-Tiny + +0.074 2018-07-30 15:35:44-04:00 America/New_York + + - No changes from 0.073-TRIAL. + +0.073 2018-07-24 11:33:53-04:00 America/New_York (TRIAL RELEASE) + + 0.071 never made it to CPAN; skipping to 0.073 + + [DOCS] + + - Documented 'protocol' field in response hash. + +0.071 2018-04-22 14:45:43+02:00 Europe/Oslo (TRIAL RELEASE) + + [DOCS] + + - Documented that method argument to request() is case-sensitive. + + [INTERNAL] + + - Minor regex cleanup + + - Updated .travis.yml for recent Perls + +0.070 2016-10-09 23:23:28-04:00 America/New_York + + - No changes from 0.069-TRIAL. + +0.069 2016-10-05 11:35:58-04:00 America/New_York (TRIAL RELEASE) + + [INTERNAL] + + - Lazy load Carp only if needed. + +0.068 2016-09-23 16:10:03-04:00 America/New_York + + - No changes from 0.067-TRIAL. + +0.067 2016-09-14 11:43:14-04:00 America/New_York (TRIAL RELEASE) + + [FIXED] + + - Includes redirect history when issuing a 599 internal error. + +0.065 2016-09-09 22:42:43-04:00 America/New_York (TRIAL RELEASE) + + [TESTS] + + - Try harder to clean up environment in t/140_proxy.t (needed for VMS) + +0.064 2016-08-16 21:37:51-04:00 America/New_York + + - No changes from 0.063-TRIAL + +0.063 2016-08-08 12:18:03-04:00 America/New_York (TRIAL RELEASE) + + [FIXED] + + - Fixed chunked transfer encoding, which previously omitted a trailing + CRLF. + +0.061 2016-08-05 12:10:19-04:00 America/New_York (TRIAL RELEASE) + + [FIXED] + + - Avoid overwriting 'If-Modified-Since' header in mirror() if + the header already exists in something other than lower-case. + + [TESTS] + + - Normalize CRLF when reading test data files in t\150-post_form.t + on Win32 + +0.059 2016-07-29 16:10:32-04:00 America/New_York (TRIAL RELEASE) + + [FIXED] + + - Timeout can now be set as a constructor argument again. + + - CVE-2016-1238: avoid loading optional modules from + @INC path with `.` at the end. + + [TESTS] + + - Updated tests for a future perl which may omit `.` from + the list of directories in @INC by default. + +0.058 2016-05-03 11:29:57-04:00 America/New_York + + - No changes from 0.057 + +0.057 2016-04-18 10:17:00-04:00 America/New_York (TRIAL RELEASE) + + [ADDED] + + - Added support for the SSL_CERT_FILE environment variable. + + - Added 'peer' attribute to force a connection to a particular + server. + + - Added 'connected' method to allow introspection of persistent + connections. + + - An array reference of redirection result hash references is included + in the final response hash reference (but only if redirects occur). + + [CHANGED] + + - Because folded headers are obsoleted in the revised RFCs, if CRLF is + found in header values followed by one or more spaces, they are all + replaced by a single space. + + [FIXED] + + - Per the RFC, control headers are now sent first before other headers + (which are sent in arbitrary order). + + - Only well-known headers have their case canonicalized; all other + headers are sent in the case provided by the user. + + - The 'keep_alive' option now also sets the SO_KEEPALIVE option + on the underlying socket to help with long-lived, idle connections. + + - Request header field values are now validated against the RFC rules + (i.e. empty or else space-or-tab separated tokens of printable + characters). + +0.056 2015-05-19 06:00:40-04:00 America/New_York + + - No changes from 0.055 + +0.055 2015-05-07 18:13:41-04:00 America/New_York (TRIAL RELEASE) + + [ADDED] + + - Added 'can_ssl' method to detect SSL support before trying and + failing with a fatal exception. + + - Added support for 308 redirects + + [FIXED] + + - When specifying a custom CA file, if that file is missing or + unreadable, HTTP::Tiny will no longer fall back to a default CA + + [DOCUMENTED] + + - Noted units are bytes for max_size + +0.054 2015-01-27 07:18:19-05:00 America/New_York + + [ADDED] + + - Added more fallback paths to find CA files (thanks golang) + + [DOCUMENTED] + + - Fixed a typo + +0.053 2014-12-11 23:42:17-05:00 America/New_York + + [FIXED] + + - Defended tests against HTTP_PROXY set in the environment + +0.052 2014-12-11 15:23:54-05:00 America/New_York + + [CHANGED] + + - Proxy allowed from environment variable HTTP_PROXY (uppercase) unless + REQUEST_METHOD is also set. + +0.051 2014-11-17 22:58:44-05:00 America/New_York + + [FIXED] + + - Checks for threads without loading threads.pm + +0.050 2014-09-23 15:30:18-04:00 America/New_York + + [FIXED] + + - Fixed CONNECT requests for some proxies + +0.049 2014-09-02 11:20:07-04:00 America/New_York + + [FIXED] + + - 'keep_alive' is now fork-safe and thread-safe + +0.048 2014-08-21 13:19:51-04:00 America/New_York + + [FIXED] + + - Protected proxy tests from ALL_PROXY in the environment + +0.047 2014-07-29 14:09:05-04:00 America/New_York + + [CHANGED] + + - Updated Mozilla::CA module recommendation version to 20130114 + + [FIXED] + + - Fixed t/00-report-prereqs.t when CPAN::Meta is not installed + +0.046 2014-07-21 10:32:32-04:00 America/New_York + + [FIXED] + + - Empty header fields are now allowed; headers with the 'undef' value + will be rendered as an empty header. + + [DOCUMENTED] + + - Updated HTTP/1.1 spec description from RFC 2616 to RFC 7230-7235 + +0.045 2014-07-19 23:17:28-04:00 America/New_York (TRIAL RELEASE) + + [FIXED] + + - Fixed t/002_croakage.t for various operating systems. + +0.044 2014-07-16 23:46:09-04:00 America/New_York + + [CHANGED] + + - Providing a custom 'Host' header is now a fatal exception. Previously, it + was silently ignored, as the RFC mandates that Host be set from the + URL, but ignoring it could lead to unexpected, confusing errors. + + - optimized URL splitting + + - Passing 'undef' for any proxy attribute will prevent HTTP::Tiny from + setting the proxy from the environment. + +0.043 2014-02-20 20:40:23-05:00 America/New_York + + [FIXED] + + - Does not send absolute request URI when tunneling SSL via proxy + + - Fixes regression in setting host name to verify SSL + + - Protects tests from https_proxy and all_proxy when doing mock testing + +0.042 2014-02-18 11:23:17EST-0500 America/New_York + + [ADDED] + + - If IO::Socket::IP 0.25+ is installed, HTTP::Tiny will use it for + transparent IPv4 or IPv6 support. + +0.041 2014-02-17 13:07:54-05:00 America/New_York + + [no code change, only an amended Changes file] + + [INCOMPATIBLE CHANGES (from 0.039)] + + - The 'proxy' attribute no longer takes precedence over the + 'http_proxy' environment variable. With the addition of http_proxy + and https_proxy attributes (and corresponding environment variable + defaults), the legacy 'proxy' attribute now maps to the + all_proxy/ALL_PROXY environment variable and only takes effect when + other proxy attributes are not defined. + + [ADDED (since 0.039)] + + - Added 'keep_alive' attribute for single-server persistent connections + (Clinton Gormley) + + - Added support for Basic authorization with proxies + + - Added support for https proxies via CONNECT + + [FIXED (since 0.039)] + + - Requests are made with one less write for lower latency (Martin + Evans) + +0.040 2014-02-17 13:02:47-05:00 America/New_York + + [INCOMPATIBLE CHANGES] + + - The 'proxy' attribute no longer takes precedence over the + 'http_proxy' environment variable. With the addition of http_proxy + and https_proxy attributes (and corresponding environment variable + defaults), the legacy 'proxy' attribute now maps to the + all_proxy/ALL_PROXY environment variable and only takes effect when + other proxy attributes are not defined. + + [ADDED] + + - Added support for Basic authorization with proxies + + - Added support for https proxies via CONNECT + +0.039 2013-11-27 19:48:29 America/New_York + + [FIXED] + + - Temporary file creating during mirror() is now opened with O_EXCL + for added security + +0.038 2013-11-18 12:56:26 America/New_York + + [FIXED] + + - Fixed a bug where authentication parameters in the URL would override + an existing Authorization header + +0.037 2013-10-28 13:26:21 America/New_York + + [FIXED] + + - Basic authentication in the URL is now unescaped before being encoded + into the authentication header + + [DOCUMENTED] + + - Added HTTP::Tiny::UA to SEE ALSO and suggested it as the appropriate + place for new features + +0.036 2013-09-25 12:10:06 America/New_York + + [FIXED] + + - Compile test could hang on Windows + + [PREREQS] + + - Dropped configure_requires for ExtUtils::MakeMaker to 6.17 + + [META] + + - Updated support files + +0.035 2013-09-10 12:29:28 America/New_York + + [CHANGED] + + - Encoded from data from 'post_form' preserves term order if data is + provided as an array reference. (They are still sorted for consistency + if provided as a hash reference.) + +0.034 2013-06-26 19:02:25 America/New_York + + [ADDED] + + - Added support for 'Basic' authorization from + user:password parameters in the URL + +0.033 2013-06-21 06:26:51 America/New_York + + [FIXED] + + - Modifying the 'agent' attribute with the accessor will append the + default agent string, just like setting it during construction + +0.032 2013-06-20 11:41:24 America/New_York + + [ADDED] + + - Added 'no_proxy' attribute, defaulting to $ENV{no_proxy} + +0.031 2013-06-16 23:18:18 America/New_York + + [FIXED] + + - Fixed bug receiving 0-length content bodies + +0.030 2013-06-13 11:46:15 America/New_York + + [FIXED] + + - Requests with the empty string as body content no longer generate + 'content-type' and 'content-length' headers. + +0.029 2013-04-17 13:49:07 America/New_York + + [FIXED] + + - Checks for new enough OpenSSL library before using SNI (otherwise + IO::Socket::SSL throws warnings) + +0.028 2013-03-05 14:11:57 America/New_York + + [SUPPORT] + + - Fix repository/issue links to reflect proper repo name + +0.027 2013-03-05 12:02:58 America/New_York + + [SUPPORT] + + - Changed metadata to point to the chansen github repository + for code and issues + + [DOCUMENTATION] + + - Added hyperlink for HTTP::CookieJar + +0.026 2013-03-04 22:53:39 America/New_York + + [ADDED] + + - Added cookie support if an HTTP::CookieJar object is provided in the + 'cookie_jar' attribute [Edward Zborowski] + +0.025 2012-12-26 12:09:43 America/New_York + + [ADDED] + + - Agent string appends default if it ends in a space, just like LWP + [Chris Weyl] + +0.024 2012-10-09 20:44:53 America/New_York + + [ADDED] + + - SSL connections now auto-retry I/O after SSL renegotiation [Alan + Gardner] + + [FIXED] + + - User-specified CA bundles take precedence over Mozilla::CA [Alan + Gardner] + + [PREREQS] + + - SSL support now requires Net::SSLeay 1.49 or greater to support + auto-retry [Mike Doherty] + + - Downgraded IO::Socket::SSL and related prereqs to 'suggests' again + +0.023 2012-09-19 09:55:46 America/New_York + + [PREREQS] + + - IO::Socket::SSL and related prereqs changed to 'required' for dev + release to get better failure diagnostics from CPAN Testers + + [TESTING] + + - Skip live SSL testing unless IO::Socket::SSL 1.56+ installed + +0.022 2012-06-01 23:31:40 America/New_York + + [ADDED] + + - Supports local_address option to set local socket interface + [Chris Nehren, David Golden] + +0.021 2012-05-15 22:38:57 America/New_York + + [TESTING] + + - Skip live SSL testing if $ENV{http_proxy} is set + +0.020 2012-05-14 15:24:37 America/New_York + + [TESTING] + + - Capture prerequisite versions under AUTOMATED_TESTING to help + chase down some failures from CPAN Testers + +0.019 2012-05-14 07:14:00 America/New_York + + [ADDED] + + - Require IO::Socket::SSL 1.56 (which added SSL_hostname support) when + doing HTTPS. [Mike Doherty] + + [TESTING] + + - Provide better diagnostic output in t/210_live_ssl.t [Mike + Doherty] + +0.018 2012-04-18 09:39:50 America/New_York + + [ADDED] + + - Add verify_SSL option to do more secure SSL operations, incl. + attempting to validate against a CA bundle (Mozilla::CA + recommended, but will attempt to find some OS bundles). Also + add SSL_opts, which passes through IO::Socket::SSL's SSL_* + options to control SSL verification. (GH #6, #9) [Mike Doherty] + + + - Reponse hashref includes final URL (including any redirections) + [Lukas Eklund] + +0.017 2012-02-22 21:57:37 EST5EDT + + [DOCUMENTATION] + + - Clarified how max_size exceptions work [rt.cpan.org #75142] + + - Clarify that 2XX is success for most methods (except mirror + where 304 is also success) [rt.cpan.org #75141] + +0.016 2011-10-26 23:05:50 America/New_York + + [BUG FIXES] + + - Fixed Perl 5.6 compatibility by emulating utf8::encode [David Golden] + +0.015 2011-10-26 16:42:26 America/New_York + + [BUG FIXES] + + - Make sure PERL_UNICODE doesn't affect PUT test data [Tony Cook] + + [DOCUMENTATION] + + - Fixed typo + +0.014 2011-10-20 13:54:13 America/New_York + + [NEW FEATURES] + + - Adds additional shorthand methods for all common HTTP verbs + (HEAD, PUT, POST, DELETE) [David Golden] + + - post_form() method for POST-ing x-www-form-urlencoded data + [David Golden] + + - www_form_urlencode() utility method [David Golden] + +0.013 2011-07-17 23:14:22 America/New_York + + [NEW FEATURES] + + - $ENV{http_proxy} support added [Claes Jakobsson] + + [OTHER] + + - Internal/private errors converted from "croak" to "die" as internal + errors are caught by "eval" + +0.012 2011-03-31 15:48:02 America/New_York + + [BUG FIXES] + + - mirror() now uses binmode during output (RT #67118) [Serguei Trouchelle] + + [DOCUMENTATION] + + - noted that SSL certificates are not verified against CA's + (RT #66907) + +0.011 2011-03-19 20:48:39 America/New_York + + [BUG FIXES] + + - Made t/000_load.t less verbose under harness (RT#65507) [Dave Mitchell] + + - Removed 'Errno' as an explicit prefix (it is a core module, but not + indexed by PAUSE, which might confuse some installers) + +0.010 2011-02-04 02:45:31 EST5EDT + + [BUG FIXES] + + - Fixed test errors on VMS (RT#65430) [Craig Berry] + +0.009 2011-01-17 16:29:22 EST5EDT + + - Added workaround for IO::Socket::SSL certificate verification bug + + - Minor documentation improvements + + - POST example added to the eg/ directory in the distribution tarball + +0.008 2011-01-14 06:34:55 EST5EDT + + - Added support for direct 'https' connections if IO::Socket::SSL + is installed + + - Added support for a callback to provide trailing headers for + chunked transfer encoding + + - Data callbacks receive the response hashref as a second argument + for greater flexibility + + - Additional limitations documented + +0.007 2011-01-12 04:56:16 EST5EDT + + - Added support for redirecting 303 and 307 response codes + + - Retry (once) a request that fails due to a closed socket + (per RFC2616 8.1.4) + + - Automatically sets request Content-Type to 'application/octet-stream' + if there is content the user has not defined its type + + - Trailing headers from chunked transfer encoding are now merged + into the response headers instead of ignored + + - Improved handling of malformed or unsupported HTTP protocols + + - Expanded http:///.../ as http://localhost/.../ and set Host header + + - Documented that URL's must be escaped/encoded + + - Documented that the headers hash option may contain an array reference + to output multiple values of the same header field + + - Improved documentation of limitations + + - Added numerous new tests to ensure compliance with the HTTP/1.1 spec + +0.006 2011-01-10 07:28:11 EST5EDT + + - Transfer-Encodings are case insensitive + + - Add additional test for proper behavior when both Content-Length + and Transfer-Encoding headers are received + +0.005 2011-01-08 06:32:05 EST5EDT + + - Fixed bug getting content for servers which do not sent Content-Length + + - Add test coverage for get(), mirror() and request() + + - Add test coverage for requests with static and generated content + +0.004 2010-12-15 22:53:59 EST5EDT + + - Renamed 'ok' response field to 'success' + + - Handle all required HTTP/1.1 date formats + + - Documented how callbacks are supposed to work + +0.003 2010-12-15 12:30:42 EST5EDT + + - Added 'ok' response field to simplify checking success + + - Added a 'mirror' method mirror content to a file, but shortcut + if not modified + +0.002 2010-12-13 21:59:39 EST5EDT + + - Added some initial documentation + + - Skips utf8::* code on Perls older than 5.8 + +0.001 2010-12-11 07:59:16 EST5EDT + + - Initial CPAN release + +# vim: ts=2 sts=2 sw=2 et: diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..c5916b5 --- /dev/null +++ b/LICENSE @@ -0,0 +1,379 @@ +This software is copyright (c) 2018 by Christian Hansen. + +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) 2018 by Christian Hansen. + +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, Fifth Floor, Boston, MA 02110-1301 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) 2018 by Christian Hansen. + +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 + diff --git a/MANIFEST b/MANIFEST new file mode 100644 index 0000000..4caab96 --- /dev/null +++ b/MANIFEST @@ -0,0 +1,127 @@ +# This file was automatically generated by Dist::Zilla::Plugin::Manifest v6.012. +CONTRIBUTING.mkdn +Changes +LICENSE +MANIFEST +META.json +META.yml +Makefile.PL +README +corpus/auth-01.txt +corpus/auth-02.txt +corpus/auth-03.txt +corpus/auth-04.txt +corpus/auth-05.txt +corpus/cookies-01.txt +corpus/cookies-02.txt +corpus/cookies-03.txt +corpus/cookies-04.txt +corpus/cookies-05.txt +corpus/cookies-06.txt +corpus/cookies-07.txt +corpus/delete-01.txt +corpus/form-01.txt +corpus/form-02.txt +corpus/form-03.txt +corpus/form-04.txt +corpus/form-05.txt +corpus/get-01.txt +corpus/get-02.txt +corpus/get-03.txt +corpus/get-04.txt +corpus/get-05.txt +corpus/get-06.txt +corpus/get-07.txt +corpus/get-08.txt +corpus/get-09.txt +corpus/get-10.txt +corpus/get-11.txt +corpus/get-12.txt +corpus/get-13.txt +corpus/get-14.txt +corpus/get-15.txt +corpus/get-16.txt +corpus/get-17.txt +corpus/get-18.txt +corpus/get-19.txt +corpus/get-20.txt +corpus/get-21.txt +corpus/get-22.txt +corpus/head-01.txt +corpus/keepalive-01.txt +corpus/keepalive-02.txt +corpus/keepalive-03.txt +corpus/keepalive-04.txt +corpus/keepalive-05.txt +corpus/mirror-01.txt +corpus/mirror-02.txt +corpus/mirror-03.txt +corpus/mirror-04.txt +corpus/mirror-05.txt +corpus/post-01.txt +corpus/proxy-auth-01.txt +corpus/put-01.txt +corpus/put-02.txt +corpus/put-03.txt +corpus/put-04.txt +corpus/put-05.txt +corpus/redirect-01.txt +corpus/redirect-02.txt +corpus/redirect-03.txt +corpus/redirect-04.txt +corpus/redirect-05.txt +corpus/redirect-06.txt +corpus/redirect-07.txt +corpus/redirect-08.txt +corpus/redirect-09.txt +corpus/redirect-10.txt +cpanfile +dist.ini +eg/get.pl +eg/mirror.pl +eg/post.pl +lib/HTTP/Tiny.pm +perlcritic.rc +t/00-report-prereqs.dd +t/00-report-prereqs.t +t/000_load.t +t/001_api.t +t/002_croakage.t +t/003_agent.t +t/004_timeout.t +t/010_url.t +t/020_headers.t +t/030_response.t +t/040_content.t +t/050_chunked_body.t +t/060_http_date.t +t/070_cookie_jar.t +t/100_get.t +t/101_head.t +t/102_put.t +t/103_delete.t +t/104_post.t +t/110_mirror.t +t/130_redirect.t +t/140_proxy.t +t/141_no_proxy.t +t/150_post_form.t +t/160_cookies.t +t/161_basic_auth.t +t/162_proxy_auth.t +t/170_keepalive.t +t/200_live.t +t/200_live_local_ip.t +t/210_live_ssl.t +t/BrokenCookieJar.pm +t/SimpleCookieJar.pm +t/Util.pm +xt/author/00-compile.t +xt/author/critic.t +xt/author/minimum-version.t +xt/author/pod-coverage.t +xt/author/pod-spell.t +xt/author/pod-syntax.t +xt/author/portability.t +xt/author/test-version.t +xt/release/distmeta.t diff --git a/META.json b/META.json new file mode 100644 index 0000000..49802b9 --- /dev/null +++ b/META.json @@ -0,0 +1,172 @@ +{ + "abstract" : "A small, simple, correct HTTP/1.1 client", + "author" : [ + "Christian Hansen ", + "David Golden " + ], + "dynamic_config" : 0, + "generated_by" : "Dist::Zilla version 6.012, CPAN::Meta::Converter version 2.150010", + "license" : [ + "perl_5" + ], + "meta-spec" : { + "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec", + "version" : 2 + }, + "name" : "HTTP-Tiny", + "no_index" : { + "directory" : [ + "corpus", + "examples", + "t", + "xt" + ], + "package" : [ + "DB" + ] + }, + "prereqs" : { + "configure" : { + "requires" : { + "ExtUtils::MakeMaker" : "6.17", + "perl" : "5.006" + }, + "suggests" : { + "JSON::PP" : "2.27300" + } + }, + "develop" : { + "requires" : { + "Dist::Zilla" : "5", + "Dist::Zilla::Plugin::Prereqs" : "0", + "Dist::Zilla::Plugin::ReleaseStatus::FromVersion" : "0", + "Dist::Zilla::Plugin::RemovePrereqs" : "0", + "Dist::Zilla::PluginBundle::DAGOLDEN" : "0.072", + "File::Spec" : "0", + "File::Temp" : "0", + "IO::Handle" : "0", + "IPC::Open3" : "0", + "Perl::Critic::Policy::Lax::ProhibitStringyEval::ExceptForRequire" : "0", + "Pod::Coverage::TrustPod" : "0", + "Pod::Wordlist" : "0", + "Software::License::Perl_5" : "0", + "Test::CPAN::Meta" : "0", + "Test::MinimumVersion" : "0", + "Test::More" : "0", + "Test::Perl::Critic" : "0", + "Test::Pod" : "1.41", + "Test::Pod::Coverage" : "1.08", + "Test::Portability::Files" : "0", + "Test::Spelling" : "0.12", + "Test::Version" : "1", + "perl" : "5.006" + } + }, + "runtime" : { + "recommends" : { + "HTTP::CookieJar" : "0.001", + "IO::Socket::IP" : "0.32", + "IO::Socket::SSL" : "1.42", + "Mozilla::CA" : "20160104", + "Net::SSLeay" : "1.49" + }, + "requires" : { + "Carp" : "0", + "Fcntl" : "0", + "IO::Socket" : "0", + "MIME::Base64" : "0", + "Socket" : "0", + "Time::Local" : "0", + "bytes" : "0", + "perl" : "5.006", + "strict" : "0", + "warnings" : "0" + }, + "suggests" : { + "IO::Socket::SSL" : "1.56" + } + }, + "test" : { + "recommends" : { + "CPAN::Meta" : "2.120900" + }, + "requires" : { + "Data::Dumper" : "0", + "Exporter" : "0", + "ExtUtils::MakeMaker" : "0", + "File::Basename" : "0", + "File::Spec" : "0", + "File::Temp" : "0", + "IO::Dir" : "0", + "IO::File" : "0", + "IO::Socket::INET" : "0", + "IPC::Cmd" : "0", + "Test::More" : "0.96", + "lib" : "0", + "open" : "0", + "perl" : "5.006" + } + } + }, + "provides" : { + "HTTP::Tiny" : { + "file" : "lib/HTTP/Tiny.pm", + "version" : "0.074" + } + }, + "release_status" : "stable", + "resources" : { + "bugtracker" : { + "web" : "https://github.com/chansen/p5-http-tiny/issues" + }, + "homepage" : "https://github.com/chansen/p5-http-tiny", + "repository" : { + "type" : "git", + "url" : "https://github.com/chansen/p5-http-tiny.git", + "web" : "https://github.com/chansen/p5-http-tiny" + } + }, + "version" : "0.074", + "x_authority" : "cpan:DAGOLDEN", + "x_contributors" : [ + "Alan Gardner ", + "Alessandro Ghedini ", + "A. Sinan Unur ", + "Brad Gilbert ", + "brian m. carlson ", + "Chris Nehren ", + "Chris Weyl ", + "Claes Jakobsson ", + "Clinton Gormley ", + "Craig A. Berry ", + "Craig Berry ", + "David Golden ", + "David Mitchell ", + "Dean Pearce ", + "Edward Zborowski ", + "James Raspass ", + "Jeremy Mates ", + "Jess Robinson ", + "Karen Etheridge ", + "Lukas Eklund ", + "Martin J. Evans ", + "Martin-Louis Bright ", + "Mike Doherty ", + "Nicolas Rochelemagne ", + "Olaf Alders ", + "Olivier Mengu\u00e9 ", + "Petr P\u00edsa\u0159 ", + "Serguei Trouchelle ", + "Shoichi Kaji ", + "SkyMarshal ", + "S\u00f6ren Kornetzki ", + "Steve Grazzini ", + "Syohei YOSHIDA ", + "Tatsuhiko Miyagawa ", + "Tom Hukins ", + "Tony Cook " + ], + "x_generated_by_perl" : "v5.28.0", + "x_serialization_backend" : "Cpanel::JSON::XS version 4.04" +} + diff --git a/META.yml b/META.yml new file mode 100644 index 0000000..1725a7c --- /dev/null +++ b/META.yml @@ -0,0 +1,104 @@ +--- +abstract: 'A small, simple, correct HTTP/1.1 client' +author: + - 'Christian Hansen ' + - 'David Golden ' +build_requires: + Data::Dumper: '0' + Exporter: '0' + ExtUtils::MakeMaker: '0' + File::Basename: '0' + File::Spec: '0' + File::Temp: '0' + IO::Dir: '0' + IO::File: '0' + IO::Socket::INET: '0' + IPC::Cmd: '0' + Test::More: '0.96' + lib: '0' + open: '0' + perl: '5.006' +configure_requires: + ExtUtils::MakeMaker: '6.17' + perl: '5.006' +dynamic_config: 0 +generated_by: 'Dist::Zilla version 6.012, CPAN::Meta::Converter version 2.150010' +license: perl +meta-spec: + url: http://module-build.sourceforge.net/META-spec-v1.4.html + version: '1.4' +name: HTTP-Tiny +no_index: + directory: + - corpus + - examples + - t + - xt + package: + - DB +provides: + HTTP::Tiny: + file: lib/HTTP/Tiny.pm + version: '0.074' +recommends: + HTTP::CookieJar: '0.001' + IO::Socket::IP: '0.32' + IO::Socket::SSL: '1.42' + Mozilla::CA: '20160104' + Net::SSLeay: '1.49' +requires: + Carp: '0' + Fcntl: '0' + IO::Socket: '0' + MIME::Base64: '0' + Socket: '0' + Time::Local: '0' + bytes: '0' + perl: '5.006' + strict: '0' + warnings: '0' +resources: + bugtracker: https://github.com/chansen/p5-http-tiny/issues + homepage: https://github.com/chansen/p5-http-tiny + repository: https://github.com/chansen/p5-http-tiny.git +version: '0.074' +x_authority: cpan:DAGOLDEN +x_contributors: + - 'Alan Gardner ' + - 'Alessandro Ghedini ' + - 'A. Sinan Unur ' + - 'Brad Gilbert ' + - 'brian m. carlson ' + - 'Chris Nehren ' + - 'Chris Weyl ' + - 'Claes Jakobsson ' + - 'Clinton Gormley ' + - 'Craig A. Berry ' + - 'Craig Berry ' + - 'David Golden ' + - 'David Mitchell ' + - 'Dean Pearce ' + - 'Edward Zborowski ' + - 'James Raspass ' + - 'Jeremy Mates ' + - 'Jess Robinson ' + - 'Karen Etheridge ' + - 'Lukas Eklund ' + - 'Martin J. Evans ' + - 'Martin-Louis Bright ' + - 'Mike Doherty ' + - 'Nicolas Rochelemagne ' + - 'Olaf Alders ' + - 'Olivier Mengué ' + - 'Petr Písař ' + - 'Serguei Trouchelle ' + - 'Shoichi Kaji ' + - 'SkyMarshal ' + - 'Sören Kornetzki ' + - 'Steve Grazzini ' + - 'Syohei YOSHIDA ' + - 'Tatsuhiko Miyagawa ' + - 'Tom Hukins ' + - 'Tony Cook ' +x_generated_by_perl: v5.28.0 +x_serialization_backend: 'YAML::Tiny version 1.73' diff --git a/Makefile.PL b/Makefile.PL new file mode 100644 index 0000000..b9a3e64 --- /dev/null +++ b/Makefile.PL @@ -0,0 +1,87 @@ +# This file was automatically generated by Dist::Zilla::Plugin::MakeMaker v6.012. +use strict; +use warnings; + +use 5.006; + +use ExtUtils::MakeMaker 6.17; + +my %WriteMakefileArgs = ( + "ABSTRACT" => "A small, simple, correct HTTP/1.1 client", + "AUTHOR" => "Christian Hansen , David Golden ", + "CONFIGURE_REQUIRES" => { + "ExtUtils::MakeMaker" => "6.17" + }, + "DISTNAME" => "HTTP-Tiny", + "LICENSE" => "perl", + "MIN_PERL_VERSION" => "5.006", + "NAME" => "HTTP::Tiny", + "PREREQ_PM" => { + "Carp" => 0, + "Fcntl" => 0, + "IO::Socket" => 0, + "MIME::Base64" => 0, + "Socket" => 0, + "Time::Local" => 0, + "bytes" => 0, + "strict" => 0, + "warnings" => 0 + }, + "TEST_REQUIRES" => { + "Data::Dumper" => 0, + "Exporter" => 0, + "ExtUtils::MakeMaker" => 0, + "File::Basename" => 0, + "File::Spec" => 0, + "File::Temp" => 0, + "IO::Dir" => 0, + "IO::File" => 0, + "IO::Socket::INET" => 0, + "IPC::Cmd" => 0, + "Test::More" => "0.96", + "lib" => 0, + "open" => 0 + }, + "VERSION" => "0.074", + "test" => { + "TESTS" => "t/*.t" + } +); + + +my %FallbackPrereqs = ( + "Carp" => 0, + "Data::Dumper" => 0, + "Exporter" => 0, + "ExtUtils::MakeMaker" => 0, + "Fcntl" => 0, + "File::Basename" => 0, + "File::Spec" => 0, + "File::Temp" => 0, + "IO::Dir" => 0, + "IO::File" => 0, + "IO::Socket" => 0, + "IO::Socket::INET" => 0, + "IPC::Cmd" => 0, + "MIME::Base64" => 0, + "Socket" => 0, + "Test::More" => "0.96", + "Time::Local" => 0, + "bytes" => 0, + "lib" => 0, + "open" => 0, + "strict" => 0, + "warnings" => 0 +); + + +unless ( eval { ExtUtils::MakeMaker->VERSION(6.63_03) } ) { + delete $WriteMakefileArgs{TEST_REQUIRES}; + delete $WriteMakefileArgs{BUILD_REQUIRES}; + $WriteMakefileArgs{PREREQ_PM} = \%FallbackPrereqs; +} + +delete $WriteMakefileArgs{CONFIGURE_REQUIRES} + unless eval { ExtUtils::MakeMaker->VERSION(6.52) }; + +WriteMakefile(%WriteMakefileArgs); diff --git a/README b/README new file mode 100644 index 0000000..fcba6e1 --- /dev/null +++ b/README @@ -0,0 +1,582 @@ +NAME + HTTP::Tiny - A small, simple, correct HTTP/1.1 client + +VERSION + version 0.074 + +SYNOPSIS + use HTTP::Tiny; + + my $response = HTTP::Tiny->new->get('http://example.com/'); + + die "Failed!\n" unless $response->{success}; + + print "$response->{status} $response->{reason}\n"; + + while (my ($k, $v) = each %{$response->{headers}}) { + for (ref $v eq 'ARRAY' ? @$v : $v) { + print "$k: $_\n"; + } + } + + print $response->{content} if length $response->{content}; + +DESCRIPTION + This is a very simple HTTP/1.1 client, designed for doing simple + requests without the overhead of a large framework like LWP::UserAgent. + + It is more correct and more complete than HTTP::Lite. It supports + proxies and redirection. It also correctly resumes after EINTR. + + If IO::Socket::IP 0.25 or later is installed, HTTP::Tiny will use it + instead of IO::Socket::INET for transparent support for both IPv4 and + IPv6. + + Cookie support requires HTTP::CookieJar or an equivalent class. + +METHODS + new + $http = HTTP::Tiny->new( %attributes ); + + This constructor returns a new HTTP::Tiny object. Valid attributes + include: + + * "agent" — A user-agent string (defaults to 'HTTP-Tiny/$VERSION'). If + "agent" — ends in a space character, the default user-agent string + is appended. + + * "cookie_jar" — An instance of HTTP::CookieJar — or equivalent class + that supports the "add" and "cookie_header" methods + + * "default_headers" — A hashref of default headers to apply to + requests + + * "local_address" — The local IP address to bind to + + * "keep_alive" — Whether to reuse the last connection (if for the same + scheme, host and port) (defaults to 1) + + * "max_redirect" — Maximum number of redirects allowed (defaults to 5) + + * "max_size" — Maximum response size in bytes (only when not using a + data callback). If defined, responses larger than this will return + an exception. + + * "http_proxy" — URL of a proxy server to use for HTTP connections + (default is $ENV{http_proxy} — if set) + + * "https_proxy" — URL of a proxy server to use for HTTPS connections + (default is $ENV{https_proxy} — if set) + + * "proxy" — URL of a generic proxy server for both HTTP and HTTPS + connections (default is $ENV{all_proxy} — if set) + + * "no_proxy" — List of domain suffixes that should not be proxied. + Must be a comma-separated string or an array reference. (default is + $ENV{no_proxy} —) + + * "timeout" — Request timeout in seconds (default is 60) If a socket + open, read or write takes longer than the timeout, an exception is + thrown. + + * "verify_SSL" — A boolean that indicates whether to validate the SSL + certificate of an "https" — connection (default is false) + + * "SSL_options" — A hashref of "SSL_*" — options to pass through to + IO::Socket::SSL + + Passing an explicit "undef" for "proxy", "http_proxy" or "https_proxy" + will prevent getting the corresponding proxies from the environment. + + Exceptions from "max_size", "timeout" or other errors will result in a + pseudo-HTTP status code of 599 and a reason of "Internal Exception". The + content field in the response will contain the text of the exception. + + The "keep_alive" parameter enables a persistent connection, but only to + a single destination scheme, host and port. Also, if any + connection-relevant attributes are modified, or if the process ID or + thread ID change, the persistent connection will be dropped. If you want + persistent connections across multiple destinations, use multiple + HTTP::Tiny objects. + + See "SSL SUPPORT" for more on the "verify_SSL" and "SSL_options" + attributes. + + get|head|put|post|delete + $response = $http->get($url); + $response = $http->get($url, \%options); + $response = $http->head($url); + + These methods are shorthand for calling "request()" for the given + method. The URL must have unsafe characters escaped and international + domain names encoded. See "request()" for valid options and a + description of the response. + + The "success" field of the response will be true if the status code is + 2XX. + + post_form + $response = $http->post_form($url, $form_data); + $response = $http->post_form($url, $form_data, \%options); + + This method executes a "POST" request and sends the key/value pairs from + a form data hash or array reference to the given URL with a + "content-type" of "application/x-www-form-urlencoded". If data is + provided as an array reference, the order is preserved; if provided as a + hash reference, the terms are sorted on key and value for consistency. + See documentation for the "www_form_urlencode" method for details on the + encoding. + + The URL must have unsafe characters escaped and international domain + names encoded. See "request()" for valid options and a description of + the response. Any "content-type" header or content in the options + hashref will be ignored. + + The "success" field of the response will be true if the status code is + 2XX. + + mirror + $response = $http->mirror($url, $file, \%options) + if ( $response->{success} ) { + print "$file is up to date\n"; + } + + Executes a "GET" request for the URL and saves the response body to the + file name provided. The URL must have unsafe characters escaped and + international domain names encoded. If the file already exists, the + request will include an "If-Modified-Since" header with the modification + timestamp of the file. You may specify a different "If-Modified-Since" + header yourself in the "$options->{headers}" hash. + + The "success" field of the response will be true if the status code is + 2XX or if the status code is 304 (unmodified). + + If the file was modified and the server response includes a properly + formatted "Last-Modified" header, the file modification time will be + updated accordingly. + + request + $response = $http->request($method, $url); + $response = $http->request($method, $url, \%options); + + Executes an HTTP request of the given method type ('GET', 'HEAD', + 'POST', 'PUT', etc.) on the given URL. The URL must have unsafe + characters escaped and international domain names encoded. + + NOTE: Method names are case-sensitive per the HTTP/1.1 specification. + Don't use "get" when you really want "GET". See LIMITATIONS for how this + applies to redirection. + + If the URL includes a "user:password" stanza, they will be used for + Basic-style authorization headers. (Authorization headers will not be + included in a redirected request.) For example: + + $http->request('GET', 'http://Aladdin:open sesame@example.com/'); + + If the "user:password" stanza contains reserved characters, they must be + percent-escaped: + + $http->request('GET', 'http://john%40example.com:password@example.com/'); + + A hashref of options may be appended to modify the request. + + Valid options are: + + * "headers" — A hashref containing headers to include with the + request. If the value for a header is an array reference, the header + will be output multiple times with each value in the array. These + headers over-write any default headers. + + * "content" — A scalar to include as the body of the request OR a code + reference that will be called iteratively to produce the body of the + request + + * "trailer_callback" — A code reference that will be called if it + exists to provide a hashref of trailing headers (only used with + chunked transfer-encoding) + + * "data_callback" — A code reference that will be called for each + chunks of the response body received. + + * "peer" — Override host resolution and force all connections to go + only to a specific peer address, regardless of the URL of the + request. This will include any redirections! This options should be + used with extreme caution (e.g. debugging or very special + circumstances). + + The "Host" header is generated from the URL in accordance with RFC 2616. + It is a fatal error to specify "Host" in the "headers" option. Other + headers may be ignored or overwritten if necessary for transport + compliance. + + If the "content" option is a code reference, it will be called + iteratively to provide the content body of the request. It should return + the empty string or undef when the iterator is exhausted. + + If the "content" option is the empty string, no "content-type" or + "content-length" headers will be generated. + + If the "data_callback" option is provided, it will be called iteratively + until the entire response body is received. The first argument will be a + string containing a chunk of the response body, the second argument will + be the in-progress response hash reference, as described below. (This + allows customizing the action of the callback based on the "status" or + "headers" received prior to the content body.) + + The "request" method returns a hashref containing the response. The + hashref will have the following keys: + + * "success" — Boolean indicating whether the operation returned a 2XX + status code + + * "url" — URL that provided the response. This is the URL of the + request unless there were redirections, in which case it is the last + URL queried in a redirection chain + + * "status" — The HTTP status code of the response + + * "reason" — The response phrase returned by the server + + * "content" — The body of the response. If the response does not have + any content or if a data callback is provided to consume the + response body, this will be the empty string + + * "headers" — A hashref of header fields. All header field names will + be normalized to be lower case. If a header is repeated, the value + will be an arrayref; it will otherwise be a scalar string containing + the value + + * "protocol" - If this field exists, it is the protocol of the + response such as HTTP/1.0 or HTTP/1.1 + + * "redirects" If this field exists, it is an arrayref of response hash + references from redirects in the same order that redirections + occurred. If it does not exist, then no redirections occurred. + + On an exception during the execution of the request, the "status" field + will contain 599, and the "content" field will contain the text of the + exception. + + www_form_urlencode + $params = $http->www_form_urlencode( $data ); + $response = $http->get("http://example.com/query?$params"); + + This method converts the key/value pairs from a data hash or array + reference into a "x-www-form-urlencoded" string. The keys and values + from the data reference will be UTF-8 encoded and escaped per RFC 3986. + If a value is an array reference, the key will be repeated with each of + the values of the array reference. If data is provided as a hash + reference, the key/value pairs in the resulting string will be sorted by + key and value for consistent ordering. + + can_ssl + $ok = HTTP::Tiny->can_ssl; + ($ok, $why) = HTTP::Tiny->can_ssl; + ($ok, $why) = $http->can_ssl; + + Indicates if SSL support is available. When called as a class object, it + checks for the correct version of Net::SSLeay and IO::Socket::SSL. When + called as an object methods, if "SSL_verify" is true or if + "SSL_verify_mode" is set in "SSL_options", it checks that a CA file is + available. + + In scalar context, returns a boolean indicating if SSL is available. In + list context, returns the boolean and a (possibly multi-line) string of + errors indicating why SSL isn't available. + + connected + $host = $http->connected; + ($host, $port) = $http->connected; + + Indicates if a connection to a peer is being kept alive, per the + "keep_alive" option. + + In scalar context, returns the peer host and port, joined with a colon, + or "undef" (if no peer is connected). In list context, returns the peer + host and port or an empty list (if no peer is connected). + + Note: This method cannot reliably be used to discover whether the remote + host has closed its end of the socket. + +SSL SUPPORT + Direct "https" connections are supported only if IO::Socket::SSL 1.56 or + greater and Net::SSLeay 1.49 or greater are installed. An exception will + be thrown if new enough versions of these modules are not installed or + if the SSL encryption fails. You can also use "HTTP::Tiny::can_ssl()" + utility function that returns boolean to see if the required modules are + installed. + + An "https" connection may be made via an "http" proxy that supports the + CONNECT command (i.e. RFC 2817). You may not proxy "https" via a proxy + that itself requires "https" to communicate. + + SSL provides two distinct capabilities: + + * Encrypted communication channel + + * Verification of server identity + + By default, HTTP::Tiny does not verify server identity. + + Server identity verification is controversial and potentially tricky + because it depends on a (usually paid) third-party Certificate Authority + (CA) trust model to validate a certificate as legitimate. This + discriminates against servers with self-signed certificates or + certificates signed by free, community-driven CA's such as CAcert.org + . + + By default, HTTP::Tiny does not make any assumptions about your trust + model, threat level or risk tolerance. It just aims to give you an + encrypted channel when you need one. + + Setting the "verify_SSL" attribute to a true value will make HTTP::Tiny + verify that an SSL connection has a valid SSL certificate corresponding + to the host name of the connection and that the SSL certificate has been + verified by a CA. Assuming you trust the CA, this will protect against a + man-in-the-middle attack + . If you are + concerned about security, you should enable this option. + + Certificate verification requires a file containing trusted CA + certificates. + + If the environment variable "SSL_CERT_FILE" is present, HTTP::Tiny will + try to find a CA certificate file in that location. + + If the Mozilla::CA module is installed, HTTP::Tiny will use the CA file + included with it as a source of trusted CA's. (This means you trust + Mozilla, the author of Mozilla::CA, the CPAN mirror where you got + Mozilla::CA, the toolchain used to install it, and your operating system + security, right?) + + If that module is not available, then HTTP::Tiny will search several + system-specific default locations for a CA certificate file: + + * /etc/ssl/certs/ca-certificates.crt + + * /etc/pki/tls/certs/ca-bundle.crt + + * /etc/ssl/ca-bundle.pem + + An exception will be raised if "verify_SSL" is true and no CA + certificate file is available. + + If you desire complete control over SSL connections, the "SSL_options" + attribute lets you provide a hash reference that will be passed through + to "IO::Socket::SSL::start_SSL()", overriding any options set by + HTTP::Tiny. For example, to provide your own trusted CA file: + + SSL_options => { + SSL_ca_file => $file_path, + } + + The "SSL_options" attribute could also be used for such things as + providing a client certificate for authentication to a server or + controlling the choice of cipher used for the SSL connection. See + IO::Socket::SSL documentation for details. + +PROXY SUPPORT + HTTP::Tiny can proxy both "http" and "https" requests. Only Basic proxy + authorization is supported and it must be provided as part of the proxy + URL: "http://user:pass@proxy.example.com/". + + HTTP::Tiny supports the following proxy environment variables: + + * http_proxy or HTTP_PROXY + + * https_proxy or HTTPS_PROXY + + * all_proxy or ALL_PROXY + + If the "REQUEST_METHOD" environment variable is set, then this might be + a CGI process and "HTTP_PROXY" would be set from the "Proxy:" header, + which is a security risk. If "REQUEST_METHOD" is set, "HTTP_PROXY" (the + upper case variant only) is ignored. + + Tunnelling "https" over an "http" proxy using the CONNECT method is + supported. If your proxy uses "https" itself, you can not tunnel "https" + over it. + + Be warned that proxying an "https" connection opens you to the risk of a + man-in-the-middle attack by the proxy server. + + The "no_proxy" environment variable is supported in the format of a + comma-separated list of domain extensions proxy should not be used for. + + Proxy arguments passed to "new" will override their corresponding + environment variables. + +LIMITATIONS + HTTP::Tiny is *conditionally compliant* with the HTTP/1.1 specifications + : + + * "Message Syntax and Routing" [RFC7230] + + * "Semantics and Content" [RFC7231] + + * "Conditional Requests" [RFC7232] + + * "Range Requests" [RFC7233] + + * "Caching" [RFC7234] + + * "Authentication" [RFC7235] + + It attempts to meet all "MUST" requirements of the specification, but + does not implement all "SHOULD" requirements. (Note: it was developed + against the earlier RFC 2616 specification and may not yet meet the + revised RFC 7230-7235 spec.) + + Some particular limitations of note include: + + * HTTP::Tiny focuses on correct transport. Users are responsible for + ensuring that user-defined headers and content are compliant with + the HTTP/1.1 specification. + + * Users must ensure that URLs are properly escaped for unsafe + characters and that international domain names are properly encoded + to ASCII. See URI::Escape, URI::_punycode and Net::IDN::Encode. + + * Redirection is very strict against the specification. Redirection is + only automatic for response codes 301, 302, 307 and 308 if the + request method is 'GET' or 'HEAD'. Response code 303 is always + converted into a 'GET' redirection, as mandated by the + specification. There is no automatic support for status 305 ("Use + proxy") redirections. + + * There is no provision for delaying a request body using an "Expect" + header. Unexpected "1XX" responses are silently ignored as per the + specification. + + * Only 'chunked' "Transfer-Encoding" is supported. + + * There is no support for a Request-URI of '*' for the 'OPTIONS' + request. + + * Headers mentioned in the RFCs and some other, well-known headers are + generated with their canonical case. Other headers are sent in the + case provided by the user. Except for control headers (which are + sent first), headers are sent in arbitrary order. + + Despite the limitations listed above, HTTP::Tiny is considered + feature-complete. New feature requests should be directed to + HTTP::Tiny::UA. + +SEE ALSO + * HTTP::Tiny::UA - Higher level UA features for HTTP::Tiny + + * HTTP::Thin - HTTP::Tiny wrapper with HTTP::Request/HTTP::Response + compatibility + + * HTTP::Tiny::Mech - Wrap WWW::Mechanize instance in HTTP::Tiny + compatible interface + + * IO::Socket::IP - Required for IPv6 support + + * IO::Socket::SSL - Required for SSL support + + * LWP::UserAgent - If HTTP::Tiny isn't enough for you, this is the + "standard" way to do things + + * Mozilla::CA - Required if you want to validate SSL certificates + + * Net::SSLeay - Required for SSL support + +SUPPORT + Bugs / Feature Requests + Please report any bugs or feature requests through the issue tracker at + . You will be notified + automatically of any progress on your issue. + + Source Code + This is open source software. The code repository is available for + public review and contribution under the terms of the license. + + + + git clone https://github.com/chansen/p5-http-tiny.git + +AUTHORS + * Christian Hansen + + * David Golden + +CONTRIBUTORS + * Alan Gardner + + * Alessandro Ghedini + + * A. Sinan Unur + + * Brad Gilbert + + * brian m. carlson + + * Chris Nehren + + * Chris Weyl + + * Claes Jakobsson + + * Clinton Gormley + + * Craig A. Berry + + * Craig Berry + + * David Golden + + * David Mitchell + + * Dean Pearce + + * Edward Zborowski + + * James Raspass + + * Jeremy Mates + + * Jess Robinson + + * Karen Etheridge + + * Lukas Eklund + + * Martin J. Evans + + * Martin-Louis Bright + + * Mike Doherty + + * Nicolas Rochelemagne + + * Olaf Alders + + * Olivier Mengué + + * Petr Písař + + * Serguei Trouchelle + + * Shoichi Kaji + + * SkyMarshal + + * Sören Kornetzki + + * Steve Grazzini + + * Syohei YOSHIDA + + * Tatsuhiko Miyagawa + + * Tom Hukins + + * Tony Cook + +COPYRIGHT AND LICENSE + This software is copyright (c) 2018 by Christian Hansen. + + This is free software; you can redistribute it and/or modify it under + the same terms as the Perl 5 programming language system itself. + diff --git a/corpus/auth-01.txt b/corpus/auth-01.txt new file mode 100644 index 0000000..e4a97c8 --- /dev/null +++ b/corpus/auth-01.txt @@ -0,0 +1,18 @@ +url + http://foo:bar@example.com/index.html +expected + abcdefghijklmnopqrstuvwxyz1234567890abcdef +---------- +GET /index.html HTTP/1.1 +Host: example.com +Connection: close +User-Agent: HTTP-Tiny/VERSION +Authorization: Basic Zm9vOmJhcg== + +---------- +HTTP/1.1 200 OK +Date: Thu, 03 Feb 1994 00:00:00 GMT +Content-Type: text/plain +Content-Length: 42 + +abcdefghijklmnopqrstuvwxyz1234567890abcdef diff --git a/corpus/auth-02.txt b/corpus/auth-02.txt new file mode 100644 index 0000000..9b9a0fa --- /dev/null +++ b/corpus/auth-02.txt @@ -0,0 +1,18 @@ +url + http://Aladdin:open sesame@example.com/index.html +expected + abcdefghijklmnopqrstuvwxyz1234567890abcdef +---------- +GET /index.html HTTP/1.1 +Host: example.com +Connection: close +User-Agent: HTTP-Tiny/VERSION +Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ== + +---------- +HTTP/1.1 200 OK +Date: Thu, 03 Feb 1994 00:00:00 GMT +Content-Type: text/plain +Content-Length: 42 + +abcdefghijklmnopqrstuvwxyz1234567890abcdef diff --git a/corpus/auth-03.txt b/corpus/auth-03.txt new file mode 100644 index 0000000..8852e9a --- /dev/null +++ b/corpus/auth-03.txt @@ -0,0 +1,36 @@ +url + http://foo:bar@example.com/index.html +expected + abcdefghijklmnopqrstuvwxyz1234567890abcdef +expected_url + http://example.com/index2.html +---------- +GET /index.html HTTP/1.1 +Host: example.com +Connection: close +User-Agent: HTTP-Tiny/VERSION +Authorization: Basic Zm9vOmJhcg== + +---------- +HTTP/1.1 302 Found +Date: Thu, 03 Feb 1994 00:00:00 GMT +Content-Type: text/html +Content-Length: 53 +Location: http://example.com/index2.html + +redirect + +---------- +GET /index2.html HTTP/1.1 +Host: example.com +Connection: close +User-Agent: HTTP-Tiny/VERSION + +---------- +HTTP/1.1 200 OK +Date: Thu, 03 Feb 1994 00:00:00 GMT +Content-Type: text/plain +Content-Length: 42 + +abcdefghijklmnopqrstuvwxyz1234567890abcdef + diff --git a/corpus/auth-04.txt b/corpus/auth-04.txt new file mode 100644 index 0000000..2c832a0 --- /dev/null +++ b/corpus/auth-04.txt @@ -0,0 +1,18 @@ +url + http://rjbs%40cpan.org:password@example.com/index.html +expected + abcdefghijklmnopqrstuvwxyz1234567890abcdef +---------- +GET /index.html HTTP/1.1 +Host: example.com +Connection: close +User-Agent: HTTP-Tiny/VERSION +Authorization: Basic cmpic0BjcGFuLm9yZzpwYXNzd29yZA== + +---------- +HTTP/1.1 200 OK +Date: Thu, 03 Feb 1994 00:00:00 GMT +Content-Type: text/plain +Content-Length: 42 + +abcdefghijklmnopqrstuvwxyz1234567890abcdef diff --git a/corpus/auth-05.txt b/corpus/auth-05.txt new file mode 100644 index 0000000..3a9970e --- /dev/null +++ b/corpus/auth-05.txt @@ -0,0 +1,20 @@ +url + http://foo:bar@example.com/index.html +headers + authorization: Whatever +expected + abcdefghijklmnopqrstuvwxyz1234567890abcdef +---------- +GET /index.html HTTP/1.1 +Host: example.com +Connection: close +User-Agent: HTTP-Tiny/VERSION +Authorization: Whatever + +---------- +HTTP/1.1 200 OK +Date: Thu, 03 Feb 1994 00:00:00 GMT +Content-Type: text/plain +Content-Length: 42 + +abcdefghijklmnopqrstuvwxyz1234567890abcdef diff --git a/corpus/cookies-01.txt b/corpus/cookies-01.txt new file mode 100644 index 0000000..e71bf01 --- /dev/null +++ b/corpus/cookies-01.txt @@ -0,0 +1,34 @@ +url + http://example.com/index.html +---------- +GET /index.html HTTP/1.1 +Host: example.com +Connection: close +User-Agent: HTTP-Tiny/VERSION + +---------- +HTTP/1.1 200 OK +Date: Sat, 02 Mar 2013 00:00:00 GMT +Set-Cookie: cname=cvalue; domain=example.com; path=/ +Content-Type: text/plain +Content-Length: 44 + +abcdefghijklmnopqrstuvwxyz1234567890abcdef +---------- +url + http://example.com/index.html +---------- +GET /index.html HTTP/1.1 +Host: example.com +Connection: close +User-Agent: HTTP-Tiny/VERSION +Cookie: cname=cvalue + +---------- +HTTP/1.1 200 OK +Date: Sat, 02 Mar 2013 00:00:00 GMT +Set-Cookie: cname=cvalue; domain=example.com; path=/ +Content-Type: text/plain +Content-Length: 44 + +abcdefghijklmnopqrstuvwxyz1234567890abcdef diff --git a/corpus/cookies-02.txt b/corpus/cookies-02.txt new file mode 100644 index 0000000..7e7db2c --- /dev/null +++ b/corpus/cookies-02.txt @@ -0,0 +1,52 @@ +url + http://example.com/index.html +---------- +GET /index.html HTTP/1.1 +Host: example.com +Connection: close +User-Agent: HTTP-Tiny/VERSION + +---------- +HTTP/1.1 200 OK +Date: Sat, 02 Mar 2013 00:00:00 GMT +Set-Cookie: cname=cvalue01; domain=example.com; path=/ +Content-Type: text/plain +Content-Length: 44 + +abcdefghijklmnopqrstuvwxyz1234567890abcdef +---------- +url + http://example.com/index.html +---------- +GET /index.html HTTP/1.1 +Host: example.com +Connection: close +User-Agent: HTTP-Tiny/VERSION +Cookie: cname=cvalue01 + +---------- +HTTP/1.1 200 OK +Date: Sat, 02 Mar 2013 00:00:00 GMT +Set-Cookie: cname=cvalue02; domain=example.com; path=/ +Content-Type: text/plain +Content-Length: 44 + +abcdefghijklmnopqrstuvwxyz1234567890abcdef +---------- +url + http://example.com/index.html +---------- +GET /index.html HTTP/1.1 +Host: example.com +Connection: close +User-Agent: HTTP-Tiny/VERSION +Cookie: cname=cvalue02 + +---------- +HTTP/1.1 200 OK +Date: Sat, 02 Mar 2013 00:00:00 GMT +Set-Cookie: cname=cvalue02; domain=example.com; path=/ +Content-Type: text/plain +Content-Length: 44 + +abcdefghijklmnopqrstuvwxyz1234567890abcdef diff --git a/corpus/cookies-03.txt b/corpus/cookies-03.txt new file mode 100644 index 0000000..c7eab4e --- /dev/null +++ b/corpus/cookies-03.txt @@ -0,0 +1,87 @@ +url + http://example.com/index.html +---------- +GET /index.html HTTP/1.1 +Host: example.com +Connection: close +User-Agent: HTTP-Tiny/VERSION + +---------- +HTTP/1.1 200 OK +Date: Sat, 02 Mar 2013 00:00:00 GMT +Set-Cookie: cname=cvalue01; domain=example.com; path=/ +Content-Type: text/plain +Content-Length: 44 + +abcdefghijklmnopqrstuvwxyz1234567890abcdef +---------- +url + http://example.com/index.html +---------- +GET /index.html HTTP/1.1 +Host: example.com +Connection: close +User-Agent: HTTP-Tiny/VERSION +Cookie: cname=cvalue01 + +---------- +HTTP/1.1 200 OK +Date: Sat, 02 Mar 2013 00:00:00 GMT +Set-Cookie: cname=cvalue02; domain=example.com; path=/ +Content-Type: text/plain +Content-Length: 44 + +abcdefghijklmnopqrstuvwxyz1234567890abcdef +---------- +url + http://example.com/index02.html +---------- +GET /index02.html HTTP/1.1 +Host: example.com +Connection: close +User-Agent: HTTP-Tiny/VERSION + +---------- +HTTP/1.1 200 OK +Date: Sat, 02 Mar 2013 00:00:00 GMT +Set-Cookie: cname=cvalue03; domain=example.com; path=/ +Content-Type: text/plain +Content-Length: 44 + +abcdefghijklmnopqrstuvwxyz1234567890abcdef +---------- +url + http://example.com/index.html +---------- +GET /index.html HTTP/1.1 +Host: example.com +Connection: close +User-Agent: HTTP-Tiny/VERSION +Cookie: cname=cvalue02 + +---------- +HTTP/1.1 200 OK +Date: Sat, 02 Mar 2013 00:00:00 GMT +Set-Cookie: cname=cvalue02; domain=example.com; path=/ +Content-Type: text/plain +Content-Length: 44 + +abcdefghijklmnopqrstuvwxyz1234567890abcdef +---------- +url + http://example.com/index02.html +---------- +GET /index02.html HTTP/1.1 +Host: example.com +Connection: close +User-Agent: HTTP-Tiny/VERSION +Cookie: cname=cvalue03 + +---------- +HTTP/1.1 200 OK +Date: Sat, 02 Mar 2013 00:00:00 GMT +Set-Cookie: cname=cvalue03; domain=example.com; path=/ +Content-Type: text/plain +Content-Length: 44 + +abcdefghijklmnopqrstuvwxyz1234567890abcdef diff --git a/corpus/cookies-04.txt b/corpus/cookies-04.txt new file mode 100644 index 0000000..7c9c1cb --- /dev/null +++ b/corpus/cookies-04.txt @@ -0,0 +1,37 @@ +url + http://example.com/index.html +headers + Cookie: cname=cvalue05; domain=example.com; path=/ +---------- +GET /index.html HTTP/1.1 +Host: example.com +Connection: close +User-Agent: HTTP-Tiny/VERSION +Cookie: cname=cvalue05 + +---------- +HTTP/1.1 200 OK +Date: Sat, 02 Mar 2013 00:00:00 GMT +Set-Cookie: cname=cvalue06; domain=example.com; path=/ +Content-Type: text/plain +Content-Length: 44 + +abcdefghijklmnopqrstuvwxyz1234567890abcdef +---------- +url + http://example.com/index.html +---------- +GET /index.html HTTP/1.1 +Host: example.com +Connection: close +User-Agent: HTTP-Tiny/VERSION +Cookie: cname=cvalue06 + +---------- +HTTP/1.1 200 OK +Date: Sat, 02 Mar 2013 00:00:00 GMT +Set-Cookie: cname=cvalue06; domain=example.com; path=/ +Content-Type: text/plain +Content-Length: 44 + +abcdefghijklmnopqrstuvwxyz1234567890abcdef diff --git a/corpus/cookies-05.txt b/corpus/cookies-05.txt new file mode 100644 index 0000000..0d41152 --- /dev/null +++ b/corpus/cookies-05.txt @@ -0,0 +1,35 @@ +url + http://example.com/index.html +headers + No-Cookie-Jar: 1 +---------- +GET /index.html HTTP/1.1 +Host: example.com +Connection: close +User-Agent: HTTP-Tiny/VERSION + +---------- +HTTP/1.1 200 OK +Date: Sat, 02 Mar 2013 00:00:00 GMT +Set-Cookie: cname=cvalue06; domain=example.com; path=/ +Content-Type: text/plain +Content-Length: 44 + +abcdefghijklmnopqrstuvwxyz1234567890abcdef +---------- +url + http://example.com/index.html +---------- +GET /index.html HTTP/1.1 +Host: example.com +Connection: close +User-Agent: HTTP-Tiny/VERSION + +---------- +HTTP/1.1 200 OK +Date: Sat, 02 Mar 2013 00:00:00 GMT +Set-Cookie: cname=cvalue06; domain=example.com; path=/ +Content-Type: text/plain +Content-Length: 44 + +abcdefghijklmnopqrstuvwxyz1234567890abcdef diff --git a/corpus/cookies-06.txt b/corpus/cookies-06.txt new file mode 100644 index 0000000..86632b0 --- /dev/null +++ b/corpus/cookies-06.txt @@ -0,0 +1,51 @@ +url + http://example.com/index.html +---------- +GET /index.html HTTP/1.1 +Host: example.com +Connection: close +User-Agent: HTTP-Tiny/VERSION + +---------- +HTTP/1.1 200 OK +Date: Sat, 02 Mar 2013 00:00:00 GMT +Set-Cookie: cname1=cvalue01; domain=example.com; path=/ +Content-Type: text/plain +Content-Length: 44 + +abcdefghijklmnopqrstuvwxyz1234567890abcdef +---------- +url + http://example.com/index.html +---------- +GET /index.html HTTP/1.1 +Host: example.com +Connection: close +User-Agent: HTTP-Tiny/VERSION +Cookie: cname1=cvalue01 + +---------- +HTTP/1.1 200 OK +Date: Sat, 02 Mar 2013 00:00:00 GMT +Set-Cookie: cname2=cvalue02; domain=example.com; path=/ +Content-Type: text/plain +Content-Length: 44 + +abcdefghijklmnopqrstuvwxyz1234567890abcdef +---------- +url + http://example.com/index.html +---------- +GET /index.html HTTP/1.1 +Host: example.com +Connection: close +User-Agent: HTTP-Tiny/VERSION +Cookie: cname1=cvalue01; cname2=cvalue02 + +---------- +HTTP/1.1 200 OK +Date: Sat, 02 Mar 2013 00:00:00 GMT +Content-Type: text/plain +Content-Length: 44 + +abcdefghijklmnopqrstuvwxyz1234567890abcdef diff --git a/corpus/cookies-07.txt b/corpus/cookies-07.txt new file mode 100644 index 0000000..69c4a62 --- /dev/null +++ b/corpus/cookies-07.txt @@ -0,0 +1,34 @@ +url + http://example.com/index.html +---------- +GET /index.html HTTP/1.1 +Host: example.com +Connection: close +User-Agent: HTTP-Tiny/VERSION + +---------- +HTTP/1.1 200 OK +Date: Sat, 02 Mar 2013 00:00:00 GMT +Set-Cookie: cname1=cvalue01; domain=example.com; path=/ +Set-Cookie: cname2=cvalue02; domain=example.com; path=/ +Content-Type: text/plain +Content-Length: 44 + +abcdefghijklmnopqrstuvwxyz1234567890abcdef +---------- +url + http://example.com/index.html +---------- +GET /index.html HTTP/1.1 +Host: example.com +Connection: close +User-Agent: HTTP-Tiny/VERSION +Cookie: cname1=cvalue01; cname2=cvalue02 + +---------- +HTTP/1.1 200 OK +Date: Sat, 02 Mar 2013 00:00:00 GMT +Content-Type: text/plain +Content-Length: 44 + +abcdefghijklmnopqrstuvwxyz1234567890abcdef diff --git a/corpus/delete-01.txt b/corpus/delete-01.txt new file mode 100644 index 0000000..a83069e --- /dev/null +++ b/corpus/delete-01.txt @@ -0,0 +1,15 @@ +url + http://example.com/index.html +expected + abcdefghijklmnopqrstuvwxyz1234567890abcdef +---------- +DELETE /index.html HTTP/1.1 +Host: example.com +Connection: close +User-Agent: HTTP-Tiny/VERSION + +---------- +HTTP/1.1 200 OK +Date: Thu, 03 Feb 1994 00:00:00 GMT +Content-Length: 0 + diff --git a/corpus/form-01.txt b/corpus/form-01.txt new file mode 100644 index 0000000..876f1b6 --- /dev/null +++ b/corpus/form-01.txt @@ -0,0 +1,23 @@ +url + http://example.com/new +headers + Content-Type: text/plain +content + key|value|name|John Doe|noise|!@#$%^&*() +datatype + ARRAY +---------- +POST /new HTTP/1.1 +Host: example.com +Connection: close +User-Agent: HTTP-Tiny/VERSION +Content-Type: application/x-www-form-urlencoded +Content-Length: 60 + +key=value&name=John+Doe&noise=%21%40%23%24%25%5E%26%2A%28%29 +---------- +HTTP/1.1 201 Created +Date: Thu, 03 Feb 1994 00:00:00 GMT +Location: http://example.com/new/01.txt +Content-Length: 0 + diff --git a/corpus/form-02.txt b/corpus/form-02.txt new file mode 100644 index 0000000..5ec2d91 --- /dev/null +++ b/corpus/form-02.txt @@ -0,0 +1,21 @@ +url + http://example.com/new +content + key|value|name|John Doe|noise|!@#$%^&*() +datatype + HASH +---------- +POST /new HTTP/1.1 +Host: example.com +Connection: close +User-Agent: HTTP-Tiny/VERSION +Content-Type: application/x-www-form-urlencoded +Content-Length: 60 + +key=value&name=John+Doe&noise=%21%40%23%24%25%5E%26%2A%28%29 +---------- +HTTP/1.1 201 Created +Date: Thu, 03 Feb 1994 00:00:00 GMT +Location: http://example.com/new/01.txt +Content-Length: 0 + diff --git a/corpus/form-03.txt b/corpus/form-03.txt new file mode 100644 index 0000000..101224d --- /dev/null +++ b/corpus/form-03.txt @@ -0,0 +1,21 @@ +url + http://example.com/new +content + bar|baz|ack|foo +datatype + HASH +---------- +POST /new HTTP/1.1 +Host: example.com +Connection: close +User-Agent: HTTP-Tiny/VERSION +Content-Type: application/x-www-form-urlencoded +Content-Length: 15 + +ack=foo&bar=baz +---------- +HTTP/1.1 201 Created +Date: Thu, 03 Feb 1994 00:00:00 GMT +Location: http://example.com/new/01.txt +Content-Length: 0 + diff --git a/corpus/form-04.txt b/corpus/form-04.txt new file mode 100644 index 0000000..3f762c0 --- /dev/null +++ b/corpus/form-04.txt @@ -0,0 +1,21 @@ +url + http://example.com/new +content + utf8|☺ +datatype + ARRAY +---------- +POST /new HTTP/1.1 +Host: example.com +Connection: close +User-Agent: HTTP-Tiny/VERSION +Content-Type: application/x-www-form-urlencoded +Content-Length: 23 + +utf8=%C3%A2%C2%98%C2%BA +---------- +HTTP/1.1 201 Created +Date: Thu, 03 Feb 1994 00:00:00 GMT +Location: http://example.com/new/01.txt +Content-Length: 0 + diff --git a/corpus/form-05.txt b/corpus/form-05.txt new file mode 100644 index 0000000..825604f --- /dev/null +++ b/corpus/form-05.txt @@ -0,0 +1,21 @@ +url + http://example.com/new +content + bar|baz|ack|foo +datatype + ARRAY +---------- +POST /new HTTP/1.1 +Host: example.com +Connection: close +User-Agent: HTTP-Tiny/VERSION +Content-Type: application/x-www-form-urlencoded +Content-Length: 15 + +bar=baz&ack=foo +---------- +HTTP/1.1 201 Created +Date: Thu, 03 Feb 1994 00:00:00 GMT +Location: http://example.com/new/01.txt +Content-Length: 0 + diff --git a/corpus/get-01.txt b/corpus/get-01.txt new file mode 100644 index 0000000..e03fb38 --- /dev/null +++ b/corpus/get-01.txt @@ -0,0 +1,17 @@ +url + http://example.com/index.html +expected + abcdefghijklmnopqrstuvwxyz1234567890abcdef +---------- +GET /index.html HTTP/1.1 +Host: example.com +Connection: close +User-Agent: HTTP-Tiny/VERSION + +---------- +HTTP/1.1 200 OK +Date: Thu, 03 Feb 1994 00:00:00 GMT +Content-Type: text/plain +Content-Length: 44 + +abcdefghijklmnopqrstuvwxyz1234567890abcdef diff --git a/corpus/get-02.txt b/corpus/get-02.txt new file mode 100644 index 0000000..3d359d5 --- /dev/null +++ b/corpus/get-02.txt @@ -0,0 +1,26 @@ +url + http://example.com/index.html +expected + abcdefghijklmnopqrstuvwxyz1234567890abcdef +headers + Accept: */* + X-Custom: This is a custom header + x-lower: This is a lower-case custom header + authorization: fake auth data +---------- +GET /index.html HTTP/1.1 +Host: example.com +Accept: */* +Authorization: fake auth data +Connection: close +User-Agent: HTTP-Tiny/VERSION +X-Custom: This is a custom header +x-lower: This is a lower-case custom header + +---------- +HTTP/1.1 200 OK +Date: Thu, 03 Feb 1994 00:00:00 GMT +Content-Type: text/plain +Content-Length: 44 + +abcdefghijklmnopqrstuvwxyz1234567890abcdef diff --git a/corpus/get-03.txt b/corpus/get-03.txt new file mode 100644 index 0000000..e5eed63 --- /dev/null +++ b/corpus/get-03.txt @@ -0,0 +1,13 @@ +url + http://example.com/missing.html +---------- +GET /missing.html HTTP/1.1 +Host: example.com +Connection: close +User-Agent: HTTP-Tiny/VERSION + +---------- +HTTP/1.1 404 Not Found +Date: Thu, 03 Feb 1994 00:00:00 GMT +Content-Length: 0 + diff --git a/corpus/get-04.txt b/corpus/get-04.txt new file mode 100644 index 0000000..71698f9 --- /dev/null +++ b/corpus/get-04.txt @@ -0,0 +1,17 @@ +url + http://example.com:9000/index.html +expected + abcdefghijklmnopqrstuvwxyz1234567890abcdef +---------- +GET /index.html HTTP/1.1 +Host: example.com:9000 +Connection: close +User-Agent: HTTP-Tiny/VERSION + +---------- +HTTP/1.1 200 OK +Date: Thu, 03 Feb 1994 00:00:00 GMT +Content-Type: text/plain +Content-Length: 44 + +abcdefghijklmnopqrstuvwxyz1234567890abcdef diff --git a/corpus/get-05.txt b/corpus/get-05.txt new file mode 100644 index 0000000..b689aaa --- /dev/null +++ b/corpus/get-05.txt @@ -0,0 +1,21 @@ +url + http://example.com/chunked.html +expected + abcdefghijklmnopqrstuvwxyz1234567890abcdef +---------- +GET /chunked.html HTTP/1.1 +Host: example.com +Connection: close +User-Agent: HTTP-Tiny/VERSION + +---------- +HTTP/1.1 200 OK +Date: Thu, 03 Feb 1994 00:00:00 GMT +Content-Type: text/plain +Transfer-Encoding: chunked + +2C +abcdefghijklmnopqrstuvwxyz1234567890abcdef + +0 + diff --git a/corpus/get-06.txt b/corpus/get-06.txt new file mode 100644 index 0000000..131bb58 --- /dev/null +++ b/corpus/get-06.txt @@ -0,0 +1,19 @@ +url + http://example.com/cb.html +expected + abcdefghijklmnopqrstuvwxyz1234567890abcdef +data_cb + sub { $main::data .= $_[0] } +---------- +GET /cb.html HTTP/1.1 +Host: example.com +Connection: close +User-Agent: HTTP-Tiny/VERSION + +---------- +HTTP/1.1 200 OK +Date: Thu, 03 Feb 1994 00:00:00 GMT +Content-Type: text/plain +Content-Length: 44 + +abcdefghijklmnopqrstuvwxyz1234567890abcdef diff --git a/corpus/get-07.txt b/corpus/get-07.txt new file mode 100644 index 0000000..dec18fd --- /dev/null +++ b/corpus/get-07.txt @@ -0,0 +1,19 @@ +new_args + proxy: http://proxy.example.com:8080/ +url + http://example.com/index.html +expected + abcdefghijklmnopqrstuvwxyz1234567890abcdef +---------- +GET http://example.com/index.html HTTP/1.1 +Host: example.com +Connection: close +User-Agent: HTTP-Tiny/VERSION + +---------- +HTTP/1.1 200 OK +Date: Thu, 03 Feb 1994 00:00:00 GMT +Content-Type: text/plain +Content-Length: 44 + +abcdefghijklmnopqrstuvwxyz1234567890abcdef diff --git a/corpus/get-08.txt b/corpus/get-08.txt new file mode 100644 index 0000000..3044db1 --- /dev/null +++ b/corpus/get-08.txt @@ -0,0 +1,21 @@ +new_args + max_size: 26 +url + http://example.com/index.html +expected_rc + 599 +expected_like + Size of response body exceeds the maximum allowed of 26 +---------- +GET /index.html HTTP/1.1 +Host: example.com +Connection: close +User-Agent: HTTP-Tiny/VERSION + +---------- +HTTP/1.1 200 OK +Date: Thu, 03 Feb 1994 00:00:00 GMT +Content-Type: text/plain +Content-Length: 44 + +abcdefghijklmnopqrstuvwxyz1234567890abcdef diff --git a/corpus/get-09.txt b/corpus/get-09.txt new file mode 100644 index 0000000..0d5eb5d --- /dev/null +++ b/corpus/get-09.txt @@ -0,0 +1,16 @@ +url + http://example.com/index.html +expected + abcdefghijklmnopqrstuvwxyz1234567890abcdef +---------- +GET /index.html HTTP/1.1 +Host: example.com +Connection: close +User-Agent: HTTP-Tiny/VERSION + +---------- +HTTP/1.1 200 OK +Date: Thu, 03 Feb 1994 00:00:00 GMT +Content-Type: text/plain + +abcdefghijklmnopqrstuvwxyz1234567890abcdef diff --git a/corpus/get-10.txt b/corpus/get-10.txt new file mode 100644 index 0000000..23c0163 --- /dev/null +++ b/corpus/get-10.txt @@ -0,0 +1,21 @@ +url + http://example.com/chunked.html +expected + abcdefghijklmnopqrstuvwxyz1234567890abcdef +---------- +GET /chunked.html HTTP/1.1 +Host: example.com +Connection: close +User-Agent: HTTP-Tiny/VERSION + +---------- +HTTP/1.1 200 OK +Date: Thu, 03 Feb 1994 00:00:00 GMT +Content-Type: text/plain +Transfer-Encoding: CHUNKED + +2C +abcdefghijklmnopqrstuvwxyz1234567890abcdef + +0 + diff --git a/corpus/get-11.txt b/corpus/get-11.txt new file mode 100644 index 0000000..dca6d14 --- /dev/null +++ b/corpus/get-11.txt @@ -0,0 +1,22 @@ +url + http://example.com/chunked.html +expected + abcdefghijklmnopqrstuvwxyz1234567890abcdef +---------- +GET /chunked.html HTTP/1.1 +Host: example.com +Connection: close +User-Agent: HTTP-Tiny/VERSION + +---------- +HTTP/1.1 200 OK +Date: Thu, 03 Feb 1994 00:00:00 GMT +Content-Type: text/plain +Content-Length: 1024 +Transfer-Encoding: chunked + +2C +abcdefghijklmnopqrstuvwxyz1234567890abcdef + +0 + diff --git a/corpus/get-12.txt b/corpus/get-12.txt new file mode 100644 index 0000000..9cf2bf7 --- /dev/null +++ b/corpus/get-12.txt @@ -0,0 +1,17 @@ +url + http:///index.html +expected + abcdefghijklmnopqrstuvwxyz1234567890abcdef +---------- +GET /index.html HTTP/1.1 +Host: localhost +Connection: close +User-Agent: HTTP-Tiny/VERSION + +---------- +HTTP/1.1 200 OK +Date: Thu, 03 Feb 1994 00:00:00 GMT +Content-Type: text/plain +Content-Length: 44 + +abcdefghijklmnopqrstuvwxyz1234567890abcdef diff --git a/corpus/get-13.txt b/corpus/get-13.txt new file mode 100644 index 0000000..7e43755 --- /dev/null +++ b/corpus/get-13.txt @@ -0,0 +1,21 @@ +url + http://example.com/chunked.html +expected + abcdefghijklmnopqrstuvwxyz1234567890abcdef +---------- +GET /chunked.html HTTP/1.1 +Host: example.com +Connection: close +User-Agent: HTTP-Tiny/VERSION + +---------- +HTTP/1.1 200 OK +Date: Thu, 03 Feb 1994 00:00:00 GMT +Content-Type: text/plain +Transfer-Encoding: chunked + +2C; this_extension=foo +abcdefghijklmnopqrstuvwxyz1234567890abcdef + +0 + diff --git a/corpus/get-14.txt b/corpus/get-14.txt new file mode 100644 index 0000000..e232aa2 --- /dev/null +++ b/corpus/get-14.txt @@ -0,0 +1,22 @@ +url + http://example.com/index.html +expected + abcdefghijklmnopqrstuvwxyz1234567890abcdef +headers + X-Foo: one + X-Foo: two +---------- +GET /index.html HTTP/1.1 +Host: example.com +Connection: close +User-Agent: HTTP-Tiny/VERSION +X-Foo: one +X-Foo: two + +---------- +HTTP/1.1 200 OK +Date: Thu, 03 Feb 1994 00:00:00 GMT +Content-Type: text/plain +Content-Length: 44 + +abcdefghijklmnopqrstuvwxyz1234567890abcdef diff --git a/corpus/get-15.txt b/corpus/get-15.txt new file mode 100644 index 0000000..0bd5065 --- /dev/null +++ b/corpus/get-15.txt @@ -0,0 +1,17 @@ +url + http://example.com/index.html +expected + abcdefghijklmnopqrstuvwxyz1234567890abcdef +---------- +GET /index.html HTTP/1.1 +Host: example.com +Connection: close +User-Agent: HTTP-Tiny/VERSION + +---------- +HTTP/01.01 200 OK +Date: Thu, 03 Feb 1994 00:00:00 GMT +Content-Type: text/plain +Content-Length: 44 + +abcdefghijklmnopqrstuvwxyz1234567890abcdef diff --git a/corpus/get-16.txt b/corpus/get-16.txt new file mode 100644 index 0000000..e17076d --- /dev/null +++ b/corpus/get-16.txt @@ -0,0 +1,19 @@ +url + http://example.com/index.html +expected_rc + 599 +expected_like + Malformed Status-Line +---------- +GET /index.html HTTP/1.1 +Host: example.com +Connection: close +User-Agent: HTTP-Tiny/VERSION + +---------- +HTTP/0 200 OK +Date: Thu, 03 Feb 1994 00:00:00 GMT +Content-Type: text/plain +Content-Length: 44 + +abcdefghijklmnopqrstuvwxyz1234567890abcdef diff --git a/corpus/get-17.txt b/corpus/get-17.txt new file mode 100644 index 0000000..e5d3d12 --- /dev/null +++ b/corpus/get-17.txt @@ -0,0 +1,19 @@ +url + http://example.com/index.html +expected_rc + 599 +expected_like + Unsupported HTTP protocol +---------- +GET /index.html HTTP/1.1 +Host: example.com +Connection: close +User-Agent: HTTP-Tiny/VERSION + +---------- +HTTP/1.2 200 OK +Date: Thu, 03 Feb 1994 00:00:00 GMT +Content-Type: text/plain +Content-Length: 44 + +abcdefghijklmnopqrstuvwxyz1234567890abcdef diff --git a/corpus/get-18.txt b/corpus/get-18.txt new file mode 100644 index 0000000..f46f48c --- /dev/null +++ b/corpus/get-18.txt @@ -0,0 +1,36 @@ +url + http://example.com/chunked.html +expected + abcdefghijklmnopqrstuvwxyz1234567890abcdef +expected_headers + transfer-encoding: chunked + x-a: Foo + x-a: Bar + x-a: Baz + x-b: Foo + x-b: Bar + x-b: Baz + x-c: Foo + +---------- +GET /chunked.html HTTP/1.1 +Host: example.com +Connection: close +User-Agent: HTTP-Tiny/VERSION + +---------- +HTTP/1.1 200 OK +Transfer-Encoding: chunked +X-A: Foo +X-B: Foo +X-B: Bar + +2C +abcdefghijklmnopqrstuvwxyz1234567890abcdef + +0 +X-A: Bar +X-A: Baz +X-B: Baz +X-C: Foo + diff --git a/corpus/get-19.txt b/corpus/get-19.txt new file mode 100644 index 0000000..2ebb9a8 --- /dev/null +++ b/corpus/get-19.txt @@ -0,0 +1,22 @@ +url + http://example.com/chunked.html +expected + abcdefghijklmnopqrstuvwxyz1234567890abcdef +---------- +GET /chunked.html HTTP/1.1 +Host: example.com +Connection: close +User-Agent: HTTP-Tiny/VERSION + +---------- +HTTP/1.1 200 OK +Date: Thu, 03 Feb 1994 00:00:00 GMT +Content-Type: text/plain +Transfer-Encoding: wacky +Transfer-Encoding: chunked + +2C +abcdefghijklmnopqrstuvwxyz1234567890abcdef + +0 + diff --git a/corpus/get-20.txt b/corpus/get-20.txt new file mode 100644 index 0000000..8873793 --- /dev/null +++ b/corpus/get-20.txt @@ -0,0 +1,21 @@ +url + http://example.com/index.html +headers + connection: X-Foo + X-Foo: bar +expected + abcdefghijklmnopqrstuvwxyz1234567890abcdef +---------- +GET /index.html HTTP/1.1 +Host: example.com +Connection: close +X-Foo: bar +User-Agent: HTTP-Tiny/VERSION + +---------- +HTTP/1.1 200 OK +Date: Thu, 03 Feb 1994 00:00:00 GMT +Content-Type: text/plain +Content-Length: 44 + +abcdefghijklmnopqrstuvwxyz1234567890abcdef diff --git a/corpus/get-21.txt b/corpus/get-21.txt new file mode 100644 index 0000000..df207f6 --- /dev/null +++ b/corpus/get-21.txt @@ -0,0 +1,23 @@ +url + http://example.com/index.html +expected + abcdefghijklmnopqrstuvwxyz1234567890abcdef +expected_rc + 200 +---------- +GET /index.html HTTP/1.1 +Host: example.com +Connection: close +User-Agent: HTTP-Tiny/VERSION + +---------- +HTTP/1.1 100 Continue + +HTTP/1.1 110 Arbitrary 1XX status code + +HTTP/1.1 200 OK +Date: Thu, 03 Feb 1994 00:00:00 GMT +Content-Type: text/plain +Content-Length: 44 + +abcdefghijklmnopqrstuvwxyz1234567890abcdef diff --git a/corpus/get-22.txt b/corpus/get-22.txt new file mode 100644 index 0000000..d6f20c0 --- /dev/null +++ b/corpus/get-22.txt @@ -0,0 +1,10 @@ +url + http://example.com/index.html +expected_rc + 599 +expected_like + Invalid HTTP header field +headers + trailing-space : foo +---------- +---------- diff --git a/corpus/head-01.txt b/corpus/head-01.txt new file mode 100644 index 0000000..1c6357a --- /dev/null +++ b/corpus/head-01.txt @@ -0,0 +1,16 @@ +url + http://example.com/index.html +expected + abcdefghijklmnopqrstuvwxyz1234567890abcdef +---------- +HEAD /index.html HTTP/1.1 +Host: example.com +Connection: close +User-Agent: HTTP-Tiny/VERSION + +---------- +HTTP/1.1 200 OK +Date: Thu, 03 Feb 1994 00:00:00 GMT +Content-Type: text/plain +Content-Length: 44 + diff --git a/corpus/keepalive-01.txt b/corpus/keepalive-01.txt new file mode 100644 index 0000000..0853912 --- /dev/null +++ b/corpus/keepalive-01.txt @@ -0,0 +1,9 @@ +No content length +---------- +0 +---------- +HTTP/1.1 200 OK +Date: Thu, 03 Feb 1994 00:00:00 GMT +Content-Type: text/html + +0123456789 diff --git a/corpus/keepalive-02.txt b/corpus/keepalive-02.txt new file mode 100644 index 0000000..970360b --- /dev/null +++ b/corpus/keepalive-02.txt @@ -0,0 +1,10 @@ +Wrong content length +---------- +0 +---------- +HTTP/1.1 200 OK +Date: Thu, 03 Feb 1994 00:00:00 GMT +Content-Type: text/html +Content-Length: 10 + +01234567890123456789 diff --git a/corpus/keepalive-03.txt b/corpus/keepalive-03.txt new file mode 100644 index 0000000..1792b8c --- /dev/null +++ b/corpus/keepalive-03.txt @@ -0,0 +1,11 @@ +Connection close +---------- +0 +---------- +HTTP/1.1 200 OK +Date: Thu, 03 Feb 1994 00:00:00 GMT +Content-Type: text/html +Content-Length: 10 +Connection: close + +0123456789 diff --git a/corpus/keepalive-04.txt b/corpus/keepalive-04.txt new file mode 100644 index 0000000..ef5e4d9 --- /dev/null +++ b/corpus/keepalive-04.txt @@ -0,0 +1,10 @@ +Not HTTP/1.1 +---------- +0 +---------- +HTTP/1.0 200 OK +Date: Thu, 03 Feb 1994 00:00:00 GMT +Content-Type: text/html +Content-Length: 10 + +0123456789 diff --git a/corpus/keepalive-05.txt b/corpus/keepalive-05.txt new file mode 100644 index 0000000..75872c9 --- /dev/null +++ b/corpus/keepalive-05.txt @@ -0,0 +1,11 @@ +Not HTTP/1.1 with keep-alive +---------- +0 +---------- +HTTP/1.0 200 OK +Date: Thu, 03 Feb 1994 00:00:00 GMT +Content-Type: text/html +Content-Length: 10 +Connection: keep-alive + +0123456789 diff --git a/corpus/mirror-01.txt b/corpus/mirror-01.txt new file mode 100644 index 0000000..5ac0eb5 --- /dev/null +++ b/corpus/mirror-01.txt @@ -0,0 +1,18 @@ +url + http://example.com/modified.txt +---------- +GET /modified.txt HTTP/1.1 +Host: example.com +Connection: close +If-Modified-Since: Tue, 01 Feb 1994 00:00:00 GMT +User-Agent: HTTP-Tiny/VERSION + +---------- +HTTP/1.1 200 OK +Date: Thu, 03 Feb 1994 00:00:00 GMT +Last-Modified: Wed, 02 Feb 1994 00:00:00 GMT +Content-Type: text/plain +Content-Length: 42 + +abcdefghijklmnopqrstuvwxyz1234567890abcdef + diff --git a/corpus/mirror-02.txt b/corpus/mirror-02.txt new file mode 100644 index 0000000..4799a2e --- /dev/null +++ b/corpus/mirror-02.txt @@ -0,0 +1,14 @@ +url + http://example.com/not-modified.txt +---------- +GET /not-modified.txt HTTP/1.1 +Host: example.com +Connection: close +If-Modified-Since: Tue, 01 Feb 1994 00:00:00 GMT +User-Agent: HTTP-Tiny/VERSION + +---------- +HTTP/1.1 304 Not Modified +Date: Thu, 03 Feb 1994 00:00:00 GMT +Last-Modified: Tue, 01 Feb 1994 00:00:00 GMT + diff --git a/corpus/mirror-03.txt b/corpus/mirror-03.txt new file mode 100644 index 0000000..711710d --- /dev/null +++ b/corpus/mirror-03.txt @@ -0,0 +1,17 @@ +url + http://example.com/new.txt +---------- +GET /new.txt HTTP/1.1 +Host: example.com +Connection: close +User-Agent: HTTP-Tiny/VERSION + +---------- +HTTP/1.1 200 OK +Date: Thu, 03 Feb 1994 00:00:00 GMT +Last-Modified: Tue, 01 Feb 1994 00:00:00 GMT +Content-Type: text/plain +Content-Length: 42 + +abcdefghijklmnopqrstuvwxyz1234567890abcdef + diff --git a/corpus/mirror-04.txt b/corpus/mirror-04.txt new file mode 100644 index 0000000..9beeeb3 --- /dev/null +++ b/corpus/mirror-04.txt @@ -0,0 +1,13 @@ +url + http://example.com/missing.txt +---------- +GET /missing.txt HTTP/1.1 +Host: example.com +Connection: close +User-Agent: HTTP-Tiny/VERSION + +---------- +HTTP/1.1 404 Not Found +Date: Thu, 03 Feb 1994 00:00:00 GMT +Content-Length: 0 + diff --git a/corpus/mirror-05.txt b/corpus/mirror-05.txt new file mode 100644 index 0000000..f1f3573 --- /dev/null +++ b/corpus/mirror-05.txt @@ -0,0 +1,20 @@ +url + http://example.com/modified.txt +headers + if-modified-since: Tue, 01 Feb 1994 12:00:00 GMT +---------- +GET /modified.txt HTTP/1.1 +Host: example.com +Connection: close +If-Modified-Since: Tue, 01 Feb 1994 12:00:00 GMT +User-Agent: HTTP-Tiny/VERSION + +---------- +HTTP/1.1 200 OK +Date: Thu, 03 Feb 1994 00:00:00 GMT +Last-Modified: Wed, 02 Feb 1994 00:00:00 GMT +Content-Type: text/plain +Content-Length: 42 + +abcdefghijklmnopqrstuvwxyz1234567890abcdef + diff --git a/corpus/post-01.txt b/corpus/post-01.txt new file mode 100644 index 0000000..644731b --- /dev/null +++ b/corpus/post-01.txt @@ -0,0 +1,24 @@ +url + http://example.com/index.html +headers + Content-Type: text/plain + Content-Length: 42 +content + abcdefghijklmnopqrstuvwxyz1234567890abcdef +---------- +POST /index.html HTTP/1.1 +Host: example.com +Connection: close +User-Agent: HTTP-Tiny/VERSION +Content-Type: text/plain +Content-Length: 42 + +abcdefghijklmnopqrstuvwxyz1234567890abcdef + +---------- +HTTP/1.1 200 OK +Date: Thu, 03 Feb 1994 00:00:00 GMT +Content-Type: text/plain +Content-Length: 42 + +abcdefghijklmnopqrstuvwxyz1234567890abcdef diff --git a/corpus/proxy-auth-01.txt b/corpus/proxy-auth-01.txt new file mode 100644 index 0000000..548a599 --- /dev/null +++ b/corpus/proxy-auth-01.txt @@ -0,0 +1,21 @@ +url + http://example.com/index.html +expected + abcdefghijklmnopqrstuvwxyz1234567890abcdef +new_args + proxy: http://foo:bar@proxy.example.com/ + +---------- +GET http://example.com/index.html HTTP/1.1 +Host: example.com +Connection: close +User-Agent: HTTP-Tiny/VERSION +Proxy-Authorization: Basic Zm9vOmJhcg== + +---------- +HTTP/1.1 200 OK +Date: Thu, 03 Feb 1994 00:00:00 GMT +Content-Type: text/plain +Content-Length: 42 + +abcdefghijklmnopqrstuvwxyz1234567890abcdef diff --git a/corpus/put-01.txt b/corpus/put-01.txt new file mode 100644 index 0000000..b8d6286 --- /dev/null +++ b/corpus/put-01.txt @@ -0,0 +1,22 @@ +url + http://example.com/new.txt +headers + Content-Type: text/plain +content + abcdefghijklmnopqrstuvwxyz1234567890abcdef +---------- +PUT /new.txt HTTP/1.1 +Host: example.com +Connection: close +User-Agent: HTTP-Tiny/VERSION +Content-Type: text/plain +Content-Length: 42 + +abcdefghijklmnopqrstuvwxyz1234567890abcdef + +---------- +HTTP/1.1 201 Created +Date: Thu, 03 Feb 1994 00:00:00 GMT +Location: http://example.com/new.txt +Content-Length: 0 + diff --git a/corpus/put-02.txt b/corpus/put-02.txt new file mode 100644 index 0000000..04d2675 --- /dev/null +++ b/corpus/put-02.txt @@ -0,0 +1,24 @@ +url + http://example.com/callback.txt +headers + Content-Type: text/plain + Content-Length: 42 +content_cb + my @content = qq{abcdefghijklmnopqrstuvwxyz1234567890abcdef}; + sub { shift @content } +---------- +PUT /callback.txt HTTP/1.1 +Host: example.com +Connection: close +User-Agent: HTTP-Tiny/VERSION +Content-Type: text/plain +Content-Length: 42 + +abcdefghijklmnopqrstuvwxyz1234567890abcdef + +---------- +HTTP/1.1 201 Created +Date: Thu, 03 Feb 1994 00:00:00 GMT +Location: http://example.com/callback.txt +Content-Length: 0 + diff --git a/corpus/put-03.txt b/corpus/put-03.txt new file mode 100644 index 0000000..99fe188 --- /dev/null +++ b/corpus/put-03.txt @@ -0,0 +1,25 @@ +url + http://example.com/chunked.txt +headers + Content-Type: text/plain +content_cb + my @content = qq{abcdefghijklmnopqrstuvwxyz1234567890abcdef}; + sub { shift @content } +---------- +PUT /chunked.txt HTTP/1.1 +Host: example.com +Connection: close +User-Agent: HTTP-Tiny/VERSION +Content-Type: text/plain +Transfer-Encoding: chunked + +2A +abcdefghijklmnopqrstuvwxyz1234567890abcdef +0 + +---------- +HTTP/1.1 201 Created +Date: Thu, 03 Feb 1994 00:00:00 GMT +Location: http://example.com/chunked.txt +Content-Length: 0 + diff --git a/corpus/put-04.txt b/corpus/put-04.txt new file mode 100644 index 0000000..eeec295 --- /dev/null +++ b/corpus/put-04.txt @@ -0,0 +1,19 @@ +url + http://example.com/new.txt +content + abcdefghijklmnopqrstuvwxyz1234567890abcdef +---------- +PUT /new.txt HTTP/1.1 +Host: example.com +Connection: close +User-Agent: HTTP-Tiny/VERSION +Content-Type: application/octet-stream +Content-Length: 42 + +abcdefghijklmnopqrstuvwxyz1234567890abcdef +---------- +HTTP/1.1 201 Created +Date: Thu, 03 Feb 1994 00:00:00 GMT +Location: http://example.com/new.txt +Content-Length: 0 + diff --git a/corpus/put-05.txt b/corpus/put-05.txt new file mode 100644 index 0000000..f4bcaf1 --- /dev/null +++ b/corpus/put-05.txt @@ -0,0 +1,27 @@ +url + http://example.com/chunked.txt +headers + Content-Type: text/plain +content_cb + my @content = qq{abcdefghijklmnopqrstuvwxyz1234567890abcdef}; + sub { shift @content } +trailer_cb + sub { return { 'x-foo' => 'bar' } } +---------- +PUT /chunked.txt HTTP/1.1 +Host: example.com +Connection: close +User-Agent: HTTP-Tiny/VERSION +Content-Type: text/plain +Transfer-Encoding: chunked + +2A +abcdefghijklmnopqrstuvwxyz1234567890abcdef +0 +X-Foo: bar +---------- +HTTP/1.1 201 Created +Date: Thu, 03 Feb 1994 00:00:00 GMT +Location: http://example.com/chunked.txt +Content-Length: 0 + diff --git a/corpus/redirect-01.txt b/corpus/redirect-01.txt new file mode 100644 index 0000000..c6ed575 --- /dev/null +++ b/corpus/redirect-01.txt @@ -0,0 +1,35 @@ +url + http://example.com/index.html +expected + abcdefghijklmnopqrstuvwxyz1234567890abcdef +expected_url + http://example.com/index2.html +---------- +GET /index.html HTTP/1.1 +Host: example.com +Connection: close +User-Agent: HTTP-Tiny/VERSION + +---------- +HTTP/1.1 302 Found +Date: Thu, 03 Feb 1994 00:00:00 GMT +Content-Type: text/html +Content-Length: 53 +Location: http://example.com/index2.html + +redirect + +---------- +GET /index2.html HTTP/1.1 +Host: example.com +Connection: close +User-Agent: HTTP-Tiny/VERSION + +---------- +HTTP/1.1 200 OK +Date: Thu, 03 Feb 1994 00:00:00 GMT +Content-Type: text/plain +Content-Length: 42 + +abcdefghijklmnopqrstuvwxyz1234567890abcdef + diff --git a/corpus/redirect-02.txt b/corpus/redirect-02.txt new file mode 100644 index 0000000..b2a2967 --- /dev/null +++ b/corpus/redirect-02.txt @@ -0,0 +1,52 @@ +new_args + max_redirect: 0 +url + http://example.com/index.html +expected + redirect +expected_url + http://example.com/index.html +---------- +GET /index.html HTTP/1.1 +Host: example.com +Connection: close +User-Agent: HTTP-Tiny/VERSION + +---------- +HTTP/1.1 302 Found +Date: Thu, 03 Feb 1994 00:00:00 GMT +Content-Type: text/html +Content-Length: 53 +Location: http://example.com/index2.html + +redirect + +---------- +GET /index2.html HTTP/1.1 +Host: example.com +Connection: close +User-Agent: HTTP-Tiny/VERSION + +---------- +HTTP/1.1 302 Found +Date: Thu, 03 Feb 1994 00:00:00 GMT +Content-Type: text/html +Content-Length: 53 +Location: http://example.com/index3.html + +redirect + +---------- +GET /index3.html HTTP/1.1 +Host: example.com +Connection: close +User-Agent: HTTP-Tiny/VERSION + +---------- +HTTP/1.1 200 OK +Date: Thu, 03 Feb 1994 00:00:00 GMT +Content-Type: text/plain +Content-Length: 42 + +abcdefghijklmnopqrstuvwxyz1234567890abcdef + diff --git a/corpus/redirect-03.txt b/corpus/redirect-03.txt new file mode 100644 index 0000000..8fc84be --- /dev/null +++ b/corpus/redirect-03.txt @@ -0,0 +1,52 @@ +new_args + max_redirect: 1 +url + http://example.com/index.html +expected + redirect +expected_url + http://example.com/index2.html +---------- +GET /index.html HTTP/1.1 +Host: example.com +Connection: close +User-Agent: HTTP-Tiny/VERSION + +---------- +HTTP/1.1 302 Found +Date: Thu, 03 Feb 1994 00:00:00 GMT +Content-Type: text/html +Content-Length: 53 +Location: http://example.com/index2.html + +redirect + +---------- +GET /index2.html HTTP/1.1 +Host: example.com +Connection: close +User-Agent: HTTP-Tiny/VERSION + +---------- +HTTP/1.1 302 Found +Date: Thu, 03 Feb 1994 00:00:00 GMT +Content-Type: text/html +Content-Length: 53 +Location: http://example.com/index3.html + +redirect + +---------- +GET /index3.html HTTP/1.1 +Host: example.com +Connection: close +User-Agent: HTTP-Tiny/VERSION + +---------- +HTTP/1.1 200 OK +Date: Thu, 03 Feb 1994 00:00:00 GMT +Content-Type: text/plain +Content-Length: 42 + +abcdefghijklmnopqrstuvwxyz1234567890abcdef + diff --git a/corpus/redirect-04.txt b/corpus/redirect-04.txt new file mode 100644 index 0000000..3c19ada --- /dev/null +++ b/corpus/redirect-04.txt @@ -0,0 +1,52 @@ +new_args + max_redirect: 2 +url + http://example.com/index.html +expected + abcdefghijklmnopqrstuvwxyz1234567890abcdef +expected_url + http://example.com/index3.html +---------- +GET /index.html HTTP/1.1 +Host: example.com +Connection: close +User-Agent: HTTP-Tiny/VERSION + +---------- +HTTP/1.1 302 Found +Date: Thu, 03 Feb 1994 00:00:00 GMT +Content-Type: text/html +Content-Length: 53 +Location: http://example.com/index2.html + +redirect + +---------- +GET /index2.html HTTP/1.1 +Host: example.com +Connection: close +User-Agent: HTTP-Tiny/VERSION + +---------- +HTTP/1.1 302 Found +Date: Thu, 03 Feb 1994 00:00:00 GMT +Content-Type: text/html +Content-Length: 53 +Location: http://example.com/index3.html + +redirect + +---------- +GET /index3.html HTTP/1.1 +Host: example.com +Connection: close +User-Agent: HTTP-Tiny/VERSION + +---------- +HTTP/1.1 200 OK +Date: Thu, 03 Feb 1994 00:00:00 GMT +Content-Type: text/plain +Content-Length: 42 + +abcdefghijklmnopqrstuvwxyz1234567890abcdef + diff --git a/corpus/redirect-05.txt b/corpus/redirect-05.txt new file mode 100644 index 0000000..5ffce18 --- /dev/null +++ b/corpus/redirect-05.txt @@ -0,0 +1,50 @@ +url + http://example.com/index.html +expected + abcdefghijklmnopqrstuvwxyz1234567890abcdef +expected_url + http://example.com/index3.html +---------- +GET /index.html HTTP/1.1 +Host: example.com +Connection: close +User-Agent: HTTP-Tiny/VERSION + +---------- +HTTP/1.1 302 Found +Date: Thu, 03 Feb 1994 00:00:00 GMT +Content-Type: text/html +Content-Length: 53 +Location: http://example.com/index2.html + +redirect + +---------- +GET /index2.html HTTP/1.1 +Host: example.com +Connection: close +User-Agent: HTTP-Tiny/VERSION + +---------- +HTTP/1.1 301 Found +Date: Thu, 03 Feb 1994 00:00:00 GMT +Content-Type: text/html +Content-Length: 53 +Location: /index3.html + +redirect + +---------- +GET /index3.html HTTP/1.1 +Host: example.com +Connection: close +User-Agent: HTTP-Tiny/VERSION + +---------- +HTTP/1.1 200 OK +Date: Thu, 03 Feb 1994 00:00:00 GMT +Content-Type: text/plain +Content-Length: 42 + +abcdefghijklmnopqrstuvwxyz1234567890abcdef + diff --git a/corpus/redirect-06.txt b/corpus/redirect-06.txt new file mode 100644 index 0000000..27e3e4a --- /dev/null +++ b/corpus/redirect-06.txt @@ -0,0 +1,35 @@ +url + http://example.com/index.html +expected + abcdefghijklmnopqrstuvwxyz1234567890abcdef +expected_url + http://example.com/index2.html +---------- +GET /index.html HTTP/1.1 +Host: example.com +Connection: close +User-Agent: HTTP-Tiny/VERSION + +---------- +HTTP/1.1 303 See Other +Date: Thu, 03 Feb 1994 00:00:00 GMT +Content-Type: text/html +Content-Length: 53 +Location: http://example.com/index2.html + +redirect + +---------- +GET /index2.html HTTP/1.1 +Host: example.com +Connection: close +User-Agent: HTTP-Tiny/VERSION + +---------- +HTTP/1.1 200 OK +Date: Thu, 03 Feb 1994 00:00:00 GMT +Content-Type: text/plain +Content-Length: 42 + +abcdefghijklmnopqrstuvwxyz1234567890abcdef + diff --git a/corpus/redirect-07.txt b/corpus/redirect-07.txt new file mode 100644 index 0000000..11b4480 --- /dev/null +++ b/corpus/redirect-07.txt @@ -0,0 +1,35 @@ +url + http://example.com/index.html +expected + abcdefghijklmnopqrstuvwxyz1234567890abcdef +expected_url + http://example.com/index2.html +---------- +GET /index.html HTTP/1.1 +Host: example.com +Connection: close +User-Agent: HTTP-Tiny/VERSION + +---------- +HTTP/1.1 307 Temporary Redirect +Date: Thu, 03 Feb 1994 00:00:00 GMT +Content-Type: text/html +Content-Length: 53 +Location: http://example.com/index2.html + +redirect + +---------- +GET /index2.html HTTP/1.1 +Host: example.com +Connection: close +User-Agent: HTTP-Tiny/VERSION + +---------- +HTTP/1.1 200 OK +Date: Thu, 03 Feb 1994 00:00:00 GMT +Content-Type: text/plain +Content-Length: 42 + +abcdefghijklmnopqrstuvwxyz1234567890abcdef + diff --git a/corpus/redirect-08.txt b/corpus/redirect-08.txt new file mode 100644 index 0000000..67a59da --- /dev/null +++ b/corpus/redirect-08.txt @@ -0,0 +1,21 @@ +url + http://example.com/index.html +expected + redirect +expected_url + http://example.com/index.html +---------- +GET /index.html HTTP/1.1 +Host: example.com +Connection: close +User-Agent: HTTP-Tiny/VERSION + +---------- +HTTP/1.1 305 Use Proxy +Date: Thu, 03 Feb 1994 00:00:00 GMT +Content-Type: text/html +Content-Length: 53 +Location: http://example.com/index2.html + +redirect + diff --git a/corpus/redirect-09.txt b/corpus/redirect-09.txt new file mode 100644 index 0000000..afb0ec2 --- /dev/null +++ b/corpus/redirect-09.txt @@ -0,0 +1,37 @@ +url + http://example.com/index.html +method + POST +expected + abcdefghijklmnopqrstuvwxyz1234567890abcdef +expected_url + http://example.com/index2.html +---------- +POST /index.html HTTP/1.1 +Host: example.com +Connection: close +User-Agent: HTTP-Tiny/VERSION + +---------- +HTTP/1.1 303 See Other +Date: Thu, 03 Feb 1994 00:00:00 GMT +Content-Type: text/html +Content-Length: 53 +Location: http://example.com/index2.html + +redirect + +---------- +GET /index2.html HTTP/1.1 +Host: example.com +Connection: close +User-Agent: HTTP-Tiny/VERSION + +---------- +HTTP/1.1 200 OK +Date: Thu, 03 Feb 1994 00:00:00 GMT +Content-Type: text/plain +Content-Length: 42 + +abcdefghijklmnopqrstuvwxyz1234567890abcdef + diff --git a/corpus/redirect-10.txt b/corpus/redirect-10.txt new file mode 100644 index 0000000..3abe585 --- /dev/null +++ b/corpus/redirect-10.txt @@ -0,0 +1,35 @@ +url + http://example.com/index.html +expected + abcdefghijklmnopqrstuvwxyz1234567890abcdef +expected_url + http://example.com/index2.html +---------- +GET /index.html HTTP/1.1 +Host: example.com +Connection: close +User-Agent: HTTP-Tiny/VERSION + +---------- +HTTP/1.1 308 Permanent Redirect +Date: Thu, 03 Feb 1994 00:00:00 GMT +Content-Type: text/html +Content-Length: 53 +Location: http://example.com/index2.html + +redirect + +---------- +GET /index2.html HTTP/1.1 +Host: example.com +Connection: close +User-Agent: HTTP-Tiny/VERSION + +---------- +HTTP/1.1 200 OK +Date: Thu, 03 Feb 1994 00:00:00 GMT +Content-Type: text/plain +Content-Length: 42 + +abcdefghijklmnopqrstuvwxyz1234567890abcdef + diff --git a/cpanfile b/cpanfile new file mode 100644 index 0000000..43df210 --- /dev/null +++ b/cpanfile @@ -0,0 +1,72 @@ +requires "Carp" => "0"; +requires "Fcntl" => "0"; +requires "IO::Socket" => "0"; +requires "MIME::Base64" => "0"; +requires "Socket" => "0"; +requires "Time::Local" => "0"; +requires "bytes" => "0"; +requires "perl" => "5.006"; +requires "strict" => "0"; +requires "warnings" => "0"; +recommends "HTTP::CookieJar" => "0.001"; +recommends "IO::Socket::IP" => "0.32"; +recommends "IO::Socket::SSL" => "1.42"; +recommends "Mozilla::CA" => "20160104"; +recommends "Net::SSLeay" => "1.49"; +suggests "IO::Socket::SSL" => "1.56"; + +on 'test' => sub { + requires "Data::Dumper" => "0"; + requires "Exporter" => "0"; + requires "ExtUtils::MakeMaker" => "0"; + requires "File::Basename" => "0"; + requires "File::Spec" => "0"; + requires "File::Temp" => "0"; + requires "IO::Dir" => "0"; + requires "IO::File" => "0"; + requires "IO::Socket::INET" => "0"; + requires "IPC::Cmd" => "0"; + requires "Test::More" => "0.96"; + requires "lib" => "0"; + requires "open" => "0"; + requires "perl" => "5.006"; +}; + +on 'test' => sub { + recommends "CPAN::Meta" => "2.120900"; +}; + +on 'configure' => sub { + requires "ExtUtils::MakeMaker" => "6.17"; + requires "perl" => "5.006"; +}; + +on 'configure' => sub { + suggests "JSON::PP" => "2.27300"; +}; + +on 'develop' => sub { + requires "Dist::Zilla" => "5"; + requires "Dist::Zilla::Plugin::Prereqs" => "0"; + requires "Dist::Zilla::Plugin::ReleaseStatus::FromVersion" => "0"; + requires "Dist::Zilla::Plugin::RemovePrereqs" => "0"; + requires "Dist::Zilla::PluginBundle::DAGOLDEN" => "0.072"; + requires "File::Spec" => "0"; + requires "File::Temp" => "0"; + requires "IO::Handle" => "0"; + requires "IPC::Open3" => "0"; + requires "Perl::Critic::Policy::Lax::ProhibitStringyEval::ExceptForRequire" => "0"; + requires "Pod::Coverage::TrustPod" => "0"; + requires "Pod::Wordlist" => "0"; + requires "Software::License::Perl_5" => "0"; + requires "Test::CPAN::Meta" => "0"; + requires "Test::MinimumVersion" => "0"; + requires "Test::More" => "0"; + requires "Test::Perl::Critic" => "0"; + requires "Test::Pod" => "1.41"; + requires "Test::Pod::Coverage" => "1.08"; + requires "Test::Portability::Files" => "0"; + requires "Test::Spelling" => "0.12"; + requires "Test::Version" => "1"; + requires "perl" => "5.006"; +}; diff --git a/dist.ini b/dist.ini new file mode 100644 index 0000000..0c3f9a4 --- /dev/null +++ b/dist.ini @@ -0,0 +1,38 @@ +name = HTTP-Tiny +author = Christian Hansen +author = David Golden +license = Perl_5 +copyright_holder = Christian Hansen + +[@DAGOLDEN] +:version = 0.072 +stopwords = CA +stopwords = CGI +stopwords = UA +stopwords = proxying + +[ReleaseStatus::FromVersion] +testing = third_decimal_odd + +[RemovePrereqs] +remove = Errno +remove = HTTP::CookieJar +remove = IO::Socket::IP +remove = IO::Socket::SSL +remove = Mozilla::CA +remove = Net::SSLeay +remove = threads + +[Prereqs / Recommends] +HTTP::CookieJar = 0.001 +IO::Socket::IP = 0.32 +IO::Socket::SSL = 1.42 +Mozilla::CA = 20160104 +Net::SSLeay = 1.49 + +[Prereqs / Suggests] +IO::Socket::SSL = 1.56 + +[Prereqs / DevelopRequires] +; Non core Perl::Critic policy used in perlcritic.rc +Perl::Critic::Policy::Lax::ProhibitStringyEval::ExceptForRequire = 0 diff --git a/eg/get.pl b/eg/get.pl new file mode 100644 index 0000000..c5cc32b --- /dev/null +++ b/eg/get.pl @@ -0,0 +1,21 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use HTTP::Tiny; + +my $url = shift(@ARGV) || 'http://example.com'; + +my $response = HTTP::Tiny->new->get($url); + +print "$response->{status} $response->{reason}\n"; + +while (my ($k, $v) = each %{$response->{headers}}) { + for (ref $v eq 'ARRAY' ? @$v : $v) { + print "$k: $_\n"; + } +} + +print $response->{content} if length $response->{content}; + diff --git a/eg/mirror.pl b/eg/mirror.pl new file mode 100644 index 0000000..a979ed5 --- /dev/null +++ b/eg/mirror.pl @@ -0,0 +1,20 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use HTTP::Tiny; + +my $url = shift(@ARGV) || 'http://example.com'; +my $file = shift(@ARGV) || 'example.output'; + +my $response = HTTP::Tiny->new->mirror($url, $file); + +print "$response->{status} $response->{reason}\n"; + +while (my ($k, $v) = each %{$response->{headers}}) { + for (ref $v eq 'ARRAY' ? @$v : $v) { + print "$k: $_\n"; + } +} + diff --git a/eg/post.pl b/eg/post.pl new file mode 100644 index 0000000..927eb67 --- /dev/null +++ b/eg/post.pl @@ -0,0 +1,29 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use HTTP::Tiny; +use URI::Escape qw/uri_escape_utf8/; + +my $url = 'http://search.cpan.org/search'; + +my %form_data = ( + query => 'DAGOLDEN', + mode => 'author', +); + +my @params; +while( my @pair = each %form_data ) { + push @params, join("=", map { uri_escape_utf8($_) } @pair); +} + +my $response = HTTP::Tiny->new->request('POST', $url, { + content => join("&", @params), + headers => { 'content-type' => 'application/x-www-form-urlencoded' } +}); + +print "$response->{status} $response->{reason}\n"; + +print $response->{content}; + diff --git a/lib/HTTP/Tiny.pm b/lib/HTTP/Tiny.pm new file mode 100644 index 0000000..2c67e78 --- /dev/null +++ b/lib/HTTP/Tiny.pm @@ -0,0 +1,2456 @@ +# vim: ts=4 sts=4 sw=4 et: +package HTTP::Tiny; +use strict; +use warnings; +# ABSTRACT: A small, simple, correct HTTP/1.1 client + +our $VERSION = '0.074'; + +sub _croak { require Carp; Carp::croak(@_) } + +#pod =method new +#pod +#pod $http = HTTP::Tiny->new( %attributes ); +#pod +#pod This constructor returns a new HTTP::Tiny object. Valid attributes include: +#pod +#pod =for :list +#pod * C — A user-agent string (defaults to 'HTTP-Tiny/$VERSION'). If +#pod C — ends in a space character, the default user-agent string is +#pod appended. +#pod * C — An instance of L — or equivalent class +#pod that supports the C and C methods +#pod * C — A hashref of default headers to apply to requests +#pod * C — The local IP address to bind to +#pod * C — Whether to reuse the last connection (if for the same +#pod scheme, host and port) (defaults to 1) +#pod * C — Maximum number of redirects allowed (defaults to 5) +#pod * C — Maximum response size in bytes (only when not using a data +#pod callback). If defined, responses larger than this will return an +#pod exception. +#pod * C — URL of a proxy server to use for HTTP connections +#pod (default is C<$ENV{http_proxy}> — if set) +#pod * C — URL of a proxy server to use for HTTPS connections +#pod (default is C<$ENV{https_proxy}> — if set) +#pod * C — URL of a generic proxy server for both HTTP and HTTPS +#pod connections (default is C<$ENV{all_proxy}> — if set) +#pod * C — List of domain suffixes that should not be proxied. Must +#pod be a comma-separated string or an array reference. (default is +#pod C<$ENV{no_proxy}> —) +#pod * C — Request timeout in seconds (default is 60) If a socket open, +#pod read or write takes longer than the timeout, an exception is thrown. +#pod * C — A boolean that indicates whether to validate the SSL +#pod certificate of an C — connection (default is false) +#pod * C — A hashref of C — options to pass through to +#pod L +#pod +#pod Passing an explicit C for C, C or C will +#pod prevent getting the corresponding proxies from the environment. +#pod +#pod Exceptions from C, C or other errors will result in a +#pod pseudo-HTTP status code of 599 and a reason of "Internal Exception". The +#pod content field in the response will contain the text of the exception. +#pod +#pod The C parameter enables a persistent connection, but only to a +#pod single destination scheme, host and port. Also, if any connection-relevant +#pod attributes are modified, or if the process ID or thread ID change, the +#pod persistent connection will be dropped. If you want persistent connections +#pod across multiple destinations, use multiple HTTP::Tiny objects. +#pod +#pod See L for more on the C and C attributes. +#pod +#pod =cut + +my @attributes; +BEGIN { + @attributes = qw( + cookie_jar default_headers http_proxy https_proxy keep_alive + local_address max_redirect max_size proxy no_proxy + SSL_options verify_SSL + ); + my %persist_ok = map {; $_ => 1 } qw( + cookie_jar default_headers max_redirect max_size + ); + no strict 'refs'; + no warnings 'uninitialized'; + for my $accessor ( @attributes ) { + *{$accessor} = sub { + @_ > 1 + ? do { + delete $_[0]->{handle} if !$persist_ok{$accessor} && $_[1] ne $_[0]->{$accessor}; + $_[0]->{$accessor} = $_[1] + } + : $_[0]->{$accessor}; + }; + } +} + +sub agent { + my($self, $agent) = @_; + if( @_ > 1 ){ + $self->{agent} = + (defined $agent && $agent =~ / $/) ? $agent . $self->_agent : $agent; + } + return $self->{agent}; +} + +sub timeout { + my ($self, $timeout) = @_; + if ( @_ > 1 ) { + $self->{timeout} = $timeout; + if ($self->{handle}) { + $self->{handle}->timeout($timeout); + } + } + return $self->{timeout}; +} + +sub new { + my($class, %args) = @_; + + my $self = { + max_redirect => 5, + timeout => defined $args{timeout} ? $args{timeout} : 60, + keep_alive => 1, + verify_SSL => $args{verify_SSL} || $args{verify_ssl} || 0, # no verification by default + no_proxy => $ENV{no_proxy}, + }; + + bless $self, $class; + + $class->_validate_cookie_jar( $args{cookie_jar} ) if $args{cookie_jar}; + + for my $key ( @attributes ) { + $self->{$key} = $args{$key} if exists $args{$key} + } + + $self->agent( exists $args{agent} ? $args{agent} : $class->_agent ); + + $self->_set_proxies; + + return $self; +} + +sub _set_proxies { + my ($self) = @_; + + # get proxies from %ENV only if not provided; explicit undef will disable + # getting proxies from the environment + + # generic proxy + if (! exists $self->{proxy} ) { + $self->{proxy} = $ENV{all_proxy} || $ENV{ALL_PROXY}; + } + + if ( defined $self->{proxy} ) { + $self->_split_proxy( 'generic proxy' => $self->{proxy} ); # validate + } + else { + delete $self->{proxy}; + } + + # http proxy + if (! exists $self->{http_proxy} ) { + # under CGI, bypass HTTP_PROXY as request sets it from Proxy header + local $ENV{HTTP_PROXY} if $ENV{REQUEST_METHOD}; + $self->{http_proxy} = $ENV{http_proxy} || $ENV{HTTP_PROXY} || $self->{proxy}; + } + + if ( defined $self->{http_proxy} ) { + $self->_split_proxy( http_proxy => $self->{http_proxy} ); # validate + $self->{_has_proxy}{http} = 1; + } + else { + delete $self->{http_proxy}; + } + + # https proxy + if (! exists $self->{https_proxy} ) { + $self->{https_proxy} = $ENV{https_proxy} || $ENV{HTTPS_PROXY} || $self->{proxy}; + } + + if ( $self->{https_proxy} ) { + $self->_split_proxy( https_proxy => $self->{https_proxy} ); # validate + $self->{_has_proxy}{https} = 1; + } + else { + delete $self->{https_proxy}; + } + + # Split no_proxy to array reference if not provided as such + unless ( ref $self->{no_proxy} eq 'ARRAY' ) { + $self->{no_proxy} = + (defined $self->{no_proxy}) ? [ split /\s*,\s*/, $self->{no_proxy} ] : []; + } + + return; +} + +#pod =method get|head|put|post|delete +#pod +#pod $response = $http->get($url); +#pod $response = $http->get($url, \%options); +#pod $response = $http->head($url); +#pod +#pod These methods are shorthand for calling C for the given method. The +#pod URL must have unsafe characters escaped and international domain names encoded. +#pod See C for valid options and a description of the response. +#pod +#pod The C field of the response will be true if the status code is 2XX. +#pod +#pod =cut + +for my $sub_name ( qw/get head put post delete/ ) { + my $req_method = uc $sub_name; + no strict 'refs'; + eval <<"HERE"; ## no critic + sub $sub_name { + my (\$self, \$url, \$args) = \@_; + \@_ == 2 || (\@_ == 3 && ref \$args eq 'HASH') + or _croak(q/Usage: \$http->$sub_name(URL, [HASHREF])/ . "\n"); + return \$self->request('$req_method', \$url, \$args || {}); + } +HERE +} + +#pod =method post_form +#pod +#pod $response = $http->post_form($url, $form_data); +#pod $response = $http->post_form($url, $form_data, \%options); +#pod +#pod This method executes a C request and sends the key/value pairs from a +#pod form data hash or array reference to the given URL with a C of +#pod C. If data is provided as an array +#pod reference, the order is preserved; if provided as a hash reference, the terms +#pod are sorted on key and value for consistency. See documentation for the +#pod C method for details on the encoding. +#pod +#pod The URL must have unsafe characters escaped and international domain names +#pod encoded. See C for valid options and a description of the response. +#pod Any C header or content in the options hashref will be ignored. +#pod +#pod The C field of the response will be true if the status code is 2XX. +#pod +#pod =cut + +sub post_form { + my ($self, $url, $data, $args) = @_; + (@_ == 3 || @_ == 4 && ref $args eq 'HASH') + or _croak(q/Usage: $http->post_form(URL, DATAREF, [HASHREF])/ . "\n"); + + my $headers = {}; + while ( my ($key, $value) = each %{$args->{headers} || {}} ) { + $headers->{lc $key} = $value; + } + delete $args->{headers}; + + return $self->request('POST', $url, { + %$args, + content => $self->www_form_urlencode($data), + headers => { + %$headers, + 'content-type' => 'application/x-www-form-urlencoded' + }, + } + ); +} + +#pod =method mirror +#pod +#pod $response = $http->mirror($url, $file, \%options) +#pod if ( $response->{success} ) { +#pod print "$file is up to date\n"; +#pod } +#pod +#pod Executes a C request for the URL and saves the response body to the file +#pod name provided. The URL must have unsafe characters escaped and international +#pod domain names encoded. If the file already exists, the request will include an +#pod C header with the modification timestamp of the file. You +#pod may specify a different C header yourself in the C<< +#pod $options->{headers} >> hash. +#pod +#pod The C field of the response will be true if the status code is 2XX +#pod or if the status code is 304 (unmodified). +#pod +#pod If the file was modified and the server response includes a properly +#pod formatted C header, the file modification time will +#pod be updated accordingly. +#pod +#pod =cut + +sub mirror { + my ($self, $url, $file, $args) = @_; + @_ == 3 || (@_ == 4 && ref $args eq 'HASH') + or _croak(q/Usage: $http->mirror(URL, FILE, [HASHREF])/ . "\n"); + + if ( exists $args->{headers} ) { + my $headers = {}; + while ( my ($key, $value) = each %{$args->{headers} || {}} ) { + $headers->{lc $key} = $value; + } + $args->{headers} = $headers; + } + + if ( -e $file and my $mtime = (stat($file))[9] ) { + $args->{headers}{'if-modified-since'} ||= $self->_http_date($mtime); + } + my $tempfile = $file . int(rand(2**31)); + + require Fcntl; + sysopen my $fh, $tempfile, Fcntl::O_CREAT()|Fcntl::O_EXCL()|Fcntl::O_WRONLY() + or _croak(qq/Error: Could not create temporary file $tempfile for downloading: $!\n/); + binmode $fh; + $args->{data_callback} = sub { print {$fh} $_[0] }; + my $response = $self->request('GET', $url, $args); + close $fh + or _croak(qq/Error: Caught error closing temporary file $tempfile: $!\n/); + + if ( $response->{success} ) { + rename $tempfile, $file + or _croak(qq/Error replacing $file with $tempfile: $!\n/); + my $lm = $response->{headers}{'last-modified'}; + if ( $lm and my $mtime = $self->_parse_http_date($lm) ) { + utime $mtime, $mtime, $file; + } + } + $response->{success} ||= $response->{status} eq '304'; + unlink $tempfile; + return $response; +} + +#pod =method request +#pod +#pod $response = $http->request($method, $url); +#pod $response = $http->request($method, $url, \%options); +#pod +#pod Executes an HTTP request of the given method type ('GET', 'HEAD', 'POST', +#pod 'PUT', etc.) on the given URL. The URL must have unsafe characters escaped and +#pod international domain names encoded. +#pod +#pod B: Method names are B per the HTTP/1.1 specification. +#pod Don't use C when you really want C. See L for +#pod how this applies to redirection. +#pod +#pod If the URL includes a "user:password" stanza, they will be used for Basic-style +#pod authorization headers. (Authorization headers will not be included in a +#pod redirected request.) For example: +#pod +#pod $http->request('GET', 'http://Aladdin:open sesame@example.com/'); +#pod +#pod If the "user:password" stanza contains reserved characters, they must +#pod be percent-escaped: +#pod +#pod $http->request('GET', 'http://john%40example.com:password@example.com/'); +#pod +#pod A hashref of options may be appended to modify the request. +#pod +#pod Valid options are: +#pod +#pod =for :list +#pod * C — +#pod A hashref containing headers to include with the request. If the value for +#pod a header is an array reference, the header will be output multiple times with +#pod each value in the array. These headers over-write any default headers. +#pod * C — +#pod A scalar to include as the body of the request OR a code reference +#pod that will be called iteratively to produce the body of the request +#pod * C — +#pod A code reference that will be called if it exists to provide a hashref +#pod of trailing headers (only used with chunked transfer-encoding) +#pod * C — +#pod A code reference that will be called for each chunks of the response +#pod body received. +#pod * C — +#pod Override host resolution and force all connections to go only to a +#pod specific peer address, regardless of the URL of the request. This will +#pod include any redirections! This options should be used with extreme +#pod caution (e.g. debugging or very special circumstances). +#pod +#pod The C header is generated from the URL in accordance with RFC 2616. It +#pod is a fatal error to specify C in the C option. Other headers +#pod may be ignored or overwritten if necessary for transport compliance. +#pod +#pod If the C option is a code reference, it will be called iteratively +#pod to provide the content body of the request. It should return the empty +#pod string or undef when the iterator is exhausted. +#pod +#pod If the C option is the empty string, no C or +#pod C headers will be generated. +#pod +#pod If the C option is provided, it will be called iteratively until +#pod the entire response body is received. The first argument will be a string +#pod containing a chunk of the response body, the second argument will be the +#pod in-progress response hash reference, as described below. (This allows +#pod customizing the action of the callback based on the C or C +#pod received prior to the content body.) +#pod +#pod The C method returns a hashref containing the response. The hashref +#pod will have the following keys: +#pod +#pod =for :list +#pod * C — +#pod Boolean indicating whether the operation returned a 2XX status code +#pod * C — +#pod URL that provided the response. This is the URL of the request unless +#pod there were redirections, in which case it is the last URL queried +#pod in a redirection chain +#pod * C — +#pod The HTTP status code of the response +#pod * C — +#pod The response phrase returned by the server +#pod * C — +#pod The body of the response. If the response does not have any content +#pod or if a data callback is provided to consume the response body, +#pod this will be the empty string +#pod * C — +#pod A hashref of header fields. All header field names will be normalized +#pod to be lower case. If a header is repeated, the value will be an arrayref; +#pod it will otherwise be a scalar string containing the value +#pod * C - +#pod If this field exists, it is the protocol of the response +#pod such as HTTP/1.0 or HTTP/1.1 +#pod * C +#pod If this field exists, it is an arrayref of response hash references from +#pod redirects in the same order that redirections occurred. If it does +#pod not exist, then no redirections occurred. +#pod +#pod On an exception during the execution of the request, the C field will +#pod contain 599, and the C field will contain the text of the exception. +#pod +#pod =cut + +my %idempotent = map { $_ => 1 } qw/GET HEAD PUT DELETE OPTIONS TRACE/; + +sub request { + my ($self, $method, $url, $args) = @_; + @_ == 3 || (@_ == 4 && ref $args eq 'HASH') + or _croak(q/Usage: $http->request(METHOD, URL, [HASHREF])/ . "\n"); + $args ||= {}; # we keep some state in this during _request + + # RFC 2616 Section 8.1.4 mandates a single retry on broken socket + my $response; + for ( 0 .. 1 ) { + $response = eval { $self->_request($method, $url, $args) }; + last unless $@ && $idempotent{$method} + && $@ =~ m{^(?:Socket closed|Unexpected end)}; + } + + if (my $e = $@) { + # maybe we got a response hash thrown from somewhere deep + if ( ref $e eq 'HASH' && exists $e->{status} ) { + $e->{redirects} = delete $args->{_redirects} if @{ $args->{_redirects} || []}; + return $e; + } + + # otherwise, stringify it + $e = "$e"; + $response = { + url => $url, + success => q{}, + status => 599, + reason => 'Internal Exception', + content => $e, + headers => { + 'content-type' => 'text/plain', + 'content-length' => length $e, + }, + ( @{$args->{_redirects} || []} ? (redirects => delete $args->{_redirects}) : () ), + }; + } + return $response; +} + +#pod =method www_form_urlencode +#pod +#pod $params = $http->www_form_urlencode( $data ); +#pod $response = $http->get("http://example.com/query?$params"); +#pod +#pod This method converts the key/value pairs from a data hash or array reference +#pod into a C string. The keys and values from the data +#pod reference will be UTF-8 encoded and escaped per RFC 3986. If a value is an +#pod array reference, the key will be repeated with each of the values of the array +#pod reference. If data is provided as a hash reference, the key/value pairs in the +#pod resulting string will be sorted by key and value for consistent ordering. +#pod +#pod =cut + +sub www_form_urlencode { + my ($self, $data) = @_; + (@_ == 2 && ref $data) + or _croak(q/Usage: $http->www_form_urlencode(DATAREF)/ . "\n"); + (ref $data eq 'HASH' || ref $data eq 'ARRAY') + or _croak("form data must be a hash or array reference\n"); + + my @params = ref $data eq 'HASH' ? %$data : @$data; + @params % 2 == 0 + or _croak("form data reference must have an even number of terms\n"); + + my @terms; + while( @params ) { + my ($key, $value) = splice(@params, 0, 2); + if ( ref $value eq 'ARRAY' ) { + unshift @params, map { $key => $_ } @$value; + } + else { + push @terms, join("=", map { $self->_uri_escape($_) } $key, $value); + } + } + + return join("&", (ref $data eq 'ARRAY') ? (@terms) : (sort @terms) ); +} + +#pod =method can_ssl +#pod +#pod $ok = HTTP::Tiny->can_ssl; +#pod ($ok, $why) = HTTP::Tiny->can_ssl; +#pod ($ok, $why) = $http->can_ssl; +#pod +#pod Indicates if SSL support is available. When called as a class object, it +#pod checks for the correct version of L and L. +#pod When called as an object methods, if C is true or if C +#pod is set in C, it checks that a CA file is available. +#pod +#pod In scalar context, returns a boolean indicating if SSL is available. +#pod In list context, returns the boolean and a (possibly multi-line) string of +#pod errors indicating why SSL isn't available. +#pod +#pod =cut + +sub can_ssl { + my ($self) = @_; + + my($ok, $reason) = (1, ''); + + # Need IO::Socket::SSL 1.42 for SSL_create_ctx_callback + local @INC = @INC; + pop @INC if $INC[-1] eq '.'; + unless (eval {require IO::Socket::SSL; IO::Socket::SSL->VERSION(1.42)}) { + $ok = 0; + $reason .= qq/IO::Socket::SSL 1.42 must be installed for https support\n/; + } + + # Need Net::SSLeay 1.49 for MODE_AUTO_RETRY + unless (eval {require Net::SSLeay; Net::SSLeay->VERSION(1.49)}) { + $ok = 0; + $reason .= qq/Net::SSLeay 1.49 must be installed for https support\n/; + } + + # If an object, check that SSL config lets us get a CA if necessary + if ( ref($self) && ( $self->{verify_SSL} || $self->{SSL_options}{SSL_verify_mode} ) ) { + my $handle = HTTP::Tiny::Handle->new( + SSL_options => $self->{SSL_options}, + verify_SSL => $self->{verify_SSL}, + ); + unless ( eval { $handle->_find_CA_file; 1 } ) { + $ok = 0; + $reason .= "$@"; + } + } + + wantarray ? ($ok, $reason) : $ok; +} + +#pod =method connected +#pod +#pod $host = $http->connected; +#pod ($host, $port) = $http->connected; +#pod +#pod Indicates if a connection to a peer is being kept alive, per the C +#pod option. +#pod +#pod In scalar context, returns the peer host and port, joined with a colon, or +#pod C (if no peer is connected). +#pod In list context, returns the peer host and port or an empty list (if no peer +#pod is connected). +#pod +#pod B: This method cannot reliably be used to discover whether the remote +#pod host has closed its end of the socket. +#pod +#pod =cut + +sub connected { + my ($self) = @_; + + # If a socket exists... + if ($self->{handle} && $self->{handle}{fh}) { + my $socket = $self->{handle}{fh}; + + # ...and is connected, return the peer host and port. + if ($socket->connected) { + return wantarray + ? ($socket->peerhost, $socket->peerport) + : join(':', $socket->peerhost, $socket->peerport); + } + } + return; +} + +#--------------------------------------------------------------------------# +# private methods +#--------------------------------------------------------------------------# + +my %DefaultPort = ( + http => 80, + https => 443, +); + +sub _agent { + my $class = ref($_[0]) || $_[0]; + (my $default_agent = $class) =~ s{::}{-}g; + return $default_agent . "/" . $class->VERSION; +} + +sub _request { + my ($self, $method, $url, $args) = @_; + + my ($scheme, $host, $port, $path_query, $auth) = $self->_split_url($url); + + my $request = { + method => $method, + scheme => $scheme, + host => $host, + port => $port, + host_port => ($port == $DefaultPort{$scheme} ? $host : "$host:$port"), + uri => $path_query, + headers => {}, + }; + + my $peer = $args->{peer} || $host; + + # We remove the cached handle so it is not reused in the case of redirect. + # If all is well, it will be recached at the end of _request. We only + # reuse for the same scheme, host and port + my $handle = delete $self->{handle}; + if ( $handle ) { + unless ( $handle->can_reuse( $scheme, $host, $port, $peer ) ) { + $handle->close; + undef $handle; + } + } + $handle ||= $self->_open_handle( $request, $scheme, $host, $port, $peer ); + + $self->_prepare_headers_and_cb($request, $args, $url, $auth); + $handle->write_request($request); + + my $response; + do { $response = $handle->read_response_header } + until (substr($response->{status},0,1) ne '1'); + + $self->_update_cookie_jar( $url, $response ) if $self->{cookie_jar}; + my @redir_args = $self->_maybe_redirect($request, $response, $args); + + my $known_message_length; + if ($method eq 'HEAD' || $response->{status} =~ /^[23]04/) { + # response has no message body + $known_message_length = 1; + } + else { + # Ignore any data callbacks during redirection. + my $cb_args = @redir_args ? +{} : $args; + my $data_cb = $self->_prepare_data_cb($response, $cb_args); + $known_message_length = $handle->read_body($data_cb, $response); + } + + if ( $self->{keep_alive} + && $known_message_length + && $response->{protocol} eq 'HTTP/1.1' + && ($response->{headers}{connection} || '') ne 'close' + ) { + $self->{handle} = $handle; + } + else { + $handle->close; + } + + $response->{success} = substr( $response->{status}, 0, 1 ) eq '2'; + $response->{url} = $url; + + # Push the current response onto the stack of redirects if redirecting. + if (@redir_args) { + push @{$args->{_redirects}}, $response; + return $self->_request(@redir_args, $args); + } + + # Copy the stack of redirects into the response before returning. + $response->{redirects} = delete $args->{_redirects} + if @{$args->{_redirects}}; + return $response; +} + +sub _open_handle { + my ($self, $request, $scheme, $host, $port, $peer) = @_; + + my $handle = HTTP::Tiny::Handle->new( + timeout => $self->{timeout}, + SSL_options => $self->{SSL_options}, + verify_SSL => $self->{verify_SSL}, + local_address => $self->{local_address}, + keep_alive => $self->{keep_alive} + ); + + if ($self->{_has_proxy}{$scheme} && ! grep { $host =~ /\Q$_\E$/ } @{$self->{no_proxy}}) { + return $self->_proxy_connect( $request, $handle ); + } + else { + return $handle->connect($scheme, $host, $port, $peer); + } +} + +sub _proxy_connect { + my ($self, $request, $handle) = @_; + + my @proxy_vars; + if ( $request->{scheme} eq 'https' ) { + _croak(qq{No https_proxy defined}) unless $self->{https_proxy}; + @proxy_vars = $self->_split_proxy( https_proxy => $self->{https_proxy} ); + if ( $proxy_vars[0] eq 'https' ) { + _croak(qq{Can't proxy https over https: $request->{uri} via $self->{https_proxy}}); + } + } + else { + _croak(qq{No http_proxy defined}) unless $self->{http_proxy}; + @proxy_vars = $self->_split_proxy( http_proxy => $self->{http_proxy} ); + } + + my ($p_scheme, $p_host, $p_port, $p_auth) = @proxy_vars; + + if ( length $p_auth && ! defined $request->{headers}{'proxy-authorization'} ) { + $self->_add_basic_auth_header( $request, 'proxy-authorization' => $p_auth ); + } + + $handle->connect($p_scheme, $p_host, $p_port, $p_host); + + if ($request->{scheme} eq 'https') { + $self->_create_proxy_tunnel( $request, $handle ); + } + else { + # non-tunneled proxy requires absolute URI + $request->{uri} = "$request->{scheme}://$request->{host_port}$request->{uri}"; + } + + return $handle; +} + +sub _split_proxy { + my ($self, $type, $proxy) = @_; + + my ($scheme, $host, $port, $path_query, $auth) = eval { $self->_split_url($proxy) }; + + unless( + defined($scheme) && length($scheme) && length($host) && length($port) + && $path_query eq '/' + ) { + _croak(qq{$type URL must be in format http[s]://[auth@]:/\n}); + } + + return ($scheme, $host, $port, $auth); +} + +sub _create_proxy_tunnel { + my ($self, $request, $handle) = @_; + + $handle->_assert_ssl; + + my $agent = exists($request->{headers}{'user-agent'}) + ? $request->{headers}{'user-agent'} : $self->{agent}; + + my $connect_request = { + method => 'CONNECT', + uri => "$request->{host}:$request->{port}", + headers => { + host => "$request->{host}:$request->{port}", + 'user-agent' => $agent, + } + }; + + if ( $request->{headers}{'proxy-authorization'} ) { + $connect_request->{headers}{'proxy-authorization'} = + delete $request->{headers}{'proxy-authorization'}; + } + + $handle->write_request($connect_request); + my $response; + do { $response = $handle->read_response_header } + until (substr($response->{status},0,1) ne '1'); + + # if CONNECT failed, throw the response so it will be + # returned from the original request() method; + unless (substr($response->{status},0,1) eq '2') { + die $response; + } + + # tunnel established, so start SSL handshake + $handle->start_ssl( $request->{host} ); + + return; +} + +sub _prepare_headers_and_cb { + my ($self, $request, $args, $url, $auth) = @_; + + for ($self->{default_headers}, $args->{headers}) { + next unless defined; + while (my ($k, $v) = each %$_) { + $request->{headers}{lc $k} = $v; + $request->{header_case}{lc $k} = $k; + } + } + + if (exists $request->{headers}{'host'}) { + die(qq/The 'Host' header must not be provided as header option\n/); + } + + $request->{headers}{'host'} = $request->{host_port}; + $request->{headers}{'user-agent'} ||= $self->{agent}; + $request->{headers}{'connection'} = "close" + unless $self->{keep_alive}; + + if ( defined $args->{content} ) { + if (ref $args->{content} eq 'CODE') { + $request->{headers}{'content-type'} ||= "application/octet-stream"; + $request->{headers}{'transfer-encoding'} = 'chunked' + unless $request->{headers}{'content-length'} + || $request->{headers}{'transfer-encoding'}; + $request->{cb} = $args->{content}; + } + elsif ( length $args->{content} ) { + my $content = $args->{content}; + if ( $] ge '5.008' ) { + utf8::downgrade($content, 1) + or die(qq/Wide character in request message body\n/); + } + $request->{headers}{'content-type'} ||= "application/octet-stream"; + $request->{headers}{'content-length'} = length $content + unless $request->{headers}{'content-length'} + || $request->{headers}{'transfer-encoding'}; + $request->{cb} = sub { substr $content, 0, length $content, '' }; + } + $request->{trailer_cb} = $args->{trailer_callback} + if ref $args->{trailer_callback} eq 'CODE'; + } + + ### If we have a cookie jar, then maybe add relevant cookies + if ( $self->{cookie_jar} ) { + my $cookies = $self->cookie_jar->cookie_header( $url ); + $request->{headers}{cookie} = $cookies if length $cookies; + } + + # if we have Basic auth parameters, add them + if ( length $auth && ! defined $request->{headers}{authorization} ) { + $self->_add_basic_auth_header( $request, 'authorization' => $auth ); + } + + return; +} + +sub _add_basic_auth_header { + my ($self, $request, $header, $auth) = @_; + require MIME::Base64; + $request->{headers}{$header} = + "Basic " . MIME::Base64::encode_base64($auth, ""); + return; +} + +sub _prepare_data_cb { + my ($self, $response, $args) = @_; + my $data_cb = $args->{data_callback}; + $response->{content} = ''; + + if (!$data_cb || $response->{status} !~ /^2/) { + if (defined $self->{max_size}) { + $data_cb = sub { + $_[1]->{content} .= $_[0]; + die(qq/Size of response body exceeds the maximum allowed of $self->{max_size}\n/) + if length $_[1]->{content} > $self->{max_size}; + }; + } + else { + $data_cb = sub { $_[1]->{content} .= $_[0] }; + } + } + return $data_cb; +} + +sub _update_cookie_jar { + my ($self, $url, $response) = @_; + + my $cookies = $response->{headers}->{'set-cookie'}; + return unless defined $cookies; + + my @cookies = ref $cookies ? @$cookies : $cookies; + + $self->cookie_jar->add( $url, $_ ) for @cookies; + + return; +} + +sub _validate_cookie_jar { + my ($class, $jar) = @_; + + # duck typing + for my $method ( qw/add cookie_header/ ) { + _croak(qq/Cookie jar must provide the '$method' method\n/) + unless ref($jar) && ref($jar)->can($method); + } + + return; +} + +sub _maybe_redirect { + my ($self, $request, $response, $args) = @_; + my $headers = $response->{headers}; + my ($status, $method) = ($response->{status}, $request->{method}); + $args->{_redirects} ||= []; + + if (($status eq '303' or ($status =~ /^30[1278]/ && $method =~ /^GET|HEAD$/)) + and $headers->{location} + and @{$args->{_redirects}} < $self->{max_redirect} + ) { + my $location = ($headers->{location} =~ /^\//) + ? "$request->{scheme}://$request->{host_port}$headers->{location}" + : $headers->{location} ; + return (($status eq '303' ? 'GET' : $method), $location); + } + return; +} + +sub _split_url { + my $url = pop; + + # URI regex adapted from the URI module + my ($scheme, $host, $path_query) = $url =~ m<\A([^:/?#]+)://([^/?#]*)([^#]*)> + or die(qq/Cannot parse URL: '$url'\n/); + + $scheme = lc $scheme; + $path_query = "/$path_query" unless $path_query =~ m<\A/>; + + my $auth = ''; + if ( (my $i = index $host, '@') != -1 ) { + # user:pass@host + $auth = substr $host, 0, $i, ''; # take up to the @ for auth + substr $host, 0, 1, ''; # knock the @ off the host + + # userinfo might be percent escaped, so recover real auth info + $auth =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg; + } + my $port = $host =~ s/:(\d*)\z// && length $1 ? $1 + : $scheme eq 'http' ? 80 + : $scheme eq 'https' ? 443 + : undef; + + return ($scheme, (length $host ? lc $host : "localhost") , $port, $path_query, $auth); +} + +# Date conversions adapted from HTTP::Date +my $DoW = "Sun|Mon|Tue|Wed|Thu|Fri|Sat"; +my $MoY = "Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec"; +sub _http_date { + my ($sec, $min, $hour, $mday, $mon, $year, $wday) = gmtime($_[1]); + return sprintf("%s, %02d %s %04d %02d:%02d:%02d GMT", + substr($DoW,$wday*4,3), + $mday, substr($MoY,$mon*4,3), $year+1900, + $hour, $min, $sec + ); +} + +sub _parse_http_date { + my ($self, $str) = @_; + require Time::Local; + my @tl_parts; + if ($str =~ /^[SMTWF][a-z]+, +(\d{1,2}) ($MoY) +(\d\d\d\d) +(\d\d):(\d\d):(\d\d) +GMT$/) { + @tl_parts = ($6, $5, $4, $1, (index($MoY,$2)/4), $3); + } + elsif ($str =~ /^[SMTWF][a-z]+, +(\d\d)-($MoY)-(\d{2,4}) +(\d\d):(\d\d):(\d\d) +GMT$/ ) { + @tl_parts = ($6, $5, $4, $1, (index($MoY,$2)/4), $3); + } + elsif ($str =~ /^[SMTWF][a-z]+ +($MoY) +(\d{1,2}) +(\d\d):(\d\d):(\d\d) +(?:[^0-9]+ +)?(\d\d\d\d)$/ ) { + @tl_parts = ($5, $4, $3, $2, (index($MoY,$1)/4), $6); + } + return eval { + my $t = @tl_parts ? Time::Local::timegm(@tl_parts) : -1; + $t < 0 ? undef : $t; + }; +} + +# URI escaping adapted from URI::Escape +# c.f. http://www.w3.org/TR/html4/interact/forms.html#h-17.13.4.1 +# perl 5.6 ready UTF-8 encoding adapted from JSON::PP +my %escapes = map { chr($_) => sprintf("%%%02X", $_) } 0..255; +$escapes{' '}="+"; +my $unsafe_char = qr/[^A-Za-z0-9\-\._~]/; + +sub _uri_escape { + my ($self, $str) = @_; + if ( $] ge '5.008' ) { + utf8::encode($str); + } + else { + $str = pack("U*", unpack("C*", $str)) # UTF-8 encode a byte string + if ( length $str == do { use bytes; length $str } ); + $str = pack("C*", unpack("C*", $str)); # clear UTF-8 flag + } + $str =~ s/($unsafe_char)/$escapes{$1}/g; + return $str; +} + +package + HTTP::Tiny::Handle; # hide from PAUSE/indexers +use strict; +use warnings; + +use Errno qw[EINTR EPIPE]; +use IO::Socket qw[SOCK_STREAM]; +use Socket qw[SOL_SOCKET SO_KEEPALIVE]; + +# PERL_HTTP_TINY_IPV4_ONLY is a private environment variable to force old +# behavior if someone is unable to boostrap CPAN from a new perl install; it is +# not intended for general, per-client use and may be removed in the future +my $SOCKET_CLASS = + $ENV{PERL_HTTP_TINY_IPV4_ONLY} ? 'IO::Socket::INET' : + eval { require IO::Socket::IP; IO::Socket::IP->VERSION(0.25) } ? 'IO::Socket::IP' : + 'IO::Socket::INET'; + +sub BUFSIZE () { 32768 } ## no critic + +my $Printable = sub { + local $_ = shift; + s/\r/\\r/g; + s/\n/\\n/g; + s/\t/\\t/g; + s/([^\x20-\x7E])/sprintf('\\x%.2X', ord($1))/ge; + $_; +}; + +my $Token = qr/[\x21\x23-\x27\x2A\x2B\x2D\x2E\x30-\x39\x41-\x5A\x5E-\x7A\x7C\x7E]/; +my $Field_Content = qr/[[:print:]]+ (?: [\x20\x09]+ [[:print:]]+ )*/x; + +sub new { + my ($class, %args) = @_; + return bless { + rbuf => '', + timeout => 60, + max_line_size => 16384, + max_header_lines => 64, + verify_SSL => 0, + SSL_options => {}, + %args + }, $class; +} + +sub timeout { + my ($self, $timeout) = @_; + if ( @_ > 1 ) { + $self->{timeout} = $timeout; + if ( $self->{fh} && $self->{fh}->can('timeout') ) { + $self->{fh}->timeout($timeout); + } + } + return $self->{timeout}; +} + +sub connect { + @_ == 5 || die(q/Usage: $handle->connect(scheme, host, port, peer)/ . "\n"); + my ($self, $scheme, $host, $port, $peer) = @_; + + if ( $scheme eq 'https' ) { + $self->_assert_ssl; + } + elsif ( $scheme ne 'http' ) { + die(qq/Unsupported URL scheme '$scheme'\n/); + } + $self->{fh} = $SOCKET_CLASS->new( + PeerHost => $peer, + PeerPort => $port, + $self->{local_address} ? + ( LocalAddr => $self->{local_address} ) : (), + Proto => 'tcp', + Type => SOCK_STREAM, + Timeout => $self->{timeout}, + ) or die(qq/Could not connect to '$host:$port': $@\n/); + + binmode($self->{fh}) + or die(qq/Could not binmode() socket: '$!'\n/); + + if ( $self->{keep_alive} ) { + unless ( defined( $self->{fh}->setsockopt( SOL_SOCKET, SO_KEEPALIVE, 1 ) ) ) { + CORE::close($self->{fh}); + die(qq/Could not set SO_KEEPALIVE on socket: '$!'\n/); + } + } + + $self->start_ssl($host) if $scheme eq 'https'; + + $self->{scheme} = $scheme; + $self->{host} = $host; + $self->{peer} = $peer; + $self->{port} = $port; + $self->{pid} = $$; + $self->{tid} = _get_tid(); + + return $self; +} + +sub start_ssl { + my ($self, $host) = @_; + + # As this might be used via CONNECT after an SSL session + # to a proxy, we shut down any existing SSL before attempting + # the handshake + if ( ref($self->{fh}) eq 'IO::Socket::SSL' ) { + unless ( $self->{fh}->stop_SSL ) { + my $ssl_err = IO::Socket::SSL->errstr; + die(qq/Error halting prior SSL connection: $ssl_err/); + } + } + + my $ssl_args = $self->_ssl_args($host); + IO::Socket::SSL->start_SSL( + $self->{fh}, + %$ssl_args, + SSL_create_ctx_callback => sub { + my $ctx = shift; + Net::SSLeay::CTX_set_mode($ctx, Net::SSLeay::MODE_AUTO_RETRY()); + }, + ); + + unless ( ref($self->{fh}) eq 'IO::Socket::SSL' ) { + my $ssl_err = IO::Socket::SSL->errstr; + die(qq/SSL connection failed for $host: $ssl_err\n/); + } +} + +sub close { + @_ == 1 || die(q/Usage: $handle->close()/ . "\n"); + my ($self) = @_; + CORE::close($self->{fh}) + or die(qq/Could not close socket: '$!'\n/); +} + +sub write { + @_ == 2 || die(q/Usage: $handle->write(buf)/ . "\n"); + my ($self, $buf) = @_; + + if ( $] ge '5.008' ) { + utf8::downgrade($buf, 1) + or die(qq/Wide character in write()\n/); + } + + my $len = length $buf; + my $off = 0; + + local $SIG{PIPE} = 'IGNORE'; + + while () { + $self->can_write + or die(qq/Timed out while waiting for socket to become ready for writing\n/); + my $r = syswrite($self->{fh}, $buf, $len, $off); + if (defined $r) { + $len -= $r; + $off += $r; + last unless $len > 0; + } + elsif ($! == EPIPE) { + die(qq/Socket closed by remote server: $!\n/); + } + elsif ($! != EINTR) { + if ($self->{fh}->can('errstr')){ + my $err = $self->{fh}->errstr(); + die (qq/Could not write to SSL socket: '$err'\n /); + } + else { + die(qq/Could not write to socket: '$!'\n/); + } + + } + } + return $off; +} + +sub read { + @_ == 2 || @_ == 3 || die(q/Usage: $handle->read(len [, allow_partial])/ . "\n"); + my ($self, $len, $allow_partial) = @_; + + my $buf = ''; + my $got = length $self->{rbuf}; + + if ($got) { + my $take = ($got < $len) ? $got : $len; + $buf = substr($self->{rbuf}, 0, $take, ''); + $len -= $take; + } + + while ($len > 0) { + $self->can_read + or die(q/Timed out while waiting for socket to become ready for reading/ . "\n"); + my $r = sysread($self->{fh}, $buf, $len, length $buf); + if (defined $r) { + last unless $r; + $len -= $r; + } + elsif ($! != EINTR) { + if ($self->{fh}->can('errstr')){ + my $err = $self->{fh}->errstr(); + die (qq/Could not read from SSL socket: '$err'\n /); + } + else { + die(qq/Could not read from socket: '$!'\n/); + } + } + } + if ($len && !$allow_partial) { + die(qq/Unexpected end of stream\n/); + } + return $buf; +} + +sub readline { + @_ == 1 || die(q/Usage: $handle->readline()/ . "\n"); + my ($self) = @_; + + while () { + if ($self->{rbuf} =~ s/\A ([^\x0D\x0A]* \x0D?\x0A)//x) { + return $1; + } + if (length $self->{rbuf} >= $self->{max_line_size}) { + die(qq/Line size exceeds the maximum allowed size of $self->{max_line_size}\n/); + } + $self->can_read + or die(qq/Timed out while waiting for socket to become ready for reading\n/); + my $r = sysread($self->{fh}, $self->{rbuf}, BUFSIZE, length $self->{rbuf}); + if (defined $r) { + last unless $r; + } + elsif ($! != EINTR) { + if ($self->{fh}->can('errstr')){ + my $err = $self->{fh}->errstr(); + die (qq/Could not read from SSL socket: '$err'\n /); + } + else { + die(qq/Could not read from socket: '$!'\n/); + } + } + } + die(qq/Unexpected end of stream while looking for line\n/); +} + +sub read_header_lines { + @_ == 1 || @_ == 2 || die(q/Usage: $handle->read_header_lines([headers])/ . "\n"); + my ($self, $headers) = @_; + $headers ||= {}; + my $lines = 0; + my $val; + + while () { + my $line = $self->readline; + + if (++$lines >= $self->{max_header_lines}) { + die(qq/Header lines exceeds maximum number allowed of $self->{max_header_lines}\n/); + } + elsif ($line =~ /\A ([^\x00-\x1F\x7F:]+) : [\x09\x20]* ([^\x0D\x0A]*)/x) { + my ($field_name) = lc $1; + if (exists $headers->{$field_name}) { + for ($headers->{$field_name}) { + $_ = [$_] unless ref $_ eq "ARRAY"; + push @$_, $2; + $val = \$_->[-1]; + } + } + else { + $val = \($headers->{$field_name} = $2); + } + } + elsif ($line =~ /\A [\x09\x20]+ ([^\x0D\x0A]*)/x) { + $val + or die(qq/Unexpected header continuation line\n/); + next unless length $1; + $$val .= ' ' if length $$val; + $$val .= $1; + } + elsif ($line =~ /\A \x0D?\x0A \z/x) { + last; + } + else { + die(q/Malformed header line: / . $Printable->($line) . "\n"); + } + } + return $headers; +} + +sub write_request { + @_ == 2 || die(q/Usage: $handle->write_request(request)/ . "\n"); + my($self, $request) = @_; + $self->write_request_header(@{$request}{qw/method uri headers header_case/}); + $self->write_body($request) if $request->{cb}; + return; +} + +# Standard request header names/case from HTTP/1.1 RFCs +my @rfc_request_headers = qw( + Accept Accept-Charset Accept-Encoding Accept-Language Authorization + Cache-Control Connection Content-Length Expect From Host + If-Match If-Modified-Since If-None-Match If-Range If-Unmodified-Since + Max-Forwards Pragma Proxy-Authorization Range Referer TE Trailer + Transfer-Encoding Upgrade User-Agent Via +); + +my @other_request_headers = qw( + Content-Encoding Content-MD5 Content-Type Cookie DNT Date Origin + X-XSS-Protection +); + +my %HeaderCase = map { lc($_) => $_ } @rfc_request_headers, @other_request_headers; + +# to avoid multiple small writes and hence nagle, you can pass the method line or anything else to +# combine writes. +sub write_header_lines { + (@_ >= 2 && @_ <= 4 && ref $_[1] eq 'HASH') || die(q/Usage: $handle->write_header_lines(headers, [header_case, prefix])/ . "\n"); + my($self, $headers, $header_case, $prefix_data) = @_; + $header_case ||= {}; + + my $buf = (defined $prefix_data ? $prefix_data : ''); + + # Per RFC, control fields should be listed first + my %seen; + for my $k ( qw/host cache-control expect max-forwards pragma range te/ ) { + next unless exists $headers->{$k}; + $seen{$k}++; + my $field_name = $HeaderCase{$k}; + my $v = $headers->{$k}; + for (ref $v eq 'ARRAY' ? @$v : $v) { + $_ = '' unless defined $_; + $buf .= "$field_name: $_\x0D\x0A"; + } + } + + # Other headers sent in arbitrary order + while (my ($k, $v) = each %$headers) { + my $field_name = lc $k; + next if $seen{$field_name}; + if (exists $HeaderCase{$field_name}) { + $field_name = $HeaderCase{$field_name}; + } + else { + if (exists $header_case->{$field_name}) { + $field_name = $header_case->{$field_name}; + } + else { + $field_name =~ s/\b(\w)/\u$1/g; + } + $field_name =~ /\A $Token+ \z/xo + or die(q/Invalid HTTP header field name: / . $Printable->($field_name) . "\n"); + $HeaderCase{lc $field_name} = $field_name; + } + for (ref $v eq 'ARRAY' ? @$v : $v) { + # unwrap a field value if pre-wrapped by user + s/\x0D?\x0A\s+/ /g; + die(qq/Invalid HTTP header field value ($field_name): / . $Printable->($_). "\n") + unless $_ eq '' || /\A $Field_Content \z/xo; + $_ = '' unless defined $_; + $buf .= "$field_name: $_\x0D\x0A"; + } + } + $buf .= "\x0D\x0A"; + return $self->write($buf); +} + +# return value indicates whether message length was defined; this is generally +# true unless there was no content-length header and we just read until EOF. +# Other message length errors are thrown as exceptions +sub read_body { + @_ == 3 || die(q/Usage: $handle->read_body(callback, response)/ . "\n"); + my ($self, $cb, $response) = @_; + my $te = $response->{headers}{'transfer-encoding'} || ''; + my $chunked = grep { /chunked/i } ( ref $te eq 'ARRAY' ? @$te : $te ) ; + return $chunked + ? $self->read_chunked_body($cb, $response) + : $self->read_content_body($cb, $response); +} + +sub write_body { + @_ == 2 || die(q/Usage: $handle->write_body(request)/ . "\n"); + my ($self, $request) = @_; + if ($request->{headers}{'content-length'}) { + return $self->write_content_body($request); + } + else { + return $self->write_chunked_body($request); + } +} + +sub read_content_body { + @_ == 3 || @_ == 4 || die(q/Usage: $handle->read_content_body(callback, response, [read_length])/ . "\n"); + my ($self, $cb, $response, $content_length) = @_; + $content_length ||= $response->{headers}{'content-length'}; + + if ( defined $content_length ) { + my $len = $content_length; + while ($len > 0) { + my $read = ($len > BUFSIZE) ? BUFSIZE : $len; + $cb->($self->read($read, 0), $response); + $len -= $read; + } + return length($self->{rbuf}) == 0; + } + + my $chunk; + $cb->($chunk, $response) while length( $chunk = $self->read(BUFSIZE, 1) ); + + return; +} + +sub write_content_body { + @_ == 2 || die(q/Usage: $handle->write_content_body(request)/ . "\n"); + my ($self, $request) = @_; + + my ($len, $content_length) = (0, $request->{headers}{'content-length'}); + while () { + my $data = $request->{cb}->(); + + defined $data && length $data + or last; + + if ( $] ge '5.008' ) { + utf8::downgrade($data, 1) + or die(qq/Wide character in write_content()\n/); + } + + $len += $self->write($data); + } + + $len == $content_length + or die(qq/Content-Length mismatch (got: $len expected: $content_length)\n/); + + return $len; +} + +sub read_chunked_body { + @_ == 3 || die(q/Usage: $handle->read_chunked_body(callback, $response)/ . "\n"); + my ($self, $cb, $response) = @_; + + while () { + my $head = $self->readline; + + $head =~ /\A ([A-Fa-f0-9]+)/x + or die(q/Malformed chunk head: / . $Printable->($head) . "\n"); + + my $len = hex($1) + or last; + + $self->read_content_body($cb, $response, $len); + + $self->read(2) eq "\x0D\x0A" + or die(qq/Malformed chunk: missing CRLF after chunk data\n/); + } + $self->read_header_lines($response->{headers}); + return 1; +} + +sub write_chunked_body { + @_ == 2 || die(q/Usage: $handle->write_chunked_body(request)/ . "\n"); + my ($self, $request) = @_; + + my $len = 0; + while () { + my $data = $request->{cb}->(); + + defined $data && length $data + or last; + + if ( $] ge '5.008' ) { + utf8::downgrade($data, 1) + or die(qq/Wide character in write_chunked_body()\n/); + } + + $len += length $data; + + my $chunk = sprintf '%X', length $data; + $chunk .= "\x0D\x0A"; + $chunk .= $data; + $chunk .= "\x0D\x0A"; + + $self->write($chunk); + } + $self->write("0\x0D\x0A"); + if ( ref $request->{trailer_cb} eq 'CODE' ) { + $self->write_header_lines($request->{trailer_cb}->()) + } + else { + $self->write("\x0D\x0A"); + } + return $len; +} + +sub read_response_header { + @_ == 1 || die(q/Usage: $handle->read_response_header()/ . "\n"); + my ($self) = @_; + + my $line = $self->readline; + + $line =~ /\A (HTTP\/(0*\d+\.0*\d+)) [\x09\x20]+ ([0-9]{3}) [\x09\x20]+ ([^\x0D\x0A]*) \x0D?\x0A/x + or die(q/Malformed Status-Line: / . $Printable->($line). "\n"); + + my ($protocol, $version, $status, $reason) = ($1, $2, $3, $4); + + die (qq/Unsupported HTTP protocol: $protocol\n/) + unless $version =~ /0*1\.0*[01]/; + + return { + status => $status, + reason => $reason, + headers => $self->read_header_lines, + protocol => $protocol, + }; +} + +sub write_request_header { + @_ == 5 || die(q/Usage: $handle->write_request_header(method, request_uri, headers, header_case)/ . "\n"); + my ($self, $method, $request_uri, $headers, $header_case) = @_; + + return $self->write_header_lines($headers, $header_case, "$method $request_uri HTTP/1.1\x0D\x0A"); +} + +sub _do_timeout { + my ($self, $type, $timeout) = @_; + $timeout = $self->{timeout} + unless defined $timeout && $timeout >= 0; + + my $fd = fileno $self->{fh}; + defined $fd && $fd >= 0 + or die(qq/select(2): 'Bad file descriptor'\n/); + + my $initial = time; + my $pending = $timeout; + my $nfound; + + vec(my $fdset = '', $fd, 1) = 1; + + while () { + $nfound = ($type eq 'read') + ? select($fdset, undef, undef, $pending) + : select(undef, $fdset, undef, $pending) ; + if ($nfound == -1) { + $! == EINTR + or die(qq/select(2): '$!'\n/); + redo if !$timeout || ($pending = $timeout - (time - $initial)) > 0; + $nfound = 0; + } + last; + } + $! = 0; + return $nfound; +} + +sub can_read { + @_ == 1 || @_ == 2 || die(q/Usage: $handle->can_read([timeout])/ . "\n"); + my $self = shift; + if ( ref($self->{fh}) eq 'IO::Socket::SSL' ) { + return 1 if $self->{fh}->pending; + } + return $self->_do_timeout('read', @_) +} + +sub can_write { + @_ == 1 || @_ == 2 || die(q/Usage: $handle->can_write([timeout])/ . "\n"); + my $self = shift; + return $self->_do_timeout('write', @_) +} + +sub _assert_ssl { + my($ok, $reason) = HTTP::Tiny->can_ssl(); + die $reason unless $ok; +} + +sub can_reuse { + my ($self,$scheme,$host,$port,$peer) = @_; + return 0 if + $self->{pid} != $$ + || $self->{tid} != _get_tid() + || length($self->{rbuf}) + || $scheme ne $self->{scheme} + || $host ne $self->{host} + || $port ne $self->{port} + || $peer ne $self->{peer} + || eval { $self->can_read(0) } + || $@ ; + return 1; +} + +# Try to find a CA bundle to validate the SSL cert, +# prefer Mozilla::CA or fallback to a system file +sub _find_CA_file { + my $self = shift(); + + my $ca_file = + defined( $self->{SSL_options}->{SSL_ca_file} ) + ? $self->{SSL_options}->{SSL_ca_file} + : $ENV{SSL_CERT_FILE}; + + if ( defined $ca_file ) { + unless ( -r $ca_file ) { + die qq/SSL_ca_file '$ca_file' not found or not readable\n/; + } + return $ca_file; + } + + local @INC = @INC; + pop @INC if $INC[-1] eq '.'; + return Mozilla::CA::SSL_ca_file() + if eval { require Mozilla::CA; 1 }; + + # cert list copied from golang src/crypto/x509/root_unix.go + foreach my $ca_bundle ( + "/etc/ssl/certs/ca-certificates.crt", # Debian/Ubuntu/Gentoo etc. + "/etc/pki/tls/certs/ca-bundle.crt", # Fedora/RHEL + "/etc/ssl/ca-bundle.pem", # OpenSUSE + "/etc/openssl/certs/ca-certificates.crt", # NetBSD + "/etc/ssl/cert.pem", # OpenBSD + "/usr/local/share/certs/ca-root-nss.crt", # FreeBSD/DragonFly + "/etc/pki/tls/cacert.pem", # OpenELEC + "/etc/certs/ca-certificates.crt", # Solaris 11.2+ + ) { + return $ca_bundle if -e $ca_bundle; + } + + die qq/Couldn't find a CA bundle with which to verify the SSL certificate.\n/ + . qq/Try installing Mozilla::CA from CPAN\n/; +} + +# for thread safety, we need to know thread id if threads are loaded +sub _get_tid { + no warnings 'reserved'; # for 'threads' + return threads->can("tid") ? threads->tid : 0; +} + +sub _ssl_args { + my ($self, $host) = @_; + + my %ssl_args; + + # This test reimplements IO::Socket::SSL::can_client_sni(), which wasn't + # added until IO::Socket::SSL 1.84 + if ( Net::SSLeay::OPENSSL_VERSION_NUMBER() >= 0x01000000 ) { + $ssl_args{SSL_hostname} = $host, # Sane SNI support + } + + if ($self->{verify_SSL}) { + $ssl_args{SSL_verifycn_scheme} = 'http'; # enable CN validation + $ssl_args{SSL_verifycn_name} = $host; # set validation hostname + $ssl_args{SSL_verify_mode} = 0x01; # enable cert validation + $ssl_args{SSL_ca_file} = $self->_find_CA_file; + } + else { + $ssl_args{SSL_verifycn_scheme} = 'none'; # disable CN validation + $ssl_args{SSL_verify_mode} = 0x00; # disable cert validation + } + + # user options override settings from verify_SSL + for my $k ( keys %{$self->{SSL_options}} ) { + $ssl_args{$k} = $self->{SSL_options}{$k} if $k =~ m/^SSL_/; + } + + return \%ssl_args; +} + +1; + +__END__ + +=pod + +=encoding UTF-8 + +=head1 NAME + +HTTP::Tiny - A small, simple, correct HTTP/1.1 client + +=head1 VERSION + +version 0.074 + +=head1 SYNOPSIS + + use HTTP::Tiny; + + my $response = HTTP::Tiny->new->get('http://example.com/'); + + die "Failed!\n" unless $response->{success}; + + print "$response->{status} $response->{reason}\n"; + + while (my ($k, $v) = each %{$response->{headers}}) { + for (ref $v eq 'ARRAY' ? @$v : $v) { + print "$k: $_\n"; + } + } + + print $response->{content} if length $response->{content}; + +=head1 DESCRIPTION + +This is a very simple HTTP/1.1 client, designed for doing simple +requests without the overhead of a large framework like L. + +It is more correct and more complete than L. It supports +proxies and redirection. It also correctly resumes after EINTR. + +If L 0.25 or later is installed, HTTP::Tiny will use it instead +of L for transparent support for both IPv4 and IPv6. + +Cookie support requires L or an equivalent class. + +=head1 METHODS + +=head2 new + + $http = HTTP::Tiny->new( %attributes ); + +This constructor returns a new HTTP::Tiny object. Valid attributes include: + +=over 4 + +=item * + +C — A user-agent string (defaults to 'HTTP-Tiny/$VERSION'). If C — ends in a space character, the default user-agent string is appended. + +=item * + +C — An instance of L — or equivalent class that supports the C and C methods + +=item * + +C — A hashref of default headers to apply to requests + +=item * + +C — The local IP address to bind to + +=item * + +C — Whether to reuse the last connection (if for the same scheme, host and port) (defaults to 1) + +=item * + +C — Maximum number of redirects allowed (defaults to 5) + +=item * + +C — Maximum response size in bytes (only when not using a data callback). If defined, responses larger than this will return an exception. + +=item * + +C — URL of a proxy server to use for HTTP connections (default is C<$ENV{http_proxy}> — if set) + +=item * + +C — URL of a proxy server to use for HTTPS connections (default is C<$ENV{https_proxy}> — if set) + +=item * + +C — URL of a generic proxy server for both HTTP and HTTPS connections (default is C<$ENV{all_proxy}> — if set) + +=item * + +C — List of domain suffixes that should not be proxied. Must be a comma-separated string or an array reference. (default is C<$ENV{no_proxy}> —) + +=item * + +C — Request timeout in seconds (default is 60) If a socket open, read or write takes longer than the timeout, an exception is thrown. + +=item * + +C — A boolean that indicates whether to validate the SSL certificate of an C — connection (default is false) + +=item * + +C — A hashref of C — options to pass through to L + +=back + +Passing an explicit C for C, C or C will +prevent getting the corresponding proxies from the environment. + +Exceptions from C, C or other errors will result in a +pseudo-HTTP status code of 599 and a reason of "Internal Exception". The +content field in the response will contain the text of the exception. + +The C parameter enables a persistent connection, but only to a +single destination scheme, host and port. Also, if any connection-relevant +attributes are modified, or if the process ID or thread ID change, the +persistent connection will be dropped. If you want persistent connections +across multiple destinations, use multiple HTTP::Tiny objects. + +See L for more on the C and C attributes. + +=head2 get|head|put|post|delete + + $response = $http->get($url); + $response = $http->get($url, \%options); + $response = $http->head($url); + +These methods are shorthand for calling C for the given method. The +URL must have unsafe characters escaped and international domain names encoded. +See C for valid options and a description of the response. + +The C field of the response will be true if the status code is 2XX. + +=head2 post_form + + $response = $http->post_form($url, $form_data); + $response = $http->post_form($url, $form_data, \%options); + +This method executes a C request and sends the key/value pairs from a +form data hash or array reference to the given URL with a C of +C. If data is provided as an array +reference, the order is preserved; if provided as a hash reference, the terms +are sorted on key and value for consistency. See documentation for the +C method for details on the encoding. + +The URL must have unsafe characters escaped and international domain names +encoded. See C for valid options and a description of the response. +Any C header or content in the options hashref will be ignored. + +The C field of the response will be true if the status code is 2XX. + +=head2 mirror + + $response = $http->mirror($url, $file, \%options) + if ( $response->{success} ) { + print "$file is up to date\n"; + } + +Executes a C request for the URL and saves the response body to the file +name provided. The URL must have unsafe characters escaped and international +domain names encoded. If the file already exists, the request will include an +C header with the modification timestamp of the file. You +may specify a different C header yourself in the C<< +$options->{headers} >> hash. + +The C field of the response will be true if the status code is 2XX +or if the status code is 304 (unmodified). + +If the file was modified and the server response includes a properly +formatted C header, the file modification time will +be updated accordingly. + +=head2 request + + $response = $http->request($method, $url); + $response = $http->request($method, $url, \%options); + +Executes an HTTP request of the given method type ('GET', 'HEAD', 'POST', +'PUT', etc.) on the given URL. The URL must have unsafe characters escaped and +international domain names encoded. + +B: Method names are B per the HTTP/1.1 specification. +Don't use C when you really want C. See L for +how this applies to redirection. + +If the URL includes a "user:password" stanza, they will be used for Basic-style +authorization headers. (Authorization headers will not be included in a +redirected request.) For example: + + $http->request('GET', 'http://Aladdin:open sesame@example.com/'); + +If the "user:password" stanza contains reserved characters, they must +be percent-escaped: + + $http->request('GET', 'http://john%40example.com:password@example.com/'); + +A hashref of options may be appended to modify the request. + +Valid options are: + +=over 4 + +=item * + +C — A hashref containing headers to include with the request. If the value for a header is an array reference, the header will be output multiple times with each value in the array. These headers over-write any default headers. + +=item * + +C — A scalar to include as the body of the request OR a code reference that will be called iteratively to produce the body of the request + +=item * + +C — A code reference that will be called if it exists to provide a hashref of trailing headers (only used with chunked transfer-encoding) + +=item * + +C — A code reference that will be called for each chunks of the response body received. + +=item * + +C — Override host resolution and force all connections to go only to a specific peer address, regardless of the URL of the request. This will include any redirections! This options should be used with extreme caution (e.g. debugging or very special circumstances). + +=back + +The C header is generated from the URL in accordance with RFC 2616. It +is a fatal error to specify C in the C option. Other headers +may be ignored or overwritten if necessary for transport compliance. + +If the C option is a code reference, it will be called iteratively +to provide the content body of the request. It should return the empty +string or undef when the iterator is exhausted. + +If the C option is the empty string, no C or +C headers will be generated. + +If the C option is provided, it will be called iteratively until +the entire response body is received. The first argument will be a string +containing a chunk of the response body, the second argument will be the +in-progress response hash reference, as described below. (This allows +customizing the action of the callback based on the C or C +received prior to the content body.) + +The C method returns a hashref containing the response. The hashref +will have the following keys: + +=over 4 + +=item * + +C — Boolean indicating whether the operation returned a 2XX status code + +=item * + +C — URL that provided the response. This is the URL of the request unless there were redirections, in which case it is the last URL queried in a redirection chain + +=item * + +C — The HTTP status code of the response + +=item * + +C — The response phrase returned by the server + +=item * + +C — The body of the response. If the response does not have any content or if a data callback is provided to consume the response body, this will be the empty string + +=item * + +C — A hashref of header fields. All header field names will be normalized to be lower case. If a header is repeated, the value will be an arrayref; it will otherwise be a scalar string containing the value + +=item * + +C - If this field exists, it is the protocol of the response such as HTTP/1.0 or HTTP/1.1 + +=item * + +C If this field exists, it is an arrayref of response hash references from redirects in the same order that redirections occurred. If it does not exist, then no redirections occurred. + +=back + +On an exception during the execution of the request, the C field will +contain 599, and the C field will contain the text of the exception. + +=head2 www_form_urlencode + + $params = $http->www_form_urlencode( $data ); + $response = $http->get("http://example.com/query?$params"); + +This method converts the key/value pairs from a data hash or array reference +into a C string. The keys and values from the data +reference will be UTF-8 encoded and escaped per RFC 3986. If a value is an +array reference, the key will be repeated with each of the values of the array +reference. If data is provided as a hash reference, the key/value pairs in the +resulting string will be sorted by key and value for consistent ordering. + +=head2 can_ssl + + $ok = HTTP::Tiny->can_ssl; + ($ok, $why) = HTTP::Tiny->can_ssl; + ($ok, $why) = $http->can_ssl; + +Indicates if SSL support is available. When called as a class object, it +checks for the correct version of L and L. +When called as an object methods, if C is true or if C +is set in C, it checks that a CA file is available. + +In scalar context, returns a boolean indicating if SSL is available. +In list context, returns the boolean and a (possibly multi-line) string of +errors indicating why SSL isn't available. + +=head2 connected + + $host = $http->connected; + ($host, $port) = $http->connected; + +Indicates if a connection to a peer is being kept alive, per the C +option. + +In scalar context, returns the peer host and port, joined with a colon, or +C (if no peer is connected). +In list context, returns the peer host and port or an empty list (if no peer +is connected). + +B: This method cannot reliably be used to discover whether the remote +host has closed its end of the socket. + +=for Pod::Coverage SSL_options +agent +cookie_jar +default_headers +http_proxy +https_proxy +keep_alive +local_address +max_redirect +max_size +no_proxy +proxy +timeout +verify_SSL + +=head1 SSL SUPPORT + +Direct C connections are supported only if L 1.56 or +greater and L 1.49 or greater are installed. An exception will be +thrown if new enough versions of these modules are not installed or if the SSL +encryption fails. You can also use C utility function +that returns boolean to see if the required modules are installed. + +An C connection may be made via an C proxy that supports the CONNECT +command (i.e. RFC 2817). You may not proxy C via a proxy that itself +requires C to communicate. + +SSL provides two distinct capabilities: + +=over 4 + +=item * + +Encrypted communication channel + +=item * + +Verification of server identity + +=back + +B. + +Server identity verification is controversial and potentially tricky because it +depends on a (usually paid) third-party Certificate Authority (CA) trust model +to validate a certificate as legitimate. This discriminates against servers +with self-signed certificates or certificates signed by free, community-driven +CA's such as L. + +By default, HTTP::Tiny does not make any assumptions about your trust model, +threat level or risk tolerance. It just aims to give you an encrypted channel +when you need one. + +Setting the C attribute to a true value will make HTTP::Tiny verify +that an SSL connection has a valid SSL certificate corresponding to the host +name of the connection and that the SSL certificate has been verified by a CA. +Assuming you trust the CA, this will protect against a L. If you are +concerned about security, you should enable this option. + +Certificate verification requires a file containing trusted CA certificates. + +If the environment variable C is present, HTTP::Tiny +will try to find a CA certificate file in that location. + +If the L module is installed, HTTP::Tiny will use the CA file +included with it as a source of trusted CA's. (This means you trust Mozilla, +the author of Mozilla::CA, the CPAN mirror where you got Mozilla::CA, the +toolchain used to install it, and your operating system security, right?) + +If that module is not available, then HTTP::Tiny will search several +system-specific default locations for a CA certificate file: + +=over 4 + +=item * + +/etc/ssl/certs/ca-certificates.crt + +=item * + +/etc/pki/tls/certs/ca-bundle.crt + +=item * + +/etc/ssl/ca-bundle.pem + +=back + +An exception will be raised if C is true and no CA certificate file +is available. + +If you desire complete control over SSL connections, the C attribute +lets you provide a hash reference that will be passed through to +C, overriding any options set by HTTP::Tiny. For +example, to provide your own trusted CA file: + + SSL_options => { + SSL_ca_file => $file_path, + } + +The C attribute could also be used for such things as providing a +client certificate for authentication to a server or controlling the choice of +cipher used for the SSL connection. See L documentation for +details. + +=head1 PROXY SUPPORT + +HTTP::Tiny can proxy both C and C requests. Only Basic proxy +authorization is supported and it must be provided as part of the proxy URL: +C. + +HTTP::Tiny supports the following proxy environment variables: + +=over 4 + +=item * + +http_proxy or HTTP_PROXY + +=item * + +https_proxy or HTTPS_PROXY + +=item * + +all_proxy or ALL_PROXY + +=back + +If the C environment variable is set, then this might be a CGI +process and C would be set from the C header, which is a +security risk. If C is set, C (the upper case +variant only) is ignored. + +Tunnelling C over an C proxy using the CONNECT method is +supported. If your proxy uses C itself, you can not tunnel C +over it. + +Be warned that proxying an C connection opens you to the risk of a +man-in-the-middle attack by the proxy server. + +The C environment variable is supported in the format of a +comma-separated list of domain extensions proxy should not be used for. + +Proxy arguments passed to C will override their corresponding +environment variables. + +=head1 LIMITATIONS + +HTTP::Tiny is I with the +L: + +=over 4 + +=item * + +"Message Syntax and Routing" [RFC7230] + +=item * + +"Semantics and Content" [RFC7231] + +=item * + +"Conditional Requests" [RFC7232] + +=item * + +"Range Requests" [RFC7233] + +=item * + +"Caching" [RFC7234] + +=item * + +"Authentication" [RFC7235] + +=back + +It attempts to meet all "MUST" requirements of the specification, but does not +implement all "SHOULD" requirements. (Note: it was developed against the +earlier RFC 2616 specification and may not yet meet the revised RFC 7230-7235 +spec.) + +Some particular limitations of note include: + +=over + +=item * + +HTTP::Tiny focuses on correct transport. Users are responsible for ensuring +that user-defined headers and content are compliant with the HTTP/1.1 +specification. + +=item * + +Users must ensure that URLs are properly escaped for unsafe characters and that +international domain names are properly encoded to ASCII. See L, +L and L. + +=item * + +Redirection is very strict against the specification. Redirection is only +automatic for response codes 301, 302, 307 and 308 if the request method is +'GET' or 'HEAD'. Response code 303 is always converted into a 'GET' +redirection, as mandated by the specification. There is no automatic support +for status 305 ("Use proxy") redirections. + +=item * + +There is no provision for delaying a request body using an C header. +Unexpected C<1XX> responses are silently ignored as per the specification. + +=item * + +Only 'chunked' C is supported. + +=item * + +There is no support for a Request-URI of '*' for the 'OPTIONS' request. + +=item * + +Headers mentioned in the RFCs and some other, well-known headers are +generated with their canonical case. Other headers are sent in the +case provided by the user. Except for control headers (which are sent first), +headers are sent in arbitrary order. + +=back + +Despite the limitations listed above, HTTP::Tiny is considered +feature-complete. New feature requests should be directed to +L. + +=head1 SEE ALSO + +=over 4 + +=item * + +L - Higher level UA features for HTTP::Tiny + +=item * + +L - HTTP::Tiny wrapper with L/L compatibility + +=item * + +L - Wrap L instance in HTTP::Tiny compatible interface + +=item * + +L - Required for IPv6 support + +=item * + +L - Required for SSL support + +=item * + +L - If HTTP::Tiny isn't enough for you, this is the "standard" way to do things + +=item * + +L - Required if you want to validate SSL certificates + +=item * + +L - Required for SSL support + +=back + +=for :stopwords cpan testmatrix url annocpan anno bugtracker rt cpants kwalitee diff irc mailto metadata placeholders metacpan + +=head1 SUPPORT + +=head2 Bugs / Feature Requests + +Please report any bugs or feature requests through the issue tracker +at L. +You will be notified automatically of any progress on your issue. + +=head2 Source Code + +This is open source software. The code repository is available for +public review and contribution under the terms of the license. + +L + + git clone https://github.com/chansen/p5-http-tiny.git + +=head1 AUTHORS + +=over 4 + +=item * + +Christian Hansen + +=item * + +David Golden + +=back + +=head1 CONTRIBUTORS + +=for stopwords Alan Gardner Alessandro Ghedini A. Sinan Unur Brad Gilbert brian m. carlson Chris Nehren Weyl Claes Jakobsson Clinton Gormley Craig Berry David Golden Mitchell Dean Pearce Edward Zborowski James Raspass Jeremy Mates Jess Robinson Karen Etheridge Lukas Eklund Martin J. Evans Martin-Louis Bright Mike Doherty Nicolas Rochelemagne Olaf Alders Olivier Mengué Petr Písař Serguei Trouchelle Shoichi Kaji SkyMarshal Sören Kornetzki Steve Grazzini Syohei YOSHIDA Tatsuhiko Miyagawa Tom Hukins Tony Cook + +=over 4 + +=item * + +Alan Gardner + +=item * + +Alessandro Ghedini + +=item * + +A. Sinan Unur + +=item * + +Brad Gilbert + +=item * + +brian m. carlson + +=item * + +Chris Nehren + +=item * + +Chris Weyl + +=item * + +Claes Jakobsson + +=item * + +Clinton Gormley + +=item * + +Craig A. Berry + +=item * + +Craig Berry + +=item * + +David Golden + +=item * + +David Mitchell + +=item * + +Dean Pearce + +=item * + +Edward Zborowski + +=item * + +James Raspass + +=item * + +Jeremy Mates + +=item * + +Jess Robinson + +=item * + +Karen Etheridge + +=item * + +Lukas Eklund + +=item * + +Martin J. Evans + +=item * + +Martin-Louis Bright + +=item * + +Mike Doherty + +=item * + +Nicolas Rochelemagne + +=item * + +Olaf Alders + +=item * + +Olivier Mengué + +=item * + +Petr Písař + +=item * + +Serguei Trouchelle + +=item * + +Shoichi Kaji + +=item * + +SkyMarshal + +=item * + +Sören Kornetzki + +=item * + +Steve Grazzini + +=item * + +Syohei YOSHIDA + +=item * + +Tatsuhiko Miyagawa + +=item * + +Tom Hukins + +=item * + +Tony Cook + +=back + +=head1 COPYRIGHT AND LICENSE + +This software is copyright (c) 2018 by Christian Hansen. + +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 diff --git a/perlcritic.rc b/perlcritic.rc new file mode 100644 index 0000000..bcbbb45 --- /dev/null +++ b/perlcritic.rc @@ -0,0 +1,26 @@ +severity = 5 +verbose = 8 + +[Variables::ProhibitPunctuationVars] +allow = $@ $! + +[TestingAndDebugging::ProhibitNoStrict] +allow = refs + +[Variables::ProhibitEvilVariables] +variables = $DB::single + +# Turn these off +[-BuiltinFunctions::ProhibitStringyEval] +[-ControlStructures::ProhibitPostfixControls] +[-ControlStructures::ProhibitUnlessBlocks] +[-Documentation::RequirePodSections] +[-InputOutput::ProhibitInteractiveTest] +[-References::ProhibitDoubleSigils] +[-RegularExpressions::RequireExtendedFormatting] +[-InputOutput::ProhibitTwoArgOpen] +[-Modules::ProhibitEvilModules] + +# Turn this on +[Lax::ProhibitStringyEval::ExceptForRequire] + diff --git a/t/00-report-prereqs.dd b/t/00-report-prereqs.dd new file mode 100644 index 0000000..7b5a076 --- /dev/null +++ b/t/00-report-prereqs.dd @@ -0,0 +1,85 @@ +do { my $x = { + 'configure' => { + 'requires' => { + 'ExtUtils::MakeMaker' => '6.17', + 'perl' => '5.006' + }, + 'suggests' => { + 'JSON::PP' => '2.27300' + } + }, + 'develop' => { + 'requires' => { + 'Dist::Zilla' => '5', + 'Dist::Zilla::Plugin::Prereqs' => '0', + 'Dist::Zilla::Plugin::ReleaseStatus::FromVersion' => '0', + 'Dist::Zilla::Plugin::RemovePrereqs' => '0', + 'Dist::Zilla::PluginBundle::DAGOLDEN' => '0.072', + 'File::Spec' => '0', + 'File::Temp' => '0', + 'IO::Handle' => '0', + 'IPC::Open3' => '0', + 'Perl::Critic::Policy::Lax::ProhibitStringyEval::ExceptForRequire' => '0', + 'Pod::Coverage::TrustPod' => '0', + 'Pod::Wordlist' => '0', + 'Software::License::Perl_5' => '0', + 'Test::CPAN::Meta' => '0', + 'Test::MinimumVersion' => '0', + 'Test::More' => '0', + 'Test::Perl::Critic' => '0', + 'Test::Pod' => '1.41', + 'Test::Pod::Coverage' => '1.08', + 'Test::Portability::Files' => '0', + 'Test::Spelling' => '0.12', + 'Test::Version' => '1', + 'perl' => '5.006' + } + }, + 'runtime' => { + 'recommends' => { + 'HTTP::CookieJar' => '0.001', + 'IO::Socket::IP' => '0.32', + 'IO::Socket::SSL' => '1.42', + 'Mozilla::CA' => '20160104', + 'Net::SSLeay' => '1.49' + }, + 'requires' => { + 'Carp' => '0', + 'Fcntl' => '0', + 'IO::Socket' => '0', + 'MIME::Base64' => '0', + 'Socket' => '0', + 'Time::Local' => '0', + 'bytes' => '0', + 'perl' => '5.006', + 'strict' => '0', + 'warnings' => '0' + }, + 'suggests' => { + 'IO::Socket::SSL' => '1.56' + } + }, + 'test' => { + 'recommends' => { + 'CPAN::Meta' => '2.120900' + }, + 'requires' => { + 'Data::Dumper' => '0', + 'Exporter' => '0', + 'ExtUtils::MakeMaker' => '0', + 'File::Basename' => '0', + 'File::Spec' => '0', + 'File::Temp' => '0', + 'IO::Dir' => '0', + 'IO::File' => '0', + 'IO::Socket::INET' => '0', + 'IPC::Cmd' => '0', + 'Test::More' => '0.96', + 'lib' => '0', + 'open' => '0', + 'perl' => '5.006' + } + } + }; + $x; + } \ No newline at end of file diff --git a/t/00-report-prereqs.t b/t/00-report-prereqs.t new file mode 100644 index 0000000..c72183a --- /dev/null +++ b/t/00-report-prereqs.t @@ -0,0 +1,193 @@ +#!perl + +use strict; +use warnings; + +# This test was generated by Dist::Zilla::Plugin::Test::ReportPrereqs 0.027 + +use Test::More tests => 1; + +use ExtUtils::MakeMaker; +use File::Spec; + +# from $version::LAX +my $lax_version_re = + qr/(?: undef | (?: (?:[0-9]+) (?: \. | (?:\.[0-9]+) (?:_[0-9]+)? )? + | + (?:\.[0-9]+) (?:_[0-9]+)? + ) | (?: + v (?:[0-9]+) (?: (?:\.[0-9]+)+ (?:_[0-9]+)? )? + | + (?:[0-9]+)? (?:\.[0-9]+){2,} (?:_[0-9]+)? + ) + )/x; + +# hide optional CPAN::Meta modules from prereq scanner +# and check if they are available +my $cpan_meta = "CPAN::Meta"; +my $cpan_meta_pre = "CPAN::Meta::Prereqs"; +my $HAS_CPAN_META = eval "require $cpan_meta; $cpan_meta->VERSION('2.120900')" && eval "require $cpan_meta_pre"; ## no critic + +# Verify requirements? +my $DO_VERIFY_PREREQS = 1; + +sub _max { + my $max = shift; + $max = ( $_ > $max ) ? $_ : $max for @_; + return $max; +} + +sub _merge_prereqs { + my ($collector, $prereqs) = @_; + + # CPAN::Meta::Prereqs object + if (ref $collector eq $cpan_meta_pre) { + return $collector->with_merged_prereqs( + CPAN::Meta::Prereqs->new( $prereqs ) + ); + } + + # Raw hashrefs + for my $phase ( keys %$prereqs ) { + for my $type ( keys %{ $prereqs->{$phase} } ) { + for my $module ( keys %{ $prereqs->{$phase}{$type} } ) { + $collector->{$phase}{$type}{$module} = $prereqs->{$phase}{$type}{$module}; + } + } + } + + return $collector; +} + +my @include = qw( + +); + +my @exclude = qw( + +); + +# Add static prereqs to the included modules list +my $static_prereqs = do './t/00-report-prereqs.dd'; + +# Merge all prereqs (either with ::Prereqs or a hashref) +my $full_prereqs = _merge_prereqs( + ( $HAS_CPAN_META ? $cpan_meta_pre->new : {} ), + $static_prereqs +); + +# Add dynamic prereqs to the included modules list (if we can) +my ($source) = grep { -f } 'MYMETA.json', 'MYMETA.yml'; +my $cpan_meta_error; +if ( $source && $HAS_CPAN_META + && (my $meta = eval { CPAN::Meta->load_file($source) } ) +) { + $full_prereqs = _merge_prereqs($full_prereqs, $meta->prereqs); +} +else { + $cpan_meta_error = $@; # capture error from CPAN::Meta->load_file($source) + $source = 'static metadata'; +} + +my @full_reports; +my @dep_errors; +my $req_hash = $HAS_CPAN_META ? $full_prereqs->as_string_hash : $full_prereqs; + +# Add static includes into a fake section +for my $mod (@include) { + $req_hash->{other}{modules}{$mod} = 0; +} + +for my $phase ( qw(configure build test runtime develop other) ) { + next unless $req_hash->{$phase}; + next if ($phase eq 'develop' and not $ENV{AUTHOR_TESTING}); + + for my $type ( qw(requires recommends suggests conflicts modules) ) { + next unless $req_hash->{$phase}{$type}; + + my $title = ucfirst($phase).' '.ucfirst($type); + my @reports = [qw/Module Want Have/]; + + for my $mod ( sort keys %{ $req_hash->{$phase}{$type} } ) { + next if $mod eq 'perl'; + next if grep { $_ eq $mod } @exclude; + + my $file = $mod; + $file =~ s{::}{/}g; + $file .= ".pm"; + my ($prefix) = grep { -e File::Spec->catfile($_, $file) } @INC; + + my $want = $req_hash->{$phase}{$type}{$mod}; + $want = "undef" unless defined $want; + $want = "any" if !$want && $want == 0; + + my $req_string = $want eq 'any' ? 'any version required' : "version '$want' required"; + + if ($prefix) { + my $have = MM->parse_version( File::Spec->catfile($prefix, $file) ); + $have = "undef" unless defined $have; + push @reports, [$mod, $want, $have]; + + if ( $DO_VERIFY_PREREQS && $HAS_CPAN_META && $type eq 'requires' ) { + if ( $have !~ /\A$lax_version_re\z/ ) { + push @dep_errors, "$mod version '$have' cannot be parsed ($req_string)"; + } + elsif ( ! $full_prereqs->requirements_for( $phase, $type )->accepts_module( $mod => $have ) ) { + push @dep_errors, "$mod version '$have' is not in required range '$want'"; + } + } + } + else { + push @reports, [$mod, $want, "missing"]; + + if ( $DO_VERIFY_PREREQS && $type eq 'requires' ) { + push @dep_errors, "$mod is not installed ($req_string)"; + } + } + } + + if ( @reports ) { + push @full_reports, "=== $title ===\n\n"; + + my $ml = _max( map { length $_->[0] } @reports ); + my $wl = _max( map { length $_->[1] } @reports ); + my $hl = _max( map { length $_->[2] } @reports ); + + if ($type eq 'modules') { + splice @reports, 1, 0, ["-" x $ml, "", "-" x $hl]; + push @full_reports, map { sprintf(" %*s %*s\n", -$ml, $_->[0], $hl, $_->[2]) } @reports; + } + else { + splice @reports, 1, 0, ["-" x $ml, "-" x $wl, "-" x $hl]; + push @full_reports, map { sprintf(" %*s %*s %*s\n", -$ml, $_->[0], $wl, $_->[1], $hl, $_->[2]) } @reports; + } + + push @full_reports, "\n"; + } + } +} + +if ( @full_reports ) { + diag "\nVersions for all modules listed in $source (including optional ones):\n\n", @full_reports; +} + +if ( $cpan_meta_error || @dep_errors ) { + diag "\n*** WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING ***\n"; +} + +if ( $cpan_meta_error ) { + my ($orig_source) = grep { -f } 'MYMETA.json', 'MYMETA.yml'; + diag "\nCPAN::Meta->load_file('$orig_source') failed with: $cpan_meta_error\n"; +} + +if ( @dep_errors ) { + diag join("\n", + "\nThe following REQUIRED prerequisites were not satisfied:\n", + @dep_errors, + "\n" + ); +} + +pass; + +# vim: ts=4 sts=4 sw=4 et: diff --git a/t/000_load.t b/t/000_load.t new file mode 100644 index 0000000..052298a --- /dev/null +++ b/t/000_load.t @@ -0,0 +1,12 @@ +#!perl + +use strict; +use warnings; + +use Test::More 0.88 tests => 1; + +require_ok('HTTP::Tiny'); + +local $HTTP::Tiny::VERSION = $HTTP::Tiny::VERSION || 'from repo'; +note("HTTP::Tiny $HTTP::Tiny::VERSION, Perl $], $^X"); + diff --git a/t/001_api.t b/t/001_api.t new file mode 100755 index 0000000..3e6864e --- /dev/null +++ b/t/001_api.t @@ -0,0 +1,31 @@ +#!perl + +use strict; +use warnings; + +use Test::More tests => 2; +use HTTP::Tiny; + +my @accessors = qw( + agent default_headers http_proxy https_proxy keep_alive local_address + max_redirect max_size proxy no_proxy timeout SSL_options verify_SSL cookie_jar +); +my @methods = qw( + new get head put post delete post_form request mirror www_form_urlencode can_ssl + connected +); + +my %api; +@api{@accessors} = (1) x @accessors; +@api{@methods} = (1) x @methods; + +can_ok('HTTP::Tiny', @methods, @accessors); + +my @extra = + grep {! $api{$_} } + grep { $_ !~ /\A_/ } + grep {; no strict 'refs'; *{"HTTP::Tiny::$_"}{CODE} } + sort keys %HTTP::Tiny::; + +ok( ! scalar @extra, "No unexpected subroutines defined" ) + or diag "Found: @extra"; diff --git a/t/002_croakage.t b/t/002_croakage.t new file mode 100644 index 0000000..ac3c8b9 --- /dev/null +++ b/t/002_croakage.t @@ -0,0 +1,52 @@ +#!perl + +use strict; +use warnings; + +use Test::More; +use lib 't'; +use Util qw[tmpfile monkey_patch set_socket_source]; + +use HTTP::Tiny; + +BEGIN { monkey_patch() } + +my %usage = ( + 'get' => q/Usage: $http->get(URL, [HASHREF])/, + 'mirror' => q/Usage: $http->mirror(URL, FILE, [HASHREF])/, + 'request' => q/Usage: $http->request(METHOD, URL, [HASHREF])/, +); + +my @cases = ( + ['get'], + ['get','http://www.example.com/','extra'], + ['get','http://www.example.com/','extra', 'extra'], + ['mirror'], + ['mirror','http://www.example.com/',], + ['mirror','http://www.example.com/','extra', 'extra'], + ['mirror','http://www.example.com/','extra', 'extra', 'extra'], + ['request'], + ['request','GET'], + ['request','GET','http://www.example.com/','extra'], + ['request','GET','http://www.example.com/','extra', 'extra'], +); + +my $res_fh = tmpfile(); +my $req_fh = tmpfile(); + +my $http = HTTP::Tiny->new; +set_socket_source($req_fh, $res_fh); + +for my $c ( @cases ) { + my ($method, @args) = @$c; + eval {$http->$method(@args)}; + my $err = $@; + like ($err, qr/\Q$usage{$method}\E/, join("|",@$c) ); +} + +my $res = eval{ $http->get("http://www.example.com/", { headers => { host => "www.example2.com" } } ) }; +is( $res->{status}, 599, "Providing a Host header errors with 599" ); +like( $res->{content}, qr/'Host' header/, "Providing a Host header gives right error message" ); + +done_testing; + diff --git a/t/003_agent.t b/t/003_agent.t new file mode 100644 index 0000000..6962c66 --- /dev/null +++ b/t/003_agent.t @@ -0,0 +1,53 @@ +#!perl + +use strict; +use warnings; + +use Test::More tests => 8; +use HTTP::Tiny; + +# a couple tests to ensure that we get the default agent expected, the coorect +# agent when specified, and the correct agent when specifified with a space at +# the end of the string (as LWP::UserAgent does) + + +my $default = 'HTTP-Tiny/' . (HTTP::Tiny->VERSION || 0); + +{ + my $ua = HTTP::Tiny->new(); + is $ua->agent, $default, 'default agent string is as expected'; +} + +{ + my $ua = HTTP::Tiny->new(agent => 'something else'); + is $ua->agent, 'something else', 'agent string is as expected'; +} + +{ + my $ua = HTTP::Tiny->new(agent => 'something else '); + is + $ua->agent, + "something else $default", + 'agent string is as properly appended to', + ; +} + +{ + my $ua = HTTP::Tiny->new(); + + is( HTTP::Tiny->_agent(), $default, 'check _agent on class' ); + is $ua->_agent(), $default, 'check _agent on object'; + + $ua->agent(undef); + is $ua->agent, undef, 'agent string is empty'; + + $ua->agent('something else'); + is $ua->agent, 'something else', 'agent string is as expected'; + + $ua->agent('something else '); + is + $ua->agent, + "something else $default", + 'agent string is as properly appended to', + ; +} diff --git a/t/004_timeout.t b/t/004_timeout.t new file mode 100644 index 0000000..95f9bd4 --- /dev/null +++ b/t/004_timeout.t @@ -0,0 +1,38 @@ +#!perl + +use strict; +use warnings; + +use Test::More tests => 5; +use HTTP::Tiny; + +# Just make sure timeout is handled correctly as a constructor param, +# and that it works as expected as an "attribute". + +my $default = 60; + +{ + my $ua = HTTP::Tiny->new(); + is $ua->timeout, $default, 'default timeout is as expected'; +} + +{ + my $ua = HTTP::Tiny->new(timeout => 10); + is $ua->timeout, 10, 'timeout is handled as a constructor param'; +} + +{ + my $ua = HTTP::Tiny->new(timeout => 0); + is $ua->timeout, 0, 'constructor arg of timeout=0 is passed through'; +} + +{ + my $ua = HTTP::Tiny->new(timeout => undef); + is $ua->timeout, $default, 'constructor arg of timeout=undef is ignored'; +} + +{ + my $ua = HTTP::Tiny->new(); + $ua->timeout(15); + is $ua->timeout, 15, 'timeout works as expected as a r/w attribute'; +} diff --git a/t/010_url.t b/t/010_url.t new file mode 100644 index 0000000..bbaf14e --- /dev/null +++ b/t/010_url.t @@ -0,0 +1,35 @@ +#!perl + +use strict; +use warnings; + +use Test::More 0.86; +use HTTP::Tiny; + +my @tests = ( + [ 'HtTp://Example.COM/', 'http', 'example.com', 80, '/', '', ], + [ 'HtTp://Example.com:1024/', 'http', 'example.com', 1024, '/', '', ], + [ 'http://example.com', 'http', 'example.com', 80, '/', '', ], + [ 'http://example.com:', 'http', 'example.com', 80, '/', '', ], + [ 'http://foo@example.com:', 'http', 'example.com', 80, '/', 'foo', ], + [ 'http://foo:pass@example.com:', 'http', 'example.com', 80, '/', 'foo:pass', ], + [ 'http://@example.com:', 'http', 'example.com', 80, '/', '', ], + [ 'http://example.com?foo=bar', 'http', 'example.com', 80, '/?foo=bar', '', ], + [ 'http://example.com?foo=bar#fragment', 'http', 'example.com', 80, '/?foo=bar', '', ], + [ 'http://example.com/path?foo=bar', 'http', 'example.com', 80, '/path?foo=bar', '', ], + [ 'http:///path?foo=bar', 'http', 'localhost', 80, '/path?foo=bar', '', ], + [ 'HTTPS://example.com/', 'https', 'example.com', 443, '/', '', ], + [ 'http://[::]:1024', 'http', '[::]', 1024, '/', '', ], + [ 'xxx://foo/', 'xxx', 'foo', undef, '/', '', ], +); + +plan tests => scalar @tests; + +for my $test (@tests) { + my $url = shift(@$test); + my $got = [ HTTP::Tiny->_split_url($url) ]; + my $exp = $test; + is_deeply($got, $exp, "->split_url('$url')") or diag explain $got; +} + + diff --git a/t/020_headers.t b/t/020_headers.t new file mode 100644 index 0000000..a4254e5 --- /dev/null +++ b/t/020_headers.t @@ -0,0 +1,61 @@ +#!perl + +use strict; +use warnings; + +use Test::More qw[no_plan]; +use lib 't'; +use Util qw[tmpfile rewind $CRLF $LF]; +use HTTP::Tiny; + +{ + no warnings 'redefine'; + sub HTTP::Tiny::Handle::can_read { 1 }; + sub HTTP::Tiny::Handle::can_write { 1 }; +} + +{ + my $header = join $CRLF, 'Foo: Foo', 'Foo: Baz', 'Bar: Bar', '', ''; + my $fh = tmpfile($header); + my $exp = { foo => ['Foo', 'Baz'], bar => 'Bar' }; + my $handle = HTTP::Tiny::Handle->new(fh => $fh); + my $got = $handle->read_header_lines; + is_deeply($got, $exp, "->read_header_lines() CRLF"); +} + +{ + my $header = join $LF, 'Foo: Foo', 'Foo: Baz', 'Bar: Bar', '', ''; + my $fh = tmpfile($header); + my $exp = { foo => ['Foo', 'Baz'], bar => 'Bar' }; + my $handle = HTTP::Tiny::Handle->new(fh => $fh); + my $got = $handle->read_header_lines; + is_deeply($got, $exp, "->read_header_lines() LF"); +} + +{ + my $header = "Foo: $CRLF\x09Bar$CRLF\x09$CRLF\x09Baz$CRLF$CRLF"; + my $fh = tmpfile($header); + my $exp = { foo => 'Bar Baz' }; + my $handle = HTTP::Tiny::Handle->new(fh => $fh); + my $got = $handle->read_header_lines; + is_deeply($got, $exp, "->read_header_lines() insane continuations"); +} + +{ + my $fh = tmpfile(); + my $handle = HTTP::Tiny::Handle->new(fh => $fh); + my $headers = { foo => ['Foo', 'Baz'], bar => 'Bar' }; + $handle->write_header_lines($headers); + rewind($fh); + is_deeply($handle->read_header_lines, $headers, "roundtrip header lines"); +} + +{ + my $fh = tmpfile(); + my $handle = HTTP::Tiny::Handle->new(fh => $fh); + my $headers = { foo => ['Foo', 'Baz'], bar => 'Bar', baz => '' }; + $handle->write_header_lines($headers); + rewind($fh); + is_deeply($handle->read_header_lines, $headers, "roundtrip header lines"); +} + diff --git a/t/030_response.t b/t/030_response.t new file mode 100644 index 0000000..e519a62 --- /dev/null +++ b/t/030_response.t @@ -0,0 +1,36 @@ +#!perl + +use strict; +use warnings; + +use Test::More qw[no_plan]; +use lib 't'; +use Util qw[tmpfile rewind $CRLF $LF]; +use HTTP::Tiny; + +sub _header { + return [ @{$_[0]}{qw/status reason headers protocol/} ] +} + +{ + no warnings 'redefine'; + sub HTTP::Tiny::Handle::can_read { 1 }; + sub HTTP::Tiny::Handle::can_write { 1 }; +} + +{ + my $response = join $CRLF, 'HTTP/1.1 200 OK', 'Foo: Foo', 'Bar: Bar', '', ''; + my $fh = tmpfile($response); + my $handle = HTTP::Tiny::Handle->new(fh => $fh); + my $exp = [ 200, 'OK', { foo => 'Foo', bar => 'Bar' }, 'HTTP/1.1' ]; + is_deeply(_header($handle->read_response_header), $exp, "->read_response_header CRLF"); +} + +{ + my $response = join $LF, 'HTTP/1.1 200 OK', 'Foo: Foo', 'Bar: Bar', '', ''; + my $fh = tmpfile($response); + my $handle = HTTP::Tiny::Handle->new(fh => $fh); + my $exp = [ 200, 'OK', { foo => 'Foo', bar => 'Bar' }, 'HTTP/1.1' ]; + is_deeply(_header($handle->read_response_header), $exp, "->read_response_header LF"); +} + diff --git a/t/040_content.t b/t/040_content.t new file mode 100644 index 0000000..b7406de --- /dev/null +++ b/t/040_content.t @@ -0,0 +1,41 @@ +#!perl + +use strict; +use warnings; + +use Test::More qw[no_plan]; +use lib 't'; +use Util qw[tmpfile rewind $CRLF $LF]; +use HTTP::Tiny; + +{ + no warnings 'redefine'; + sub HTTP::Tiny::Handle::can_read { 1 }; + sub HTTP::Tiny::Handle::can_write { 1 }; +} + +{ + my $chunk = join('', '0' .. '9', 'A' .. 'Z', 'a' .. 'z', '_', $LF) x 16; # 1024 + my $fh = tmpfile(); + my $handle = HTTP::Tiny::Handle->new(fh => $fh); + my $nchunks = 128; + my $length = $nchunks * length $chunk; + + { + my $request = { + cb => sub { $nchunks-- ? $chunk : undef }, + headers => { 'content-length' => $length } + }; + my $got = $handle->write_content_body($request); + is($got, $length, "written $length octets"); + } + + rewind($fh); + + { + my $got = 0; + $handle->read_content_body(sub { $got += length $_[0] }, {}, $length); + is($got, $length, "read $length octets"); + } +} + diff --git a/t/050_chunked_body.t b/t/050_chunked_body.t new file mode 100644 index 0000000..7aff9e0 --- /dev/null +++ b/t/050_chunked_body.t @@ -0,0 +1,72 @@ +#!perl + +use strict; +use warnings; + +use Test::More qw[no_plan]; +use lib 't'; +use Util qw[tmpfile rewind $CRLF]; +use HTTP::Tiny; + +{ + no warnings 'redefine'; + sub HTTP::Tiny::Handle::can_read { 1 }; + sub HTTP::Tiny::Handle::can_write { 1 }; +} + +{ + my $fh = tmpfile(); + my $handle = HTTP::Tiny::Handle->new(fh => $fh); + + my $exp = ['A'..'Z']; + my $got = []; + + { + my @chunks = @$exp; + my $request = { + cb => sub { shift @chunks }, + }; + $handle->write_chunked_body($request); + } + + rewind($fh); + + { + my $cb = sub { push @$got, $_[0] }; + my $response = { headers => {} }; + $handle->read_chunked_body($cb, $response); + } + + is_deeply($got, $exp, "roundtrip chunked chunks w/o trailers"); +} + +{ + my $fh = tmpfile(); + my $handle = HTTP::Tiny::Handle->new(fh => $fh); + + my $exp = ['A'..'Z']; + my $trailers = { foo => 'Bar', bar => 'Baz' }; + my $got = []; + + { + my @chunks = @$exp; + my $request = { + cb => sub { shift @chunks }, + trailer_cb => sub { $trailers }, + }; + $handle->write_chunked_body($request); + } + + rewind($fh); + + { + my $cb = sub { push @$got, $_[0] }; + my $response = { headers => {} }; + $handle->read_chunked_body($cb, $response); + is_deeply($response->{headers}, $trailers, 'roundtrip chunked trailers'); + } + + is_deeply($got, $exp, "roundtrip chunked chunks (with trailers)"); +} + + diff --git a/t/060_http_date.t b/t/060_http_date.t new file mode 100644 index 0000000..367c078 --- /dev/null +++ b/t/060_http_date.t @@ -0,0 +1,29 @@ +#!perl + +use strict; +use warnings; + +use Test::More; +use HTTP::Tiny; + +# test cases adapted from HTTP::Date +my $epoch = 760233600; + +my @cases = ( + ['Thu, 03 Feb 1994 00:00:00 GMT', 'RFC822+RFC1123'], + ['Thu, 3 Feb 1994 00:00:00 GMT', 'broken RFC822+RFC1123'], + ['Thursday, 03-Feb-94 00:00:00 GMT', 'old rfc850 HTTP format'], + ['Thursday, 03-Feb-1994 00:00:00 GMT', 'broken rfc850 HTTP format'], + ['Thu Feb 3 00:00:00 GMT 1994', 'ctime format'], + ['Thu Feb 3 00:00:00 1994', 'same as ctime, except no TZ'], +); + +plan tests => 1 + @cases; + +is(HTTP::Tiny->_http_date($epoch), $cases[0][0], "epoch -> RFC822/RFC1123"); + +for my $c ( @cases ) { + is( HTTP::Tiny->_parse_http_date($c->[0]), $epoch, $c->[1] . " -> epoch"); +} + + diff --git a/t/070_cookie_jar.t b/t/070_cookie_jar.t new file mode 100644 index 0000000..f62c481 --- /dev/null +++ b/t/070_cookie_jar.t @@ -0,0 +1,43 @@ +#!perl + +use strict; +use warnings; + +use Test::More tests => 4; +use lib 't'; +use SimpleCookieJar; +use BrokenCookieJar; +use HTTP::Tiny; + +### a couple tests to ensure that: +### * by default there is no cookie jar defined +### * the correct cookie jar is returned when specified +### * error when cookie jar does not support the add and cookie_header methods + + +my $default = undef; +my $jar = SimpleCookieJar->new(); +my $mug = BrokenCookieJar->new(); +my $dog = BrokenCookieJar2->new(); + +{ + my $ua = HTTP::Tiny->new(); + is $ua->cookie_jar, $default, 'default cookie jar is as expected'; +} + +{ + my $ua = HTTP::Tiny->new(cookie_jar => $jar); + is $ua->cookie_jar, $jar, 'cookie_jar is as expected'; +} + +{ + my $ua = eval { HTTP::Tiny->new(cookie_jar => $mug) }; + my $err = $@; + like( $err, qr/must provide .* 'add' method/ + => 'invalid jar does not support add method' ); + + $ua = eval { HTTP::Tiny->new(cookie_jar => $dog) }; + $err = $@; + like( $err, qr/must provide .* 'cookie_header' method/ + => 'invalid jar does not support cookie_header method' ); +} diff --git a/t/100_get.t b/t/100_get.t new file mode 100644 index 0000000..5dbf3e8 --- /dev/null +++ b/t/100_get.t @@ -0,0 +1,113 @@ +#!perl + +use strict; +use warnings; + +use File::Basename; +use Test::More 0.88; +use lib 't'; +use Util qw[tmpfile rewind slurp monkey_patch dir_list parse_case + hashify connect_args set_socket_source sort_headers $CRLF $LF]; + +use HTTP::Tiny; +BEGIN { monkey_patch() } + +for my $file ( dir_list("corpus", qr/^get/ ) ) { + my $label = basename($file); + my $data = do { local (@ARGV,$/) = $file; <> }; + my ($params, $expect_req, $give_res) = split /--+\n/, $data; + my $case = parse_case($params); + + my $url = $case->{url}[0]; + my %headers = hashify( $case->{headers} ); + my %new_args = hashify( $case->{new_args} ); + + my %options; + $options{headers} = \%headers if %headers; + if ( $case->{data_cb} ) { + $main::data = ''; + $options{data_callback} = eval join "\n", @{$case->{data_cb}}; + die unless ref( $options{data_callback} ) eq 'CODE'; + } + + my $version = HTTP::Tiny->VERSION || 0; + my $agent = $new_args{agent} || "HTTP-Tiny/$version"; + + # cleanup source data + $expect_req =~ s{HTTP-Tiny/VERSION}{$agent}; + s{\n}{$CRLF}g for ($expect_req, $give_res); + + # setup mocking and test + my $res_fh = tmpfile($give_res); + my $req_fh = tmpfile(); + + my $http = HTTP::Tiny->new(keep_alive => 0, %new_args); + set_socket_source($req_fh, $res_fh); + + (my $url_basename = $url) =~ s{.*/}{}; + + my @call_args = %options ? ($url, \%options) : ($url); + my $response = $http->get(@call_args); + + my ($got_host, $got_port) = connect_args(); + my ($exp_host, $exp_port) = ( + ($new_args{proxy} || $url ) =~ m{^http://([^:/]+?):?(\d*)/}g + ); + $exp_host ||= 'localhost'; + $exp_port ||= 80; + + my $got_req = slurp($req_fh); + + is ($got_host, $exp_host, "$label host $exp_host"); + is ($got_port, $exp_port, "$label port $exp_port"); + is( sort_headers($got_req), sort_headers($expect_req), "$label request data"); + + my ($rc) = $give_res =~ m{\S+\s+(\d+)}g; + # maybe override + $rc = $case->{expected_rc}[0] if defined $case->{expected_rc}; + + is( $response->{status}, $rc, "$label response code $rc" ) + or diag $response->{content}; + + if ( substr($rc,0,1) eq '2' ) { + ok( $response->{success}, "$label success flag true" ); + } + else { + ok( ! $response->{success}, "$label success flag false" ); + } + + is ( $response->{url}, $url, "$label response URL" ); + + if (defined $case->{expected_headers}) { + my %expected = hashify( $case->{expected_headers} ); + is_deeply($response->{headers}, \%expected, "$label expected headers"); + } + + my $check_expected = $case->{expected_like} + ? sub { + my ($text, $msg) = @_; + like( $text, "/".$case->{expected_like}[0]."/", $msg ); + } + : sub { + my ($text, $msg) = @_; + my $exp_content = + $case->{expected} ? join("$CRLF", @{$case->{expected}}, '') : ''; + is ( $text, $exp_content, $msg ); + } + ; + + + + if ( $options{data_callback} ) { + $check_expected->( $main::data, "$label cb got content" ); + is ( $response->{content}, '', "$label resp content empty" ); + } + else { + $check_expected->( $response->{content}, "$label content" ); + } + + ok ( ! exists $response->{redirects}, "$label redirects array doesn't exist") + or diag explain $response->{redirects}; +} + +done_testing; diff --git a/t/101_head.t b/t/101_head.t new file mode 100644 index 0000000..ace0745 --- /dev/null +++ b/t/101_head.t @@ -0,0 +1,75 @@ +#!perl + +use strict; +use warnings; + +use File::Basename; +use Test::More 0.88; +use lib 't'; +use Util qw[tmpfile rewind slurp monkey_patch dir_list parse_case + set_socket_source sort_headers $CRLF $LF]; +use HTTP::Tiny; +BEGIN { monkey_patch() } + +for my $file ( dir_list("corpus", qr/^head/ ) ) { + my $data = do { local (@ARGV,$/) = $file; <> }; + my ($params, $expect_req, $give_res) = split /--+\n/, $data; + # cleanup source data + my $version = HTTP::Tiny->VERSION || 0; + $expect_req =~ s{VERSION}{$version}; + s{\n}{$CRLF}g for ($expect_req, $give_res); + + # figure out what request to make + my $case = parse_case($params); + my $url = $case->{url}[0]; + my %options; + + my %headers; + for my $line ( @{ $case->{headers} } ) { + my ($k,$v) = ($line =~ m{^([^:]+): (.*)$}g); + $headers{$k} = $v; + } + $options{headers} = \%headers if %headers; + + if ( $case->{content} ) { + $options{content} = $case->{content}[0]; + } + elsif ( $case->{content_cb} ) { + $options{content} = eval join "\n", @{$case->{content_cb}}; + } + + if ( $case->{trailer_cb} ) { + $options{trailer_callback} = eval join "\n", @{$case->{trailer_cb}}; + } + + # setup mocking and test + my $res_fh = tmpfile($give_res); + my $req_fh = tmpfile(); + + my $http = HTTP::Tiny->new( keep_alive => 0 ); + set_socket_source($req_fh, $res_fh); + + (my $url_basename = $url) =~ s{.*/}{}; + + my @call_args = %options ? ($url, \%options) : ($url); + my $response = $http->head(@call_args); + + my $got_req = slurp($req_fh); + + my $label = basename($file); + + is( sort_headers($got_req), sort_headers($expect_req), "$label request" ); + + my ($rc) = $give_res =~ m{\S+\s+(\d+)}g; + is( $response->{status}, $rc, "$label response code $rc" ) + or diag $response->{content}; + + if ( substr($rc,0,1) eq '2' ) { + ok( $response->{success}, "$label success flag true" ); + } + else { + ok( ! $response->{success}, "$label success flag false" ); + } +} + +done_testing; diff --git a/t/102_put.t b/t/102_put.t new file mode 100644 index 0000000..2fe3626 --- /dev/null +++ b/t/102_put.t @@ -0,0 +1,75 @@ +#!perl + +use strict; +use warnings; + +use File::Basename; +use Test::More 0.88; +use lib 't'; +use Util qw[tmpfile rewind slurp monkey_patch dir_list parse_case + set_socket_source sort_headers $CRLF $LF]; +use HTTP::Tiny; +BEGIN { monkey_patch() } + +for my $file ( dir_list("corpus", qr/^put/ ) ) { + my $data = do { local (@ARGV,$/) = $file; <> }; + my ($params, $expect_req, $give_res) = split /--+\n/, $data; + # cleanup source data + my $version = HTTP::Tiny->VERSION || 0; + $expect_req =~ s{VERSION}{$version}; + s{\n}{$CRLF}g for ($expect_req, $give_res); + + # figure out what request to make + my $case = parse_case($params); + my $url = $case->{url}[0]; + my %options; + + my %headers; + for my $line ( @{ $case->{headers} } ) { + my ($k,$v) = ($line =~ m{^([^:]+): (.*)$}g); + $headers{$k} = $v; + } + $options{headers} = \%headers if %headers; + + if ( $case->{content} ) { + $options{content} = $case->{content}[0]; + } + elsif ( $case->{content_cb} ) { + $options{content} = eval join "\n", @{$case->{content_cb}}; + } + + if ( $case->{trailer_cb} ) { + $options{trailer_callback} = eval join "\n", @{$case->{trailer_cb}}; + } + + # setup mocking and test + my $res_fh = tmpfile($give_res); + my $req_fh = tmpfile(); + + my $http = HTTP::Tiny->new( keep_alive => 0 ); + set_socket_source($req_fh, $res_fh); + + (my $url_basename = $url) =~ s{.*/}{}; + + my @call_args = %options ? ($url, \%options) : ($url); + my $response = $http->put(@call_args); + + my $got_req = slurp($req_fh); + + my $label = basename($file); + + is( sort_headers($got_req), sort_headers($expect_req), "$label request" ); + + my ($rc) = $give_res =~ m{\S+\s+(\d+)}g; + is( $response->{status}, $rc, "$label response code $rc" ) + or diag $response->{content}; + + if ( substr($rc,0,1) eq '2' ) { + ok( $response->{success}, "$label success flag true" ); + } + else { + ok( ! $response->{success}, "$label success flag false" ); + } +} + +done_testing; diff --git a/t/103_delete.t b/t/103_delete.t new file mode 100644 index 0000000..66d345b --- /dev/null +++ b/t/103_delete.t @@ -0,0 +1,75 @@ +#!perl + +use strict; +use warnings; + +use File::Basename; +use Test::More 0.88; +use lib 't'; +use Util qw[tmpfile rewind slurp monkey_patch dir_list parse_case + set_socket_source sort_headers $CRLF $LF]; +use HTTP::Tiny; +BEGIN { monkey_patch() } + +for my $file ( dir_list("corpus", qr/^delete/ ) ) { + my $data = do { local (@ARGV,$/) = $file; <> }; + my ($params, $expect_req, $give_res) = split /--+\n/, $data; + # cleanup source data + my $version = HTTP::Tiny->VERSION || 0; + $expect_req =~ s{VERSION}{$version}; + s{\n}{$CRLF}g for ($expect_req, $give_res); + + # figure out what request to make + my $case = parse_case($params); + my $url = $case->{url}[0]; + my %options; + + my %headers; + for my $line ( @{ $case->{headers} } ) { + my ($k,$v) = ($line =~ m{^([^:]+): (.*)$}g); + $headers{$k} = $v; + } + $options{headers} = \%headers if %headers; + + if ( $case->{content} ) { + $options{content} = $case->{content}[0]; + } + elsif ( $case->{content_cb} ) { + $options{content} = eval join "\n", @{$case->{content_cb}}; + } + + if ( $case->{trailer_cb} ) { + $options{trailer_callback} = eval join "\n", @{$case->{trailer_cb}}; + } + + # setup mocking and test + my $res_fh = tmpfile($give_res); + my $req_fh = tmpfile(); + + my $http = HTTP::Tiny->new( keep_alive => 0 ); + set_socket_source($req_fh, $res_fh); + + (my $url_basename = $url) =~ s{.*/}{}; + + my @call_args = %options ? ($url, \%options) : ($url); + my $response = $http->delete(@call_args); + + my $got_req = slurp($req_fh); + + my $label = basename($file); + + is( sort_headers($got_req), sort_headers($expect_req), "$label request" ); + + my ($rc) = $give_res =~ m{\S+\s+(\d+)}g; + is( $response->{status}, $rc, "$label response code $rc" ) + or diag $response->{content}; + + if ( substr($rc,0,1) eq '2' ) { + ok( $response->{success}, "$label success flag true" ); + } + else { + ok( ! $response->{success}, "$label success flag false" ); + } +} + +done_testing; diff --git a/t/104_post.t b/t/104_post.t new file mode 100644 index 0000000..9c765bd --- /dev/null +++ b/t/104_post.t @@ -0,0 +1,75 @@ +#!perl + +use strict; +use warnings; + +use File::Basename; +use Test::More 0.88; +use lib 't'; +use Util qw[tmpfile rewind slurp monkey_patch dir_list parse_case + set_socket_source sort_headers $CRLF $LF]; +use HTTP::Tiny; +BEGIN { monkey_patch() } + +for my $file ( dir_list("corpus", qr/^post/ ) ) { + my $data = do { local (@ARGV,$/) = $file; <> }; + my ($params, $expect_req, $give_res) = split /--+\n/, $data; + # cleanup source data + my $version = HTTP::Tiny->VERSION || 0; + $expect_req =~ s{VERSION}{$version}; + s{\n}{$CRLF}g for ($expect_req, $give_res); + + # figure out what request to make + my $case = parse_case($params); + my $url = $case->{url}[0]; + my %options; + + my %headers; + for my $line ( @{ $case->{headers} } ) { + my ($k,$v) = ($line =~ m{^([^:]+): (.*)$}g); + $headers{$k} = $v; + } + $options{headers} = \%headers if %headers; + + if ( $case->{content} ) { + $options{content} = $case->{content}[0]; + } + elsif ( $case->{content_cb} ) { + $options{content} = eval join "\n", @{$case->{content_cb}}; + } + + if ( $case->{trailer_cb} ) { + $options{trailer_callback} = eval join "\n", @{$case->{trailer_cb}}; + } + + # setup mocking and test + my $res_fh = tmpfile($give_res); + my $req_fh = tmpfile(); + + my $http = HTTP::Tiny->new( keep_alive => 0 ); + set_socket_source($req_fh, $res_fh); + + (my $url_basename = $url) =~ s{.*/}{}; + + my @call_args = %options ? ($url, \%options) : ($url); + my $response = $http->post(@call_args); + + my $got_req = slurp($req_fh); + + my $label = basename($file); + + is( sort_headers($got_req), sort_headers($expect_req), "$label request" ); + + my ($rc) = $give_res =~ m{\S+\s+(\d+)}g; + is( $response->{status}, $rc, "$label response code $rc" ) + or diag $response->{content}; + + if ( substr($rc,0,1) eq '2' ) { + ok( $response->{success}, "$label success flag true" ); + } + else { + ok( ! $response->{success}, "$label success flag false" ); + } +} + +done_testing; diff --git a/t/110_mirror.t b/t/110_mirror.t new file mode 100644 index 0000000..de38fd1 --- /dev/null +++ b/t/110_mirror.t @@ -0,0 +1,97 @@ +#!perl + +use strict; +use warnings; + +use File::Basename; +use Test::More 0.88; +use lib 't'; +use Util qw[tmpfile rewind slurp monkey_patch dir_list parse_case + set_socket_source sort_headers $CRLF $LF]; +use HTTP::Tiny; +use File::Temp qw/tempdir/; +use File::Spec; + +BEGIN { monkey_patch() } + +my $tempdir = tempdir( TMPDIR => 1, CLEANUP => 1 ); +my $tempfile = File::Spec->catfile( $tempdir, "tempfile.txt" ); + +my $known_epoch = 760233600; +my $day = 24*3600; + +my %timestamp = ( + 'modified.txt' => $known_epoch - 2 * $day, + 'not-modified.txt' => $known_epoch - 2 * $day, +); + +for my $file ( dir_list("corpus", qr/^mirror/ ) ) { + 1 while unlink $tempfile; + my $data = do { local (@ARGV,$/) = $file; <> }; + my ($params, $expect_req, $give_res) = split /--+\n/, $data; + # cleanup source data + my $version = HTTP::Tiny->VERSION || 0; + $expect_req =~ s{VERSION}{$version}; + s{\n}{$CRLF}g for ($expect_req, $give_res); + + # figure out what request to make + my $case = parse_case($params); + my $url = $case->{url}->[0]; + my %options; + + my %headers; + for my $line ( @{ $case->{headers} } ) { + my ($k,$v) = ($line =~ m{^([^:]+): (.*)$}g); + $headers{$k} = $v; + } + $options{headers} = \%headers if %headers; + + # maybe create a file + (my $url_basename = $url) =~ s{.*/}{}; + if ( my $mtime = $timestamp{$url_basename} ) { + open my $fh, ">", $tempfile; + close $fh; + utime $mtime, $mtime, $tempfile; + if ($^O eq 'MSWin32') { + # Deal with stat and daylight savings issues on Windows + # by reading back mtime + $timestamp{$url_basename} = (stat $tempfile)[9]; + } + } + + # setup mocking and test + my $res_fh = tmpfile($give_res); + my $req_fh = tmpfile(); + + my $http = HTTP::Tiny->new( keep_alive => 0 ); + set_socket_source($req_fh, $res_fh); + + my @call_args = %options ? ($url, $tempfile, \%options) : ($url, $tempfile); + my $response = $http->mirror(@call_args); + + my $got_req = slurp($req_fh); + + my $label = basename($file); + + is( sort_headers($got_req), sort_headers($expect_req), "$label request" ); + + my ($rc) = $give_res =~ m{\S+\s+(\d+)}g; + is( $response->{status}, $rc, "$label response code $rc" ) + or diag $response->{content}; + + if ( substr($rc,0,1) eq '2' ) { + ok( $response->{success}, "$label success flag true" ); + ok( -e $tempfile, "$label file created" ); + } + elsif ( $rc eq '304' ) { + ok( $response->{success}, "$label success flag true" ); + is( (stat($tempfile))[9], $timestamp{$url_basename}, + "$label file not overwritten" ); + } + else { + ok( ! $response->{success}, "$label success flag false" ); + ok( ! -e $tempfile, "$label file not created" ); + } +} + +done_testing; diff --git a/t/130_redirect.t b/t/130_redirect.t new file mode 100644 index 0000000..cc8c15c --- /dev/null +++ b/t/130_redirect.t @@ -0,0 +1,82 @@ +#!perl + +use strict; +use warnings; + +use File::Basename; +use Test::More 0.88; +use lib 't'; +use Util qw[tmpfile rewind slurp monkey_patch dir_list parse_case + hashify connect_args clear_socket_source set_socket_source sort_headers + $CRLF $LF]; + +use HTTP::Tiny; +BEGIN { monkey_patch() } + +for my $file ( dir_list("corpus", qr/^redirect/ ) ) { + my $label = basename($file); + my $data = do { local (@ARGV,$/) = $file; <> }; + my ($params, @case_pairs) = split /--+\n/, $data; + my $case = parse_case($params); + my $number_of_requests = @case_pairs / 2; + + my $url = $case->{url}[0]; + my $method = $case->{method}[0] || 'GET'; + my %headers = hashify( $case->{headers} ); + my %new_args = hashify( $case->{new_args} ); + + my %options; + $options{headers} = \%headers if %headers; + my $call_args = %options ? [$method, $url, \%options] : [$method, $url]; + + my $version = HTTP::Tiny->VERSION || 0; + my $agent = $new_args{agent} || "HTTP-Tiny/$version"; + + my (@socket_pairs); + while ( @case_pairs ) { + my ($expect_req, $give_res) = splice( @case_pairs, 0, 2 ); + # cleanup source data + $expect_req =~ s{HTTP-Tiny/VERSION}{$agent}; + s{\n}{$CRLF}g for ($expect_req, $give_res); + + # setup mocking and test + my $req_fh = tmpfile(); + my $res_fh = tmpfile($give_res); + + push @socket_pairs, [$req_fh, $res_fh, $expect_req]; + } + + clear_socket_source(); + set_socket_source(@$_) for @socket_pairs; + + my $http = HTTP::Tiny->new(keep_alive => 0, %new_args); + my $response = $http->request(@$call_args); + my $max_redirects = defined($new_args{max_redirect}) ? $new_args{max_redirect} : 5; + my $calls = 0 + $max_redirects; + + for my $i ( 0 .. $calls ) { + last unless @socket_pairs; + my ($req_fh, $res_fh, $expect_req) = @{ shift @socket_pairs }; + my $got_req = slurp($req_fh); + is( sort_headers($got_req), sort_headers($expect_req), "$label request ($i)"); + $i++; + } + + my $exp_content = $case->{expected} + ? join("$CRLF", @{$case->{expected}}) : ''; + + is ( $response->{content}, $exp_content, "$label content" ); + + my $number_of_redirects = + $max_redirects < $number_of_requests ? $max_redirects : $number_of_requests - 1; + is ( @{ $response->{redirects} || [] }, $number_of_redirects, + "$label redirects array size" + ) or diag explain $response->{redirects}; + + if ( $case->{expected_url} ) { + is ( $response->{url}, $case->{expected_url}[0], "$label response URL" ); + } + +} + +done_testing; diff --git a/t/140_proxy.t b/t/140_proxy.t new file mode 100644 index 0000000..dd38462 --- /dev/null +++ b/t/140_proxy.t @@ -0,0 +1,83 @@ +#!perl + +use strict; +use warnings; + +use File::Basename; +use Test::More 0.88; + +use lib 't'; +use Util qw[ monkey_patch ]; +use HTTP::Tiny; + +BEGIN { + monkey_patch(); +} + + +# Require a true value +for my $proxy (undef, "", 0){ + local $ENV{all_proxy} = undef; + local $ENV{ALL_PROXY} = undef; + local $ENV{http_proxy} = $proxy; + my $c = HTTP::Tiny->new(); + ok(!defined $c->http_proxy); +} + +# trailing / is optional +for my $proxy ("http://localhost:8080/", "http://localhost:8080"){ + local $ENV{http_proxy} = $proxy; + my $c = HTTP::Tiny->new(); + is($c->http_proxy, $proxy); +} + +# http_proxy must be http://: format +{ + local $ENV{http_proxy} = "localhost:8080"; + eval { + my $c = HTTP::Tiny->new(); + }; + like($@, qr{http_proxy URL must be in format http\[s\]://\[auth\@\]:/}); +} + +# Explicitly disable proxy +{ + local $ENV{all_proxy} = "http://localhost:8080"; + local $ENV{http_proxy} = "http://localhost:8080"; + local $ENV{https_proxy} = "http://localhost:8080"; + my $c = HTTP::Tiny->new( + proxy => undef, + http_proxy => undef, + https_proxy => undef, + ); + ok(!defined $c->proxy, "proxy => undef disables ENV proxy"); + ok(!defined $c->http_proxy, "http_proxy => undef disables ENV proxy"); + ok(!defined $c->https_proxy, "https_proxy => undef disables ENV proxy"); +} + +# case variations +for my $var ( qw/http_proxy https_proxy all_proxy/ ) { + my $proxy = "http://localhost:8080"; + for my $s ( uc($var), lc($var) ) { + local $ENV{$s} = $proxy; + my $c = HTTP::Tiny->new(); + my $m = ($s =~ /all/i) ? 'proxy' : lc($s); + is( $c->$m, $proxy, "set $m from $s" ); + } +} + +# ignore HTTP_PROXY with REQUEST_METHOD +{ + # in case previous clean-up failed for some reason + delete local @ENV{'http_proxy', 'https_proxy', 'all_proxy', + 'HTTP_PROXY', 'HTTPS_PROXY', 'ALL_PROXY'}; + + local $ENV{HTTP_PROXY} = "http://localhost:8080"; + local $ENV{REQUEST_METHOD} = 'GET'; + my $c = HTTP::Tiny->new(); + ok(!defined $c->http_proxy, + "http_proxy not set from HTTP_PROXY if REQUEST_METHOD set"); + +} + +done_testing(); diff --git a/t/141_no_proxy.t b/t/141_no_proxy.t new file mode 100644 index 0000000..f12a268 --- /dev/null +++ b/t/141_no_proxy.t @@ -0,0 +1,72 @@ +#!perl +use strict; +use warnings; + +use Test::More 0.88; + +use HTTP::Tiny; + +# blank slate for testing +delete $ENV{no_proxy}; + +{ + my $c = HTTP::Tiny->new(); + is_deeply( $c->no_proxy, [], "no no_proxy given" ); +} + +my @cases = ( + #<<< No perltidy + { + no_proxy => [ + undef, + [], + ], + expect => [], + }, + { + no_proxy => [ + "localhost", + ["localhost"], + ], + expect => ["localhost"], + }, + { + no_proxy => [ + "localhost,example.com", + "localhost, example.com", + [qw/localhost example.com/] + ], + expect => [ "localhost", "example.com" ], + }, + #>>> +); + +for my $c (@cases) { + for my $no_proxy ( @{ $c->{no_proxy} } ) { + my $label = + !defined $no_proxy ? 'undef' + : ref $no_proxy ? "[qw/@$no_proxy/]" + : "'$no_proxy'"; + + # Can't test environment with array ref (ENV stringifies in new perls) + if ( ref $no_proxy ) { + my $ht = HTTP::Tiny->new( no_proxy => $no_proxy ); + is_deeply( $ht->no_proxy, $c->{expect}, "new(no_proxy => $label)" ); + } + else { + { + local $ENV{no_proxy} = $no_proxy; + my $ht = HTTP::Tiny->new(); + is_deeply( $ht->no_proxy, $c->{expect}, "\$ENV{no_proxy} = $label" ); + } + { + local $ENV{no_proxy} = "Shouldnt,see,this"; + my $ht = HTTP::Tiny->new( no_proxy => $no_proxy ); + is_deeply( $ht->no_proxy, $c->{expect}, + "new(no_proxy => $label) versus other \$ENV{no_proxy}" ); + } + } + } +} + +done_testing(); diff --git a/t/150_post_form.t b/t/150_post_form.t new file mode 100644 index 0000000..790cbc0 --- /dev/null +++ b/t/150_post_form.t @@ -0,0 +1,86 @@ +#!perl + +use strict; +use warnings; +use open IN => ':raw'; + +use File::Basename; +use Test::More 0.88; +use lib 't'; +use Util qw[tmpfile rewind slurp monkey_patch dir_list parse_case + set_socket_source sort_headers $CRLF $LF]; +use HTTP::Tiny; +BEGIN { monkey_patch() } + +for my $file ( dir_list("corpus", qr/^form/ ) ) { + my $data = do { local (@ARGV,$/) = $file; <> }; + $data =~ s/$CRLF/$LF/gm if $^O eq 'MSWin32'; + my ($params, $expect_req, $give_res) = split /--+\n/, $data; + # cleanup source data + my $version = HTTP::Tiny->VERSION || 0; + $expect_req =~ s{VERSION}{$version}; + s{\n}{$CRLF}g for ($expect_req, $give_res); + + # figure out what request to make + my $case = parse_case($params); + my $url = $case->{url}[0]; + my %options; + + my %headers; + for my $line ( @{ $case->{headers} } ) { + my ($k,$v) = ($line =~ m{^([^:]+): (.*)$}g); + $headers{$k} = $v; + } + $options{headers} = \%headers if %headers; + + my @params = split "\\|", $case->{content}[0]; + my $formdata; + if ( $case->{datatype}[0] eq 'HASH' ) { + while ( @params ) { + my ($key, $value) = splice( @params, 0, 2 ); + if ( ref $formdata->{$key} ) { + push @{$formdata->{$key}}, $value; + } + elsif ( exists $formdata->{$key} ) { + $formdata->{$key} = [ $formdata->{$key}, $value ]; + } + else { + $formdata->{$key} = $value; + } + } + } + else { + $formdata = [ @params ]; + } + + # setup mocking and test + my $res_fh = tmpfile($give_res); + my $req_fh = tmpfile(); + + my $http = HTTP::Tiny->new( keep_alive => 0 ); + set_socket_source($req_fh, $res_fh); + + (my $url_basename = $url) =~ s{.*/}{}; + + my $response = $http->post_form( $url, $formdata, %options ? (\%options) : ()); + + my $got_req = slurp($req_fh); + + my $label = basename($file); + + + is( sort_headers($got_req), sort_headers($expect_req), "$label request" ); + + my ($rc) = $give_res =~ m{\S+\s+(\d+)}g; + is( $response->{status}, $rc, "$label response code $rc" ) + or diag $response->{content}; + + if ( substr($rc,0,1) eq '2' ) { + ok( $response->{success}, "$label success flag true" ); + } + else { + ok( ! $response->{success}, "$label success flag false" ); + } +} + +done_testing; diff --git a/t/160_cookies.t b/t/160_cookies.t new file mode 100644 index 0000000..03b9184 --- /dev/null +++ b/t/160_cookies.t @@ -0,0 +1,79 @@ +#!perl + +use strict; +use warnings; + +use File::Basename; +use Test::More 0.96; +use lib 't'; +use Util qw[tmpfile rewind slurp monkey_patch dir_list parse_case + hashify connect_args clear_socket_source set_socket_source sort_headers + $CRLF $LF]; + +use HTTP::Tiny; +BEGIN { monkey_patch() } + +SKIP: for my $class ( qw/SimpleCookieJar HTTP::CookieJar/ ) { + + subtest $class => sub { + eval "require $class; 1" + or plan skip_all => "Needs $class"; + + for my $file ( dir_list("corpus", qr/^cookies/ ) ) { + my $label = basename($file); + my $data = do { local (@ARGV,$/) = $file; <> }; + my @cases = split /--+\n/, $data; + + my $jar = SimpleCookieJar->new(); + my $http = undef; + while (@cases) { + my ($params, $expect_req, $give_res) = splice( @cases, 0, 3 ); + + my $case = parse_case($params); + + my $url = $case->{url}[0]; + my $method = $case->{method}[0] || 'GET'; + my %headers = hashify( $case->{headers} ); + my %new_args = hashify( $case->{new_args} ); + + if( exists $headers{Cookie} ) { + my $cookies = delete $headers{Cookie}; + $jar->add( $url, $cookies ); + } + + if( exists $headers{'No-Cookie-Jar'} ) { + delete $headers{'No-Cookie-Jar'}; + $jar = undef; + } + + my %options; + $options{headers} = \%headers if %headers; + + my $version = HTTP::Tiny->VERSION || 0; + my $agent = $new_args{agent} || "HTTP-Tiny/$version"; + + $new_args{cookie_jar} = $jar; + + # cleanup source data + $expect_req =~ s{HTTP-Tiny/VERSION}{$agent}; + s{\n}{$CRLF}g for ($expect_req, $give_res); + + # setup mocking and test + my $res_fh = tmpfile($give_res); + my $req_fh = tmpfile(); + + $http = HTTP::Tiny->new(keep_alive => 0, %new_args) if !defined $http; + clear_socket_source(); + set_socket_source($req_fh, $res_fh); + + my @call_args = %options ? ($url, \%options) : ($url); + my $response = $http->get(@call_args); + + my $got_req = slurp($req_fh); + is( sort_headers($got_req), sort_headers($expect_req), "$label request data"); + } + } + }; +} + +done_testing; diff --git a/t/161_basic_auth.t b/t/161_basic_auth.t new file mode 100644 index 0000000..379c5cc --- /dev/null +++ b/t/161_basic_auth.t @@ -0,0 +1,76 @@ +#!perl + +use strict; +use warnings; + +use File::Basename; +use Test::More 0.88; +use lib 't'; +use Util qw[tmpfile rewind slurp monkey_patch dir_list parse_case + hashify connect_args clear_socket_source set_socket_source sort_headers + $CRLF $LF]; + +use HTTP::Tiny; +BEGIN { monkey_patch() } + +for my $file ( dir_list("corpus", qr/^auth/ ) ) { + my $label = basename($file); + my $data = do { local (@ARGV,$/) = $file; <> }; + my ($params, @case_pairs) = split /--+\n/, $data; + my $case = parse_case($params); + + my $url = $case->{url}[0]; + my $method = $case->{method}[0] || 'GET'; + my %headers = hashify( $case->{headers} ); + my %new_args = hashify( $case->{new_args} ); + + my %options; + $options{headers} = \%headers if %headers; + my $call_args = %options ? [$method, $url, \%options] : [$method, $url]; + + my $version = HTTP::Tiny->VERSION || 0; + my $agent = $new_args{agent} || "HTTP-Tiny/$version"; + + my (@socket_pairs); + while ( @case_pairs ) { + my ($expect_req, $give_res) = splice( @case_pairs, 0, 2 ); + # cleanup source data + $expect_req =~ s{HTTP-Tiny/VERSION}{$agent}; + s{\n}{$CRLF}g for ($expect_req, $give_res); + + # setup mocking and test + my $req_fh = tmpfile(); + my $res_fh = tmpfile($give_res); + + push @socket_pairs, [$req_fh, $res_fh, $expect_req]; + } + + clear_socket_source(); + set_socket_source(@$_) for @socket_pairs; + + my $http = HTTP::Tiny->new(keep_alive => 0, %new_args); + my $response = $http->request(@$call_args); + + my $calls = 0 + + (defined($new_args{max_redirect}) ? $new_args{max_redirect} : 5); + + for my $i ( 0 .. $calls ) { + last unless @socket_pairs; + my ($req_fh, $res_fh, $expect_req) = @{ shift @socket_pairs }; + my $got_req = slurp($req_fh); + is( sort_headers($got_req), sort_headers($expect_req), "$label request ($i)"); + $i++; + } + + my $exp_content = $case->{expected} + ? join("$CRLF", @{$case->{expected}}) : ''; + + is ( $response->{content}, $exp_content, "$label content" ); + + if ( $case->{expected_url} ) { + is ( $response->{url}, $case->{expected_url}[0], "$label response URL" ); + } + +} + +done_testing; diff --git a/t/162_proxy_auth.t b/t/162_proxy_auth.t new file mode 100644 index 0000000..cf12e88 --- /dev/null +++ b/t/162_proxy_auth.t @@ -0,0 +1,76 @@ +#!perl + +use strict; +use warnings; + +use File::Basename; +use Test::More 0.88; +use lib 't'; +use Util qw[tmpfile rewind slurp monkey_patch dir_list parse_case + hashify connect_args clear_socket_source set_socket_source sort_headers + $CRLF $LF]; + +use HTTP::Tiny; +BEGIN { monkey_patch() } + +for my $file ( dir_list("corpus", qr/^proxy-auth/ ) ) { + my $label = basename($file); + my $data = do { local (@ARGV,$/) = $file; <> }; + my ($params, @case_pairs) = split /--+\n/, $data; + my $case = parse_case($params); + + my $url = $case->{url}[0]; + my $method = $case->{method}[0] || 'GET'; + my %headers = hashify( $case->{headers} ); + my %new_args = hashify( $case->{new_args} ); + + my %options; + $options{headers} = \%headers if %headers; + my $call_args = %options ? [$method, $url, \%options] : [$method, $url]; + + my $version = HTTP::Tiny->VERSION || 0; + my $agent = $new_args{agent} || "HTTP-Tiny/$version"; + + my (@socket_pairs); + while ( @case_pairs ) { + my ($expect_req, $give_res) = splice( @case_pairs, 0, 2 ); + # cleanup source data + $expect_req =~ s{HTTP-Tiny/VERSION}{$agent}; + s{\n}{$CRLF}g for ($expect_req, $give_res); + + # setup mocking and test + my $req_fh = tmpfile(); + my $res_fh = tmpfile($give_res); + + push @socket_pairs, [$req_fh, $res_fh, $expect_req]; + } + + clear_socket_source(); + set_socket_source(@$_) for @socket_pairs; + + my $http = HTTP::Tiny->new(keep_alive => 0, %new_args); + my $response = $http->request(@$call_args); + + my $calls = 0 + + (defined($new_args{max_redirect}) ? $new_args{max_redirect} : 5); + + for my $i ( 0 .. $calls ) { + last unless @socket_pairs; + my ($req_fh, $res_fh, $expect_req) = @{ shift @socket_pairs }; + my $got_req = slurp($req_fh); + is( sort_headers($got_req), sort_headers($expect_req), "$label request ($i)"); + $i++; + } + + my $exp_content = $case->{expected} + ? join("$CRLF", @{$case->{expected}}) : ''; + + is ( $response->{content}, $exp_content, "$label content" ); + + if ( $case->{expected_url} ) { + is ( $response->{url}, $case->{expected_url}[0], "$label response URL" ); + } + +} + +done_testing; diff --git a/t/170_keepalive.t b/t/170_keepalive.t new file mode 100644 index 0000000..02162ae --- /dev/null +++ b/t/170_keepalive.t @@ -0,0 +1,99 @@ +#!perl + +use strict; +use warnings; +use File::Basename; +use Test::More 0.88; +use lib 't'; +use Util qw[ + tmpfile monkey_patch dir_list clear_socket_source set_socket_source + $CRLF +]; +use HTTP::Tiny; +our $can_read; + +BEGIN { + no warnings qw/redefine once/; + monkey_patch(); + *HTTP::Tiny::Handle::can_read = sub { $can_read++ }; +} + +my $response = <<'RESPONSE'; +HTTP/1.1 200 OK +Date: Thu, 03 Feb 1994 00:00:00 GMT +Content-Type: text/html +Content-Length: 10 + +0123456789 + +RESPONSE + +trim($response); + +my $h; + +new_ht(); +test_ht( "Keep-alive", 1, 'http://foo.com' ); + +new_ht(); +test_ht( "Different scheme", 0, 'https://foo.com' ); + +new_ht(); +test_ht( "Different host", 0, 'http://bar.com' ); + +new_ht(); +test_ht( "Different port", 0, 'http://foo.com:8000' ); + +new_ht(); +$h->timeout(30); +test_ht( "Different timeout", 0, 'http://foo.com' ); + +new_ht(); +$h->timeout(60); +test_ht( "Same timeout", 1, 'http://foo.com' ); + +new_ht(); +$h->default_headers({ 'X-Foo' => 'Bar' }); +test_ht( "Default headers change", 1, 'http://foo.com' ); + +new_ht(); +$h->{handle}->close; +test_ht( "Socket closed", 0, 'http://foo.com' ); + +for my $file ( dir_list( "corpus", qr/^keepalive/ ) ) { + my $label = basename($file); + my $data = do { local ( @ARGV, $/ ) = $file; <> }; + my ( $title, $ok, $response ) = map { trim($_) } split /--+/, $data; + new_ht(); + clear_socket_source(); + set_socket_source( tmpfile(), tmpfile($response) ); + $h->request( 'POST', 'http://foo.com', { content => 'xx' } ); + is !!$h->{handle}, !!$ok, "$label - $title"; +} + +sub test_ht { + my $title = shift; + my $result = !!shift(); + my $url = shift; + + clear_socket_source(); + set_socket_source( tmpfile(), tmpfile($response) ); + $can_read = 0 if $result; + my $old = $h->{handle} || 'old'; + $h->request( 'POST', $url, { content => 'xx' } ); + my $new = $h->{handle} || 'new'; + is $old eq $new, $result, $title; +} + +sub new_ht { + $h = HTTP::Tiny->new( keep_alive => 1, @_ ); + $can_read = 1; + clear_socket_source(); + set_socket_source( tmpfile(), tmpfile($response) ); + $h->request( 'POST', 'http://foo.com' ); +} + +sub trim { $_[0] =~ s/^\s+//; $_[0] =~ s/\s+$//; return $_ } + +done_testing; + diff --git a/t/200_live.t b/t/200_live.t new file mode 100644 index 0000000..399d9a9 --- /dev/null +++ b/t/200_live.t @@ -0,0 +1,45 @@ +#!perl + +use strict; +use warnings; + +use IO::Socket::INET; +use Test::More 0.88; +use HTTP::Tiny; + +my $test_host = "google.com"; +my $test_url = "http://www.google.com/"; +my $test_re = qr/google/; + +plan 'skip_all' => "Only run for \$ENV{AUTOMATED_TESTING}" + unless $ENV{AUTOMATED_TESTING}; + +plan 'skip_all' => "Internet connection timed out" + unless IO::Socket::INET->new( + PeerHost => $test_host, + PeerPort => 80, + Proto => 'tcp', + Timeout => 10, + ); + +my $ua = HTTP::Tiny->new; +my $response = $ua->get($test_url); +if ( $ua->{handle} ) { + my $socket_class = ref $ua->{handle}{fh}; + diag "HTTP::Tiny using $socket_class version " . $socket_class->VERSION; +} + +ok( $response->{status} ne '599', "Request to $test_url completed" ) + or dump_hash($response); +ok( $response->{content}, "Got content" ); + +sub dump_hash { + my $hash = shift; + $hash->{content} = substr($hash->{content},0,160) . "..."; + require Data::Dumper; + my $dumped = Data::Dumper::Dumper($hash); + $dumped =~ s{^}{# }; + print $dumped; +} + +done_testing; diff --git a/t/200_live_local_ip.t b/t/200_live_local_ip.t new file mode 100644 index 0000000..0993a8d --- /dev/null +++ b/t/200_live_local_ip.t @@ -0,0 +1,42 @@ +#!perl + +use strict; +use warnings; + +use IO::Socket::INET; +use Test::More 0.88; +use HTTP::Tiny; + +my $test_host = "www.google.com"; +my $test_url = "http://www.google.com/"; + +plan 'skip_all' => "Only run for \$ENV{AUTOMATED_TESTING}" + unless $ENV{AUTOMATED_TESTING}; + +plan 'skip_all' => "Internet connection timed out" + unless IO::Socket::INET->new( + PeerHost => $test_host, + PeerPort => 80, + Proto => 'tcp', + Timeout => 10, + ); + +my ($tiny, $response); + +# default local address should work; try three times since the test url +# can have intermittent failures +$tiny = HTTP::Tiny->new; +for (1 .. 3) { + $response = $tiny->get($test_url); + last if $response->{success}; + sleep 2; +} +isnt( $response->{status}, '599', "Request to $test_url completed (default local address)" ); + +# bad local IP should fail +$tiny = HTTP::Tiny->new(local_address => '999.999.999.999'); # bad IP is error +$response = $tiny->get($test_url); +is( $response->{status}, '599', "Request to $test_url failed (invalid local address)" ) + or diag explain $response; + +done_testing; diff --git a/t/210_live_ssl.t b/t/210_live_ssl.t new file mode 100644 index 0000000..6f80e51 --- /dev/null +++ b/t/210_live_ssl.t @@ -0,0 +1,110 @@ +#!perl + +use strict; +use warnings; + +use Test::More 0.96; +use IO::Socket::INET; +BEGIN { + eval { require IO::Socket::SSL; IO::Socket::SSL->VERSION(1.56); 1 }; + plan skip_all => 'IO::Socket::SSL 1.56 required for SSL tests' if $@; + # $IO::Socket::SSL::DEBUG = 3; + + eval { require Net::SSLeay; Net::SSLeay->VERSION(1.49); 1}; + plan skip_all => 'Net::SSLeay 1.49 required for SSL tests' if $@; + + eval { require Mozilla::CA; 1 }; + plan skip_all => 'Mozilla::CA required for SSL tests' if $@; +} +use HTTP::Tiny; + +plan skip_all => 'Only run for $ENV{AUTOMATED_TESTING}' + unless $ENV{AUTOMATED_TESTING}; + +use IPC::Cmd qw/can_run/; + +if ( can_run('openssl') ) { + diag "\nNote: running test with ", qx/openssl version/; +} + +my $data = { + 'https://www.google.ca/' => { + host => 'www.google.ca', + pass => { SSL_verifycn_scheme => 'http', SSL_verifycn_name => 'www.google.ca', SSL_verify_mode => 0x01, SSL_ca_file => Mozilla::CA::SSL_ca_file() }, + fail => { SSL_verify_callback => sub { 0 }, SSL_verify_mode => 0x01 }, + default_should_yield => '1', + }, + 'https://twitter.com/' => { + host => 'twitter.com', + pass => { SSL_verifycn_scheme => 'http', SSL_verifycn_name => 'twitter.com', SSL_verify_mode => 0x01, SSL_ca_file => Mozilla::CA::SSL_ca_file() }, + fail => { SSL_verify_callback => sub { 0 }, SSL_verify_mode => 0x01 }, + default_should_yield => '1', + }, + 'https://github.com/' => { + host => 'github.com', + pass => { SSL_verifycn_scheme => 'http', SSL_verifycn_name => 'github.com', SSL_verify_mode => 0x01, SSL_ca_file => Mozilla::CA::SSL_ca_file() }, + fail => { SSL_verify_callback => sub { 0 }, SSL_verify_mode => 0x01 }, + default_should_yield => '1', + }, + 'https://spinrite.com/' => { + host => 'spinrite.com', + pass => { SSL_verifycn_scheme => 'none', SSL_verifycn_name => 'spinrite.com', SSL_verify_mode => 0x00 }, + fail => { SSL_verifycn_scheme => 'http', SSL_verifycn_name => 'spinrite.com', SSL_verify_mode => 0x01, SSL_ca_file => Mozilla::CA::SSL_ca_file() }, + default_should_yield => '', + } +}; +plan tests => 1+ scalar keys %$data; + +subtest "can_ssl" => sub { + ok( HTTP::Tiny->can_ssl, "class method" ); + ok( HTTP::Tiny->new->can_ssl, "object method, default params" ); + ok( HTTP::Tiny->new(verify_SSL => 1)->can_ssl, "object method, verify_SSL" ); + + my $ht = HTTP::Tiny->new( + verify_SSL => 1, + SSL_options => { SSL_ca_file => 'adlfadkfadlfad' }, + ); + my ($ok, $why) = $ht->can_ssl; + ok( ! $ok, "object methods, verify_SSL, bogus CA file (FAILS)" ); + like( $why, qr/not found or not readable/, "failure reason" ); +}; + + +while (my ($url, $data) = each %$data) { + subtest $url => sub { + plan 'skip_all' => 'Internet connection timed out' + unless IO::Socket::INET->new( + PeerHost => $data->{host}, + PeerPort => 443, + Proto => 'tcp', + Timeout => 10, + ); + + # the default verification + my $response = HTTP::Tiny->new(verify_ssl => 1)->get($url); + is $response->{success}, $data->{default_should_yield}, "Request to $url passed/failed using default as expected" + or do { + # $response->{content} = substr $response->{content}, 0, 50; + $response->{content} =~ s{\n.*}{}s; + diag explain [IO::Socket::SSL::errstr(), $response] + }; + + # force validation to succeed + my $pass = HTTP::Tiny->new( SSL_options => $data->{pass} )->get($url); + isnt $pass->{status}, '599', "Request to $url completed (forced pass)" + or do { + $pass->{content} =~ s{\n.*}{}s; + diag explain $pass + }; + ok $pass->{content}, 'Got some content'; + + # force validation to fail + my $fail = HTTP::Tiny->new( SSL_options => $data->{fail} )->get($url); + is $fail->{status}, '599', "Request to $url failed (forced fail)" + or do { + $fail->{content} =~ s{\n.*}{}s; + diag explain [IO::Socket::SSL::errstr(), $fail] + }; + ok $fail->{content}, 'Got some content'; + }; +} diff --git a/t/BrokenCookieJar.pm b/t/BrokenCookieJar.pm new file mode 100644 index 0000000..3b7d457 --- /dev/null +++ b/t/BrokenCookieJar.pm @@ -0,0 +1,24 @@ +package BrokenCookieJar; + +use strict; +use warnings; + +sub new { + my $class = shift; + return bless {} => $class; +} + +package BrokenCookieJar2; + +use strict; +use warnings; + +sub new { + my $class = shift; + return bless {} => $class; +} + +sub add { +} + +1; diff --git a/t/SimpleCookieJar.pm b/t/SimpleCookieJar.pm new file mode 100644 index 0000000..a4ffed4 --- /dev/null +++ b/t/SimpleCookieJar.pm @@ -0,0 +1,29 @@ +package SimpleCookieJar; + +use strict; +use warnings; + +sub new { + my $class = shift; + return bless {} => $class; +} + +sub add { + my ($self, $url, $cookie) = @_; + + my ($kv) = split qr/;/, $cookie; + my ($k, $v) = split qr/\s*=\s*/, $kv, 2; + + $self->{$url}{$k} = $v; +} + +sub cookie_header { + my ($self, $url) = @_; + + my $cookies = $self->{$url} + or return ''; + + return join( "; ", map{ "$_=$cookies->{$_}" } sort keys %$cookies ); +} + +1; diff --git a/t/Util.pm b/t/Util.pm new file mode 100644 index 0000000..2e85b04 --- /dev/null +++ b/t/Util.pm @@ -0,0 +1,180 @@ +package Util; + +use strict; +use warnings; + +use IO::File qw(SEEK_SET SEEK_END); +use IO::Dir; + +BEGIN { + our @EXPORT_OK = qw( + rewind + tmpfile + dir_list + slurp + parse_case + hashify + sort_headers + connect_args + clear_socket_source + set_socket_source + monkey_patch + $CRLF + $LF + ); + + require Exporter; + *import = \&Exporter::import; +} + +our $CRLF = "\x0D\x0A"; +our $LF = "\x0A"; + +sub rewind(*) { + seek($_[0], 0, SEEK_SET) + || die(qq/Couldn't rewind file handle: '$!'/); +} + +sub tmpfile { + my $fh = IO::File->new_tmpfile + || die(qq/Couldn't create a new temporary file: '$!'/); + + binmode($fh) + || die(qq/Couldn't binmode temporary file handle: '$!'/); + + if (@_) { + print({$fh} @_) + || die(qq/Couldn't write to temporary file handle: '$!'/); + + seek($fh, 0, SEEK_SET) + || die(qq/Couldn't rewind temporary file handle: '$!'/); + } + + return $fh; +} + +sub dir_list { + my ($dir, $filter) = @_; + $filter ||= qr/./; + my $d = IO::Dir->new($dir) + or return; + return map { "$dir/$_" } sort grep { /$filter/ } grep { /^[^.]/ } $d->read; +} + +sub slurp (*) { + my ($fh) = @_; + + seek($fh, 0, SEEK_END) + || die(qq/Couldn't navigate to EOF on file handle: '$!'/); + + my $exp = tell($fh); + + rewind($fh); + + binmode($fh) + || die(qq/Couldn't binmode file handle: '$!'/); + + my $buf = do { local $/; <$fh> }; + my $got = length $buf; + + ($exp == $got) + || die(qq[I/O read mismatch (expexted: $exp got: $got)]); + + return $buf; +} + +sub parse_case { + my ($case) = @_; + my %args; + my $key = ''; + for my $line ( split "\n", $case ) { + chomp $line; + if ( substr($line,0,1) eq q{ } ) { + $line =~ s/^\s+//; + push @{$args{$key}}, $line; + } + else { + $key = $line; + } + } + return \%args; +} + +sub hashify { + my ($lines) = @_; + return unless $lines; + my %hash; + for my $line ( @$lines ) { + my ($k,$v) = ($line =~ m{^([^:]+): (.*)$}g); + $hash{$k} = [ $hash{$k} ] if exists $hash{$k} && ref $hash{$k} ne 'ARRAY'; + if ( ref($hash{$k}) eq 'ARRAY' ) { + push @{$hash{$k}}, $v; + } + else { + $hash{$k} = $v; + } + } + return %hash; +} + +sub sort_headers { + my ($text) = shift; + my @lines = split /$CRLF/, $text; + my $request = shift(@lines) || ''; + my @headers; + while (my $line = shift @lines) { + last unless length $line; + push @headers, $line; + } + @headers = sort @headers; + return join($CRLF, $request, @headers, '', @lines); +} + +{ + my (@req_fh, @res_fh, $monkey_host, $monkey_port); + + sub clear_socket_source { + @req_fh = (); + @res_fh = (); + } + + sub set_socket_source { + my ($req_fh, $res_fh) = @_; + push @req_fh, $req_fh; + push @res_fh, $res_fh; + } + + sub connect_args { return ($monkey_host, $monkey_port) } + + sub monkey_patch { + no warnings qw/redefine once/; + *HTTP::Tiny::Handle::can_read = sub {1}; + *HTTP::Tiny::Handle::can_write = sub {1}; + *HTTP::Tiny::Handle::connect = sub { + my ($self, $scheme, $host, $port, $peer) = @_; + $self->{host} = $monkey_host = $host; + $self->{port} = $monkey_port = $port; + $self->{peer} = $peer; + $self->{scheme} = $scheme; + $self->{fh} = shift @req_fh; + $self->{pid} = $$; + $self->{tid} = HTTP::Tiny::Handle::_get_tid(); + return $self; + }; + my $original_write_request = \&HTTP::Tiny::Handle::write_request; + *HTTP::Tiny::Handle::write_request = sub { + my ($self, $request) = @_; + $original_write_request->($self, $request); + $self->{fh} = shift @res_fh; + }; + *HTTP::Tiny::Handle::close = sub { 1 }; # don't close our temps + + # don't try to proxy in mock-mode + delete $ENV{$_} for map { $_, uc($_) } qw/http_proxy https_proxy all_proxy/; + } +} + +1; + + +# vim: et ts=4 sts=4 sw=4: diff --git a/xt/author/00-compile.t b/xt/author/00-compile.t new file mode 100644 index 0000000..9382956 --- /dev/null +++ b/xt/author/00-compile.t @@ -0,0 +1,63 @@ +use 5.006; +use strict; +use warnings; + +# this test was generated with Dist::Zilla::Plugin::Test::Compile 2.058 + +use Test::More; + +plan tests => 2; + +my @module_files = ( + 'HTTP/Tiny.pm' +); + + + +# fake home for cpan-testers +use File::Temp; +local $ENV{HOME} = File::Temp::tempdir( CLEANUP => 1 ); + + +my @switches = ( + -d 'blib' ? '-Mblib' : '-Ilib', +); + +use File::Spec; +use IPC::Open3; +use IO::Handle; + +open my $stdin, '<', File::Spec->devnull or die "can't open devnull: $!"; + +my @warnings; +for my $lib (@module_files) +{ + # see L + my $stderr = IO::Handle->new; + + diag('Running: ', join(', ', map { my $str = $_; $str =~ s/'/\\'/g; q{'} . $str . q{'} } + $^X, @switches, '-e', "require q[$lib]")) + if $ENV{PERL_COMPILE_TEST_DEBUG}; + + my $pid = open3($stdin, '>&STDERR', $stderr, $^X, @switches, '-e', "require q[$lib]"); + binmode $stderr, ':crlf' if $^O eq 'MSWin32'; + my @_warnings = <$stderr>; + waitpid($pid, 0); + is($?, 0, "$lib loaded ok"); + + shift @_warnings if @_warnings and $_warnings[0] =~ /^Using .*\bblib/ + and not eval { +require blib; blib->VERSION('1.01') }; + + if (@_warnings) + { + warn @_warnings; + push @warnings, @_warnings; + } +} + + + +is(scalar(@warnings), 0, 'no warnings found') + or diag 'got warnings: ', ( Test::More->can('explain') ? Test::More::explain(\@warnings) : join("\n", '', @warnings) ); + + diff --git a/xt/author/critic.t b/xt/author/critic.t new file mode 100644 index 0000000..80ccdad --- /dev/null +++ b/xt/author/critic.t @@ -0,0 +1,7 @@ +#!perl + +use strict; +use warnings; + +use Test::Perl::Critic (-profile => "perlcritic.rc") x!! -e "perlcritic.rc"; +all_critic_ok(); diff --git a/xt/author/minimum-version.t b/xt/author/minimum-version.t new file mode 100644 index 0000000..d4a20c3 --- /dev/null +++ b/xt/author/minimum-version.t @@ -0,0 +1,6 @@ +#!perl + +use Test::More; + +use Test::MinimumVersion; +all_minimum_version_ok( qq{5.010} ); diff --git a/xt/author/pod-coverage.t b/xt/author/pod-coverage.t new file mode 100644 index 0000000..66b3b64 --- /dev/null +++ b/xt/author/pod-coverage.t @@ -0,0 +1,7 @@ +#!perl +# This file was automatically generated by Dist::Zilla::Plugin::PodCoverageTests. + +use Test::Pod::Coverage 1.08; +use Pod::Coverage::TrustPod; + +all_pod_coverage_ok({ coverage_class => 'Pod::Coverage::TrustPod' }); diff --git a/xt/author/pod-spell.t b/xt/author/pod-spell.t new file mode 100644 index 0000000..3d0708f --- /dev/null +++ b/xt/author/pod-spell.t @@ -0,0 +1,125 @@ +use strict; +use warnings; +use Test::More; + +# generated by Dist::Zilla::Plugin::Test::PodSpelling 2.007005 +use Test::Spelling 0.12; +use Pod::Wordlist; + + +add_stopwords(); +all_pod_files_spelling_ok( qw( bin lib ) ); +__DATA__ +Alan +Alders +Alessandro +Berry +Brad +Bright +CA +CGI +Chris +Christian +Claes +Clinton +Cook +Craig +David +Dean +Doherty +Edward +Eklund +Etheridge +Evans +Gardner +Ghedini +Gilbert +Golden +Gormley +Grazzini +HTTP +Hansen +Hukins +Jakobsson +James +Jeremy +Jess +Kaji +Karen +Kornetzki +Lukas +Martin +Mates +Mengué +Mike +Mitchell +Miyagawa +Nehren +Nicolas +Olaf +Olivier +Pearce +Petr +Písař +Raspass +Robinson +Rochelemagne +Serguei +Shoichi +Sinan +SkyMarshal +Steve +Syohei +Sören +Tatsuhiko +Tiny +Tom +Tony +Trouchelle +UA +Unur +Weyl +YOSHIDA +Zborowski +al3xbio +apeiron +bgills +brian +carlson +castaway +cberry +chansen +claes +clint +craigberry +cweyl +dagolden +davem +doherty +dolmen +ed +ether +gardner +jmates +jraspass +leklund +lib +miyagawa +mjegh +mlbright +nanis +olaf +pearce +ppisar +proxying +rochelemagne +sandals +skaji +skymarshal1729 +soeren +steve +stro +syohex +tom +tony +xdg diff --git a/xt/author/pod-syntax.t b/xt/author/pod-syntax.t new file mode 100644 index 0000000..e563e5d --- /dev/null +++ b/xt/author/pod-syntax.t @@ -0,0 +1,7 @@ +#!perl +# This file was automatically generated by Dist::Zilla::Plugin::PodSyntaxTests. +use strict; use warnings; +use Test::More; +use Test::Pod 1.41; + +all_pod_files_ok(); diff --git a/xt/author/portability.t b/xt/author/portability.t new file mode 100644 index 0000000..f6ac836 --- /dev/null +++ b/xt/author/portability.t @@ -0,0 +1,10 @@ +use strict; +use warnings; + +use Test::More; + +eval 'use Test::Portability::Files'; +plan skip_all => 'Test::Portability::Files required for testing portability' + if $@; +options(test_one_dot => 0); +run_tests(); diff --git a/xt/author/test-version.t b/xt/author/test-version.t new file mode 100644 index 0000000..247ba9a --- /dev/null +++ b/xt/author/test-version.t @@ -0,0 +1,23 @@ +use strict; +use warnings; +use Test::More; + +# generated by Dist::Zilla::Plugin::Test::Version 1.09 +use Test::Version; + +my @imports = qw( version_all_ok ); + +my $params = { + is_strict => 0, + has_version => 1, + multiple => 0, + +}; + +push @imports, $params + if version->parse( $Test::Version::VERSION ) >= version->parse('1.002'); + +Test::Version->import(@imports); + +version_all_ok; +done_testing; diff --git a/xt/release/distmeta.t b/xt/release/distmeta.t new file mode 100644 index 0000000..c2280dc --- /dev/null +++ b/xt/release/distmeta.t @@ -0,0 +1,6 @@ +#!perl +# This file was automatically generated by Dist::Zilla::Plugin::MetaTests. + +use Test::CPAN::Meta; + +meta_yaml_ok();