rename C::E::HTTP.pm to C::E::LWP.pm
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Engine / LWP / Daemon.pm
diff --git a/lib/Catalyst/Engine/LWP/Daemon.pm b/lib/Catalyst/Engine/LWP/Daemon.pm
new file mode 100644 (file)
index 0000000..84b9c27
--- /dev/null
@@ -0,0 +1,107 @@
+package Catalyst::Engine::LWP::Daemon;
+
+use strict;
+use base 'Catalyst::Engine::LWP';
+
+use IO::Socket qw(AF_INET);
+
+=head1 NAME
+
+Catalyst::Engine::LWP::Daemon - Catalyst LWP Daemon Engine
+
+=head1 SYNOPSIS
+
+A script using the Catalyst::Engine::LWP::Daemon module might look like:
+
+    #!/usr/bin/perl -w
+
+    BEGIN { 
+       $ENV{CATALYST_ENGINE} = 'LWP::Daemon';
+    }
+
+    use strict;
+    use lib '/path/to/MyApp/lib';
+    use MyApp;
+
+    MyApp->run;
+
+=head1 DESCRIPTION
+
+This is the Catalyst engine specialized for development and testing.
+
+=head1 OVERLOADED METHODS
+
+This class overloads some methods from C<Catalyst::Engine::LWP>.
+
+=over 4
+
+=item $c->run
+
+=cut
+
+$SIG{'PIPE'} = 'IGNORE';
+
+sub run {
+    my $class = shift;
+    my $port  = shift || 3000;
+
+    my $daemon = Catalyst::Engine::LWP::Daemon::Catalyst->new(
+        LocalPort => $port,
+        ReuseAddr => 1
+    );
+
+    unless ($daemon) {
+        die("Failed to create daemon: $!\n");
+    }
+
+    printf( "You can connect to your server at %s\n", $daemon->url );
+
+    while ( my $connection = $daemon->accept ) {
+
+        while ( my $request = $connection->get_request ) {
+
+            $request->uri->scheme('http');    # Force URI::http
+
+            my $lwp = Catalyst::Engine::LWP::HTTP->new(
+                request  => $request,
+                address  => $connection->peerhost,
+                hostname => gethostbyaddr( $connection->peeraddr, AF_INET )
+            );
+
+            $class->handler($lwp);
+            $connection->send_response( $lwp->response );
+        }
+
+        $connection->close;
+        undef($connection);
+    }
+}
+
+=back
+
+=head1 SEE ALSO
+
+L<Catalyst>, L<HTTP::Daemon>.
+
+=head1 AUTHOR
+
+Sebastian Riedel, C<sri@cpan.org>
+Christian Hansen, C<ch@ngmedia.com>
+
+=head1 COPYRIGHT
+
+This program is free software, you can redistribute it and/or modify it under
+the same terms as Perl itself.
+
+=cut
+
+package Catalyst::Engine::LWP::Daemon::Catalyst;
+
+use strict;
+use base 'HTTP::Daemon';
+
+sub product_tokens {
+    "Catalyst/$Catalyst::VERSION";
+}
+
+1;