2 package HTTP::Server::Simple::CGI::Environment;
6 use HTTP::Server::Simple;
8 use vars qw($VERSION %ENV_MAPPING);
9 $VERSION = $HTTP::Server::Simple::VERSION;
15 HTTP::Server::Simple::CGI::Environment - a HTTP::Server::Simple mixin to provide the CGI protocol
19 This mixin abstracts the CGI protocol out from
20 L<HTTP::Server::Simple::CGI> so that it's easier to provide your own
21 CGI handlers with L<HTTP::Server::Simple> which B<don't> use CGI.pm
23 =head2 setup_environment
25 C<setup_environemnt> is usually called in the superclass's accept_hook
27 This routine in this sub-class clears the environment to the
32 sub setup_environment {
35 SERVER_SOFTWARE => "HTTP::Server::Simple/$VERSION",
36 GATEWAY_INTERFACE => 'CGI/1.1'
40 =head2 setup_server_url
42 Sets up the C<SERVER_URL> environment variable
46 sub setup_server_url {
48 ||= ( "http://" . ($ENV{SERVER_NAME} || 'localhost') . ":" . ( $ENV{SERVER_PORT}||80) . "/" );
51 =head2 setup_environment_from_metadata
53 This method sets up CGI environment variables based on various
54 meta-headers, like the protocol, remote host name, request path, etc.
56 See the docs in L<HTTP::Server::Simple> for more detail.
61 protocol => "SERVER_PROTOCOL",
62 localport => "SERVER_PORT",
63 localname => "SERVER_NAME",
65 request_uri => "REQUEST_URI",
66 method => "REQUEST_METHOD",
67 peeraddr => "REMOTE_ADDR",
68 peername => "REMOTE_HOST",
69 query_string => "QUERY_STRING",
72 sub setup_environment_from_metadata {
73 no warnings 'uninitialized';
76 # XXX TODO: rather than clone functionality from the base class,
77 # we should call super
79 while ( my ( $item, $value ) = splice @_, 0, 2 ) {
80 if ( my $k = $ENV_MAPPING{$item} ) {
85 # Apache and lighttpd both do one layer of unescaping on
86 # path_info; we should duplicate that.
87 $ENV{PATH_INFO} = URI::Escape::uri_unescape($ENV{PATH_INFO});
92 C<header> turns a single HTTP headers into CGI environment variables.
102 $tag =~ s/^COOKIES$/COOKIE/;
104 $tag = "HTTP_" . $tag
105 unless $tag =~ m/^(?:CONTENT_(?:LENGTH|TYPE)|COOKIE)$/;
107 if ( exists $ENV{$tag} ) {
108 $ENV{$tag} .= ", $value";