.\" 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 3pm" .TH AnyEvent::HTTPD 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 \- A simple lightweight event based web (application) server .SH "VERSION" .IX Header "VERSION" Version 0.93 .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 1 \& use AnyEvent::HTTPD; \& \& my $httpd = AnyEvent::HTTPD\->new (port => 9090); \& \& $httpd\->reg_cb ( \& \*(Aq/\*(Aq => sub { \& my ($httpd, $req) = @_; \& \& $req\->respond ({ content => [\*(Aqtext/html\*(Aq, \& "

Hello World!

" \& . "another test page" \& . "" \& ]}); \& }, \& \*(Aq/test\*(Aq => sub { \& my ($httpd, $req) = @_; \& \& $req\->respond ({ content => [\*(Aqtext/html\*(Aq, \& "

Test page

" \& . "Back to the main page" \& . "" \& ]}); \& }, \& ); \& \& $httpd\->run; # making a AnyEvent condition variable would also work .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" This module provides a simple \s-1HTTPD\s0 for serving simple web application interfaces. It's completely event based and independend from any event loop by using the AnyEvent module. .PP It's \s-1HTTP\s0 implementation is a bit hacky, so before using this module make sure it works for you and the expected deployment. Feel free to improve the \s-1HTTP\s0 support and send in patches! .PP The documentation is currently only the source code, but next versions of this module will be better documented hopefully. See also the \f(CW\*(C`samples/\*(C'\fR directory in the AnyEvent::HTTPD distribution for basic starting points. .SH "FEATURES" .IX Header "FEATURES" .IP "\(bu" 4 support for \s-1GET\s0 and \s-1POST\s0 requests. .IP "\(bu" 4 support for \s-1HTTP 1.0\s0 keep-alive. .IP "\(bu" 4 processing of \f(CW\*(C`x\-www\-form\-urlencoded\*(C'\fR and \f(CW\*(C`multipart/form\-data\*(C'\fR (\f(CW\*(C`multipart/mixed\*(C'\fR) encoded form parameters. .IP "\(bu" 4 support for streaming responses. .IP "\(bu" 4 with version 0.8 no more dependend on \s-1LWP\s0 for HTTP::Date. .IP "\(bu" 4 (limited) support for \s-1SSL\s0 .SH "METHODS" .IX Header "METHODS" The AnyEvent::HTTPD class inherits directly from AnyEvent::HTTPD::HTTPServer which inherits the event callback interface from Object::Event. .PP Event callbacks can be registered via the Object::Event \s-1API\s0 (see the documentation of Object::Event for details). .PP For a list of available events see below in the \fI\s-1EVENTS\s0\fR section. .IP "\fBnew (%args)\fR" 4 .IX Item "new (%args)" This is the constructor for a AnyEvent::HTTPD object. The \f(CW%args\fR hash may contain one of these key/value pairs: .RS 4 .ie n .IP "host => $host" 4 .el .IP "host => \f(CW$host\fR" 4 .IX Item "host => $host" The \s-1TCP\s0 address of the \s-1HTTP\s0 server will listen on. Usually 0.0.0.0 (the default), for a public server, or 127.0.0.1 for a local server. .ie n .IP "port => $port" 4 .el .IP "port => \f(CW$port\fR" 4 .IX Item "port => $port" The \s-1TCP\s0 port the \s-1HTTP\s0 server will listen on. If undefined some free port will be used. You can get it via the \f(CW\*(C`port\*(C'\fR method. .ie n .IP "ssl => $tls_ctx" 4 .el .IP "ssl => \f(CW$tls_ctx\fR" 4 .IX Item "ssl => $tls_ctx" If this option is given the server will listen for a \s-1SSL/TLS\s0 connection on the configured port. As \f(CW$tls_ctx\fR you can pass anything that you can pass as \&\f(CW\*(C`tls_ctx\*(C'\fR to an AnyEvent::Handle object. .Sp Example: .Sp .Vb 5 \& my $httpd = \& AnyEvent::HTTPD\->new ( \& port => 443, \& ssl => { cert_file => "/path/to/my/server_cert_and_key.pem" } \& ); .Ve .Sp Or: .Sp .Vb 5 \& my $httpd = \& AnyEvent::HTTPD\->new ( \& port => 443, \& ssl => AnyEvent::TLS\->new (...), \& ); .Ve .ie n .IP "request_timeout => $seconds" 4 .el .IP "request_timeout => \f(CW$seconds\fR" 4 .IX Item "request_timeout => $seconds" This will set the request timeout for connections. The default value is 60 seconds. .ie n .IP "backlog => $int" 4 .el .IP "backlog => \f(CW$int\fR" 4 .IX Item "backlog => $int" The backlog argument defines the maximum length the queue of pending connections may grow to. The real maximum queue length will be 1.5 times more than the value specified in the backlog argument. .Sp See also \f(CW\*(C`man 2 listen\*(C'\fR. .Sp By default will be set by AnyEvent::Socket\f(CW\*(C`::tcp_server\*(C'\fR to \f(CW128\fR. .ie n .IP "connection_class => $class" 4 .el .IP "connection_class => \f(CW$class\fR" 4 .IX Item "connection_class => $class" This is a special parameter that you can use to pass your own connection class to AnyEvent::HTTPD::HTTPServer. This is only of interest to you if you plan to subclass AnyEvent::HTTPD::HTTPConnection. .ie n .IP "request_class => $class" 4 .el .IP "request_class => \f(CW$class\fR" 4 .IX Item "request_class => $class" This is a special parameter that you can use to pass your own request class to AnyEvent::HTTPD. This is only of interest to you if you plan to subclass AnyEvent::HTTPD::Request. .ie n .IP "allowed_methods => $arrayref" 4 .el .IP "allowed_methods => \f(CW$arrayref\fR" 4 .IX Item "allowed_methods => $arrayref" This parameter sets the allowed \s-1HTTP\s0 methods for requests, defaulting to \s-1GET, HEAD\s0 and \s-1POST.\s0 Each request received is matched against this list, and a \&'501 not implemented' is returned if no match is found. Requests using disallowed handlers will never trigger callbacks. .RE .RS 4 .RE .IP "\fBport\fR" 4 .IX Item "port" Returns the port number this server is bound to. .IP "\fBhost\fR" 4 .IX Item "host" Returns the host/ip this server is bound to. .IP "\fBallowed_methods\fR" 4 .IX Item "allowed_methods" Returns an arrayref of allowed \s-1HTTP\s0 methods, possibly as set by the allowed_methods argument to the constructor. .IP "\fBstop_request\fR" 4 .IX Item "stop_request" When the server walks the request \s-1URI\s0 path upwards you can stop the walk by calling this method. You can even stop further handling after the \f(CW\*(C`request\*(C'\fR event. .Sp Example: .Sp .Vb 3 \& $httpd\->reg_cb ( \& \*(Aq/test\*(Aq => sub { \& my ($httpd, $req) = @_; \& \& # ... \& \& $httpd\->stop_request; # will prevent that the callback below is called \& }, \& \*(Aq\*(Aq => sub { # this one wont be called by a request to \*(Aq/test\*(Aq \& my ($httpd, $req) = @_; \& \& # ... \& } \& ); .Ve .IP "\fBrun\fR" 4 .IX Item "run" This method is a simplification of the \f(CW\*(C`AnyEvent\*(C'\fR condition variable idiom. You can use it instead of writing: .Sp .Vb 2 \& my $cvar = AnyEvent\->condvar; \& $cvar\->wait; .Ve .IP "\fBstop\fR" 4 .IX Item "stop" This will stop the \s-1HTTP\s0 server and return from the \&\f(CW\*(C`run\*(C'\fR method \fBif you started the server via that method!\fR .SH "EVENTS" .IX Header "EVENTS" Every request goes to a specific \s-1URL.\s0 After a (\s-1GET\s0 or \s-1POST\s0) request is received the \s-1URL\s0's path segments are walked down and for each segment a event is generated. An example: .PP If the \s-1URL\s0 '/test/bla.jpg' is requestes following events will be generated: .PP .Vb 3 \& \*(Aq/test/bla.jpg\*(Aq \- the event for the last segment \& \*(Aq/test\*(Aq \- the event for the \*(Aqtest\*(Aq segment \& \*(Aq\*(Aq \- the root event of each request .Ve .PP To actually handle any request you just have to register a callback for the event name with the empty string. To handle all requests in the '/test' directory you have to register a callback for the event with the name \f(CW\*(Aq/test\*(Aq\fR. Here is an example how to register an event for the example \s-1URL\s0 above: .PP .Vb 3 \& $httpd\->reg_cb ( \& \*(Aq/test/bla.jpg\*(Aq => sub { \& my ($httpd, $req) = @_; \& \& $req\->respond ([200, \*(Aqok\*(Aq, { \*(AqContent\-Type\*(Aq => \*(Aqtext/html\*(Aq }, \*(Aq

Test

\*(Aq }]); \& } \& ); .Ve .PP See also \f(CW\*(C`stop_request\*(C'\fR about stopping the walk of the path segments. .PP The first argument to such a callback is always the AnyEvent::HTTPD object itself. The second argument (\f(CW$req\fR) is the AnyEvent::HTTPD::Request object for this request. It can be used to get the (possible) form parameters for this request or the transmitted content and respond to the request. .PP Along with the above mentioned events these events are also provided: .ie n .IP "request => $req" 4 .el .IP "request => \f(CW$req\fR" 4 .IX Item "request => $req" Every request also emits the \f(CW\*(C`request\*(C'\fR event, with the same arguments and semantics as the above mentioned path request events. You can use this to implement your own request multiplexing. You can use \f(CW\*(C`stop_request\*(C'\fR to stop any further processing of the request as the \f(CW\*(C`request\*(C'\fR event is the first thing that is executed for an incoming request. .Sp An example of one of many possible uses: .Sp .Vb 3 \& $httpd\->reg_cb ( \& request => sub { \& my ($httpd, $req) = @_; \& \& my $url = $req\->url; \& \& if ($url\->path =~ /\e/images\e/img_(\ed+).jpg$/) { \& handle_image_request ($req, $1); # your task :) \& \& # stop the request from emitting further events \& # so that the \*(Aq/images/img_001.jpg\*(Aq and the \& # \*(Aq/images\*(Aq and \*(Aq\*(Aq events are NOT emitted: \& $httpd\->stop_request; \& } \& } \& ); .Ve .ie n .IP "client_connected => $host, $port" 4 .el .IP "client_connected => \f(CW$host\fR, \f(CW$port\fR" 4 .IX Item "client_connected => $host, $port" .PD 0 .ie n .IP "client_disconnected => $host, $port" 4 .el .IP "client_disconnected => \f(CW$host\fR, \f(CW$port\fR" 4 .IX Item "client_disconnected => $host, $port" .PD These events are emitted whenever a client coming from \f(CW\*(C`$host:$port\*(C'\fR connects to your server or is disconnected from it. .SH "CACHING" .IX Header "CACHING" Any response from the \s-1HTTP\s0 server will have \f(CW\*(C`Cache\-Control\*(C'\fR set to \f(CW\*(C`max\-age=0\*(C'\fR and also the \f(CW\*(C`Expires\*(C'\fR header set to the \f(CW\*(C`Date\*(C'\fR header. Meaning: Caching is disabled. .PP You can of course set those headers yourself in the response, or remove them by setting them to undef, but keep in mind that the default for those headers are like mentioned above. .PP If you need more support here you can send me a mail or even better: a patch :) .SH "AUTHOR" .IX Header "AUTHOR" Robin Redeker, \f(CW\*(C`\*(C'\fR .SH "BUGS" .IX Header "BUGS" Please report any bugs or feature requests to \f(CW\*(C`bug\-bs\-httpd at rt.cpan.org\*(C'\fR, or through the web interface at . I will be notified, and then you'll automatically be notified of progress on your bug as I make changes. .SH "SUPPORT" .IX Header "SUPPORT" You can find documentation for this module with the perldoc command. .PP .Vb 1 \& perldoc AnyEvent::HTTPD .Ve .PP You can also look for information at: .IP "\(bu" 4 Git repository .Sp .IP "\(bu" 4 \&\s-1RT: CPAN\s0's request tracker .Sp .IP "\(bu" 4 AnnoCPAN: Annotated \s-1CPAN\s0 documentation .Sp .IP "\(bu" 4 \&\s-1CPAN\s0 Ratings .Sp .IP "\(bu" 4 Search \s-1CPAN\s0 .Sp .SH "ACKNOWLEDGEMENTS" .IX Header "ACKNOWLEDGEMENTS" .Vb 8 \& Andrey Smirnov \- for keep\-alive patches. \& Pedro Melo \- for valuable input in general and patches. \& Nicholas Harteau \- patch for \*(Aq;\*(Aq pair separator support, \& patch for allowed_methods support \& Chris Kastorff \- patch for making default headers removable \& and more fault tolerant w.r.t. case. \& Mons Anderson \- Optimizing the regexes in L \& and adding the C option to L. .Ve .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.