1HTTP::Daemon(3) User Contributed Perl Documentation HTTP::Daemon(3)
2
3
4
6 HTTP::Daemon - A simple http server class
7
9 version 6.06
10
12 use HTTP::Daemon;
13 use HTTP::Status;
14
15 my $d = HTTP::Daemon->new || die;
16 print "Please contact me at: <URL:", $d->url, ">\n";
17 while (my $c = $d->accept) {
18 while (my $r = $c->get_request) {
19 if ($r->method eq 'GET' and $r->uri->path eq "/xyzzy") {
20 # remember, this is *not* recommended practice :-)
21 $c->send_file_response("/etc/passwd");
22 }
23 else {
24 $c->send_error(RC_FORBIDDEN)
25 }
26 }
27 $c->close;
28 undef($c);
29 }
30
32 Instances of the "HTTP::Daemon" class are HTTP/1.1 servers that listen
33 on a socket for incoming requests. The "HTTP::Daemon" is a subclass of
34 "IO::Socket::IP", so you can perform socket operations directly on it
35 too.
36
37 The accept() method will return when a connection from a client is
38 available. The returned value will be an "HTTP::Daemon::ClientConn"
39 object which is another "IO::Socket::IP" subclass. Calling the
40 get_request() method on this object will read data from the client and
41 return an "HTTP::Request" object. The ClientConn object also provide
42 methods to send back various responses.
43
44 This HTTP daemon does not fork(2) for you. Your application, i.e. the
45 user of the "HTTP::Daemon" is responsible for forking if that is
46 desirable. Also note that the user is responsible for generating
47 responses that conform to the HTTP/1.1 protocol.
48
49 The following methods of "HTTP::Daemon" are new (or enhanced) relative
50 to the "IO::Socket::IP" base class:
51
52 $d = HTTP::Daemon->new
53 $d = HTTP::Daemon->new( %opts )
54 The constructor method takes the same arguments as the
55 "IO::Socket::IP" constructor, but unlike its base class it can also
56 be called without any arguments. The daemon will then set up a
57 listen queue of 5 connections and allocate some random port number.
58
59 A server that wants to bind to some specific address on the
60 standard HTTP port will be constructed like this:
61
62 $d = HTTP::Daemon->new(
63 LocalAddr => 'www.thisplace.com',
64 LocalPort => 80,
65 );
66
67 See IO::Socket::IP for a description of other arguments that can be
68 used configure the daemon during construction.
69
70 $c = $d->accept
71 $c = $d->accept( $pkg )
72 ($c, $peer_addr) = $d->accept
73 This method works the same the one provided by the base class, but
74 it returns an "HTTP::Daemon::ClientConn" reference by default. If
75 a package name is provided as argument, then the returned object
76 will be blessed into the given class. It is probably a good idea
77 to make that class a subclass of "HTTP::Daemon::ClientConn".
78
79 The accept method will return "undef" if timeouts have been enabled
80 and no connection is made within the given time. The timeout()
81 method is described in IO::Socket::IP.
82
83 In list context both the client object and the peer address will be
84 returned; see the description of the accept method IO::Socket for
85 details.
86
87 $d->url
88 Returns a URL string that can be used to access the server root.
89
90 $d->product_tokens
91 Returns the name that this server will use to identify itself.
92 This is the string that is sent with the "Server" response header.
93 The main reason to have this method is that subclasses can override
94 it if they want to use another product name.
95
96 The default is the string "libwww-perl-daemon/#.##" where "#.##" is
97 replaced with the version number of this module.
98
99 The "HTTP::Daemon::ClientConn" is a "IO::Socket::IP" subclass.
100 Instances of this class are returned by the accept() method of
101 "HTTP::Daemon". The following methods are provided:
102
103 $c->get_request
104 $c->get_request( $headers_only )
105 This method reads data from the client and turns it into an
106 "HTTP::Request" object which is returned. It returns "undef" if
107 reading fails. If it fails, then the "HTTP::Daemon::ClientConn"
108 object ($c) should be discarded, and you should not try call this
109 method again on it. The $c->reason method might give you some
110 information about why $c->get_request failed.
111
112 The get_request() method will normally not return until the whole
113 request has been received from the client. This might not be what
114 you want if the request is an upload of a large file (and with
115 chunked transfer encoding HTTP can even support infinite request
116 messages - uploading live audio for instance). If you pass a TRUE
117 value as the $headers_only argument, then get_request() will return
118 immediately after parsing the request headers and you are
119 responsible for reading the rest of the request content. If you
120 are going to call $c->get_request again on the same connection you
121 better read the correct number of bytes.
122
123 $c->read_buffer
124 $c->read_buffer( $new_value )
125 Bytes read by $c->get_request, but not used are placed in the read
126 buffer. The next time $c->get_request is called it will consume
127 the bytes in this buffer before reading more data from the network
128 connection itself. The read buffer is invalid after
129 $c->get_request has failed.
130
131 If you handle the reading of the request content yourself you need
132 to empty this buffer before you read more and you need to place
133 unconsumed bytes here. You also need this buffer if you implement
134 services like 101 Switching Protocols.
135
136 This method always returns the old buffer content and can
137 optionally replace the buffer content if you pass it an argument.
138
139 $c->reason
140 When $c->get_request returns "undef" you can obtain a short string
141 describing why it happened by calling $c->reason.
142
143 $c->proto_ge( $proto )
144 Return TRUE if the client announced a protocol with version number
145 greater or equal to the given argument. The $proto argument can be
146 a string like "HTTP/1.1" or just "1.1".
147
148 $c->antique_client
149 Return TRUE if the client speaks the HTTP/0.9 protocol. No status
150 code and no headers should be returned to such a client. This
151 should be the same as !$c->proto_ge("HTTP/1.0").
152
153 $c->head_request
154 Return TRUE if the last request was a "HEAD" request. No content
155 body must be generated for these requests.
156
157 $c->force_last_request
158 Make sure that $c->get_request will not try to read more requests
159 off this connection. If you generate a response that is not self
160 delimiting, then you should signal this fact by calling this
161 method.
162
163 This attribute is turned on automatically if the client announces
164 protocol HTTP/1.0 or worse and does not include a "Connection:
165 Keep-Alive" header. It is also turned on automatically when
166 HTTP/1.1 or better clients send the "Connection: close" request
167 header.
168
169 $c->send_status_line
170 $c->send_status_line( $code )
171 $c->send_status_line( $code, $mess )
172 $c->send_status_line( $code, $mess, $proto )
173 Send the status line back to the client. If $code is omitted 200
174 is assumed. If $mess is omitted, then a message corresponding to
175 $code is inserted. If $proto is missing the content of the
176 $HTTP::Daemon::PROTO variable is used.
177
178 $c->send_crlf
179 Send the CRLF sequence to the client.
180
181 $c->send_basic_header
182 $c->send_basic_header( $code )
183 $c->send_basic_header( $code, $mess )
184 $c->send_basic_header( $code, $mess, $proto )
185 Send the status line and the "Date:" and "Server:" headers back to
186 the client. This header is assumed to be continued and does not
187 end with an empty CRLF line.
188
189 See the description of send_status_line() for the description of
190 the accepted arguments.
191
192 $c->send_header( $field, $value )
193 $c->send_header( $field1, $value1, $field2, $value2, ... )
194 Send one or more header lines.
195
196 $c->send_response( $res )
197 Write a "HTTP::Response" object to the client as a response. We
198 try hard to make sure that the response is self delimiting so that
199 the connection can stay persistent for further request/response
200 exchanges.
201
202 The content attribute of the "HTTP::Response" object can be a
203 normal string or a subroutine reference. If it is a subroutine,
204 then whatever this callback routine returns is written back to the
205 client as the response content. The routine will be called until
206 it return an undefined or empty value. If the client is HTTP/1.1
207 aware then we will use chunked transfer encoding for the response.
208
209 $c->send_redirect( $loc )
210 $c->send_redirect( $loc, $code )
211 $c->send_redirect( $loc, $code, $entity_body )
212 Send a redirect response back to the client. The location ($loc)
213 can be an absolute or relative URL. The $code must be one the
214 redirect status codes, and defaults to "301 Moved Permanently"
215
216 $c->send_error
217 $c->send_error( $code )
218 $c->send_error( $code, $error_message )
219 Send an error response back to the client. If the $code is missing
220 a "Bad Request" error is reported. The $error_message is a string
221 that is incorporated in the body of the HTML entity body.
222
223 $c->send_file_response( $filename )
224 Send back a response with the specified $filename as content. If
225 the file is a directory we try to generate an HTML index of it.
226
227 $c->send_file( $filename )
228 $c->send_file( $fd )
229 Copy the file to the client. The file can be a string (which will
230 be interpreted as a filename) or a reference to an "IO::Handle" or
231 glob.
232
233 $c->daemon
234 Return a reference to the corresponding "HTTP::Daemon" object.
235
237 RFC 2616
238
239 IO::Socket::IP, IO::Socket
240
242 bugs may be submitted through
243 <https://github.com/libwww-perl/HTTP-Daemon/issues>.
244
245 There is also a mailing list available for users of this distribution,
246 at <mailto:libwww@perl.org>.
247
248 There is also an irc channel available for users of this distribution,
249 at "#lwp" on "irc.perl.org" <irc://irc.perl.org/#lwp>.
250
252 Gisle Aas <gisle@activestate.com>
253
255 · Ville Skyttä <ville.skytta@iki.fi>
256
257 · Olaf Alders <olaf@wundersolutions.com>
258
259 · Mark Stosberg <MARKSTOS@cpan.org>
260
261 · Karen Etheridge <ether@cpan.org>
262
263 · Chase Whitener <capoeirab@cpan.org>
264
265 · Slaven Rezic <slaven@rezic.de>
266
267 · Zefram <zefram@fysh.org>
268
269 · Alexey Tourbin <at@altlinux.ru>
270
271 · Bron Gondwana <brong@fastmail.fm>
272
273 · Petr Písař <ppisar@redhat.com>
274
275 · Mike Schilli <mschilli@yahoo-inc.com>
276
277 · Tom Hukins <tom@eborcom.com>
278
279 · Ian Kilgore <iank@cpan.org>
280
281 · Jacob J <waif@chaos2.org>
282
283 · Ondrej Hanak <ondrej.hanak@ubs.com>
284
285 · Perlover <perlover@perlover.com>
286
287 · Peter Rabbitson <ribasushi@cpan.org>
288
289 · Robert Stone <talby@trap.mtview.ca.us>
290
291 · Rolf Grossmann <rg@progtech.net>
292
293 · Sean M. Burke <sburke@cpan.org>
294
295 · Spiros Denaxas <s.denaxas@gmail.com>
296
297 · Steve Hay <SteveHay@planit.com>
298
299 · Todd Lipcon <todd@amiestreet.com>
300
301 · Tony Finch <dot@dotat.at>
302
303 · Toru Yamaguchi <zigorou@cpan.org>
304
305 · Yuri Karaban <tech@askold.net>
306
307 · amire80 <amir.aharoni@gmail.com>
308
309 · jefflee <shaohua@gmail.com>
310
311 · john9art <john9art@yahoo.com>
312
313 · murphy <murphy@genome.chop.edu>
314
315 · phrstbrn <phrstbrn@gmail.com>
316
317 · ruff <ruff@ukrpost.net>
318
319 · Adam Kennedy <adamk@cpan.org>
320
321 · sasao <sasao@yugen.org>
322
323 · Adam Sjogren <asjo@koldfront.dk>
324
325 · Alex Kapranoff <ka@nadoby.ru>
326
327 · Andreas J. Koenig <andreas.koenig@anima.de>
328
329 · Bill Mann <wfmann@alum.mit.edu>
330
331 · DAVIDRW <davidrw@cpan.org>
332
333 · Daniel Hedlund <Daniel.Hedlund@eprize.com>
334
335 · David E. Wheeler <david@justatheory.com>
336
337 · FWILES <FWILES@cpan.org>
338
339 · Father Chrysostomos <sprout@cpan.org>
340
341 · Gavin Peters <gpeters@deepsky.com>
342
343 · Graeme Thompson <Graeme.Thompson@mobilecohesion.com>
344
345 · Hans-H. Froehlich <hfroehlich@co-de-co.de>
346
348 This software is copyright (c) 1995 by Gisle Aas.
349
350 This is free software; you can redistribute it and/or modify it under
351 the same terms as the Perl 5 programming language system itself.
352
353
354
355perl v5.30.1 2020-01-30 HTTP::Daemon(3)