docs: changed unrelated but confusingly similar exx. in Intro.pod
[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
296e7663 6use IO::Socket qw(AF_INET INADDR_ANY SOCK_STREAM SOMAXCONN );
2cdfbf5e 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;
296e7663 45
46 $HTTP::Daemon::PROTO = 'HTTP/1.0'; # For now until we resolve the blocking
47 # issues with HTTP 1.1
2cdfbf5e 48
296e7663 49 my $daemon = Catalyst::Engine::HTTP::Daemon::Catalyst->new(
2cdfbf5e 50 Listen => SOMAXCONN,
51 LocalPort => $port,
52 ReuseAddr => 1,
53 Type => SOCK_STREAM,
54 );
55
56 unless ($daemon) {
57 die("Failed to create daemon: $!\n");
58 }
59
60 my $base = URI->new( $daemon->url )->canonical;
61
62 printf( "You can connect to your server at %s\n", $base );
63
64 while ( my $connection = $daemon->accept ) {
65
66 $connection->timeout(5);
67
68 while ( my $request = $connection->get_request ) {
69
70 $request->uri->scheme('http'); # Force URI::http
71 $request->uri->host( $request->header('Host') || $base->host );
72 $request->uri->port( $base->port );
73
74 my $hostname = gethostbyaddr( $connection->peeraddr, AF_INET );
75
d837e1a7 76 my $http = Catalyst::Engine::HTTP::Base::struct->new(
2cdfbf5e 77 address => $connection->peerhost,
78 hostname => $hostname || $connection->peerhost,
79 request => $request,
80 response => HTTP::Response->new
81 );
82
83 $class->handler($http);
84 $connection->send_response( $http->response );
2cdfbf5e 85 }
86
87 $connection->close;
88 undef($connection);
89 }
90}
91
92=back
93
94=head1 SEE ALSO
95
96L<Catalyst>, L<Catalyst::Engine>, L<Catalyst::Engine::HTTP::Base>,
97L<HTTP::Daemon>.
98
99=head1 AUTHOR
100
101Sebastian Riedel, C<sri@cpan.org>
102Christian Hansen, C<ch@ngmedia.com>
103
104=head1 COPYRIGHT
105
106This program is free software, you can redistribute it and/or modify it under
107the same terms as Perl itself.
108
109=cut
110
296e7663 111package Catalyst::Engine::HTTP::Daemon::Catalyst;
2cdfbf5e 112
113use strict;
114use base 'HTTP::Daemon';
115
116sub product_tokens {
117 "Catalyst/$Catalyst::VERSION";
118}
119
1201;