| 1 |
|
| 2 |
package HTTP::Server::Simple::CGI; |
| 3 |
|
| 4 |
use base qw(HTTP::Server::Simple HTTP::Server::Simple::CGI::Environment); |
| 5 |
use strict; |
| 6 |
use warnings; |
| 7 |
|
| 8 |
use vars qw($default_doc $DEFAULT_CGI_INIT $DEFAULT_CGI_CLASS); |
| 9 |
|
| 10 |
$DEFAULT_CGI_CLASS = "CGI"; |
| 11 |
$DEFAULT_CGI_INIT = sub { require CGI; CGI::initialize_globals()}; |
| 12 |
|
| 13 |
|
| 14 |
=head1 NAME |
| 15 |
|
| 16 |
HTTP::Server::Simple::CGI - CGI.pm-style version of HTTP::Server::Simple |
| 17 |
|
| 18 |
=head1 DESCRIPTION |
| 19 |
|
| 20 |
HTTP::Server::Simple was already simple, but some smart-ass pointed |
| 21 |
out that there is no CGI in HTTP, and so this module was born to |
| 22 |
isolate the CGI.pm-related parts of this handler. |
| 23 |
|
| 24 |
|
| 25 |
=head2 accept_hook |
| 26 |
|
| 27 |
The accept_hook in this sub-class clears the environment to the |
| 28 |
start-up state. |
| 29 |
|
| 30 |
=cut |
| 31 |
|
| 32 |
sub accept_hook { |
| 33 |
my $self = shift; |
| 34 |
$self->setup_environment(@_); |
| 35 |
} |
| 36 |
|
| 37 |
=head2 post_setup_hook |
| 38 |
|
| 39 |
Initializes the global L<CGI> object, as well as other environment |
| 40 |
settings. |
| 41 |
|
| 42 |
=cut |
| 43 |
|
| 44 |
sub post_setup_hook { |
| 45 |
my $self = shift; |
| 46 |
$self->setup_server_url; |
| 47 |
if ( my $init = $self->cgi_init ) { |
| 48 |
$init->(); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
=head2 cgi_class [Classname] |
| 53 |
|
| 54 |
Gets or sets the class to use for creating the C<$cgi> object passed to |
| 55 |
C<handle_request>. |
| 56 |
|
| 57 |
Called with a single argument, it sets the coderef. Called with no arguments, |
| 58 |
it returns this field's current value. |
| 59 |
|
| 60 |
To provide an initialization subroutine to be run in the post_setup_hook, |
| 61 |
see L</cgi_init>. |
| 62 |
|
| 63 |
e.g. |
| 64 |
|
| 65 |
$server->cgi_class('CGI'); |
| 66 |
|
| 67 |
$server->cgi_init(sub { |
| 68 |
require CGI; |
| 69 |
CGI::initialize_globals(); |
| 70 |
}); |
| 71 |
|
| 72 |
or, if you want to use L<CGI::Simple>, |
| 73 |
|
| 74 |
$server->cgi_class('CGI::Simple'); |
| 75 |
$server->cgi_init(sub { |
| 76 |
require CGI::Simple; |
| 77 |
}); |
| 78 |
|
| 79 |
=cut |
| 80 |
|
| 81 |
sub cgi_class { |
| 82 |
my $self = shift; |
| 83 |
if (@_) { |
| 84 |
$self->{cgi_class} = shift; |
| 85 |
} |
| 86 |
return $self->{cgi_class} || $DEFAULT_CGI_CLASS; |
| 87 |
} |
| 88 |
|
| 89 |
=head2 cgi_init [CODEREF] |
| 90 |
|
| 91 |
A coderef to run in the post_setup_hook. |
| 92 |
|
| 93 |
Called with a single argument, it sets the coderef. Called with no arguments, |
| 94 |
it returns this field's current value. |
| 95 |
|
| 96 |
=cut |
| 97 |
|
| 98 |
sub cgi_init { |
| 99 |
my $self = shift; |
| 100 |
if (@_) { |
| 101 |
$self->{cgi_init} = shift; |
| 102 |
} |
| 103 |
return $self->{cgi_init} || $DEFAULT_CGI_INIT; |
| 104 |
|
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
=head2 setup |
| 109 |
|
| 110 |
This method sets up CGI environment variables based on various |
| 111 |
meta-headers, like the protocol, remote host name, request path, etc. |
| 112 |
|
| 113 |
See the docs in L<HTTP::Server::Simple> for more detail. |
| 114 |
|
| 115 |
=cut |
| 116 |
|
| 117 |
sub setup { |
| 118 |
my $self = shift; |
| 119 |
$self->setup_environment_from_metadata(@_); |
| 120 |
} |
| 121 |
|
| 122 |
=head2 handle_request CGI |
| 123 |
|
| 124 |
This routine is called whenever your server gets a request it can |
| 125 |
handle. |
| 126 |
|
| 127 |
It's called with a CGI object that's been pre-initialized. |
| 128 |
You want to override this method in your subclass |
| 129 |
|
| 130 |
|
| 131 |
=cut |
| 132 |
|
| 133 |
$default_doc = ( join "", <DATA> ); |
| 134 |
|
| 135 |
sub handle_request { |
| 136 |
my ( $self, $cgi ) = @_; |
| 137 |
|
| 138 |
print "HTTP/1.0 200 OK\r\n"; # probably OK by now |
| 139 |
print "Content-Type: text/html\r\nContent-Length: ", length($default_doc), |
| 140 |
"\r\n\r\n", $default_doc; |
| 141 |
} |
| 142 |
|
| 143 |
=head2 handler |
| 144 |
|
| 145 |
Handler implemented as part of HTTP::Server::Simple API |
| 146 |
|
| 147 |
=cut |
| 148 |
|
| 149 |
sub handler { |
| 150 |
my $self = shift; |
| 151 |
my $cgi; |
| 152 |
$cgi = $self->cgi_class->new; |
| 153 |
eval { $self->handle_request($cgi) }; |
| 154 |
if ($@) { |
| 155 |
my $error = $@; |
| 156 |
warn $error; |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
1; |
| 161 |
|
| 162 |
__DATA__ |
| 163 |
<html> |
| 164 |
<head> |
| 165 |
<title>Hello!</title> |
| 166 |
</head> |
| 167 |
<body> |
| 168 |
<h1>Congratulations!</h1> |
| 169 |
|
| 170 |
<p>You now have a functional HTTP::Server::Simple::CGI running. |
| 171 |
</p> |
| 172 |
|
| 173 |
<p><i>(If you're seeing this page, it means you haven't subclassed |
| 174 |
HTTP::Server::Simple::CGI, which you'll need to do to make it |
| 175 |
useful.)</i> |
| 176 |
</p> |
| 177 |
</body> |
| 178 |
</html> |