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

SEE ALSO

227       RFC 2616
228
229       IO::Socket::INET, IO::Socket
230
232       Copyright 1996-2003, Gisle Aas
233
234       This library is free software; you can redistribute it and/or modify it
235       under the same terms as Perl itself.
236
237
238
239perl v5.8.8                       2004-04-06                   HTTP::Daemon(3)
Impressum