.\" Automatically generated by Pod::Man v1.37, Pod::Parser v1.14 .\" .\" Standard preamble: .\" ======================================================================== .de Sh \" Subsection heading .br .if t .Sp .ne 5 .PP \fB\\$1\fR .PP .. .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. | will give a .\" real vertical bar. \*(C+ will give a nicer C++. Capital omega is used to .\" do unbreakable dashes and therefore won't be available. \*(C` and \*(C' .\" expand to `' in nroff, nothing in troff, for use with C<>. .tr \(*W-|\(bv\*(Tr .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' 'br\} .\" .\" If the F register is turned on, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . nr % 0 . rr F .\} .\" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .hy 0 .if n .na .\" .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). .\" Fear. Run. Save yourself. No user-serviceable parts. . \" fudge factors for nroff and troff .if n \{\ . ds #H 0 . ds #V .8m . ds #F .3m . ds #[ \f1 . ds #] \fP .\} .if t \{\ . ds #H ((1u-(\\\\n(.fu%2u))*.13m) . ds #V .6m . ds #F 0 . ds #[ \& . ds #] \& .\} . \" simple accents for nroff and troff .if n \{\ . ds ' \& . ds ` \& . ds ^ \& . ds , \& . ds ~ ~ . ds / .\} .if t \{\ . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' .\} . \" troff and (daisy-wheel) nroff accents .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' .ds 8 \h'\*(#H'\(*b\h'-\*(#H' .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] .ds ae a\h'-(\w'a'u*4/10)'e .ds Ae A\h'-(\w'A'u*4/10)'E . \" corrections for vroff .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' . \" for low resolution devices (crt and lpr) .if \n(.H>23 .if \n(.V>19 \ \{\ . ds : e . ds 8 ss . ds o a . ds d- d\h'-1'\(ga . ds D- D\h'-1'\(hy . ds th \o'bp' . ds Th \o'LP' . ds ae ae . ds Ae AE .\} .rm #[ #] #H #V #F C .\" ======================================================================== .\" .IX Title "HTTP::Daemon 3" .TH HTTP::Daemon 3 "2004-04-06" "perl v5.8.4" "User Contributed Perl Documentation" .SH "NAME" HTTP::Daemon \- a simple http server class .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 2 \& use HTTP::Daemon; \& use HTTP::Status; .Ve .PP .Vb 15 \& my $d = HTTP::Daemon->new || die; \& print "Please contact me at: url, ">\en"; \& while (my $c = $d->accept) { \& while (my $r = $c->get_request) { \& if ($r->method eq 'GET' and $r->url->path eq "/xyzzy") { \& # remember, this is *not* recommended practice :-) \& $c->send_file_response("/etc/passwd"); \& } \& else { \& $c->send_error(RC_FORBIDDEN) \& } \& } \& $c->close; \& undef($c); \& } .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" Instances of the \f(CW\*(C`HTTP::Daemon\*(C'\fR class are \s-1HTTP/1\s0.1 servers that listen on a socket for incoming requests. The \f(CW\*(C`HTTP::Daemon\*(C'\fR is a subclass of \f(CW\*(C`IO::Socket::INET\*(C'\fR, so you can perform socket operations directly on it too. .PP The \fIaccept()\fR method will return when a connection from a client is available. The returned value will be an \f(CW\*(C`HTTP::Daemon::ClientConn\*(C'\fR object which is another \f(CW\*(C`IO::Socket::INET\*(C'\fR subclass. Calling the \&\fIget_request()\fR method on this object will read data from the client and return an \f(CW\*(C`HTTP::Request\*(C'\fR object. The ClientConn object also provide methods to send back various responses. .PP This \s-1HTTP\s0 daemon does not \fIfork\fR\|(2) for you. Your application, i.e. the user of the \f(CW\*(C`HTTP::Daemon\*(C'\fR is responsible for forking if that is desirable. Also note that the user is responsible for generating responses that conform to the \s-1HTTP/1\s0.1 protocol. .PP The following methods of \f(CW\*(C`HTTP::Daemon\*(C'\fR are new (or enhanced) relative to the \f(CW\*(C`IO::Socket::INET\*(C'\fR base class: .IP "$d = HTTP::Daemon\->new" 4 .IX Item "$d = HTTP::Daemon->new" .PD 0 .ie n .IP "$d = HTTP::Daemon\->new( %opts )" 4 .el .IP "$d = HTTP::Daemon\->new( \f(CW%opts\fR )" 4 .IX Item "$d = HTTP::Daemon->new( %opts )" .PD The constructor method takes the same arguments as the \&\f(CW\*(C`IO::Socket::INET\*(C'\fR constructor, but unlike its base class it can also be called without any arguments. The daemon will then set up a listen queue of 5 connections and allocate some random port number. .Sp A server that wants to bind to some specific address on the standard \&\s-1HTTP\s0 port will be constructed like this: .Sp .Vb 4 \& $d = HTTP::Daemon->new( \& LocalAddr => 'www.thisplace.com', \& LocalPort => 80, \& ); .Ve .Sp See IO::Socket::INET for a description of other arguments that can be used configure the daemon during construction. .ie n .IP "$c = $d\->accept" 4 .el .IP "$c = \f(CW$d\fR\->accept" 4 .IX Item "$c = $d->accept" .PD 0 .ie n .IP "$c = $d\fR\->accept( \f(CW$pkg )" 4 .el .IP "$c = \f(CW$d\fR\->accept( \f(CW$pkg\fR )" 4 .IX Item "$c = $d->accept( $pkg )" .ie n .IP "($c, $peer_addr\fR) = \f(CW$d\->accept" 4 .el .IP "($c, \f(CW$peer_addr\fR) = \f(CW$d\fR\->accept" 4 .IX Item "($c, $peer_addr) = $d->accept" .PD This method works the same the one provided by the base class, but it returns an \f(CW\*(C`HTTP::Daemon::ClientConn\*(C'\fR reference by default. If a package name is provided as argument, then the returned object will be blessed into the given class. It is probably a good idea to make that class a subclass of \f(CW\*(C`HTTP::Daemon::ClientConn\*(C'\fR. .Sp The accept method will return \f(CW\*(C`undef\*(C'\fR if timeouts have been enabled and no connection is made within the given time. The \fItimeout()\fR method is described in IO::Socket. .Sp In list context both the client object and the peer address will be returned; see the description of the accept method IO::Socket for details. .IP "$d\->url" 4 .IX Item "$d->url" Returns a \s-1URL\s0 string that can be used to access the server root. .IP "$d\->product_tokens" 4 .IX Item "$d->product_tokens" Returns the name that this server will use to identify itself. This is the string that is sent with the \f(CW\*(C`Server\*(C'\fR response header. The main reason to have this method is that subclasses can override it if they want to use another product name. .Sp The default is the string \*(L"libwww\-perl\-daemon/#.##\*(R" where \*(L"#.##\*(R" is replaced with the version number of this module. .PP The \f(CW\*(C`HTTP::Daemon::ClientConn\*(C'\fR is a \f(CW\*(C`IO::Socket::INET\*(C'\fR subclass. Instances of this class are returned by the \fIaccept()\fR method of \f(CW\*(C`HTTP::Daemon\*(C'\fR. The following methods are provided: .IP "$c\->get_request" 4 .IX Item "$c->get_request" .PD 0 .ie n .IP "$c\->get_request( $headers_only )" 4 .el .IP "$c\->get_request( \f(CW$headers_only\fR )" 4 .IX Item "$c->get_request( $headers_only )" .PD This method read data from the client and turns it into an \&\f(CW\*(C`HTTP::Request\*(C'\fR object which is returned. It returns \f(CW\*(C`undef\*(C'\fR if reading fails. If it fails, then the \f(CW\*(C`HTTP::Daemon::ClientConn\*(C'\fR object ($c) should be discarded, and you should not try call this method again on it. The \f(CW$c\fR\->reason method might give you some information about why \f(CW$c\fR\->get_request failed. .Sp The \fIget_request()\fR method will normally not return until the whole request has been received from the client. This might not be what you want if the request is an upload of a large file (and with chunked transfer encoding \s-1HTTP\s0 can even support infinite request messages \- uploading live audio for instance). If you pass a \s-1TRUE\s0 value as the \&\f(CW$headers_only\fR argument, then \fIget_request()\fR will return immediately after parsing the request headers and you are responsible for reading the rest of the request content. If you are going to call \&\f(CW$c\fR\->get_request again on the same connection you better read the correct number of bytes. .IP "$c\->read_buffer" 4 .IX Item "$c->read_buffer" .PD 0 .ie n .IP "$c\->read_buffer( $new_value )" 4 .el .IP "$c\->read_buffer( \f(CW$new_value\fR )" 4 .IX Item "$c->read_buffer( $new_value )" .PD Bytes read by \f(CW$c\fR\->get_request, but not used are placed in the \fIread buffer\fR. The next time \f(CW$c\fR\->get_request is called it will consume the bytes in this buffer before reading more data from the network connection itself. The read buffer is invalid after \f(CW$c\fR\->get_request has failed. .Sp If you handle the reading of the request content yourself you need to empty this buffer before you read more and you need to place unconsumed bytes here. You also need this buffer if you implement services like \fI101 Switching Protocols\fR. .Sp This method always return the old buffer content and can optionally replace the buffer content if you pass it an argument. .IP "$c\->reason" 4 .IX Item "$c->reason" When \f(CW$c\fR\->get_request returns \f(CW\*(C`undef\*(C'\fR you can obtain a short string describing why it happened by calling \f(CW$c\fR\->reason. .ie n .IP "$c\->proto_ge( $proto )" 4 .el .IP "$c\->proto_ge( \f(CW$proto\fR )" 4 .IX Item "$c->proto_ge( $proto )" Return \s-1TRUE\s0 if the client announced a protocol with version number greater or equal to the given argument. The \f(CW$proto\fR argument can be a string like \*(L"\s-1HTTP/1\s0.1\*(R" or just \*(L"1.1\*(R". .IP "$c\->antique_client" 4 .IX Item "$c->antique_client" Return \s-1TRUE\s0 if the client speaks the \s-1HTTP/0\s0.9 protocol. No status code and no headers should be returned to such a client. This should be the same as !$c\->proto_ge(\*(L"\s-1HTTP/1\s0.0\*(R"). .IP "$c\->force_last_request" 4 .IX Item "$c->force_last_request" Make sure that \f(CW$c\fR\->get_request will not try to read more requests off this connection. If you generate a response that is not self delimiting, then you should signal this fact by calling this method. .Sp This attribute is turned on automatically if the client announces protocol \s-1HTTP/1\s0.0 or worse and does not include a \*(L"Connection: Keep\-Alive\*(R" header. It is also turned on automatically when \s-1HTTP/1\s0.1 or better clients send the \*(L"Connection: close\*(R" request header. .IP "$c\->send_status_line" 4 .IX Item "$c->send_status_line" .PD 0 .ie n .IP "$c\->send_status_line( $code )" 4 .el .IP "$c\->send_status_line( \f(CW$code\fR )" 4 .IX Item "$c->send_status_line( $code )" .ie n .IP "$c\->send_status_line( $code\fR, \f(CW$mess )" 4 .el .IP "$c\->send_status_line( \f(CW$code\fR, \f(CW$mess\fR )" 4 .IX Item "$c->send_status_line( $code, $mess )" .ie n .IP "$c\->send_status_line( $code\fR, \f(CW$mess\fR, \f(CW$proto )" 4 .el .IP "$c\->send_status_line( \f(CW$code\fR, \f(CW$mess\fR, \f(CW$proto\fR )" 4 .IX Item "$c->send_status_line( $code, $mess, $proto )" .PD Send the status line back to the client. If \f(CW$code\fR is omitted 200 is assumed. If \f(CW$mess\fR is omitted, then a message corresponding to \f(CW$code\fR is inserted. If \f(CW$proto\fR is missing the content of the \&\f(CW$HTTP::Daemon::PROTO\fR variable is used. .IP "$c\->send_crlf" 4 .IX Item "$c->send_crlf" Send the \s-1CRLF\s0 sequence to the client. .IP "$c\->send_basic_header" 4 .IX Item "$c->send_basic_header" .PD 0 .ie n .IP "$c\->send_basic_header( $code )" 4 .el .IP "$c\->send_basic_header( \f(CW$code\fR )" 4 .IX Item "$c->send_basic_header( $code )" .ie n .IP "$c\->send_basic_header( $code\fR, \f(CW$mess )" 4 .el .IP "$c\->send_basic_header( \f(CW$code\fR, \f(CW$mess\fR )" 4 .IX Item "$c->send_basic_header( $code, $mess )" .ie n .IP "$c\->send_basic_header( $code\fR, \f(CW$mess\fR, \f(CW$proto )" 4 .el .IP "$c\->send_basic_header( \f(CW$code\fR, \f(CW$mess\fR, \f(CW$proto\fR )" 4 .IX Item "$c->send_basic_header( $code, $mess, $proto )" .PD Send the status line and the \*(L"Date:\*(R" and \*(L"Server:\*(R" headers back to the client. This header is assumed to be continued and does not end with an empty \s-1CRLF\s0 line. .Sp See the description of \fIsend_status_line()\fR for the description of the accepted arguments. .ie n .IP "$c\->send_response( $res )" 4 .el .IP "$c\->send_response( \f(CW$res\fR )" 4 .IX Item "$c->send_response( $res )" Write a \f(CW\*(C`HTTP::Response\*(C'\fR object to the client as a response. We try hard to make sure that the response is self delimiting so that the connection can stay persistent for further request/response exchanges. .Sp The content attribute of the \f(CW\*(C`HTTP::Response\*(C'\fR object can be a normal string or a subroutine reference. If it is a subroutine, then whatever this callback routine returns is written back to the client as the response content. The routine will be called until it return an undefined or empty value. If the client is \s-1HTTP/1\s0.1 aware then we will use chunked transfer encoding for the response. .ie n .IP "$c\->send_redirect( $loc )" 4 .el .IP "$c\->send_redirect( \f(CW$loc\fR )" 4 .IX Item "$c->send_redirect( $loc )" .PD 0 .ie n .IP "$c\->send_redirect( $loc\fR, \f(CW$code )" 4 .el .IP "$c\->send_redirect( \f(CW$loc\fR, \f(CW$code\fR )" 4 .IX Item "$c->send_redirect( $loc, $code )" .ie n .IP "$c\->send_redirect( $loc\fR, \f(CW$code\fR, \f(CW$entity_body )" 4 .el .IP "$c\->send_redirect( \f(CW$loc\fR, \f(CW$code\fR, \f(CW$entity_body\fR )" 4 .IX Item "$c->send_redirect( $loc, $code, $entity_body )" .PD Send a redirect response back to the client. The location ($loc) can be an absolute or relative \s-1URL\s0. The \f(CW$code\fR must be one the redirect status codes, and defaults to \*(L"301 Moved Permanently\*(R" .IP "$c\->send_error" 4 .IX Item "$c->send_error" .PD 0 .ie n .IP "$c\->send_error( $code )" 4 .el .IP "$c\->send_error( \f(CW$code\fR )" 4 .IX Item "$c->send_error( $code )" .ie n .IP "$c\->send_error( $code\fR, \f(CW$error_message )" 4 .el .IP "$c\->send_error( \f(CW$code\fR, \f(CW$error_message\fR )" 4 .IX Item "$c->send_error( $code, $error_message )" .PD Send an error response back to the client. If the \f(CW$code\fR is missing a \&\*(L"Bad Request\*(R" error is reported. The \f(CW$error_message\fR is a string that is incorporated in the body of the \s-1HTML\s0 entity body. .ie n .IP "$c\->send_file_response( $filename )" 4 .el .IP "$c\->send_file_response( \f(CW$filename\fR )" 4 .IX Item "$c->send_file_response( $filename )" Send back a response with the specified \f(CW$filename\fR as content. If the file is a directory we try to generate an \s-1HTML\s0 index of it. .ie n .IP "$c\->send_file( $filename )" 4 .el .IP "$c\->send_file( \f(CW$filename\fR )" 4 .IX Item "$c->send_file( $filename )" .PD 0 .ie n .IP "$c\->send_file( $fd )" 4 .el .IP "$c\->send_file( \f(CW$fd\fR )" 4 .IX Item "$c->send_file( $fd )" .PD Copy the file to the client. The file can be a string (which will be interpreted as a filename) or a reference to an \f(CW\*(C`IO::Handle\*(C'\fR or glob. .IP "$c\->daemon" 4 .IX Item "$c->daemon" Return a reference to the corresponding \f(CW\*(C`HTTP::Daemon\*(C'\fR object. .SH "SEE ALSO" .IX Header "SEE ALSO" \&\s-1RFC\s0 2616 .PP IO::Socket::INET, IO::Socket .SH "COPYRIGHT" .IX Header "COPYRIGHT" Copyright 1996\-2003, Gisle Aas .PP This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.