Hijk(3pm)

πŸ“¦
version 0.28 Β· 4 versions
, in debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking

✨ Hijk(3pm) ✨ β€” Fast & minimal low-level HTTP client πŸ’…

okay so bestie, this is from the User Contributed Perl Documentation, and it's giving iconic tiny HTTP client energy. no cap. πŸ¦‹


🌸 NAME 🌸

Hijk β€” Fast & minimal low-level HTTP client πŸŽ€ slay.

πŸ’– SYNOPSIS πŸ’–

A simple GET request:

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 an 'OK' response" unless $res->{status} == 200;

say $res->{body};

A POST request, you have to manually set the appropriate headers, URI escape your values etc. not me letting you do all that by hand πŸ™ˆ

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's hope it's exciting"),
});

die "Expecting an 'OK' response" unless $res->{status} == 200;

πŸ’– DESCRIPTION πŸ’–

Hijk is a fast & minimal low-level HTTP 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. periodt β€” main character energy fr fr. πŸ’œ

It is NOT a general HTTP user agent, it doesn't support redirects, proxies, SSL and any number of other advanced HTTP 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 HTTP over sockets. lowkey unhinged but also iconic πŸ’€

Having said that it's lightning fast and extensively used in production at Booking.com (https://www.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. ate. πŸ”₯

πŸ¦‹ FUNCTION: Hijk::request( $args :HashRef ) :HashRef πŸ¦‹

Hijk::request 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. iykyk πŸ‘‘

It takes a HashRef of arguments and either dies or returns a HashRef as a response. vibe check passed. ✨

The HashRef argument to it must contain some of the key-value pairs from the following list. The value for host and port are mandatory, but others are optional with default values listed below. ngl the defaults are lowkey important. 🌸

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           => \%Hijk::SOCKET_CACHE, # (undef to disable, or \my %your_socket_cache)
on_connect             => undef, # (or sub { ... })
parse_chunked          => 0,
head_as_array          => 0,
no_default_host_header => 1,

Notice how Hijk does not take a full URI string as input, you have to specify the individual parts of the URL. Users who need to parse an existing URI string to produce a request should use the URI module to do so. 🧚

The value of head is an ArrayRef of key-value pairs instead of a HashRef, this way you can decide in which order the headers are sent, and you can send the same header name multiple times. For example:

head => [
    "Content-Type" => "application/json",
    "X-Requested-With" => "Hijk",
]

Will produce these request headers:

Content-Type: application/json
X-Requested-With: Hijk

In addition Hijk will provide a Host header for you by default with the host value you pass to request(). To suppress this (e.g. to send custom Host requests) pass a true value to the no_default_host_header option and provide your own Host header in the head ArrayRef (or don't, if you want to construct a Host-less request knock yourself out...). πŸ™ˆ

Hijk doesn't escape any values for you, it just passes them through as-is. the way that it just raw-dogs your strings is wild. You can easily produce invalid requests if e.g. any of these strings contain a newline, or aren't otherwise properly escaped. 😭

The value of connect_timeout or read_timeout 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 undef, meaning no timeout limit. If you don't supply these timeouts and the host really is unreachable or slow, we'll reach the TCP timeout limit before returning some other error to you. ⏳

The default protocol is HTTP/1.1, but you can also specify HTTP/1.0. The advantage of using HTTP/1.1 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 HTTP/1.0, or specify Connection: close in the request, but just using HTTP/1.0 is an easy way to accomplish the same thing. highkey a mood 🌈

By default we will provide a socket_cache for you which is a global singleton that we maintain keyed on join($;, $$, $host, $port). Alternatively you can pass in socket_cache hash of your own which we'll use as the cache. To completely disable the cache pass in undef. πŸŽ€

The optional on_connect callback is intended to be used for you to figure out from production traffic what you should set the connect_timeout. I.e. you can start a timer when you call Hijk::request() that you end when on_connect 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 Hijk::request() 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 read_timeout. The on_connect callback is provided with no arguments, and is called in void context. ⏱️

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 chunked_transfer_encoding off to the nginx config file. obsessed with an experimental codepath? maybe not but okay πŸ§ͺ

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 parse_chunked. Otherwise Hijk will die when it encounters chunked responses. The parse_chunked option may be turned on by default in the future. not me warning you πŸ‘€

The return value is a HashRef representing a response. It contains the following key-value pairs.

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

For example, to send a request to http://example.com/flower?color=red, pass the following parameters:

my $res = Hijk::request({
    host         => "example.com",
    port         => "80",
    path         => "/flower",
    query_string => "color=red"
});
die "Response is not 'OK'" unless $res->{status} == 200;

Notice that you do not need to put the leading ? character in the query_string. You do, however, need to properly uri_escape the content of query_string. 🌸

Again, Hijk doesn't escape any values for you, so these values MUST be properly escaped before being passed in, unless you want to issue invalid requests. literally begging you to escape them 🚨

By default the head in the response is a HashRef rather then an ArrayRef. 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 ArrayRef instead specify head_as_array. fr fr πŸ’…

If you want to fiddle with the read_length value it controls how much we POSIX::read($fd, $buf, $read_length) at a time. πŸ“–

We currently don't support servers returning a http body without an accompanying Content-Length header; bodies MUST have a Content-Length or we won't pick them up. πŸ’€

πŸ’– ERROR CODES πŸ’–

ok bestie, listen. if we hit a recoverable oopsie, we don't just ghost you β€” we drop an "error" key into the response and its value is a bitfield that you get to check against the Hijk::Error::* constants. it's giving very "here's your receipt, ma'am" energy. the whole squad is:

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

also we might hand you back "error_message", "errno_number" and "errno_string" keys β€” there's a whole discussion about the "Hijk::Error::REQUEST_*" and "Hijk::Error::RESPONSE_*" errors down below, don't skip it 🌸

the "Hijk::Error::TIMEOUT" constant is literally just "Hijk::Error::CONNECT_TIMEOUT | Hijk::Error::READ_TIMEOUT". it exists purely for convenience, so you can write:

.. if exists $res->{error} and $res->{error} & Hijk::Error::TIMEOUT;

instead of the way more yappy:

.. if exists $res->{error} and $res->{error} & (Hijk::Error::CONNECT_TIMEOUT | Hijk::Error::READ_TIMEOUT)

we love a shortcut πŸ’…

we'll return "Hijk::Error::CANNOT_RESOLVE" when we can't "gethostbyname()" the host you gave us. like, the name just does not resolve. it's not us, it's DNS.

if we flop on a "select()" or a "write()" while we're sending the request, you get "Hijk::Error::REQUEST_SELECT_ERROR" or "Hijk::Error::REQUEST_WRITE_ERROR" respectively. and just like "Hijk::Error::TIMEOUT", the "Hijk::Error::REQUEST_ERROR" constant is a union of those two plus any other request errors we might add in the future. foreshadowing, but make it friendly.

when we're pulling the response back, "Hijk::Error::RESPONSE_READ_ERROR" shows up when we can't "read()" the response, and "Hijk::Error::RESPONSE_BAD_READ_VALUE" when the value we got back from "read()" is 0. the "Hijk::Error::RESPONSE_ERROR" constant is the union of those two and any other response errors we might add in the future. same deal.

some of these "Hijk::Error::REQUEST_*" and "Hijk::Error::RESPONSE_*" errors are actually re-thrown errors from system calls, bestie. when that happens we also pass along "error_message" β€” a short, human-readable lil description of what went wrong β€” plus "errno_number" and "errno_string", which are "$!+0" and "$!" at the exact moment we had the error. timestamps for your trauma, basically.

Hijk might run into other errors along the way and it WILL call "die" if that happens. so if you don't want your whole program to just perish when a request like that fails, wrap it in "eval". no cap.

ok but the whole point of the "Hijk::Error::*" interface is that every error that happens 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 getting suddenly kill -9'd β€” should be caught, categorized, and returned in a structural way by Hijk. that's the assignment. it ate. ✨

we're not currently aware of any issues that occur in such normal operations that aren't classified as a "Hijk::Error::*", and if we find new issues that fit the criteria above, we'll likely just make a new "Hijk::Error::*" for it. new error constant just dropped πŸ’…

we're just not trying to guarantee that the library can never "die", and we aren't trying to catch truly exceptional issues like, e.g., "fcntl()" failing on a valid socket. some things are simply not our business πŸ™ˆ


πŸ‘‘ AUTHORS πŸ‘‘

  • Kang-min Liu <gugod@gugod.org>
  • AEvar Arnfjoer` Bjarmason <avar@cpan.org>
  • Borislav Nikolov <jack@sofialondonmoskva.com>
  • Damian Gryski <damian@gryski.com>

the icons who built this. bow down 🫢


Copyright (c) 2013- Kang-min Liu "<gugod@gugod.org>".


⭐ LICENCE ⭐

The MIT License. short, sweet, iconic, periodt.


🚨 DISCLAIMER OF WARRANTY 🚨

ok so this part is ALL CAPS because lawyers, not because I'm yelling at you. it means business. read it fr fr:

BECAUSE 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 "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 SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION.

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 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 (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 SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.

tl;dr: free software, zero warranty, the risk is yours, and nobody's paying for your damages. we said what we said πŸ’€

and that's the whole page, bestie!! πŸŽ€ this one's from perl v5.36.0, dated 2022-12-12 β€” yes, that's the version and the date right there, your girl checked. go make some requests, check your "error" bitfields, and stay hydrated. slay responsibly πŸ’–βœ¨

πŸ“¦ where this page comes from