| 1 |
|
| 2 |
package HTTP::Server::Simple::CGI::Environment; |
| 3 |
|
| 4 |
use strict; |
| 5 |
use warnings; |
| 6 |
use HTTP::Server::Simple; |
| 7 |
|
| 8 |
use vars qw(%ENV_MAPPING); |
| 9 |
|
| 10 |
my %clean_env = %ENV; |
| 11 |
|
| 12 |
=head1 NAME |
| 13 |
|
| 14 |
HTTP::Server::Simple::CGI::Environment - a HTTP::Server::Simple mixin to provide the CGI protocol |
| 15 |
|
| 16 |
=head1 DESCRIPTION |
| 17 |
|
| 18 |
This mixin abstracts the CGI protocol out from |
| 19 |
L<HTTP::Server::Simple::CGI> so that it's easier to provide your own |
| 20 |
CGI handlers with L<HTTP::Server::Simple> which B<don't> use CGI.pm |
| 21 |
|
| 22 |
=head2 setup_environment |
| 23 |
|
| 24 |
C<setup_environemnt> is usually called in the superclass's accept_hook |
| 25 |
|
| 26 |
This routine in this sub-class clears the environment to the |
| 27 |
start-up state. |
| 28 |
|
| 29 |
=cut |
| 30 |
|
| 31 |
sub setup_environment { |
| 32 |
%ENV = ( |
| 33 |
%clean_env, |
| 34 |
SERVER_SOFTWARE => "HTTP::Server::Simple/$HTTP::Server::Simple::VERSION", |
| 35 |
GATEWAY_INTERFACE => 'CGI/1.1' |
| 36 |
); |
| 37 |
} |
| 38 |
|
| 39 |
=head2 setup_server_url |
| 40 |
|
| 41 |
Sets up the C<SERVER_URL> environment variable |
| 42 |
|
| 43 |
=cut |
| 44 |
|
| 45 |
sub setup_server_url { |
| 46 |
$ENV{SERVER_URL} |
| 47 |
||= ( "http://" . ($ENV{SERVER_NAME} || 'localhost') . ":" . ( $ENV{SERVER_PORT}||80) . "/" ); |
| 48 |
} |
| 49 |
|
| 50 |
=head2 setup_environment_from_metadata |
| 51 |
|
| 52 |
This method sets up CGI environment variables based on various |
| 53 |
meta-headers, like the protocol, remote host name, request path, etc. |
| 54 |
|
| 55 |
See the docs in L<HTTP::Server::Simple> for more detail. |
| 56 |
|
| 57 |
=cut |
| 58 |
|
| 59 |
%ENV_MAPPING = ( |
| 60 |
protocol => "SERVER_PROTOCOL", |
| 61 |
localport => "SERVER_PORT", |
| 62 |
localname => "SERVER_NAME", |
| 63 |
path => "PATH_INFO", |
| 64 |
request_uri => "REQUEST_URI", |
| 65 |
method => "REQUEST_METHOD", |
| 66 |
peeraddr => "REMOTE_ADDR", |
| 67 |
peername => "REMOTE_HOST", |
| 68 |
peerport => "REMOTE_PORT", |
| 69 |
query_string => "QUERY_STRING", |
| 70 |
); |
| 71 |
|
| 72 |
sub setup_environment_from_metadata { |
| 73 |
no warnings 'uninitialized'; |
| 74 |
my $self = shift; |
| 75 |
|
| 76 |
# XXX TODO: rather than clone functionality from the base class, |
| 77 |
# we should call super |
| 78 |
# |
| 79 |
while ( my ( $item, $value ) = splice @_, 0, 2 ) { |
| 80 |
if ( my $k = $ENV_MAPPING{$item} ) { |
| 81 |
$ENV{$k} = $value; |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
# Apache and lighttpd both do one layer of unescaping on |
| 86 |
# path_info; we should duplicate that. |
| 87 |
$ENV{PATH_INFO} =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg; |
| 88 |
} |
| 89 |
|
| 90 |
=head2 header |
| 91 |
|
| 92 |
C<header> turns a single HTTP headers into CGI environment variables. |
| 93 |
|
| 94 |
=cut |
| 95 |
|
| 96 |
sub header { |
| 97 |
my $self = shift; |
| 98 |
my $tag = shift; |
| 99 |
my $value = shift; |
| 100 |
|
| 101 |
$tag = uc($tag); |
| 102 |
$tag =~ s/^COOKIES$/COOKIE/; |
| 103 |
$tag =~ s/-/_/g; |
| 104 |
$tag = "HTTP_" . $tag |
| 105 |
unless $tag =~ m/^CONTENT_(?:LENGTH|TYPE)$/; |
| 106 |
|
| 107 |
if ( exists $ENV{$tag} ) { |
| 108 |
$ENV{$tag} .= $tag eq 'HTTP_COOKIE' ? "; $value" : ", $value"; |
| 109 |
} |
| 110 |
else { |
| 111 |
$ENV{$tag} = $value; |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
1; |