perl -e 'use base qw(Net::Server::HTTP); main->run(port => 8080)' # will start up an echo server
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>"; }
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', );
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.
The following is a listing of the available parameters as well as sample output based on a very basic HTTP server.
%% % # a percent %a ::1 # remote ip %A ::1 # local ip %b 83 # response size (- if 0) Common Log Format %B 83 # response size %{bar}C baz # value of cookie by that name %D 916 # elapsed in microseconds %{HTTP_COOKIE}e bar=baz # value of %ENV by that name %f - # filename - unused %h ::1 # remote host if lookups are on, remote ip otherwise %H http # request protocol %{Host}i localhost:8080 # request header by that name %I 336 # bytes received including headers %l - # remote logname - unsused %m GET # request method %n Just a note # http_note by that name %{Content-type}o text/html # output header by that name %O 189 # response size including headers %p 8080 # server port %P 22999 # pid - does not support %{tid}P q ?hello=there # query_string including ? (- otherwise) r GET /bam?hello=there HTTP/1.1 # the first line of the request %s 200 # response status %u - # remote user - unused %U /bam # request path (no query string) %t [06/Jun/2012:12:14:21 -0600] # http_log_time standard format %t{%F %T %z}t [2012-06-06 12:14:21 -0600] # http_log_time with format %T 0 # elapsed time in seconds %v localhost:8080 # http_log_vhost - partial implementation %V localhost:8080 # http_log_vhost - partial implementation %X - # Connection completed and is 'close' (-)
Additionally, the log parsing allows for the following formats.
%>s 200 # status of last request %<s 200 # status of original request %400a - # remote ip if status is 400 %!400a ::1 # remote ip if status is not 400 %!200a - # remote ip if status is not 200
There are few bits not completely implemented:
> and < # There is no internal redirection %I # The answer to this is based on header size and Content-length instead of the more correct actual number of bytes read though in common cases those would be the same. %X # There is no Connection keepalive in the default server. %v and %V # There are no virtual hosts in the default HTTP server. %{tid}P # The default servers are not threaded.
See the "access_log_format" option for how to set a different format as well as to see the default string.
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.
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.
The default value is the NCSA extended/combined log format:
'%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"'
The locations will be added in the order that they are configured. They will be added to a regular expression which will be applied to the incoming PATH_INFO string. If the match is successful, the $ENV{'SCRIPT_NAME'} will be set to the matched portion and the matched portion will be removed from $ENV{'PATH_INFO'}.
Once an app has been passed, it is necessary for the server to listen on /. Therefore if ``/'' has not been specifically configured for dispatch, the first found dispatch target will also be used to handle ``/''.
For convenience, if the log_level is 2 or greater, the dispatch table is output to the log.
This mechanism is left as a generic mechanism suitable for overriding by servers meant to handle more complex dispatch. At the moment there is no handling of virtual hosts. At some point we will add in the default ability to play static content and likely for the ability to configure virtual hosts - or that may have to wait for a third party module.
app => "/home/paul/foo.cgi", # Dispatch: /home/paul/foo.cgi => home/paul/foo.cgi # Dispatch: / => home/paul/foo.cgi (default) app => "../../foo.cgi", app => "./bar.cgi", app => "baz ./bar.cgi", app => "bim=./bar.cgi", # Dispatch: /foo.cgi => ../../foo.cgi # Dispatch: /bar.cgi => ./bar.cgi # Dispatch: /baz => ./bar.cgi # Dispatch: /bim => ./bar.cgi # Dispatch: / => ../../foo.cgi (default) app => "../../foo.cgi", app => "/=./bar.cgi", # Dispatch: /foo.cgi => ../../foo.cgi # Dispatch: / => ./bar.cgi # you could also do this on the commandline net-server HTTP app ../../foo.cgi app /=./bar.cgi # extended options when configured from code Net::Server::HTTP->run(app => { # loses order of matching '/' => sub { ... }, '/foo' => sub { ... }, '/bar' => '/path/to/some.cgi', }); Net::Server::HTTP->run(app => [ '/' => sub { ... }, '/foo' => sub { ... }, '/bar' => '/path/to/some.cgi', ]);