MAN page from RedHat Other LWP.pm-5.48-2.i386.rpm
lib::HTTP::Daemon
Section: User Contributed Perl Documentation (3)
Updated: libwww-perl-5.48
Index NAME
HTTP::Daemon - a simple http server class
SYNOPSIS
use HTTP::Daemon; use HTTP::Status;
my $d = new HTTP::Daemon; print "Please contact me at: <URL:", $d->url, ">\n"; 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* recommened practice :-) $c->send_file_response("/etc/passwd"); } else { $c->send_error(RC_FORBIDDEN) } } $c->close; undef($c); } DESCRIPTION
Instances of the
HTTP::Daemon class are HTTP/1.1 servers thatlisten on a socket for incoming requests. The
HTTP::Daemon is asub-class of
IO::Socket::INET, so you can perform socket operationsdirectly on it too.
The accept() method will return when a connection from a client isavailable. The returned value will be a reference to a object of theHTTP::Daemon::ClientConn class which is another IO::Socket::INETsubclass. Calling the get_request() method on this object will readdata from the client and return an HTTP::Request object reference.
This HTTP daemon does not fork(2) for you. Your application, i.e. theuser of the HTTP::Daemon is reponsible for forking if that isdesirable. Also note that the user is responsible for generatingresponses that conform to the HTTP/1.1 protocol. TheHTTP::Daemon::ClientConn class provides some methods that make this easier.
METHODS
The following is a list of methods that are new (or enhanced) relativeto the
IO::Socket::INET base class.
- $d = new HTTP::Daemon
- The constructor takes the same parameters as theIO::Socket::INET constructor. It can also be called without specifyingany parameters. The daemon will then set up a listen queue of 5connections and allocate some random port number. A server that wantsto bind to some specific address on the standard HTTP port will beconstructed like this:
$d = new HTTP::Daemon LocalAddr => 'www.someplace.com', LocalPort => 80;
- $c = $d->accept([$pkg])
- This method is the same as IO::Socket::accept but returns anHTTP::Daemon::ClientConn reference by default. It returnsundef if you specify a timeout and no connection is made withinthat time.
- $d->url
- Returns a URL string that can be used to access the server root.
- $d->product_tokens
- Returns the name that this server will use to identify itself. Thisis the string that is sent with the Server response header. Themain reason to have this method is that subclasses can override it ifthey want to use another product name.
The HTTP::Daemon::ClientConn is also a IO::Socket::INETsubclass. Instances of this class are returned by the accept() methodof HTTP::Daemon. The following additional methods areprovided:
- $c->get_request([$headers_only])
- Read data from the client and turn it into anHTTP::Request object which is then returned. It returns undefif reading of the request fails. If it fails, then theHTTP::Daemon::ClientConn object ($c) should be discarded, and youshould not call this method again. The $c->reason method might giveyou some information about why $c->get_request returned undef.
The $c->get_request method supports HTTP/1.1 request content bodies,including chunked transfer encoding with footer and self delimitingmultipart/* content types.
The $c->get_request method will normally not return until the wholerequest has been received from the client. This might not be what youwant if the request is an upload of a multi-mega-byte file (and withchunked transfer encoding HTTP can even support infinite requestmessages - uploading live audio for instance). If you pass a TRUEvalue as the $headers_only argument, then $c->get_request will returnimmediately after parsing the request headers and you are responsiblefor reading the rest of the request content. If you are going tocall $c->get_request again on the same connection you better read thecorrect number of bytes.
- $c->read_buffer([$new_value])
- Bytes read by $c->get_request, but not used are placed in the readbuffer. The next time $c->get_request is called it will consume thebytes in this buffer before reading more data from the networkconnection itself. The read buffer is invalid after $c->get_requesthas returned an undefined value.
If you handle the reading of the request content yourself you need toempty this buffer before you read more and you need to placeunconsumed bytes here. You also need this buffer if you implementservices like 101 Switching Protocols.
This method always return the old buffer content and can optionallyreplace the buffer content if you pass it an argument.
- $c->reason
- When $c->get_request returns undef you can obtain a short stringdescribing why it happened by calling $c->reason.
- $c->proto_ge($proto)
- Return TRUE if the client announced a protocol with version numbergreater or equal to the given argument. The $proto argument can be astring like ``HTTP/1.1'' or just ``1.1''.
- $c->antique_client
- Return TRUE if the client speaks the HTTP/0.9 protocol. No statuscode and no headers should be returned to such a client. This shouldbe the same as !$c->proto_ge("HTTP/1.0").
- $c->force_last_request
- Make sure that $c->get_request will not try to read more requests offthis connection. If you generate a response that is not selfdelimiting, then you should signal this fact by calling this method.
This attribute is turned on automatically if the client announcesprotocol HTTP/1.0 or worse and does not include a ``Connection:Keep-Alive'' header. It is also turned on automatically when HTTP/1.1or better clients send the ``Connection: close'' request header.
- $c->send_status_line( [$code, [$mess, [$proto]]] )
- Send the status line back to the client. If $code is omitted 200 isassumed. If $mess is omitted, then a message corresponding to $codeis inserted. If $proto is missing the content of the$HTTP::Daemon::PROTO variable is used.
- $c->send_crlf
- Send the CRLF sequence to the client.
- $c->send_basic_header( [$code, [$mess, [$proto]]] )
- Send the status line and the ``Date:'' and ``Server:'' headers back tothe client. This header is assumed to be continued and does not endwith an empty CRLF line.
- $c->send_response( [$res] )
- Write a HTTP::Response object to theclient as a response. We try hard to make sure that the response isself delimiting so that the connection can stay persistent for furtherrequest/response exchanges.
The content attribute of the HTTP::Response object can be a normalstring or a subroutine reference. If it is a subroutine, thenwhatever this callback routine returns is written back to theclient as the response content. The routine will be called until itreturn an undefined or empty value. If the client is HTTP/1.1 awarethen we will use chunked transfer encoding for the response.
- $c->send_redirect( $loc, [$code, [$entity_body]] )
- Send a redirect response back to the client. The location ($loc) canbe an absolute or relative URL. The $code must be one the redirectstatus codes, and defaults to ``301 Moved Permanently''
- $c->send_error( [$code, [$error_message]] )
- Send an error response back to the client. If the $code is missing a``Bad Request'' error is reported. The $error_message is a string thatis incorporated in the body of the HTML entity body.
- $c->send_file_response($filename)
- Send back a response with the specified $filename as content. If thefile is a directory we try to generate an HTML index of it.
- $c->send_file($fd);
- Copy the file to the client. The file can be a string (whichwill be interpreted as a filename) or a reference to an IO::Handleor glob.
- $c->daemon
- Return a reference to the corresponding HTTP::Daemon object.
SEE ALSO
RFC 2068
the IO::Socket manpage, the Apache manpage
COPYRIGHT
Copyright 1996-1998, Gisle Aas
This library is free software; you can redistribute it and/ormodify it under the same terms as Perl itself.
Index
- NAME
- SYNOPSIS
- DESCRIPTION
- METHODS
- SEE ALSO
- COPYRIGHT
This document was created byman2html,using the manual pages.