1HTTP::Daemon(3) User Contributed Perl Documentation HTTP::Daemon(3)
2
3
4
6 HTTP::Daemon - a simple http server class
7
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
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
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::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
55 number.
56
57 A server that wants to bind to some specific address on the
58 standard 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 reads 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
117 responsible for reading the rest of the request content. If you
118 are going to call $c->get_request again on the same connection you
119 better 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 returns the old buffer content and can
135 optionally 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->head_request
152 Return TRUE if the last request was a "HEAD" request. No content
153 body must be generated for these requests.
154
155 $c->force_last_request
156 Make sure that $c->get_request will not try to read more requests
157 off this connection. If you generate a response that is not self
158 delimiting, then you should signal this fact by calling this
159 method.
160
161 This attribute is turned on automatically if the client announces
162 protocol HTTP/1.0 or worse and does not include a "Connection:
163 Keep-Alive" header. It is also turned on automatically when
164 HTTP/1.1 or better clients send the "Connection: close" request
165 header.
166
167 $c->send_status_line
168 $c->send_status_line( $code )
169 $c->send_status_line( $code, $mess )
170 $c->send_status_line( $code, $mess, $proto )
171 Send the status line back to the client. If $code is omitted 200
172 is assumed. If $mess is omitted, then a message corresponding to
173 $code is inserted. If $proto is missing the content of the
174 $HTTP::Daemon::PROTO variable is used.
175
176 $c->send_crlf
177 Send the CRLF sequence to the client.
178
179 $c->send_basic_header
180 $c->send_basic_header( $code )
181 $c->send_basic_header( $code, $mess )
182 $c->send_basic_header( $code, $mess, $proto )
183 Send the status line and the "Date:" and "Server:" headers back to
184 the client. This header is assumed to be continued and does not
185 end with an empty CRLF line.
186
187 See the description of send_status_line() for the description of
188 the accepted arguments.
189
190 $c->send_header( $field, $value )
191 $c->send_header( $field1, $value1, $field2, $value2, ... )
192 Send one or more header lines.
193
194 $c->send_response( $res )
195 Write a "HTTP::Response" object to the client as a response. We
196 try hard to make sure that the response is self delimiting so that
197 the connection can stay persistent for further request/response
198 exchanges.
199
200 The content attribute of the "HTTP::Response" object can be a
201 normal string or a subroutine reference. If it is a subroutine,
202 then whatever this callback routine returns is written back to the
203 client as the response content. The routine will be called until
204 it return an undefined or empty value. If the client is HTTP/1.1
205 aware then we will use chunked transfer encoding for the response.
206
207 $c->send_redirect( $loc )
208 $c->send_redirect( $loc, $code )
209 $c->send_redirect( $loc, $code, $entity_body )
210 Send a redirect response back to the client. The location ($loc)
211 can be an absolute or relative URL. The $code must be one the
212 redirect status codes, and defaults to "301 Moved Permanently"
213
214 $c->send_error
215 $c->send_error( $code )
216 $c->send_error( $code, $error_message )
217 Send an error response back to the client. If the $code is missing
218 a "Bad Request" error is reported. The $error_message is a string
219 that is incorporated in the body of the HTML entity body.
220
221 $c->send_file_response( $filename )
222 Send back a response with the specified $filename as content. If
223 the file is a directory we try to generate an HTML index of it.
224
225 $c->send_file( $filename )
226 $c->send_file( $fd )
227 Copy the file to the client. The file can be a string (which will
228 be interpreted as a filename) or a reference to an "IO::Handle" or
229 glob.
230
231 $c->daemon
232 Return a reference to the corresponding "HTTP::Daemon" object.
233
235 RFC 2616
236
237 IO::Socket::INET, IO::Socket
238
240 Copyright 1996-2003, Gisle Aas
241
242 This library is free software; you can redistribute it and/or modify it
243 under the same terms as Perl itself.
244
245
246
247perl v5.12.4 2009-06-15 HTTP::Daemon(3)