-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathREADME
176 lines (134 loc) · 5.37 KB
/
README
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
NAME
AnyEvent::HTTP::Server - AnyEvent HTTP/1.1 Server
SYNOPSIS
use AnyEvent::HTTP::Server;
my $s = AnyEvent::HTTP::Server->new(
host => '0.0.0.0',
port => 80,
cb => sub {
my $request = shift;
my $status = 200;
my $content = "<h1>Reply message</h1>";
my $headers = { 'content-type' => 'text/html' };
$request->reply($status, $content, headers => $headers);
}
);
$s->listen;
## you may also prefork on N cores:
# fork() ? next : last for (1..$N-1);
## Of course this is very simple example
## don't use such prefork in production
$s->accept;
my $sig = AE::signal INT => sub {
warn "Stopping server";
$s->graceful(sub {
warn "Server stopped";
EV::unloop;
});
};
EV::loop;
DESCRIPTION
AnyEvent::HTTP::Server is a very fast asynchronous HTTP server written
in perl. It has been tested in high load production environments and may
be considered both fast and stable.
One can easily implement own HTTP daemon with AnyEvent::HTTP::Server and
Daemond::Lite module, both found at <https://github.com/Mons>
This is a second verson available as AnyEvent-HTTP-Server-II. The first
version is now obsolette.
HANDLING REQUEST
You can handle HTTP request by passing cb parameter to
AnyEvent::HTTP::Server->new() like this:
my $dispatcher = sub {
my $request = shift;
#... Request processing code goes here ...
1;
};
my $s = AnyEvent::HTTP::Server->new( host => '0.0.0.0', port => 80, cb => $dispatcher,);
$dispatcher coderef will be called in a list context and it's return
value should resolve to true, or request processing will be aborted by
AnyEvent:HTTP::Server.
One able to process POST requests by returning specially crafted hash
reference from cb parameter coderef ($dispatcher in out example). This
hash must contain the form key, holding a code reference. If
conetnt-encoding header is application/x-www-form-urlencoded, form
callback will be called.
my $post_action = sub {
my ( $request, $form ) = @_;
$request->reply(
200, # HTTP Status
"You just send long_data_param_name value of $form->{long_data_param_name}", # Content
headers=> { 'content-type' =< 'text/plain'}, # Response headers
);
}
my $dispatcher = sub {
my $request = shift;
if ( $request->headers->{'content-type'} =~ m{^application/x-www-form-urlencoded\s*$} ) {
return {
form => sub {
$cb->( $request, $post_action);
},
};
} else {
# GET request processing
}
};
my $s = AnyEvent::HTTP::Server->new( host => '0.0.0.0', port => 80, cb => $dispatcher,);
EXPORT
Does not export anything
SUBROUTINES/METHODS
new - create HTTP Server object
Arguments to constractor should be passed as a key=>value list, for example
my $s = AnyEvent::HTTP::Server->new(
host => '0.0.0.0',
port => 80,
cb => sub {
my $req = shift;
return sub {
my ($is_last, $bodypart) = @_;
$r->reply(200, "<h1>Reply message</h1>", headers => { 'content-type' => 'text/html' });
}
}
);
host
Specify interfaces to bind a listening socket to
Example: host => '127.0.0.1'
port
Listen on this port
Example: port => 80
cb
This coderef will be called on incoming request
Example: cb => sub {
my $request = shift;
my $status = 200;
my $content = "<h1>Reply message</h1>";
my $headers = { 'content-type' => 'text/html' };
$request->reply($status, $content, headers => $headers);
}
The first argument to callback will be request object (AnyEvent::HTTP::Server::Req).
listen - bind server socket to host and port, start listening for connections
This method has no arguments.
This method is commonly called from master process before it forks.
Errors in host and port may result in exceptions, so you probably want to eval this call.
accept - start accepting connections
This method has no arguments.
This method is commonly called in forked children, which serve incoming requests.
noaccept - stop accepting connections (while still listening on a socket)
This method has no arguments.
graceful - Stop accepting new connections and gracefully shut down the server
Wait until all connections will be handled and execute supplied coderef after that.
This method can be useful in signal handlers.
set_favicon - change default favicon.ico
The only argument is a scalar, containing binary representation of icon.
Favicon will have content type set to 'image/x-icon'
RESOURCES
* GitHub repository
<http://github.com/Mons/AnyEvent-HTTP-Server-II>
ACKNOWLEDGEMENTS
* Thanks to Marc Lehmann for AnyEvent
* Thanks to Robin Redeker for AnyEvent::HTTPD
AUTHOR
Mons Anderson, <[email protected]>
LICENSE
This program is free software; you can redistribute it and/or modify it
under the terms of either: the GNU General Public License as published
by the Free Software Foundation; or the Artistic License.