Added SpeedyCGI, partly fixed HTTP::Daemon
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Engine / HTTP / Daemon.pm
CommitLineData
2cdfbf5e 1package Catalyst::Engine::HTTP::Daemon;
2
3use strict;
4use base 'Catalyst::Engine::HTTP::Base';
5
6use IO::Socket qw(AF_INET INADDR_ANY SOCK_STREAM SOMAXCONN);
7
8=head1 NAME
9
10Catalyst::Engine::HTTP::Daemon - Catalyst HTTP Daemon Engine
11
12=head1 SYNOPSIS
13
14A 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
28This is the Catalyst engine specialized for development and testing.
29
30=head1 OVERLOADED METHODS
31
32This class overloads some methods from C<Catalyst::Engine::HTTP::Base>.
33
34=over 4
35
36=item $c->run
37
38=cut
39
40$SIG{'PIPE'} = 'IGNORE';
41
42sub run {
43 my $class = shift;
44 my $port = shift || 3000;
45
e2fd5b5f 46 my $daemon = Catalyst::Engine::HTTP::Catalyst->new(
2cdfbf5e 47 Listen => SOMAXCONN,
48 LocalPort => $port,
49 ReuseAddr => 1,
50 Type => SOCK_STREAM,
51 );
52
53 unless ($daemon) {
54 die("Failed to create daemon: $!\n");
55 }
56
57 my $base = URI->new( $daemon->url )->canonical;
58
59 printf( "You can connect to your server at %s\n", $base );
60
61 while ( my $connection = $daemon->accept ) {
62
63 $connection->timeout(5);
64
65 while ( my $request = $connection->get_request ) {
66
67 $request->uri->scheme('http'); # Force URI::http
68 $request->uri->host( $request->header('Host') || $base->host );
69 $request->uri->port( $base->port );
70
71 my $hostname = gethostbyaddr( $connection->peeraddr, AF_INET );
72
d837e1a7 73 my $http = Catalyst::Engine::HTTP::Base::struct->new(
2cdfbf5e 74 address => $connection->peerhost,
75 hostname => $hostname || $connection->peerhost,
76 request => $request,
77 response => HTTP::Response->new
78 );
79
80 $class->handler($http);
81 $connection->send_response( $http->response );
82
83 }
84
85 $connection->close;
86 undef($connection);
87 }
88}
89
90=back
91
92=head1 SEE ALSO
93
94L<Catalyst>, L<Catalyst::Engine>, L<Catalyst::Engine::HTTP::Base>,
95L<HTTP::Daemon>.
96
97=head1 AUTHOR
98
99Sebastian Riedel, C<sri@cpan.org>
100Christian Hansen, C<ch@ngmedia.com>
101
102=head1 COPYRIGHT
103
104This program is free software, you can redistribute it and/or modify it under
105the same terms as Perl itself.
106
107=cut
108
109package Catalyst::Engine::HTTP::Catalyst;
110
111use strict;
112use base 'HTTP::Daemon';
113
114sub product_tokens {
115 "Catalyst/$Catalyst::VERSION";
116}
117
1181;