1HTTP::Daemon(3)       User Contributed Perl Documentation      HTTP::Daemon(3)
2
3
4

NAME

6       HTTP::Daemon - a simple http server class
7

SYNOPSIS

9         use HTTP::Daemon;
10         use HTTP::Status;
11
12         my $d = HTTP::Daemon->new || die;
13         print "Please contact me at: <URL:", $d->url, ">\n";
14         while (my $c = $d->accept) {
15             while (my $r = $c->get_request) {
16                 if ($r->method eq 'GET' and $r->uri->path eq "/xyzzy") {
17                     # remember, this is *not* recommended practice :-)
18                     $c->send_file_response("/etc/passwd");
19                 }
20                 else {
21                     $c->send_error(RC_FORBIDDEN)
22                 }
23             }
24             $c->close;
25             undef($c);
26         }
27

DESCRIPTION

29       Instances of the "HTTP::Daemon" class are HTTP/1.1 servers that listen
30       on a socket for incoming requests. The "HTTP::Daemon" is a subclass of
31       "IO::Socket::IP", so you can perform socket operations directly on it
32       too.
33
34       The accept() method will return when a connection from a client is
35       available.  The returned value will be an "HTTP::Daemon::ClientConn"
36       object which is another "IO::Socket::IP" subclass.  Calling the
37       get_request() method on this object will read data from the client and
38       return an "HTTP::Request" object.  The ClientConn object also provide
39       methods to send back various responses.
40
41       This HTTP daemon does not fork(2) for you.  Your application, i.e. the
42       user of the "HTTP::Daemon" is responsible for forking if that is
43       desirable.  Also note that the user is responsible for generating
44       responses that conform to the HTTP/1.1 protocol.
45
46       The following methods of "HTTP::Daemon" are new (or enhanced) relative
47       to the "IO::Socket::IP" base class:
48
49       $d = HTTP::Daemon->new
50       $d = HTTP::Daemon->new( %opts )
51           The constructor method takes the same arguments as the
52           "IO::Socket::IP" constructor, but unlike its base class it can also
53           be called without any arguments.  The daemon will then set up a
54           listen queue of 5 connections and allocate some random port number.
55
56           A server that wants to bind to some specific address on the
57           standard HTTP port will be constructed like this:
58
59             $d = HTTP::Daemon->new(
60                      LocalAddr => 'www.thisplace.com',
61                      LocalPort => 80,
62                  );
63
64           See IO::Socket::IP for a description of other arguments that can be
65           used configure the daemon during construction.
66
67       $c = $d->accept
68       $c = $d->accept( $pkg )
69       ($c, $peer_addr) = $d->accept
70           This method works the same the one provided by the base class, but
71           it returns an "HTTP::Daemon::ClientConn" reference by default.  If
72           a package name is provided as argument, then the returned object
73           will be blessed into the given class.  It is probably a good idea
74           to make that class a subclass of "HTTP::Daemon::ClientConn".
75
76           The accept method will return "undef" if timeouts have been enabled
77           and no connection is made within the given time.  The timeout()
78           method is described in IO::Socket::IP.
79
80           In list context both the client object and the peer address will be
81           returned; see the description of the accept method IO::Socket for
82           details.
83
84       $d->url
85           Returns a URL string that can be used to access the server root.
86
87       $d->product_tokens
88           Returns the name that this server will use to identify itself.
89           This is the string that is sent with the "Server" response header.
90           The main reason to have this method is that subclasses can override
91           it if they want to use another product name.
92
93           The default is the string "libwww-perl-daemon/#.##" where "#.##" is
94           replaced with the version number of this module.
95
96       The "HTTP::Daemon::ClientConn" is a "IO::Socket::IP" subclass.
97       Instances of this class are returned by the accept() method of
98       "HTTP::Daemon".  The following methods are provided:
99
100       $c->get_request
101       $c->get_request( $headers_only )
102           This method reads data from the client and turns it into an
103           "HTTP::Request" object which is returned.  It returns "undef" if
104           reading fails.  If it fails, then the "HTTP::Daemon::ClientConn"
105           object ($c) should be discarded, and you should not try call this
106           method again on it.  The $c->reason method might give you some
107           information about why $c->get_request failed.
108
109           The get_request() method will normally not return until the whole
110           request has been received from the client.  This might not be what
111           you want if the request is an upload of a large file (and with
112           chunked transfer encoding HTTP can even support infinite request
113           messages - uploading live audio for instance).  If you pass a TRUE
114           value as the $headers_only argument, then get_request() will return
115           immediately after parsing the request headers and you are
116           responsible for reading the rest of the request content.  If you
117           are going to call $c->get_request again on the same connection you
118           better read the correct number of bytes.
119
120       $c->read_buffer
121       $c->read_buffer( $new_value )
122           Bytes read by $c->get_request, but not used are placed in the read
123           buffer.  The next time $c->get_request is called it will consume
124           the bytes in this buffer before reading more data from the network
125           connection itself.  The read buffer is invalid after
126           $c->get_request has failed.
127
128           If you handle the reading of the request content yourself you need
129           to empty this buffer before you read more and you need to place
130           unconsumed bytes here.  You also need this buffer if you implement
131           services like 101 Switching Protocols.
132
133           This method always returns the old buffer content and can
134           optionally replace the buffer content if you pass it an argument.
135
136       $c->reason
137           When $c->get_request returns "undef" you can obtain a short string
138           describing why it happened by calling $c->reason.
139
140       $c->proto_ge( $proto )
141           Return TRUE if the client announced a protocol with version number
142           greater or equal to the given argument.  The $proto argument can be
143           a string like "HTTP/1.1" or just "1.1".
144
145       $c->antique_client
146           Return TRUE if the client speaks the HTTP/0.9 protocol.  No status
147           code and no headers should be returned to such a client.  This
148           should be the same as !$c->proto_ge("HTTP/1.0").
149
150       $c->head_request
151           Return TRUE if the last request was a "HEAD" request.  No content
152           body must be generated for these requests.
153
154       $c->force_last_request
155           Make sure that $c->get_request will not try to read more requests
156           off this connection.  If you generate a response that is not self
157           delimiting, then you should signal this fact by calling this
158           method.
159
160           This attribute is turned on automatically if the client announces
161           protocol HTTP/1.0 or worse and does not include a "Connection:
162           Keep-Alive" header.  It is also turned on automatically when
163           HTTP/1.1 or better clients send the "Connection: close" request
164           header.
165
166       $c->send_status_line
167       $c->send_status_line( $code )
168       $c->send_status_line( $code, $mess )
169       $c->send_status_line( $code, $mess, $proto )
170           Send the status line back to the client.  If $code is omitted 200
171           is assumed.  If $mess is omitted, then a message corresponding to
172           $code is inserted.  If $proto is missing the content of the
173           $HTTP::Daemon::PROTO variable is used.
174
175       $c->send_crlf
176           Send the CRLF sequence to the client.
177
178       $c->send_basic_header
179       $c->send_basic_header( $code )
180       $c->send_basic_header( $code, $mess )
181       $c->send_basic_header( $code, $mess, $proto )
182           Send the status line and the "Date:" and "Server:" headers back to
183           the client.  This header is assumed to be continued and does not
184           end with an empty CRLF line.
185
186           See the description of send_status_line() for the description of
187           the accepted arguments.
188
189       $c->send_header( $field, $value )
190       $c->send_header( $field1, $value1, $field2, $value2, ... )
191           Send one or more header lines.
192
193       $c->send_response( $res )
194           Write a "HTTP::Response" object to the client as a response.  We
195           try hard to make sure that the response is self delimiting so that
196           the connection can stay persistent for further request/response
197           exchanges.
198
199           The content attribute of the "HTTP::Response" object can be a
200           normal string or a subroutine reference.  If it is a subroutine,
201           then whatever this callback routine returns is written back to the
202           client as the response content.  The routine will be called until
203           it return an undefined or empty value.  If the client is HTTP/1.1
204           aware then we will use chunked transfer encoding for the response.
205
206       $c->send_redirect( $loc )
207       $c->send_redirect( $loc, $code )
208       $c->send_redirect( $loc, $code, $entity_body )
209           Send a redirect response back to the client.  The location ($loc)
210           can be an absolute or relative URL. The $code must be one the
211           redirect status codes, and defaults to "301 Moved Permanently"
212
213       $c->send_error
214       $c->send_error( $code )
215       $c->send_error( $code, $error_message )
216           Send an error response back to the client.  If the $code is missing
217           a "Bad Request" error is reported.  The $error_message is a string
218           that is incorporated in the body of the HTML entity body.
219
220       $c->send_file_response( $filename )
221           Send back a response with the specified $filename as content.  If
222           the file is a directory we try to generate an HTML index of it.
223
224       $c->send_file( $filename )
225       $c->send_file( $fd )
226           Copy the file to the client.  The file can be a string (which will
227           be interpreted as a filename) or a reference to an "IO::Handle" or
228           glob.
229
230       $c->daemon
231           Return a reference to the corresponding "HTTP::Daemon" object.
232

SEE ALSO

234       RFC 2616
235
236       IO::Socket::IP, IO::Socket
237
239       Copyright 1996-2003, Gisle Aas
240
241       This library is free software; you can redistribute it and/or modify it
242       under the same terms as Perl itself.
243
244
245
246perl v5.16.3                      2018-10-30                   HTTP::Daemon(3)
Impressum