| 1 | =pod | ||
| 2 | |||
| 3 | =begin classdoc | ||
| 4 | |||
| 5 | Convert an <cpan>HTTP::Request</cpan> object into a CGI protocol | ||
| 6 | environment, and process the response emitted by the CGI handler. | ||
| 7 | Uses <cpan>IO::Scalar</cpan> to redirect STDIN and STDOUT to scalar | ||
| 8 | buffers, so that the CGI handler input and output are buffered until the | ||
| 9 | handler exits, at which point, the accumulated output buffer is | ||
| 10 | turned into an <cpan>HTTP::Response</cpan> object and then sent | ||
| 11 | back to the client. | ||
| 12 | <p> | ||
| 13 | Derived from <cpan>HTTP::Request::AsCGI</cpan>, by Christian Hansen, C<ch@ngmedia.com> | ||
| 14 | <p> | ||
| 15 | <b>WARNING:</b> <cpan>IO::Scalar</cpan> relies on filehandle ties, which are still | ||
| 16 | considered experimental in some releases of Perl 5.8. However, the functionality | ||
| 17 | used within this package is limited to simple input or output, and thus far | ||
| 18 | appears to function well. | ||
| 19 | <p> | ||
| 20 | Developers should be judicious in their use of the CGI interface for | ||
| 21 | HTTP::Daemon::Threaded: if the request is to return a very large (i.e., | ||
| 22 | multi-megabyte) response, the underlying I/O buffering may consume | ||
| 23 | significant memory resources. Likewise, this package does not support | ||
| 24 | some methods of "Comet"-style streaming client-server interaction, as the | ||
| 25 | response buffer will not be dispatched to the client until the CGI | ||
| 26 | invokation has completed. | ||
| 27 | <p> | ||
| 28 | Copyright© 2008, Dean Arnold, Presicient Corp., USA<br> | ||
| 29 | All rights reserved. | ||
| 30 | <p> | ||
| 31 | Licensed under the Academic Free License version 3.0, as specified in the | ||
| 32 | at <a href='http://www.opensource.org/licenses/afl-3.0.php'>OpenSource.org</a>. | ||
| 33 | |||
| 34 | @author D. Arnold | ||
| 35 | @since 2008-Mar-14 | ||
| 36 | @see <cpan>HTTP::Request::AsCGI</cpan> | ||
| 37 | |||
| 38 | =end classdoc | ||
| 39 | |||
| 40 | =cut | ||
| 41 | |||
| 42 | package HTTP::Daemon::Threaded::CGIAdapter; | ||
| 43 | |||
| 44 | use strict; | ||
| 45 | use warnings; | ||
| 46 | use bytes; | ||
| 47 | |||
| 48 | use Carp; | ||
| 49 | use Socket; | ||
| 50 | use IO::Handle; | ||
| 51 | use IO::Scalar; | ||
| 52 | use HTTP::Response; | ||
| 53 | |||
| 54 | our $VERSION = '0.91'; | ||
| 55 | |||
| 56 | sub new { | ||
| 57 | my ($class, $request, $fd, $content_type) = @_; | ||
| 58 | |||
| 59 | my $self = bless { | ||
| 60 | restored => 0, | ||
| 61 | setuped => 0, | ||
| 62 | request => $request | ||
| 63 | }, $class; | ||
| 64 | |||
| 65 | my $host = $request->header('Host'); | ||
| 66 | my $uri = $request->uri->clone; | ||
| 67 | $uri->scheme('http') unless $uri->scheme; | ||
| 68 | $uri->host('localhost') unless $uri->host; | ||
| 69 | $uri->port(80) unless $uri->port; | ||
| 70 | $uri->host_port($host) unless !$host || ( $host eq $uri->host_port ); | ||
| 71 | |||
| 72 | $uri = $uri->canonical; | ||
| 73 | |||
| 74 | my $sockaddr = getpeername($fd); | ||
| 75 | my ($port, $addr) = sockaddr_in($sockaddr); | ||
| 76 | |||
| 77 | my %environment = ( | ||
| 78 | GATEWAY_INTERFACE => 'CGI/1.1', | ||
| 79 | HTTP_HOST => $uri->host_port, | ||
| 80 | HTTPS => ( $uri->scheme eq 'https' ) ? 'ON' : 'OFF', | ||
| 81 | PATH_INFO => $uri->path, | ||
| 82 | QUERY_STRING => $uri->query || '', | ||
| 83 | SCRIPT_NAME => '/', | ||
| 84 | SERVER_NAME => $uri->host, | ||
| 85 | SERVER_PORT => $uri->port, | ||
| 86 | SERVER_PROTOCOL => $request->protocol || 'HTTP/1.1', | ||
| 87 | SERVER_SOFTWARE => "HTTP-Daemon-Threaded/$VERSION", | ||
| 88 | REMOTE_ADDR => inet_ntoa($addr), | ||
| 89 | REMOTE_HOST => '', | ||
| 90 | REMOTE_PORT => $port, | ||
| 91 | REQUEST_URI => $uri->path_query, | ||
| 92 | REQUEST_METHOD => $request->method, | ||
| 93 | ); | ||
| 94 | |||
| 95 | foreach my $field ( $request->headers->header_field_names ) { | ||
| 96 | |||
| 97 | my $key = uc("HTTP_$field"); | ||
| 98 | $key =~ tr/-/_/; | ||
| 99 | $key =~ s/^HTTP_// if $field =~ /^Content-(Length|Type)$/; | ||
| 100 | |||
| 101 | $environment{$key} ||= $request->headers->header($field); | ||
| 102 | } | ||
| 103 | |||
| 104 | unless ( $environment{SCRIPT_NAME} eq '/' && $environment{PATH_INFO} ) { | ||
| 105 | $environment{PATH_INFO} =~ s/^\Q$environment{SCRIPT_NAME}\E/\//; | ||
| 106 | $environment{PATH_INFO} =~ s/^\/+/\//; | ||
| 107 | } | ||
| 108 | |||
| 109 | $environment{CONTENT_TYPE} ||= $content_type; | ||
| 110 | $self->{environment} = \%environment; | ||
| 111 | # | ||
| 112 | # remap stdin to a buffer with the request data in it | ||
| 113 | # | ||
| 114 | my $stdin_buffer = ''; | ||
| 115 | if ( $self->{request}->content_length ) { | ||
| 116 | $stdin_buffer = $self->{request}->content | ||
| 117 | or croak("Can't write request content to stdin handle: $!"); | ||
| 118 | } | ||
| 119 | |||
| 120 | $self->{stdin_buffer} = \$stdin_buffer; | ||
| 121 | # | ||
| 122 | # we should really do this just once, and reuse the values | ||
| 123 | # it could even be a package variable | ||
| 124 | # | ||
| 125 | open( $self->{restore}{stdin}, '<&=', \*STDIN ) | ||
| 126 | or croak("Can't redirect stdin: $!"); | ||
| 127 | |||
| 128 | tie *STDIN, 'IO::Scalar', \$stdin_buffer; | ||
| 129 | # | ||
| 130 | # remap stdout to a buffer to hold the response | ||
| 131 | # | ||
| 132 | my $stdout_buffer = ''; | ||
| 133 | $self->{stdout_buffer} = \$stdout_buffer; | ||
| 134 | open( $self->{restore}{stdout}, '>&=', \*STDOUT) | ||
| 135 | or croak("Can't dup stdout: $!"); | ||
| 136 | |||
| 137 | tie *STDOUT, 'IO::Scalar', \$stdout_buffer; | ||
| 138 | # | ||
| 139 | # remap the environment | ||
| 140 | # | ||
| 141 | my ($k, $v); | ||
| 142 | $ENV{$k} = $v while (($k, $v) = each %environment); | ||
| 143 | # | ||
| 144 | # is this trip really neccesary ? | ||
| 145 | # | ||
| 146 | CGI::initialize_globals() | ||
| 147 | if $INC{'CGI.pm'}; | ||
| 148 | |||
| 149 | $self->{setuped}++; | ||
| 150 | |||
| 151 | return $self; | ||
| 152 | } | ||
| 153 | |||
| 154 | sub response { | ||
| 155 | my $self = shift; | ||
| 156 | |||
| 157 | my $buf = $self->{stdout_buffer}; | ||
| 158 | return undef unless $buf; | ||
| 159 | |||
| 160 | my ($headers) = ($$buf=~/^(.*?\x0d?\x0a\x0d?\x0a)/s); | ||
| 161 | my $readlen = length($headers); | ||
| 162 | |||
| 163 | $headers = "HTTP/1.1 500 Internal Server Error\x0d\x0a" | ||
| 164 | unless $headers; | ||
| 165 | |||
| 166 | $headers = "HTTP/1.1 200 OK\x0d\x0a$headers" | ||
| 167 | unless ( $headers =~ /^HTTP/ ); | ||
| 168 | |||
| 169 | my $response = HTTP::Response->parse($headers); | ||
| 170 | $response->date( time() ) unless $response->date; | ||
| 171 | |||
| 172 | my $message = $response->message; | ||
| 173 | my $status = $response->header('Status'); | ||
| 174 | |||
| 175 | $response->message($1) | ||
| 176 | if ( $message && $message =~ /^(.+)\x0d$/); | ||
| 177 | |||
| 178 | if ( $status && $status =~ /^(\d\d\d)\s?(.+)?$/ ) { | ||
| 179 | |||
| 180 | my $code = $1; | ||
| 181 | my $message = $2 || HTTP::Status::status_message($code); | ||
| 182 | |||
| 183 | $response->code($code); | ||
| 184 | $response->message($message); | ||
| 185 | } | ||
| 186 | |||
| 187 | my $length = length($$buf) - $readlen; | ||
| 188 | |||
| 189 | $response->content( $response->error_as_HTML ), | ||
| 190 | $response->content_type('text/html'), | ||
| 191 | return $response | ||
| 192 | if ( $response->code == 500 && !$length ); | ||
| 193 | |||
| 194 | $response->add_content(substr($$buf, $readlen)); | ||
| 195 | $response->content_length($length) | ||
| 196 | if ( $length && !$response->content_length ); | ||
| 197 | # | ||
| 198 | # Now we can discard the buffer | ||
| 199 | # | ||
| 200 | delete $self->{stdout_buffer}; | ||
| 201 | return $response; | ||
| 202 | } | ||
| 203 | |||
| 204 | sub restore { | ||
| 205 | my $self = shift; | ||
| 206 | # | ||
| 207 | # NOTE: we're going to let non-CGI changes to %ENV persist | ||
| 208 | # between invokations...not sure its the right thing to do... | ||
| 209 | # | ||
| 210 | delete $ENV{$_} foreach keys (%{$self->{environment}}); | ||
| 211 | |||
| 212 | untie *STDIN; | ||
| 213 | |||
| 214 | open(STDIN, "<&=", $self->{restore}{stdin}) | ||
| 215 | or croak("Can't restore stdin: $!"); | ||
| 216 | |||
| 217 | delete $self->{stdin_buffer}; | ||
| 218 | delete $self->{restore}{stdin}; | ||
| 219 | |||
| 220 | if ( $self->{restore}{stdout} ) { | ||
| 221 | # | ||
| 222 | # !!!NOTE : we can't discard the buffer yet...response() needs it! | ||
| 223 | # | ||
| 224 | untie *STDOUT; | ||
| 225 | open(STDOUT, "<&=", $self->{restore}{stdout}) | ||
| 226 | or croak("Can't restore stdout: $!"); | ||
| 227 | delete $self->{restore}{stdout}; | ||
| 228 | } | ||
| 229 | $self->{restored}++; | ||
| 230 | |||
| 231 | return $self; | ||
| 232 | } | ||
| 233 | |||
| 234 | sub DESTROY { | ||
| 235 | my $self = shift; | ||
| 236 | $self->restore | ||
| 237 | if $self->{setuped} && !$self->{restored}; | ||
| 238 | } | ||
| 239 | |||
| 240 | 1; |