X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Runtime.git;a=blobdiff_plain;f=lib%2FCatalyst%2FEngine%2FCGI.pm;h=ca86d79c9147ed21cf7a3fbaa9eff584095c35f1;hp=56d988f34960a47a306dffd736a971bcd135cec8;hb=0bf7ab7160f4f2fd0f00cd3d53ac311e9ad50241;hpb=dfc21fff16f38b0409cf154de7f557f599726e2f diff --git a/lib/Catalyst/Engine/CGI.pm b/lib/Catalyst/Engine/CGI.pm index 56d988f..ca86d79 100644 --- a/lib/Catalyst/Engine/CGI.pm +++ b/lib/Catalyst/Engine/CGI.pm @@ -3,7 +3,6 @@ package Catalyst::Engine::CGI; use strict; use base 'Catalyst::Engine'; use NEXT; -use URI; __PACKAGE__->mk_accessors('env'); @@ -43,8 +42,8 @@ sub finalize_headers { $c->response->header( Status => $c->response->status ); - print $c->response->headers->as_string("\015\012"); - print "\015\012"; + $self->{_header_buf} + = $c->response->headers->as_string("\015\012") . "\015\012"; } =head2 $self->prepare_connection($c) @@ -94,10 +93,10 @@ sub prepare_headers { local (*ENV) = $self->env || \%ENV; # Read headers from %ENV - while ( my ( $header, $value ) = each %ENV ) { + foreach my $header ( keys %ENV ) { next unless $header =~ /^(?:HTTP|CONTENT|COOKIE)/i; ( my $field = $header ) =~ s/^HTTPS?_//; - $c->req->headers->header( $field => $value ); + $c->req->headers->header( $field => $ENV{$header} ); } } @@ -110,9 +109,16 @@ sub prepare_path { local (*ENV) = $self->env || \%ENV; my $scheme = $c->request->secure ? 'https' : 'http'; - my $host = $ENV{HTTP_HOST} || $ENV{SERVER_NAME}; - my $port = $ENV{SERVER_PORT} || 80; - my $base_path = $ENV{REDIRECT_URL} || $ENV{SCRIPT_NAME} || '/'; + my $host = $ENV{HTTP_HOST} || $ENV{SERVER_NAME}; + my $port = $ENV{SERVER_PORT} || 80; + my $base_path; + if ( exists $ENV{REDIRECT_URL} ) { + $base_path = $ENV{REDIRECT_URL}; + $base_path =~ s/$ENV{PATH_INFO}$//; + } + else { + $base_path = $ENV{SCRIPT_NAME} || '/'; + } # If we are running as a backend proxy, get the true hostname PROXY_CHECK: @@ -130,26 +136,36 @@ sub prepare_path { $port = $c->request->secure ? 443 : 80; } - my $path = $base_path . $ENV{PATH_INFO}; + # set the request URI + my $path = $base_path . ( $ENV{PATH_INFO} || '' ); $path =~ s{^/+}{}; + + # Using URI directly is way too slow, so we construct the URLs manually + my $uri_class = "URI::$scheme"; + + # HTTP_HOST will include the port even if it's 80/443 + $host =~ s/:(?:80|443)$//; + + if ( $port !~ /^(?:80|443)$/ && $host !~ /:/ ) { + $host .= ":$port"; + } + + # Escape the path + $path =~ s/([^$URI::uric])/$URI::Escape::escapes{$1}/go; + $path =~ s/\?/%3F/g; # STUPID STUPID SPECIAL CASE + + my $query = $ENV{QUERY_STRING} ? '?' . $ENV{QUERY_STRING} : ''; + my $uri = $scheme . '://' . $host . '/' . $path . $query; - my $uri = URI->new; - $uri->scheme($scheme); - $uri->host($host); - $uri->port($port); - $uri->path($path); - $uri->query( $ENV{QUERY_STRING} ) if $ENV{QUERY_STRING}; - - # sanitize the URI - $uri = $uri->canonical; - $c->request->uri($uri); + $c->request->uri( bless \$uri, $uri_class ); # set the base URI # base must end in a slash - $base_path .= '/' unless ( $base_path =~ /\/$/ ); - my $base = $uri->clone; - $base->path_query($base_path); - $c->request->base($base); + $base_path .= '/' unless $base_path =~ m{/$}; + + my $base_uri = $scheme . '://' . $host . $base_path; + + $c->request->base( bless \$base_uri, $uri_class ); } =head2 $self->prepare_query_parameters($c) @@ -192,6 +208,23 @@ sub prepare_write { $self->NEXT::prepare_write($c); } +=head2 $self->write($c, $buffer) + +Writes the buffer to the client. + +=cut + +sub write { + my ( $self, $c, $buffer ) = @_; + + # Prepend the headers if they have not yet been sent + if ( my $headers = delete $self->{_header_buf} ) { + $buffer = $headers . $buffer; + } + + return $self->NEXT::write( $c, $buffer ); +} + =head2 $self->read_chunk($c, $buffer, $length) =cut @@ -206,15 +239,11 @@ sub run { shift; shift->handle_request(@_) } =head1 SEE ALSO -L L. +L, L =head1 AUTHORS -Sebastian Riedel, - -Christian Hansen, - -Andy Grundman, +Catalyst Contributors, see Catalyst.pm =head1 COPYRIGHT