| HTTP-Server-Simple documentation | Contained in the HTTP-Server-Simple distribution. |
HTTP::Server::Simple::CGI - CGI.pm-style version of HTTP::Server::Simple
HTTP::Server::Simple was already simple, but some smart-ass pointed out that there is no CGI in HTTP, and so this module was born to isolate the CGI.pm-related parts of this handler.
The accept_hook in this sub-class clears the environment to the start-up state.
Initializes the global CGI object, as well as other environment settings.
Gets or sets the class to use for creating the $cgi object passed to
handle_request.
Called with a single argument, it sets the coderef. Called with no arguments, it returns this field's current value.
To provide an initialization subroutine to be run in the post_setup_hook, see cgi_init.
e.g.
$server->cgi_class('CGI');
$server->cgi_init(sub {
require CGI;
CGI::initialize_globals();
});
or, if you want to use CGI::Simple,
$server->cgi_class('CGI::Simple');
$server->cgi_init(sub {
require CGI::Simple;
});
A coderef to run in the post_setup_hook.
Called with a single argument, it sets the coderef. Called with no arguments, it returns this field's current value.
This method sets up CGI environment variables based on various meta-headers, like the protocol, remote host name, request path, etc.
See the docs in HTTP::Server::Simple for more detail.
This routine is called whenever your server gets a request it can handle.
It's called with a CGI object that's been pre-initialized. You want to override this method in your subclass
Handler implemented as part of HTTP::Server::Simple API
| HTTP-Server-Simple documentation | Contained in the HTTP-Server-Simple distribution. |
package HTTP::Server::Simple::CGI; use base qw(HTTP::Server::Simple HTTP::Server::Simple::CGI::Environment); use strict; use warnings; use vars qw($VERSION $default_doc $DEFAULT_CGI_INIT $DEFAULT_CGI_CLASS); $VERSION = $HTTP::Server::Simple::VERSION; $DEFAULT_CGI_CLASS = "CGI"; $DEFAULT_CGI_INIT = sub { require CGI; CGI::initialize_globals()};
sub accept_hook { my $self = shift; $self->setup_environment(@_); }
sub post_setup_hook { my $self = shift; $self->setup_server_url; if ( my $init = $self->cgi_init ) { $init->(); } }
sub cgi_class { my $self = shift; if (@_) { $self->{cgi_class} = shift; } return $self->{cgi_class} || $DEFAULT_CGI_CLASS; }
sub cgi_init { my $self = shift; if (@_) { $self->{cgi_init} = shift; } return $self->{cgi_init} || $DEFAULT_CGI_INIT; }
sub setup { my $self = shift; $self->setup_environment_from_metadata(@_); }
$default_doc = ( join "", <DATA> ); sub handle_request { my ( $self, $cgi ) = @_; print "HTTP/1.0 200 OK\r\n"; # probably OK by now print "Content-Type: text/html\r\nContent-Length: ", length($default_doc), "\r\n\r\n", $default_doc; }
sub handler { my $self = shift; my $cgi; $cgi = $self->cgi_class->new; eval { $self->handle_request($cgi) }; if ($@) { my $error = $@; warn $error; } } 1; __DATA__ <html> <head> <title>Hello!</title> </head> <body> <h1>Congratulations!</h1> <p>You now have a functional HTTP::Server::Simple::CGI running. </p> <p><i>(If you're seeing this page, it means you haven't subclassed HTTP::Server::Simple::CGI, which you'll need to do to make it useful.)</i> </p> </body> </html>