Commit | Line | Data |
ca61af20 |
1 | package Catalyst::Engine::HTTP; |
45374ac6 |
2 | |
3 | use strict; |
75fd617a |
4 | use base 'Catalyst::Engine::Test'; |
45374ac6 |
5 | |
4b19b4f3 |
6 | use IO::Socket qw(AF_INET INADDR_ANY SOCK_STREAM SOMAXCONN); |
45374ac6 |
7 | |
8 | =head1 NAME |
9 | |
ca61af20 |
10 | Catalyst::Engine::HTTP - Catalyst HTTP Engine |
45374ac6 |
11 | |
12 | =head1 SYNOPSIS |
13 | |
ca61af20 |
14 | A script using the Catalyst::Engine::HTTP module might look like: |
45374ac6 |
15 | |
16 | #!/usr/bin/perl -w |
17 | |
ca61af20 |
18 | BEGIN { $ENV{CATALYST_ENGINE} = 'HTTP' } |
45374ac6 |
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 | |
75fd617a |
32 | This class overloads some methods from C<Catalyst::Engine::Test>. |
45374ac6 |
33 | |
34 | =over 4 |
35 | |
36 | =item $c->run |
37 | |
38 | =cut |
39 | |
40 | $SIG{'PIPE'} = 'IGNORE'; |
41 | |
42 | sub run { |
43 | my $class = shift; |
44 | my $port = shift || 3000; |
45 | |
ca61af20 |
46 | my $daemon = Catalyst::Engine::HTTP::Catalyst->new( |
03978da9 |
47 | Listen => SOMAXCONN, |
45374ac6 |
48 | LocalPort => $port, |
03978da9 |
49 | ReuseAddr => 1, |
50 | Type => SOCK_STREAM, |
45374ac6 |
51 | ); |
52 | |
66d9e175 |
53 | unless ($daemon) { |
54 | die("Failed to create daemon: $!\n"); |
45374ac6 |
55 | } |
56 | |
b333ca6b |
57 | my $base = URI->new( $daemon->url )->canonical; |
58 | |
59 | printf( "You can connect to your server at %s\n", $base ); |
45374ac6 |
60 | |
72766821 |
61 | while ( my $connection = $daemon->accept ) { |
45374ac6 |
62 | |
1a80619d |
63 | $connection->timeout(5); |
64 | |
45374ac6 |
65 | while ( my $request = $connection->get_request ) { |
66 | |
67 | $request->uri->scheme('http'); # Force URI::http |
b26df351 |
68 | $request->uri->host( $request->header('Host') || $base->host ); |
523d44ec |
69 | $request->uri->port( $base->port ); |
45374ac6 |
70 | |
6dc87a0f |
71 | my $http = Catalyst::Engine::Test::HTTP->new( |
45374ac6 |
72 | address => $connection->peerhost, |
1a80619d |
73 | hostname => gethostbyaddr( $connection->peeraddr, AF_INET ), |
74 | request => $request, |
75 | response => HTTP::Response->new |
45374ac6 |
76 | ); |
77 | |
6dc87a0f |
78 | $class->handler($http); |
79 | $connection->send_response( $http->response ); |
1a80619d |
80 | |
45374ac6 |
81 | } |
82 | |
83 | $connection->close; |
84 | undef($connection); |
85 | } |
86 | } |
87 | |
88 | =back |
89 | |
90 | =head1 SEE ALSO |
91 | |
92 | L<Catalyst>, L<HTTP::Daemon>. |
93 | |
94 | =head1 AUTHOR |
95 | |
96 | Sebastian Riedel, C<sri@cpan.org> |
97 | Christian Hansen, C<ch@ngmedia.com> |
98 | |
99 | =head1 COPYRIGHT |
100 | |
101 | This program is free software, you can redistribute it and/or modify it under |
102 | the same terms as Perl itself. |
103 | |
104 | =cut |
105 | |
ca61af20 |
106 | package Catalyst::Engine::HTTP::Catalyst; |
45374ac6 |
107 | |
108 | use strict; |
109 | use base 'HTTP::Daemon'; |
110 | |
111 | sub product_tokens { |
112 | "Catalyst/$Catalyst::VERSION"; |
113 | } |
114 | |
115 | 1; |