1HTTP::Server::Simple(3)User Contributed Perl DocumentatioHnTTP::Server::Simple(3)
2
3
4
6 HTTP::Server::Simple - Lightweight HTTP server
7
9 use warnings;
10 use strict;
11
12 use HTTP::Server::Simple;
13
14 my $server = HTTP::Server::Simple->new();
15 $server->run();
16
17 However, normally you will sub-class the HTTP::Server::Simple::CGI mod‐
18 ule (see HTTP::Server::Simple::CGI);
19
20 package Your::Web::Server;
21 use base qw(HTTP::Server::Simple::CGI);
22
23 sub handle_request {
24 my ($self, $cgi) = @_;
25
26 #... do something, print output to default
27 # selected filehandle...
28
29 }
30
31 1;
32
34 This is a simple standalone HTTP server. By default, it doesn't thread
35 or fork.
36
37 It does, however, act as a simple frontend which can be used to build a
38 standalone web-based application or turn a CGI into one.
39
40 (It's possible to use Net::Server to get threading, forking, preforking
41 and so on. Autrijus Tang wrote the functionality and owes docs for that
42 ;)
43
44 By default, the server traps a few signals:
45
46 HUP When you "kill -HUP" the server, it does its best to rexec itself.
47 Please note that in order to provide restart-on-SIGHUP,
48 HTTP::Server::Simple sets a SIGHUP handler during initialisation.
49 If your request handling code forks you need to make sure you reset
50 this or unexpected things will happen if somebody sends a HUP to
51 all running processes spawned by your app (e.g. by "kill -HUP
52 <script>")
53
54 PIPE
55 If the server detects a broken pipe while writing output to the
56 client, it ignores the signal. Otherwise, a client closing the con‐
57 nection early could kill the server
58
59 HTTP::Server::Simple->new($port)
60
61 API call to start a new server. Does not actually start listening
62 until you call "->run()".
63
64 lookup_localhost
65
66 Looks up the local host's hostname and IP address.
67
68 Stuffs them into
69
70 $self->{'localname'} and $self->{'localaddr'}
71
72 port [NUMBER]
73
74 Takes an optional port number for this server to listen on.
75
76 Returns this server's port. (Defaults to 8080)
77
78 host [address]
79
80 Takes an optional host address for this server to bind to.
81
82 Returns this server's bound address (if any). Defaults to "undef"
83 (bind to all interfaces).
84
85 background
86
87 Run the server in the background. returns pid.
88
89 run
90
91 Run the server. If all goes well, this won't ever return, but it will
92 start listening for http requests.
93
94 net_server
95
96 User-overridable method. If you set it to a "Net::Server" subclass,
97 that subclass is used for the "run" method. Otherwise, a minimal
98 implementation is used as default.
99
100 stdio_handle [FILEHANDLE]
101
102 When called with an argument, sets the socket to the server to that
103 arg.
104
105 Returns the socket to the server; you should only use this for actual
106 socket-related calls like "getsockname". If all you want is to read or
107 write to the socket, you should use "stdin_handle" and "stdout_handle"
108 to get the in and out filehandles explicitly.
109
110 stdin_handle
111
112 Returns a filehandle used for input from the client. By default,
113 returns whatever was set with "stdio_handle", but a subclass could do
114 something interesting here (see HTTP::Server::Simple::Logger).
115
116 stdout_handle
117
118 Returns a filehandle used for output to the client. By default,
119 returns whatever was set with "stdio_handle", but a subclass could do
120 something interesting here (see HTTP::Server::Simple::Logger).
121
123 A selection of these methods should be provided by sub-classes of this
124 module.
125
126 handler
127
128 This method is called after setup, with no parameters. It should print
129 a valid, full HTTP response to the default selected filehandle.
130
131 setup(name => $value, ...)
132
133 This method is called with a name => value list of various things to do
134 with the request. This list is given below.
135
136 The default setup handler simply tries to call methods with the names
137 of keys of this list.
138
139 ITEM/METHOD Set to Example
140 ----------- ------------------ ------------------------
141 method Request Method "GET", "POST", "HEAD"
142 protocol HTTP version "HTTP/1.1"
143 request_uri Complete Request URI "/foobar/baz?foo=bar"
144 path Path part of URI "/foobar/baz"
145 query_string Query String undef, "foo=bar"
146 port Received Port 80, 8080
147 peername Remote name "200.2.4.5", "foo.com"
148 peeraddr Remote address "200.2.4.5", "::1"
149 localname Local interface "localhost", "myhost.com"
150
151 headers([Header => $value, ...])
152
153 Receives HTTP headers and does something useful with them. This is
154 called by the default "setup()" method.
155
156 You have lots of options when it comes to how you receive headers.
157
158 You can, if you really want, define "parse_headers()" and parse them
159 raw yourself.
160
161 Secondly, you can intercept them very slightly cooked via the "setup()"
162 method, above.
163
164 Thirdly, you can leave the "setup()" header as-is (or calling the
165 superclass "setup()" for unknown request items). Then you can define
166 "headers()" in your sub-class and receive them all at once.
167
168 Finally, you can define handlers to receive individual HTTP headers.
169 This can be useful for very simple SOAP servers (to name a crack-fueled
170 standard that defines its own special HTTP headers).
171
172 To do so, you'll want to define the "header()" method in your subclass.
173 That method will be handed a (key,value) pair of the header name and
174 the value.
175
176 accept_hook
177
178 If defined by a sub-class, this method is called directly after an
179 accept happens.
180
181 post_setup_hook
182
183 If defined by a sub-class, this method is called after all setup has
184 finished, before the handler method.
185
186 print_banner
187
188 This routine prints a banner before the server request-handling loop
189 starts.
190
191 Methods below this point are probably not terribly useful to define
192 yourself in subclasses.
193
194 parse_request
195
196 Parse the HTTP request line.
197
198 Returns three values, the request method, request URI and the protocol
199 Sub-classed versions of this should return three values - request
200 method, request URI and proto
201
202 parse_headers
203
204 Parse incoming HTTP headers from STDIN.
205
206 Remember, this is a simple HTTP server, so nothing intelligent is done
207 with them ":-)".
208
209 This should return an ARRAY ref of "(header => value)" pairs inside the
210 array.
211
212 setup_listener
213
214 This routine binds the server to a port and interface.
215
216 after_setup_listener
217
218 This method is called immediately after setup_listener. It's here just
219 for you to override.
220
221 bad_request
222
223 This method should print a valid HTTP response that says that the
224 request was invalid.
225
226 valid_http_method($method)
227
228 Given a candidate HTTP method in $method, determine if it is valid.
229 Override if, for example, you'd like to do some WebDAV.
230
232 Copyright (c) 2004-2006 Jesse Vincent, <jesse@bestpractical.com>. All
233 rights reserved.
234
235 Marcus Ramberg <drave@thefeed.no> contributed tests, cleanup, etc
236
237 Sam Vilain, <samv@cpan.org> contributed the CGI.pm split-out and
238 header/setup API.
239
241 There certainly are some. Please report them via rt.cpan.org
242
244 This library is free software; you can redistribute it and/or modify it
245 under the same terms as Perl itself.
246
247
248
249perl v5.8.8 2007-01-17 HTTP::Server::Simple(3)