minor cleaning in C::E::HTTP::Daemon
[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::Base';
5
6 use IO::Socket qw(AF_INET INADDR_ANY SOCK_STREAM SOMAXCONN );
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 {  $ENV{CATALYST_ENGINE} = 'HTTP::Daemon' }
19
20     use strict;
21     use lib '/path/to/MyApp/lib';
22     use MyApp;
23
24     MyApp->run;
25
26 =head1 DESCRIPTION
27
28 This is the Catalyst engine specialized for development and testing.
29
30 =head1 OVERLOADED METHODS
31
32 This class overloads some methods from C<Catalyst::Engine::HTTP::Base>.
33
34 =over 4
35
36 =item $c->handler
37
38 =cut
39
40 sub handler {
41     my ( $class, $client ) = @_;
42
43     $client->timeout(5);
44
45     while ( my $request = $client->get_request ) {
46
47         $request->uri->scheme('http');    # Force URI::http
48         $request->uri->host( $request->header('Host') || $client->sockhost );
49         $request->uri->port( $client->sockport );
50
51         my $hostname = gethostbyaddr( $client->peeraddr, AF_INET );
52
53         my $http = Catalyst::Engine::HTTP::Base::struct->new(
54             address  => $client->peerhost,
55             hostname => $hostname || $client->peerhost,
56             request  => $request,
57             response => HTTP::Response->new
58         );
59
60         $class->SUPER::handler($http);
61
62         $client->send_response( $http->response );
63     }
64
65     $client->close;
66 }
67
68 =item $c->run
69
70 =cut
71
72 sub run {
73     my $class = shift;
74     my $port  = shift || 3000;
75     
76     $SIG{'PIPE'} = 'IGNORE';
77     
78     $HTTP::Daemon::PROTO = 'HTTP/1.0'; # For now until we resolve the blocking 
79                                        # issues with HTTP 1.1
80
81     my $daemon = Catalyst::Engine::HTTP::Daemon::Catalyst->new(
82         Listen    => SOMAXCONN,
83         LocalPort => $port,
84         ReuseAddr => 1,
85         Type      => SOCK_STREAM,
86     );
87
88     my $base = URI->new( $daemon->url )->canonical;
89
90     printf( "You can connect to your server at %s\n", $base );
91
92     while ( my $client = $daemon->accept ) {
93         $class->handler($client);
94     }
95 }
96
97 =back
98
99 =head1 SEE ALSO
100
101 L<Catalyst>, L<Catalyst::Engine>, L<Catalyst::Engine::HTTP::Base>, 
102 L<HTTP::Daemon>.
103
104 =head1 AUTHOR
105
106 Sebastian Riedel, C<sri@cpan.org>
107 Christian Hansen, C<ch@ngmedia.com>
108
109 =head1 COPYRIGHT
110
111 This program is free software, you can redistribute it and/or modify it under
112 the same terms as Perl itself.
113
114 =cut
115
116 package Catalyst::Engine::HTTP::Daemon::Catalyst;
117
118 use strict;
119 use base 'HTTP::Daemon';
120
121 sub product_tokens {
122     "Catalyst/$Catalyst::VERSION";
123 }
124
125 1;