.\" Automatically generated by Pod::Man 2.28 (Pod::Simple 3.28) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is turned on, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{ . if \nF \{ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). .\" Fear. Run. Save yourself. No user-serviceable parts. . \" fudge factors for nroff and troff .if n \{\ . ds #H 0 . ds #V .8m . ds #F .3m . ds #[ \f1 . ds #] \fP .\} .if t \{\ . ds #H ((1u-(\\\\n(.fu%2u))*.13m) . ds #V .6m . ds #F 0 . ds #[ \& . ds #] \& .\} . \" simple accents for nroff and troff .if n \{\ . ds ' \& . ds ` \& . ds ^ \& . ds , \& . ds ~ ~ . ds / .\} .if t \{\ . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' .\} . \" troff and (daisy-wheel) nroff accents .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' .ds 8 \h'\*(#H'\(*b\h'-\*(#H' .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] .ds ae a\h'-(\w'a'u*4/10)'e .ds Ae A\h'-(\w'A'u*4/10)'E . \" corrections for vroff .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' . \" for low resolution devices (crt and lpr) .if \n(.H>23 .if \n(.V>19 \ \{\ . ds : e . ds 8 ss . ds o a . ds d- d\h'-1'\(ga . ds D- D\h'-1'\(hy . ds th \o'bp' . ds Th \o'LP' . ds ae ae . ds Ae AE .\} .rm #[ #] #H #V #F C .\" ======================================================================== .\" .IX Title "Hijk 3pm" .TH Hijk 3pm "2014-08-31" "perl v5.20.1" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Hijk \- Fast & minimal low\-level HTTP client .SH "SYNOPSIS" .IX Header "SYNOPSIS" A simple \s-1GET\s0 request: .PP .Vb 8 \& use Hijk (); \& my $res = Hijk::request({ \& method => "GET", \& host => "example.com", \& port => "80", \& path => "/flower", \& query_string => "color=red" \& }); \& \& if (exists $res\->{error} and $res\->{error} & Hijk::Error::TIMEOUT) { \& die "Oh noes we had some sort of timeout"; \& } \& \& die "Expecting a successful response" unless $res\->{status} == 200; \& \& say $res\->{body}; .Ve .PP A \s-1POST\s0 request, you have to manually set the appropriate headers, \s-1URI\s0 escape your values etc. .PP .Vb 2 \& use Hijk (); \& use URI::Escape qw(uri_escape); \& \& my $res = Hijk::request({ \& method => "POST", \& host => "example.com", \& port => "80", \& path => "/new", \& head => [ "Content\-Type" => "application/x\-www\-form\-urlencoded" ], \& query_string => "type=flower&bucket=the%20one%20out%20back", \& body => "description=" . uri_escape("Another flower, let\*(Aqs hope it\*(Aqs exciting"), \& }); \& \& die "Expecting a successful response" unless $res\->{status} == 200; .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" Hijk is a fast & minimal low-level \s-1HTTP\s0 client intended to be used where you control both the client and the server, e.g. for talking to some internal service from a frontend user-facing web application. .PP It is \f(CW\*(C`NOT\*(C'\fR a general \s-1HTTP\s0 user agent, it doesn't support redirects, proxies, \s-1SSL\s0 and any number of other advanced \s-1HTTP\s0 features like (in roughly descending order of feature completeness) LWP::UserAgent, WWW::Curl, HTTP::Tiny, HTTP::Lite or Furl. This library is basically one step above manually talking \s-1HTTP\s0 over sockets. .PP Having said that it's lightning fast and extensively used in production at Booking.com where it's used as the go-to transport layer for talking to internal services. It uses non-blocking sockets and correctly handles all combinations of connect/read timeouts and other issues you might encounter from various combinations of parts of your system going down or becoming otherwise unavailable. .ie n .SH "FUNCTION: Hijk::request( $args :HashRef ) :HashRef" .el .SH "FUNCTION: Hijk::request( \f(CW$args\fP :HashRef ) :HashRef" .IX Header "FUNCTION: Hijk::request( $args :HashRef ) :HashRef" \&\f(CW\*(C`Hijk::request\*(C'\fR is the only function you should use. It (or anything else in this package for that matter) is not exported, so you have to use the fully qualified name. .PP It takes a \f(CW\*(C`HashRef\*(C'\fR of arguments and either dies or returns a \&\f(CW\*(C`HashRef\*(C'\fR as a response. .PP The \f(CW\*(C`HashRef\*(C'\fR argument to it must contain some of the key-value pairs from the following list. The value for \f(CW\*(C`host\*(C'\fR and \f(CW\*(C`port\*(C'\fR are mandatory, but others are optional with default values listed below. .PP .Vb 10 \& protocol => "HTTP/1.1", # (or "HTTP/1.0") \& host => ..., \& port => ..., \& connect_timeout => undef, \& read_timeout => undef, \& read_length => 10240, \& method => "GET", \& path => "/", \& query_string => "", \& head => [], \& body => "", \& socket_cache => \e%Hijk::SOCKET_CACHE, # (undef to disable, or \emy %your_socket_cache) \& on_connect => undef, # (or sub { ... }) \& parse_chunked => 0, \& head_as_array => 0, .Ve .PP Notice how Hijk does not take a full \s-1URI\s0 string as input, you have to specify the individual parts of the \s-1URL.\s0 Users who need to parse an existing \s-1URI\s0 string to produce a request should use the \s-1URI\s0 module to do so. .PP The value of \f(CW\*(C`head\*(C'\fR is an \f(CW\*(C`ArrayRef\*(C'\fR of key-value pairs instead of a \&\f(CW\*(C`HashRef\*(C'\fR, this way you can decide in which order the headers are sent, and you can send the same header name multiple times. For example: .PP .Vb 4 \& head => [ \& "Content\-Type" => "application/json", \& "X\-Requested\-With" => "Hijk", \& ] .Ve .PP Will produce these request headers: .PP .Vb 2 \& Content\-Type: application/json \& X\-Requested\-With: Hijk .Ve .PP Hijk doesn't escape any values for you, it just passes them through as-is. You can easily produce invalid requests if e.g. any of these strings contain a newline, or aren't otherwise properly escaped. .PP The value of \f(CW\*(C`connect_timeout\*(C'\fR or \f(CW\*(C`read_timeout\*(C'\fR is in floating point seconds, and is used as the time limit for connecting to the host, and reading the response back from it, respectively. The default value for both is \f(CW\*(C`undef\*(C'\fR, meaning no timeout limit. If you don't supply these timeouts and the host really is unreachable or slow, we'll reach the \s-1TCP\s0 timeout limit before returning some other error to you. .PP The default \f(CW\*(C`protocol\*(C'\fR is \f(CW\*(C`HTTP/1.1\*(C'\fR, but you can also specify \&\f(CW\*(C`HTTP/1.0\*(C'\fR. The advantage of using \f(CW\*(C`HTTP/1.1\*(C'\fR is support for keep-alive, which matters a lot in environments where the connection setup represents non-trivial overhead. Sometimes that overhead is negligible (e.g. on Linux talking to an nginx on the local network), and keeping open connections down and reducing complexity is more important, in those cases you can either use \f(CW\*(C`HTTP/1.0\*(C'\fR, or specify \&\f(CW\*(C`Connection: close\*(C'\fR in the request, but just using \f(CW\*(C`HTTP/1.0\*(C'\fR is an easy way to accomplish the same thing. .PP By default we will provide a \f(CW\*(C`socket_cache\*(C'\fR for you which is a global singleton that we maintain keyed on \f(CW\*(C`join($;, $$, $host, $port)\*(C'\fR. Alternatively you can pass in \f(CW\*(C`socket_cache\*(C'\fR hash of your own which we'll use as the cache. To completely disable the cache pass in \&\f(CW\*(C`undef\*(C'\fR. .PP The optional \f(CW\*(C`on_connect\*(C'\fR callback is intended to be used for you to figure out from production traffic what you should set the \&\f(CW\*(C`connect_timeout\*(C'\fR. I.e. you can start a timer when you call \&\f(CW\*(C`Hijk::request()\*(C'\fR that you end when \f(CW\*(C`on_connect\*(C'\fR is called, that's how long it took us to get a connection. If you start another timer in that callback that you end when \f(CW\*(C`Hijk::request()\*(C'\fR returns to you that'll give you how long it took to send/receive data after we constructed the socket, i.e. it'll help you to tweak your \&\f(CW\*(C`read_timeout\*(C'\fR. The \f(CW\*(C`on_connect\*(C'\fR callback is provided with no arguments, and is called in void context. .PP We have experimental support for parsing chunked responses encoding. historically Hijk didn't support this at all and if you wanted to use it with e.g. nginx you had to add \&\f(CW\*(C`chunked_transfer_encoding off\*(C'\fR to the nginx config file. .PP Since you may just want to do that instead of having Hijk do more work to parse this out with a more complex and experimental codepath you have to explicitly enable it with \f(CW\*(C`parse_chunked\*(C'\fR. Otherwise Hijk will die when it encounters chunked responses. The \f(CW\*(C`parse_chunked\*(C'\fR option may be turned on by default in the future. .PP The return value is a \f(CW\*(C`HashRef\*(C'\fR representing a response. It contains the following key-value pairs. .PP .Vb 8 \& proto => :Str \& status => :StatusCode \& body => :Str \& head => :HashRef (or :ArrayRef with "head_as_array") \& error => :PositiveInt \& error_message => :Str \& errno_number => :Int \& errno_string => :Str .Ve .PP For example, to send request to \&\f(CW\*(C`http://example.com/flower?color=red\*(C'\fR, do the following: .PP .Vb 7 \& my $res = Hijk::request({ \& host => "example.com", \& port => "80", \& path => "/flower", \& query_string => "color=red" \& }); \& die "Response is not OK" unless $res\->{status} == 200; .Ve .PP Notice that you do not need to put the leading \f(CW"?"\fR character in the \&\f(CW\*(C`query_string\*(C'\fR. You do, however, need to properly \f(CW\*(C`uri_escape\*(C'\fR the content of \&\f(CW\*(C`query_string\*(C'\fR. .PP Again, Hijk doesn't escape any values for you, so these values \fB\s-1MUST\s0\fR be properly escaped before being passed in, unless you want to issue invalid requests. .PP By default the \f(CW\*(C`head\*(C'\fR in the response is a \f(CW\*(C`HashRef\*(C'\fR rather then an \&\f(CW\*(C`ArrayRef\*(C'\fR. This makes it easier to retrieve specific header fields, but it means that we'll clobber any duplicated header names with the most recently seen header value. To get the returned headers as an \&\f(CW\*(C`ArrayRef\*(C'\fR instead specify \f(CW\*(C`head_as_array\*(C'\fR. .PP If you want to fiddle with the \f(CW\*(C`read_length\*(C'\fR value it controls how much we \f(CW\*(C`POSIX::read($fd, $buf, $read_length)\*(C'\fR at a time. .PP We currently don't support servers returning a http body without an accompanying \&\f(CW\*(C`Content\-Length\*(C'\fR header; bodies \fB\s-1MUST\s0\fR have a \f(CW\*(C`Content\-Length\*(C'\fR or we won't pick them up. .SH "ERROR CODES" .IX Header "ERROR CODES" If we had a recoverable error we'll include an \*(L"error\*(R" key whose value is a bitfield that you can check against Hijk::Error::* constants. Those are: .PP .Vb 10 \& Hijk::Error::CONNECT_TIMEOUT \& Hijk::Error::READ_TIMEOUT \& Hijk::Error::TIMEOUT \& Hijk::Error::CANNOT_RESOLVE \& Hijk::Error::REQUEST_SELECT_ERROR \& Hijk::Error::REQUEST_WRITE_ERROR \& Hijk::Error::REQUEST_ERROR \& Hijk::Error::RESPONSE_READ_ERROR \& Hijk::Error::RESPONSE_BAD_READ_VALUE \& Hijk::Error::RESPONSE_ERROR .Ve .PP In addition we might return \f(CW\*(C`error_message\*(C'\fR, \f(CW\*(C`errno_number\*(C'\fR and \&\f(CW\*(C`errno_string\*(C'\fR keys, see the discussion of \f(CW\*(C`Hijk::Error::REQUEST_*\*(C'\fR and \f(CW\*(C`Hijk::Error::RESPONSE_*\*(C'\fR errors below. .PP The \f(CW\*(C`Hijk::Error::TIMEOUT\*(C'\fR constant is the same as \&\f(CW\*(C`Hijk::Error::CONNECT_TIMEOUT | Hijk::Error::READ_TIMEOUT\*(C'\fR. It's there for convenience so you can do: .PP .Vb 1 \& .. if exists $res\->{error} and $res\->{error} & Hijk::Error::TIMEOUT; .Ve .PP Instead of the more verbose: .PP .Vb 1 \& .. if exists $res\->{error} and $res\->{error} & (Hijk::Error::CONNECT_TIMEOUT | Hijk::Error::READ_TIMEOUT) .Ve .PP We'll return \f(CW\*(C`Hijk::Error::CANNOT_RESOLVE\*(C'\fR if we can't \&\f(CW\*(C`gethostbyname()\*(C'\fR the host you've provided. .PP If we fail to do a \f(CW\*(C`select()\*(C'\fR or \f(CW\*(C`write()\*(C'\fR during when sending the response we'll return \f(CW\*(C`Hijk::Error::REQUEST_SELECT_ERROR\*(C'\fR or \&\f(CW\*(C`Hijk::Error::REQUEST_WRITE_ERROR\*(C'\fR, respectively. Similarly to \&\f(CW\*(C`Hijk::Error::TIMEOUT\*(C'\fR the \f(CW\*(C`Hijk::Error::REQUEST_ERROR\*(C'\fR constant is a union of these two, and any other request errors we might add in the future. .PP When we're getting the response back we'll return \&\f(CW\*(C`Hijk::Error::RESPONSE_READ_ERROR\*(C'\fR when we can't \f(CW\*(C`read()\*(C'\fR the response, and \f(CW\*(C`Hijk::Error::RESPONSE_BAD_READ_VALUE\*(C'\fR when the value we got from \f(CW\*(C`read()\*(C'\fR is \f(CW0\fR. The \f(CW\*(C`Hijk::Error::RESPONSE_ERROR\*(C'\fR constant is a union of these two and any other response errors we might add in the future. .PP Some of these \f(CW\*(C`Hijk::Error::REQUEST_*\*(C'\fR and \f(CW\*(C`Hijk::Error::RESPONSE_*\*(C'\fR errors are re-thrown errors from system calls. In that case we'll also pass along \f(CW\*(C`error_message\*(C'\fR which is a short human readable error message about the error, as well as \f(CW\*(C`errno_number\*(C'\fR & \f(CW\*(C`errno_string\*(C'\fR, which are \f(CW\*(C`$!+0\*(C'\fR and \f(CW"$!"\fR at the time we had the error. .PP Hijk might encounter other errors during the course of the request and \&\f(CW\*(C`WILL\*(C'\fR call \f(CW\*(C`die\*(C'\fR if that happens, so if you don't want your program to stop when a request like that fails wrap it in \f(CW\*(C`eval\*(C'\fR. .PP Having said that the point of the \f(CW\*(C`Hijk::Error::*\*(C'\fR interface is that all errors that happen during normal operation, i.e. making valid requests against servers where you can have issues like timeouts, network blips or the server thread on the other end being suddenly kill \-9'd should be caught, categorized and returned in a structural way by Hijk. .PP We're not currently aware of any issues that occur in such normal operations that aren't classified as a \f(CW\*(C`Hijk::Error::*\*(C'\fR, and if we find new issues that fit the criteria above we'll likely just make a new \f(CW\*(C`Hijk::Error::*\*(C'\fR for it. .PP We're just not trying to guarantee that the library can never \f(CW\*(C`die\*(C'\fR, and aren't trying to catch truly exceptional issues like e.g. \f(CW\*(C`fcntl()\*(C'\fR failing on a valid socket. .SH "AUTHORS" .IX Header "AUTHORS" .IP "Kang-min Liu " 4 .IX Item "Kang-min Liu " .PD 0 .IP "\*(Aevar Arnfjo\*:r\*(d- Bjarmason " 4 .IX Item "var Arnfjo:r Bjarmason " .IP "Borislav Nikolov " 4 .IX Item "Borislav Nikolov " .IP "Damian Gryski " 4 .IX Item "Damian Gryski " .PD .SH "COPYRIGHT" .IX Header "COPYRIGHT" Copyright (c) 2013 Kang-min Liu \f(CW\*(C`\*(C'\fR. .SH "LICENCE" .IX Header "LICENCE" The \s-1MIT\s0 License .SH "DISCLAIMER OF WARRANTY" .IX Header "DISCLAIMER OF WARRANTY" \&\s-1BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE SOFTWARE \*(L"AS IS\*(R" 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 SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION.\s0 .PP \&\s-1IN 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 SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE SOFTWARE \s0(\s-1INCLUDING 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 SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE\s0), \s-1EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.\s0