NAME
Net::Server::HTTP - very basic Net::Server based HTTP server class
TEST ONE LINER
perl -e 'use base qw(Net::Server::HTTP); main->run(port => 8080)'
# will start up an echo server
SYNOPSIS
use base qw(Net::Server::HTTP);
__PACKAGE__->run;
sub process_http_request {
my $self = shift;
print "Content-type: text/html\n\n";
print "<form method=post action=/bam><input type=text name=foo><input type=submit></form>\n";
require Data::Dumper;
local $Data::Dumper::Sortkeys = 1;
require CGI;
my $form = {};
my $q = CGI->new; $form->{$_} = $q->param($_) for $q->param;
print "<pre>".Data::Dumper->Dump([\%ENV, $form], ['*ENV', 'form'])."</pre>";
}
DESCRIPTION
Even though Net::Server::HTTP doesn't fall into the normal parallel of the other Net::Server flavors, handling HTTP requests is an often requested feature and is a standard and simple protocol.
Net::Server::HTTP begins with base type MultiType defaulting to Net::Server::Fork. It is easy to change it to any of the other Net::Server flavors by passing server_type => $other_flavor in the server configurtation. The port has also been defaulted to port 80 - but could easily be changed to another through the server configuration. You can also very easily add ssl by including, proto=>"ssl" and provide a SSL_cert_file and SSL_key_file.
For example, here is a basic server that will bind to all interfaces, will speak both HTTP on port 8080 as well as HTTPS on 8443, and will speak both IPv4, as well as IPv6 if it is available.
use base qw(Net::Server::HTTP);
__PACKAGE__->run(
port => [8080, "8443/ssl"],
ipv => '*', # IPv6 if available
SSL_key_file => '/my/key',
SSL_cert_file => '/my/cert',
);
METHODS
process_http_request
-
Will be passed the client handle, and will have STDOUT and STDIN tied to the client.
During this method, the %ENV will have been set to a standard CGI style environment. You will need to be sure to print the Content-type header. This is one change from the other standard Net::Server base classes.
During this method you can read from %ENV and STDIN just like a normal HTTP request in other web servers. You can print to STDOUT and Net::Server will handle the header negotiation for you.
Note: Net::Server::HTTP has no concept of document root or script aliases or default handling of static content. That is up to the consumer of Net::Server::HTTP to work out.
Net::Server::HTTP comes with a basic %ENV display installed as the default process_http_request method.
process_request
-
This method has been overridden in Net::Server::HTTP - you should not use it while using Net::Server::HTTP. This overridden method parses the environment and sets up request alarms and handles dying failures. It calls process_http_request once the request is ready and headers have been parsed.
send_status
-
Takes an HTTP status and a message. Sends out the correct headers.
send_500
-
Calls send_status with 500 and the argument passed to send_500.
exec_cgi
-
Allow for calling an external script as a CGI. This will use IPC::Open3 to fork a new process and read/write from it.
use base qw(Net::Server::HTTP); __PACKAGE__->run; sub process_http_request { my $self = shift; if ($ENV{'PATH_INFO'} && $ENV{'PATH_INFO'} =~ s{^ (/foo) (?= $ | /) }{}x) { $ENV{'SCRIPT_NAME'} = $1; my $file = "/var/www/cgi-bin/foo"; # assuming this exists return $self->exec_cgi($file); } print "Content-type: text/html\n\n"; print "<a href=/foo>Foo</a>"; }
At this first release, the parent server is not tracking the child script which may cause issues if the script is running when a HUP is received.
exec_trusted_perl
-
Allow for calling an external perl script. This method will still fork, but instead of using IPC::Open3, it simply requires the perl script. That means that the running script will be able to make use of any shared memory. It also means that the STDIN/STDOUT/STDERR handles the script is using are those directly bound by the server process.
use base qw(Net::Server::HTTP); __PACKAGE__->run; sub process_http_request { my $self = shift; if ($ENV{'PATH_INFO'} && $ENV{'PATH_INFO'} =~ s{^ (/foo) (?= $ | /) }{}x) { $ENV{'SCRIPT_NAME'} = $1; my $file = "/var/www/cgi-bin/foo"; # assuming this exists return $self->exec_trusted_perl($file); } print "Content-type: text/html\n\n"; print "<a href=/foo>Foo</a>"; }
At this first release, the parent server is not tracking the child script which may cause issues if the script is running when a HUP is received.
OPTIONS
In addition to the command line arguments of the Net::Server base classes you can also set the following options.
- max_header_size
-
Defaults to 100_000. Maximum number of bytes to read while parsing headers.
- server_revision
-
Defaults to Net::Server::HTTP/$Net::Server::VERSION.
- timeout_header
-
Defaults to 15 - number of seconds to wait for parsing headers.
- timeout_idle
-
Defaults to 60 - number of seconds a request can be idle before the request is closed.
TODO
Add support for writing out HTTP/1.1.
AUTHOR
Paul T. Seamons paul@seamons.com
THANKS
See Net::Server
SEE ALSO
Please see also Net::Server::Fork, Net::Server::INET, Net::Server::PreFork, Net::Server::PreForkSimple, Net::Server::MultiType, Net::Server::Single Net::Server::SIG Net::Server::Daemonize Net::Server::Proto