1AnyEvent::HTTP::Server(U3s)er Contributed Perl DocumentatAinoynEvent::HTTP::Server(3)
2
3
4

NAME

6       AnyEvent::HTTP::Server - AnyEvent HTTP/1.1 Server
7

SYNOPSIS

9           use AnyEvent::HTTP::Server;
10           my $s = AnyEvent::HTTP::Server->new(
11               host => '0.0.0.0',
12               port => 80,
13               cb => sub {
14                 my $request = shift;
15                 my $status  = 200;
16                 my $content = "<h1>Reply message</h1>";
17                 my $headers = { 'content-type' => 'text/html' };
18                 $request->reply($status, $content, headers => $headers);
19               }
20           );
21           $s->listen;
22
23           ## you may also prefork on N cores:
24
25           # fork() ? next : last for (1..$N-1);
26
27           ## Of course this is very simple example
28           ## don't use such prefork in production
29
30           $s->accept;
31
32           my $sig = AE::signal INT => sub {
33               warn "Stopping server";
34               $s->graceful(sub {
35                   warn "Server stopped";
36                   EV::unloop;
37               });
38           };
39
40           EV::loop;
41

DESCRIPTION

43       AnyEvent::HTTP::Server is a very fast asynchronous HTTP server written
44       in perl.  It has been tested in high load production environments and
45       may be considered both fast and stable.
46
47       One can easily implement own HTTP daemon with AnyEvent::HTTP::Server
48       and Daemond::Lite module, both found at <https://github.com/Mons>
49
50       This is a second verson available as AnyEvent-HTTP-Server-II. The first
51       version is now obsolette.
52

HANDLING REQUEST

54       You can handle HTTP request by passing cb parameter to
55       AnyEvent::HTTP::Server->new() like this:
56
57         my $dispatcher = sub {
58           my $request = shift;
59           #... Request processing code goes here ...
60           1;
61         };
62
63         my $s = AnyEvent::HTTP::Server->new( host => '0.0.0.0', port => 80, cb => $dispatcher,);
64
65       $dispatcher coderef will be called in a list context and it's return
66       value should resolve to true, or request processing will be aborted by
67       AnyEvent:HTTP::Server.
68
69       One able to process POST requests by returning specially crafted  hash
70       reference from cb parameter coderef ($dispatcher in out example). This
71       hash must contain the form key, holding a code reference. If conetnt-
72       encoding header is application/x-www-form-urlencoded, form callback
73       will be called.
74
75         my $post_action = sub {
76           my ( $request, $form ) = @_;
77           $request->reply(
78             200, # HTTP Status
79             "You just send long_data_param_name value of $form->{long_data_param_name}",  # Content
80             headers=> { 'content-type' =< 'text/plain'}, # Response headers
81           );
82         }
83
84         my $dispatcher = sub {
85           my $request = shift;
86
87           if ( $request->headers->{'content-type'} =~ m{^application/x-www-form-urlencoded\s*$} ) {
88             return {
89               form => sub {
90                 $cb->( $request, $post_action);
91               },
92             };
93           } else {
94             # GET request processing
95           }
96
97         };
98
99         my $s = AnyEvent::HTTP::Server->new( host => '0.0.0.0', port => 80, cb => $dispatcher,);
100

EXPORT

102         Does not export anything
103

SUBROUTINES/METHODS

105   new - create HTTP Server object
106         Arguments to constractor should be passed as a key=>value list, for example
107
108           my $s = AnyEvent::HTTP::Server->new(
109               host => '0.0.0.0',
110               port => 80,
111               cb   => sub {
112                   my $req = shift;
113                   return sub {
114                       my ($is_last, $bodypart) = @_;
115                       $r->reply(200, "<h1>Reply message</h1>", headers => { 'content-type' => 'text/html' });
116                   }
117               }
118           );
119
120       host
121
122         Specify interfaces to bind a listening socket to
123         Example: host => '127.0.0.1'
124
125       port
126
127         Listen on this port
128         Example: port => 80
129
130       cb
131
132         This coderef will be called on incoming request
133         Example: cb => sub {
134           my $request = shift;
135           my $status  = 200;
136           my $content = "<h1>Reply message</h1>";
137           my $headers = { 'content-type' => 'text/html' };
138           $request->reply($status, $content, headers => $headers);
139         }
140
141         The first argument to callback will be request object (AnyEvent::HTTP::Server::Req).
142
143   listen - bind server socket to host and port, start listening for
144       connections
145         This method has no arguments.
146
147         This method is commonly called from master process before it forks.
148
149         Errors in host and port may result in exceptions, so you probably want to eval this call.
150
151   accept - start accepting connections
152         This method has no arguments.
153
154         This method is commonly called in forked children, which serve incoming requests.
155
156   noaccept - stop accepting connections (while still listening on a socket)
157         This method has no arguments.
158
159   graceful - Stop accepting new connections and gracefully shut down the
160       server
161         Wait until all connections will be handled and execute supplied coderef after that.
162         This method can be useful in signal handlers.
163
164   set_favicon - change default favicon.ico
165         The only argument is a scalar, containing binary representation of icon.
166         Favicon will have content type set to 'image/x-icon'
167

RESOURCES

169       ·   GitHub repository
170
171           <http://github.com/Mons/AnyEvent-HTTP-Server-II>
172

ACKNOWLEDGEMENTS

174       ·   Thanks to Marc Lehmann for AnyEvent
175
176       ·   Thanks to Robin Redeker for AnyEvent::HTTPD
177

AUTHOR

179       Mons Anderson, <mons@cpan.org>
180

LICENSE

182       This program is free software; you can redistribute it and/or modify it
183       under the terms of either: the GNU General Public License as published
184       by the Free Software Foundation; or the Artistic License.
185
186
187
188perl v5.32.0                      2020-07-28         AnyEvent::HTTP::Server(3)
Impressum