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