DESCRIPTION

This is a simple standalone HTTP server. By default, it doesn't thread or fork. It does, however, act as a simple frontend which can be used to build a standalone web-based application or turn a CGI into one.

It is possible to use Net::Server classes to create forking, pre-forking, and other types of more complicated servers; see "net_server".

By default, the server traps a few signals:

HUP

When you kill -HUP the server, it lets the current request finish being processed, then uses the restart method to re-exec itself. Please note that in order to provide restart-on-SIGHUP, HTTP::Server::Simple sets a SIGHUP handler during initialisation. If your request handling code forks you need to make sure you reset this or unexpected things will happen if somebody sends a HUP to all running processes spawned by your app (e.g. by "kill -HUP <script>")

PIPE

If the server detects a broken pipe while writing output to the client, it ignores the signal. Otherwise, a client closing the connection early could kill the server.

METHODS

HTTP::Server::Simple->new($port, $family)

API call to start a new server. Does not actually start listening until you call ->run(). If omitted, $port defaults to 8080, and $family defaults to Socket::AF_INET. The alternative domain is Socket::AF_INET6.

lookup_localhost

Looks up the local host's IP address, and returns it. For most hosts, this is 127.0.0.1, or possibly ::1.

port [NUMBER]

Takes an optional port number for this server to listen on.

Returns this server's port. (Defaults to 8080)

family [NUMBER]

Takes an optional address family for this server to use. Valid values are Socket::AF_INET and Socket::AF_INET6. All other values are silently changed into Socket::AF_INET for backwards compatibility with previous versions of the module.

Returns the address family of the present listening socket. (Defaults to Socket::AF_INET.)

host [address]

background [ARGUMENTS]

Runs the server in the background, and returns the process ID of the started process. Any arguments will be passed through to "run".

run [ARGUMENTS]

Run the server. If all goes well, this won't ever return, but it will start listening for HTTP requests. Any arguments passed to this will be passed on to the underlying Net::Server implementation, if one is used (see "net_server").

net_server

User-overridable method. If you set it to a Net::Server subclass, that subclass is used for the run method. Otherwise, a minimal implementation is used as default.

restart

Restarts the server. Usually called by a HUP signal, not directly.

stdio_handle [FILEHANDLE]

When called with an argument, sets the socket to the server to that arg.

Returns the socket to the server; you should only use this for actual socket-related calls like getsockname. If all you want is to read or write to the socket, you should use stdin_handle and stdout_handle to get the in and out filehandles explicitly.

stdin_handle

Returns a filehandle used for input from the client. By default, returns whatever was set with stdio_handle, but a subclass could do something interesting here.

stdout_handle

Returns a filehandle used for output to the client. By default, returns whatever was set with stdio_handle, but a subclass could do something interesting here.

IMPORTANT SUB-CLASS METHODS

A selection of these methods should be provided by sub-classes of this module.

handler

This method is called after setup, with no parameters. It should print a valid, full HTTP response to the default selected filehandle.

setup(name => $value, ...)

This method is called with a name => value list of various things to do with the request. This list is given below.

The default setup handler simply tries to call methods with the names of keys of this list.

post_setup_hook

If defined by a sub-class, this method is called after all setup has finished, before the handler method.

print_banner

This routine prints a banner before the server request-handling loop starts.

Methods below this point are probably not terribly useful to define yourself in subclasses.

parse_request

Parse the HTTP request line. Returns three values, the request method, request URI and the protocol.

parse_headers

Parses incoming HTTP headers from STDIN, and returns an arrayref of (header => value) pairs. See "headers" for possibilities on how to inspect headers.

setup_listener

This routine binds the server to a port and interface.

after_setup_listener

This method is called immediately after setup_listener. It's here just for you to override.

bad_request

This method should print a valid HTTP response that says that the request was invalid.

valid_http_method($method)

Given a candidate HTTP method in $method, determine if it is valid. Override if, for example, you'd like to do some WebDAV. The default implementation only accepts GET, POST, HEAD, PUT, PATCH, DELETE and OPTIONS.

As a valued partner and proud supporter of MetaCPAN, StickerYou is
happy to offer a 10% discount on all Custom Stickers,
Business Labels, Roll Labels,
Vinyl Lettering or Custom Decals. StickerYou.com
is your one-stop shop to make your business stick.
Use code METACPAN10 at checkout to apply your discount.