fixed forwarding to class method pairs and did some cleaning
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Engine / HTTP / Daemon.pm
1 package Catalyst::Engine::HTTP::Daemon;
2
3 use strict;
4 use base 'Catalyst::Engine::HTTP';
5
6 use IO::Socket qw(AF_INET);
7
8 =head1 NAME
9
10 Catalyst::Engine::HTTP::Daemon - Catalyst HTTP Daemon Engine
11
12 =head1 SYNOPSIS
13
14 A script using the Catalyst::Engine::HTTP::Daemon module might look like:
15
16     #!/usr/bin/perl -w
17
18     BEGIN { 
19        $ENV{CATALYST_ENGINE} = 'HTTP::Daemon';
20     }
21
22     use strict;
23     use lib '/path/to/MyApp/lib';
24     use MyApp;
25
26     MyApp->run;
27
28 =head1 DESCRIPTION
29
30 This is the Catalyst engine specialized for development and testing.
31
32 =head1 OVERLOADED METHODS
33
34 This class overloads some methods from C<Catalyst::Engine::HTTP>.
35
36 =over 4
37
38 =item $c->run
39
40 =cut
41
42 $SIG{'PIPE'} = 'IGNORE';
43
44 sub run {
45     my $class = shift;
46     my $port  = shift || 3000;
47
48     my $daemon = Catalyst::Engine::HTTP::Daemon::Catalyst->new(
49         LocalPort => $port,
50         ReuseAddr => 1
51     );
52
53     unless ($daemon) {
54         die("Failed to create daemon: $!\n");
55     }
56
57     printf( "You can connect to your server at %s\n", $daemon->url );
58
59     while ( my $connection = $daemon->accept ) {
60
61         while ( my $request = $connection->get_request ) {
62
63             $request->uri->scheme('http');    # Force URI::http
64
65             my $http = Catalyst::Engine::HTTP::LWP->new(
66                 request  => $request,
67                 address  => $connection->peerhost,
68                 hostname => gethostbyaddr( $connection->peeraddr, AF_INET )
69             );
70
71             $class->handler($http);
72             $connection->send_response( $http->response );
73         }
74
75         $connection->close;
76         undef($connection);
77     }
78 }
79
80 =back
81
82 =head1 SEE ALSO
83
84 L<Catalyst>, L<HTTP::Daemon>.
85
86 =head1 AUTHOR
87
88 Sebastian Riedel, C<sri@cpan.org>
89 Christian Hansen, C<ch@ngmedia.com>
90
91 =head1 COPYRIGHT
92
93 This program is free software, you can redistribute it and/or modify it under
94 the same terms as Perl itself.
95
96 =cut
97
98 package Catalyst::Engine::HTTP::Daemon::Catalyst;
99
100 use strict;
101 use base 'HTTP::Daemon';
102
103 sub product_tokens {
104     "Catalyst/$Catalyst::VERSION";
105 }
106
107 1;