File Coverage

blib/lib/HTTP/Server/Simple/CGI/Environment.pm
Criterion Covered Total %
statement 15 33 45.4
branch 0 8 0.0
condition 0 7 0.0
subroutine 5 9 55.5
pod 4 4 100.0
total 24 61 39.3


line stmt bran cond sub pod time code
1              
2             package HTTP::Server::Simple::CGI::Environment;
3              
4 3     3   15 use strict;
  3         3  
  3         73  
5 3     3   9 use warnings;
  3         3  
  3         65  
6 3     3   9 use HTTP::Server::Simple;
  3         7  
  3         55  
7              
8 3     3   9 use vars qw(%ENV_MAPPING);
  3         3  
  3         432  
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 so that it's easier to provide your own
20             CGI handlers with L which B use CGI.pm
21              
22             =head2 setup_environment
23              
24             C 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 0     0 1   %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 environment variable
42              
43             =cut
44              
45             sub setup_server_url {
46             $ENV{SERVER_URL}
47 0   0 0 1   ||= ( "http://" . ($ENV{SERVER_NAME} || 'localhost') . ":" . ( $ENV{SERVER_PORT}||80) . "/" );
      0        
      0        
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 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 3     3   13 no warnings 'uninitialized';
  3         2  
  3         724  
74 0     0 1   my $self = shift;
75              
76             # XXX TODO: rather than clone functionality from the base class,
77             # we should call super
78             #
79 0           while ( my ( $item, $value ) = splice @_, 0, 2 ) {
80 0 0         if ( my $k = $ENV_MAPPING{$item} ) {
81 0           $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 0           $ENV{PATH_INFO} =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
  0            
88             }
89              
90             =head2 header
91              
92             C
turns a single HTTP headers into CGI environment variables.
93              
94             =cut
95              
96             sub header {
97 0     0 1   my $self = shift;
98 0           my $tag = shift;
99 0           my $value = shift;
100              
101 0           $tag = uc($tag);
102 0           $tag =~ s/^COOKIES$/COOKIE/;
103 0           $tag =~ s/-/_/g;
104 0 0         $tag = "HTTP_" . $tag
105             unless $tag =~ m/^CONTENT_(?:LENGTH|TYPE)$/;
106              
107 0 0         if ( exists $ENV{$tag} ) {
108 0 0         $ENV{$tag} .= $tag eq 'HTTP_COOKIE' ? "; $value" : ", $value";
109             }
110             else {
111 0           $ENV{$tag} = $value;
112             }
113             }
114              
115             1;