AnyEve...equest(3pm)
π AnyEvent::HTTPD::Request β the request handle glow-up π
bestie!! welcome to the manual page for AnyEvent::HTTPD::Request from the User Contributed Perl Documentation collection (that's the 3pm manual section, iykyk). This is the lil' guy that shows up in your callbacks and holds everything you need to know about the request that just rolled up. π
β¨ NAME β¨
AnyEvent::HTTPD::Request β a web application request handle for AnyEvent::HTTPD. That's it, that's the vibe. π¦
πΈ DESCRIPTION πΈ
So basically: this is the request object. AnyEvent::HTTPD generates it, and then hands it to you in the request callbacks. It's giving main character energy, honestly. π
π METHODS π
url
Returns the URL of the current request, as a URI object. Not a string, bestie β an actual object. Periodt. π
respond ([$res])
Okay this one is the big one, the icon, the moment. π $res can be:
- an array reference π
Then the array reference has these elements, in this order:
my ($code, $message, $header_hash, $content) =
[200, 'ok', { 'Content-Type' => 'text/html' }, '<h1>Test</h1>' }]
You can remove most headers added by default (like Cache-Control, Expires, and Content-Length) by setting them to undef, like so:
$req->respond([
200, 'OK', {
'Content-Type' => 'text/html',
'Cache-Control' => 'max-age=3600',
'Expires' => undef,
},
'This data will be cached for one hour.'
]);
- a hash reference π§
If it was a hash reference, the hash is first searched for the redirect key, and if that key does not exist, for the content key.
The value for the redirect key should contain the URL that you want to redirect the request to.
The value for the content key should contain an array reference with the first value being the content type and the second the content.
Here is an example:
$httpd->reg_cb (
'/image/elmex' => sub {
my ($httpd, $req) = @_;
open IMG, "$ENV{HOME}/media/images/elmex.png"
or $req->respond (
[404, 'not found', { 'Content-Type' => 'text/plain' }, 'not found']
);
$req->respond ({ content => ['image/png', do { local $/; <IMG> }] });
}
);
π How to send large files π
For longer responses you can give a callback instead of a string to the response function for the value of the $content.
$req->respond ({ content => ['video/x-ms-asf', sub {
my ($data_cb) = @_;
# start some async retrieve operation, for example use
# IO::AIO (with AnyEvent::AIO). Or retrieve chunks of data
# to send somehow else.
} });
The given callback will receive as first argument either another callback ($data_cb in the above example) or an undefined value, which means that there is no more data required and the transfer has been completed (either by you sending no more data, or by a disconnect of the client). π
The callback given to respond will be called whenever the send queue of the HTTP connection becomes empty (meaning that the data is written out to the kernel). If it is called you have to start delivering the next chunk of data.
That doesn't have to be immediately, before the callback returns. This means that you can initiate for instance an IO::AIO request (see also AnyEvent::AIO) and send the data later. That is what the $data_cb callback is for. You have to call it once you got the next chunk of data. Once you sent a chunk of data via $data_cb you can just wait until your callback is called again to deliver the next chunk. π«Ά
If you are done transferring all data call the $data_cb with an empty string or with no argument at all.
Please consult the example script large_response_example from the samples/ directory of the AnyEvent::HTTPD distribution for an example of how to use this mechanism. Slay. β¨
NOTE: You should supply a 'Content-Length' header if you are going to send a larger file. If you don't do that the client will have no chance to know if the transfer was complete. To supply additional header fields the hash argument format will not work. You should use the array argument format for this case. Do not skip this one, bestie, fr fr. π
responded
Returns true if this request already has been responded to. Like, has it been answered? Yes or no, no cap. β
parm ($key)
Returns the first value of the form parameter $key, or undef. πΈ
params
Returns a list of parameter names. Just the names, bestie. π
vars
Returns a hash of form parameters. The value is either the value of the parameter, and in case there are multiple values present it will contain an array reference of values. So sometimes it's one value, sometimes it's a whole squad. π
method
Returns the method of the current request. (GET, POST, whatever it's giving.) π¦
content
Returns the request content, or undef if only parameters for a form were transmitted. π
headers
Returns a hash reference containing the HTTP headers for this HTTP request. All the tea, in hash form. π
client_host
Returns the host/IP of the HTTP client this request was received from. Where did you come from, diva? π
client_port
Returns the TCP port number of the HTTP client this request was received from. And what port were you on? π₯
π COPYRIGHT & LICENSE π
Copyright 2008-2011 Robin Redeker, all rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
okay that's the whole page bestie, you ate that π β¨ This was served up from User Contributed Perl Documentation, perl v5.34.0, dated 2022-06-05 β and no, we are not copying that footer line verbatim, we're just giving it a shoutout on the way out. bye bestie, go write some callbacks π«ΆπΈπ
π¦ where this page comes from
- debian bookworm: libanyevent-httpd-perl 0.93-6 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libanyevent-httpd-perl 0.93-6 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libanyevent-httpd-perl 0.93-6 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libanyevent-httpd-perl 0.93-6 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libanyevent-httpd-perl 0.93-6 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms