HTTP::Server::Simple
This code is still undergoing active development. Particularly, the API is not yet frozen. Comments about the API would be greatly appreciated.
use warnings; use strict; use HTTP::Server::Simple; my $server = HTTP::Server::Simple->new(); $server->run();
However, normally you will sub-class the HTTP::Server::Simple::CGI module (see HTTP::Server::Simple::CGI);
package Your::Web::Server; use base qw(HTTP::Server::Simple::CGI); sub handle_request { my ($self, $cgi) = @_; #... do something, print output to default # selected filehandle... } 1;
This is a simple standalone http dameon. It doesn't thread. It doesn't fork.
It does, however, act as a simple frontend which can turn a CGI into a standalone web-based application.
API call to start a new server. Does not actually start listening until you call ->run().
->run()
Takes an optional port number for this server to listen on.
Returns this server's port. (Defaults to 8080)
Takes an optional host address for this server to bind to.
Returns this server's bound address (if any). Defaults to undef (bind to all interfaces).
undef
Run the server in the background. returns pid.
Run the server. If all goes well, this won't ever return, but it will start listening for http requests.
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.
Net::Server
run
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.
getsockname
stdin_handle
stdout_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 (see HTTP::Server::Simple::Logger).
stdio_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 (see HTTP::Server::Simple::Logger).
A selection of these methods should be provided by sub-classes of this module.
This method is called after setup, with no parameters. It should print a valid, full HTTP response to the default selected filehandle.
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.
ITEM/METHOD Set to Example ----------- ------------------ ------------------------ method Request Method "GET", "POST", "HEAD" protocol HTTP version "HTTP/1.1" request_uri Complete Request URI "/foobar/baz?foo=bar" path Path part of URI "/foobar/baz" query_string Query String undef, "foo=bar" port Received Port 80, 8080 peername Remote name "200.2.4.5", "foo.com" peeraddr Remote address "200.2.4.5", "::1" localname Local interface "localhost", "myhost.com"
Receives HTTP headers and does something useful with them. This is called by the default setup() method.
setup()
You have lots of options when it comes to how you receive headers.
You can, if you really want, define parse_headers() and parse them raw yourself.
parse_headers()
Secondly, you can intercept them very slightly cooked via the setup() method, above.
Thirdly, you can leave the setup() header as-is (or calling the superclass setup() for unknown request items). Then you can define headers() in your sub-class and receive them all at once.
headers()
Finally, you can define handlers to receive individual HTTP headers. This can be useful for very simple SOAP servers (to name a crack-fueled standard that defines its own special HTTP headers).
To do so, you'll want to define the header() method in your subclass. That method will be handed a (key,value) pair of the header name and the value.
header()
If defined by a sub-class, this method is called directly after an accept happens.
If defined by a sub-class, this method is called after all setup has finished, before the handler method.
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 the HTTP request line.
Returns three values, the request method, request URI and the protocol Sub-classed versions of this should return three values - request method, request URI and proto
Parse incoming HTTP headers from STDIN.
Remember, this is a simple HTTP server, so nothing intelligent is done with them :-).
:-)
This should return an ARRAY ref of (header => value) pairs inside the array.
(header => value)
This routine binds the server to a port and interface.
This method should print a valid HTTP response that says that the request was invalid.
Copyright (c) 2004-2005 Jesse Vincent, <jesse@bestpractical.com>. All rights reserved.
Marcus Ramberg <drave@thefeed.no> contributed tests, cleanup, etc
Sam Vilain, <samv@cpan.org> contributed the CGI.pm split-out and header/setup API.
There certainly are some. Please report them via rt.cpan.org
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install HTTP::Server::Simple, copy and paste the appropriate command in to your terminal.
cpanm
cpanm HTTP::Server::Simple
CPAN shell
perl -MCPAN -e shell install HTTP::Server::Simple
For more information on module installation, please visit the detailed CPAN module installation guide.