fixed some minor bugs
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Engine / HTTP.pm
index 6eb2970..17187d8 100644 (file)
@@ -1,25 +1,9 @@
 package Catalyst::Engine::HTTP;
 
 use strict;
-use base 'Catalyst::Engine';
-
-use CGI::Simple::Cookie;
-use Class::Struct ();
-use HTTP::Headers::Util 'split_header_words';
-use HTTP::Request;
-use HTTP::Response;
-use IO::File;
-use URI;
-
-__PACKAGE__->mk_accessors(qw/http/);
-
-Class::Struct::struct 'Catalyst::Engine::HTTP::LWP' => {
-    request  => 'HTTP::Request',
-    response => 'HTTP::Response',
-    hostname => '$',
-    address  => '$'
-};
+use base 'Catalyst::Engine::Test';
 
+use IO::Socket qw(AF_INET INADDR_ANY SOCK_STREAM SOMAXCONN);
 
 =head1 NAME
 
@@ -27,197 +11,85 @@ Catalyst::Engine::HTTP - Catalyst HTTP Engine
 
 =head1 SYNOPSIS
 
-L<Catalyst>.
-
-=head1 DESCRIPTION
+A script using the Catalyst::Engine::HTTP module might look like:
 
-This Catalyst engine is meant to be subclassed.
+    #!/usr/bin/perl -w
 
-=head1 OVERLOADED METHODS
-
-This class overloads some methods from C<Catalyst::Engine>.
-
-=over 4
+    BEGIN {  $ENV{CATALYST_ENGINE} = 'HTTP' }
 
-=item $c->finalize_headers
+    use strict;
+    use lib '/path/to/MyApp/lib';
+    use MyApp;
 
-=cut
-
-sub finalize_headers {
-    my $c = shift;
-
-    my $status   = $c->response->status || 200;
-    my $headers  = $c->response->headers;
-    my $response = HTTP::Response->new( $status, undef, $headers );
-
-    while ( my ( $name, $cookie ) = each %{ $c->response->cookies } ) {
-        my $cookie = CGI::Simple::Cookie->new(
-            -name    => $name,
-            -value   => $cookie->{value},
-            -expires => $cookie->{expires},
-            -domain  => $cookie->{domain},
-            -path    => $cookie->{path},
-            -secure  => $cookie->{secure} || 0
-        );
-
-        $response->header( 'Set-Cookie' => $cookie->as_string );
-    }
-
-    $c->http->response($response);
-}
+    MyApp->run;
 
-=item $c->finalize_output
-
-=cut
-
-sub finalize_output {
-    my $c = shift;
-    $c->http->response->content_ref( \$c->response->{output} );
-}
-
-=item $c->prepare_connection
-
-=cut
-
-sub prepare_connection {
-    my $c = shift;
-    $c->req->hostname( $c->http->hostname );
-    $c->req->address( $c->http->address );
-}
-
-=item $c->prepare_cookies
-
-=cut
-
-sub prepare_cookies {
-    my $c = shift;
+=head1 DESCRIPTION
 
-    if ( my $header = $c->http->request->header('Cookie') ) {
-        $c->req->cookies( { CGI::Simple::Cookie->parse($header) } );
-    }
-}
+This is the Catalyst engine specialized for development and testing.
 
-=item $c->prepare_headers
+=head1 OVERLOADED METHODS
 
-=cut
+This class overloads some methods from C<Catalyst::Engine::Test>.
 
-sub prepare_headers {
-    my $c = shift;
-    $c->req->method( $c->http->request->method );
-    $c->req->headers( $c->http->request->headers );
-}
+=over 4
 
-=item $c->prepare_parameters
+=item $c->run
 
 =cut
 
-sub prepare_parameters {
-    my $c = shift;
+$SIG{'PIPE'} = 'IGNORE';
 
-    my @params  = ();
-    my $request = $c->http->request;
+sub run {
+    my $class = shift;
+    my $port  = shift || 3000;
 
-    push( @params, $request->uri->query_form );
+    my $daemon = Catalyst::Engine::HTTP::Catalyst->new(
+        Listen    => SOMAXCONN,
+        LocalPort => $port,
+        ReuseAddr => 1,
+        Type      => SOCK_STREAM,
+    );
 
-    if ( $request->content_type eq 'application/x-www-form-urlencoded' ) {
-        my $uri = URI->new('http:');
-        $uri->query( $request->content );
-        push( @params, $uri->query_form );
+    unless ($daemon) {
+        die("Failed to create daemon: $!\n");
     }
 
-    if ( $request->content_type eq 'multipart/form-data' ) {
+    my $base = URI->new( $daemon->url )->canonical;
 
-        for my $part ( $request->parts ) {
+    printf( "You can connect to your server at %s\n", $base );
 
-            my $disposition = $part->header('Content-Disposition');
-            my %parameters  = @{ ( split_header_words($disposition) )[0] };
+    while ( my $connection = $daemon->accept ) {
 
-            if ( $parameters{filename} ) {
+        $connection->timeout(5);
 
-                my $fh = IO::File->new_tmpfile;
-                $fh->write( $part->content ) or die $!;
-                $fh->seek( SEEK_SET, 0 ) or die $!;
+        while ( my $request = $connection->get_request ) {
 
-                $c->req->uploads->{ $parameters{filename} } = {
-                    fh   => $fh,
-                    size => ( stat $fh )[7],
-                    type => $part->content_type
-                };
+            $request->uri->scheme('http');    # Force URI::http
+            $request->uri->host( $request->header('Host') || $base->host );
+            $request->uri->port( $base->port );
 
-                push( @params, $parameters{filename}, $fh );
-            }
-            else {
-                push( @params, $parameters{name}, $part->content );
-            }
-        }
-    }
-
-    my $parameters = $c->req->parameters;
+            my $lwp = Catalyst::Engine::Test::LWP->new(
+                address  => $connection->peerhost,
+                hostname => gethostbyaddr( $connection->peeraddr, AF_INET ),
+                request  => $request,
+                response => HTTP::Response->new
+            );
 
-    while ( my ( $name, $value ) = splice( @params, 0, 2 ) ) {
+            $class->handler($lwp);
+            $connection->send_response( $lwp->response );
 
-        if ( exists $parameters->{$name} ) {
-            for ( $parameters->{$name} ) {
-                $_ = [$_] unless ref($_) eq "ARRAY";
-                push( @$_, $value );
-            }
         }
-        else {
-            $parameters->{$name} = $value;
-        }
-    }
-}
-
-=item $c->prepare_path
-
-=cut
 
-sub prepare_path {
-    my $c = shift;
-
-    my $base;
-    {
-        my $scheme = $c->http->request->uri->scheme;
-        my $host   = $c->http->request->uri->host;
-        my $port   = $c->http->request->uri->port;
-
-        $base = URI->new;
-        $base->scheme($scheme);
-        $base->host($host);
-        $base->port($port);
-
-        $base = $base->canonical->as_string;
+        $connection->close;
+        undef($connection);
     }
-
-    my $path = $c->http->request->uri->path || '/';
-    $path =~ s/^\///;
-
-    $c->req->base($base);
-    $c->req->path($path);
-}
-
-=item $c->prepare_request($r)
-
-=cut
-
-sub prepare_request {
-    my ( $c, $http ) = @_;
-    $c->http($http);
-}
-
-=item $c->prepare_uploads
-
-=cut
-
-sub prepare_uploads {
-    my $c = shift;
 }
 
 =back
 
 =head1 SEE ALSO
 
-L<Catalyst>.
+L<Catalyst>, L<HTTP::Daemon>.
 
 =head1 AUTHOR
 
@@ -231,4 +103,13 @@ the same terms as Perl itself.
 
 =cut
 
+package Catalyst::Engine::HTTP::Catalyst;
+
+use strict;
+use base 'HTTP::Daemon';
+
+sub product_tokens {
+    "Catalyst/$Catalyst::VERSION";
+}
+
 1;