.\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.42) .\" .\" 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 >0, 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 .\" ======================================================================== .\" .IX Title "AnyEvent::HTTPD::Request 3pm" .TH AnyEvent::HTTPD::Request 3pm "2022-06-05" "perl v5.34.0" "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" AnyEvent::HTTPD::Request \- A web application request handle for AnyEvent::HTTPD .SH "DESCRIPTION" .IX Header "DESCRIPTION" This is the request object as generated by AnyEvent::HTTPD and given in the request callbacks. .SH "METHODS" .IX Header "METHODS" .IP "\fBurl\fR" 4 .IX Item "url" This method returns the \s-1URL\s0 of the current request as \s-1URI\s0 object. .IP "\fBrespond ([$res])\fR" 4 .IX Item "respond ([$res])" \&\f(CW$res\fR can be: .RS 4 .IP "\(bu" 4 an array reference .Sp Then the array reference has these elements: .Sp .Vb 2 \& my ($code, $message, $header_hash, $content) = \& [200, \*(Aqok\*(Aq, { \*(AqContent\-Type\*(Aq => \*(Aqtext/html\*(Aq }, \*(Aq

Test

\*(Aq }] .Ve .Sp You can remove most headers added by default (like \f(CW\*(C`Cache\-Control\*(C'\fR, \&\f(CW\*(C`Expires\*(C'\fR, and \f(CW\*(C`Content\-Length\*(C'\fR) by setting them to undef, like so: .Sp .Vb 8 \& $req\->respond([ \& 200, \*(AqOK\*(Aq, { \& \*(AqContent\-Type\*(Aq => \*(Aqtext/html\*(Aq, \& \*(AqCache\-Control\*(Aq => \*(Aqmax\-age=3600\*(Aq, \& \*(AqExpires\*(Aq => undef, \& }, \& \*(AqThis data will be cached for one hour.\*(Aq \& ]); .Ve .IP "\(bu" 4 a hash reference .Sp If it was a hash reference the hash is first searched for the \f(CW\*(C`redirect\*(C'\fR key and if that key does not exist for the \f(CW\*(C`content\*(C'\fR key. .Sp The value for the \f(CW\*(C`redirect\*(C'\fR key should contain the \s-1URL\s0 that you want to redirect the request to. .Sp The value for the \f(CW\*(C`content\*(C'\fR key should contain an array reference with the first value being the content type and the second the content. .RE .RS 4 .Sp Here is an example: .Sp .Vb 3 \& $httpd\->reg_cb ( \& \*(Aq/image/elmex\*(Aq => sub { \& my ($httpd, $req) = @_; \& \& open IMG, "$ENV{HOME}/media/images/elmex.png" \& or $req\->respond ( \& [404, \*(Aqnot found\*(Aq, { \*(AqContent\-Type\*(Aq => \*(Aqtext/plain\*(Aq }, \*(Aqnot found\*(Aq] \& ); \& \& $req\->respond ({ content => [\*(Aqimage/png\*(Aq, do { local $/; }] }); \& } \& ); .Ve .Sp \&\fBHow to send large files:\fR .Sp For longer responses you can give a callback instead of a string to the response function for the value of the \f(CW$content\fR. .Sp .Vb 2 \& $req\->respond ({ content => [\*(Aqvideo/x\-ms\-asf\*(Aq, 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. \& \& } }); .Ve .Sp The given callback will receive as first argument either another callback (\f(CW$data_cb\fR 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). .Sp The callback given to \f(CW\*(C`respond\*(C'\fR will be called whenever the send queue of the \&\s-1HTTP\s0 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. .Sp That doesn't have to be immediately, before the callback returns. This means that you can initiate for instance an \s-1IO::AIO\s0 request (see also AnyEvent::AIO) and send the data later. That is what the \f(CW$data_cb\fR callback is for. You have to call it once you got the next chunk of data. Once you sent a chunk of data via \f(CW$data_cb\fR you can just wait until your callback is called again to deliver the next chunk. .Sp If you are done transferring all data call the \f(CW$data_cb\fR with an empty string or with no argument at all. .Sp Please consult the example script \f(CW\*(C`large_response_example\*(C'\fR from the \&\f(CW\*(C`samples/\*(C'\fR directory of the AnyEvent::HTTPD distribution for an example of how to use this mechanism. .Sp \&\fB\s-1NOTE:\s0\fR 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. .RE .IP "\fBresponded\fR" 4 .IX Item "responded" Returns true if this request already has been responded to. .IP "\fBparm ($key)\fR" 4 .IX Item "parm ($key)" Returns the first value of the form parameter \f(CW$key\fR or undef. .IP "\fBparams\fR" 4 .IX Item "params" Returns list of parameter names. .IP "\fBvars\fR" 4 .IX Item "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. .IP "\fBmethod\fR" 4 .IX Item "method" This method returns the method of the current request. .IP "\fBcontent\fR" 4 .IX Item "content" Returns the request content or undef if only parameters for a form were transmitted. .IP "\fBheaders\fR" 4 .IX Item "headers" This method will return a hash reference containing the \s-1HTTP\s0 headers for this \&\s-1HTTP\s0 request. .IP "\fBclient_host\fR" 4 .IX Item "client_host" This method returns the host/IP of the \s-1HTTP\s0 client this request was received from. .IP "\fBclient_port\fR" 4 .IX Item "client_port" This method returns the \s-1TCP\s0 port number of the \s-1HTTP\s0 client this request was received from. .SH "COPYRIGHT & LICENSE" .IX Header "COPYRIGHT & LICENSE" Copyright 2008\-2011 Robin Redeker, all rights reserved. .PP This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.