| 1 | package HTTP::Engine; |
|---|
| 2 | use Moose; |
|---|
| 3 | use HTTP::Engine::Types::Core qw( Interface ); |
|---|
| 4 | our $VERSION = '0.0.10'; |
|---|
| 5 | use HTTP::Engine::Context; |
|---|
| 6 | use HTTP::Engine::Request; |
|---|
| 7 | use HTTP::Engine::Request::Upload; |
|---|
| 8 | use HTTP::Engine::Response; |
|---|
| 9 | use HTTP::Engine::RequestProcessor; |
|---|
| 10 | |
|---|
| 11 | has 'interface' => ( |
|---|
| 12 | is => 'ro', |
|---|
| 13 | does => Interface, |
|---|
| 14 | coerce => 1, |
|---|
| 15 | handles => [ qw(run load_plugins) ], |
|---|
| 16 | ); |
|---|
| 17 | |
|---|
| 18 | sub import { |
|---|
| 19 | my($class, %args) = @_; |
|---|
| 20 | return unless $args{middlewares} && ref $args{middlewares} eq 'ARRAY'; |
|---|
| 21 | $class->load_middlewares(@{ $args{middlewares} }); |
|---|
| 22 | } |
|---|
| 23 | |
|---|
| 24 | sub load_middlewares { |
|---|
| 25 | my ($class, @middlewares) = @_; |
|---|
| 26 | for my $middleware (@middlewares) { |
|---|
| 27 | $class->load_middleware( $middleware ); |
|---|
| 28 | } |
|---|
| 29 | } |
|---|
| 30 | |
|---|
| 31 | sub load_middleware { |
|---|
| 32 | my ($class, $middleware) = @_; |
|---|
| 33 | |
|---|
| 34 | my $pkg; |
|---|
| 35 | if (($pkg = $middleware) =~ s/^(\+)//) { |
|---|
| 36 | Class::MOP::load_class($pkg) or die $@; |
|---|
| 37 | } else { |
|---|
| 38 | $pkg = 'HTTP::Engine::Middleware::' . $middleware; |
|---|
| 39 | unless (eval { Class::MOP::load_class($pkg) }) { |
|---|
| 40 | $pkg = 'HTTPEx::Middleware::' . $middleware; |
|---|
| 41 | Class::MOP::load_class($pkg); |
|---|
| 42 | } |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | if ($pkg->meta->has_method('setup')) { |
|---|
| 46 | $pkg->setup(); |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | if ($pkg->meta->has_method('wrap')) { |
|---|
| 50 | HTTP::Engine::RequestProcessor->meta->add_around_method_modifier( |
|---|
| 51 | call_handler => $pkg->meta->get_method('wrap')->body |
|---|
| 52 | ); |
|---|
| 53 | } |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | 1; |
|---|
| 57 | __END__ |
|---|
| 58 | |
|---|
| 59 | =for stopwords middlewares Middleware middleware nothingmuch |
|---|
| 60 | |
|---|
| 61 | =encoding utf8 |
|---|
| 62 | |
|---|
| 63 | =head1 NAME |
|---|
| 64 | |
|---|
| 65 | HTTP::Engine - Web Server Gateway Interface and HTTP Server Engine Drivers (Yet Another Catalyst::Engine) |
|---|
| 66 | |
|---|
| 67 | =head1 SYNOPSIS |
|---|
| 68 | |
|---|
| 69 | use HTTP::Engine; |
|---|
| 70 | my $engine = HTTP::Engine->new( |
|---|
| 71 | interface => { |
|---|
| 72 | module => 'ServerSimple', |
|---|
| 73 | args => { |
|---|
| 74 | host => 'localhost', |
|---|
| 75 | port => 1978, |
|---|
| 76 | }, |
|---|
| 77 | request_handler => 'main::handle_request',# or CODE ref |
|---|
| 78 | }, |
|---|
| 79 | ); |
|---|
| 80 | $engine->run; |
|---|
| 81 | |
|---|
| 82 | use Data::Dumper; |
|---|
| 83 | sub handle_request { |
|---|
| 84 | my $c = shift; |
|---|
| 85 | $c->res->body( Dumper($c->req) ); |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | |
|---|
| 89 | =head1 CONCEPT RELEASE |
|---|
| 90 | |
|---|
| 91 | Version 0.0.x is a concept release, the internal interface is still fluid. |
|---|
| 92 | It is mostly based on the code of Catalyst::Engine. |
|---|
| 93 | |
|---|
| 94 | =head1 DESCRIPTION |
|---|
| 95 | |
|---|
| 96 | HTTP::Engine is a bare-bones, extensible HTTP engine. It is not a |
|---|
| 97 | socket binding server. |
|---|
| 98 | |
|---|
| 99 | The purpose of this module is to be an adaptor between various HTTP-based |
|---|
| 100 | logic layers and the actual implementation of an HTTP server, such as, |
|---|
| 101 | mod_perl and FastCGI. |
|---|
| 102 | |
|---|
| 103 | Internally, the only thing HTTP::Engine will do is to prepare a |
|---|
| 104 | HTTP::Engine::Request object for you to handle, and pass to your handler's |
|---|
| 105 | C<TBD> method. In turn your C<TBD> method should return a fully prepared |
|---|
| 106 | HTTP::Engine::Response object. |
|---|
| 107 | |
|---|
| 108 | HTTP::Engine will handle absorbing the differences between the environment, |
|---|
| 109 | the I/O, etc. Your application can focus on creating response objects |
|---|
| 110 | (which is pretty much what your typical webapp is doing) |
|---|
| 111 | |
|---|
| 112 | =head1 INTERFACES |
|---|
| 113 | |
|---|
| 114 | Interfaces are the actual environment-dependent components which handles |
|---|
| 115 | the actual interaction between your clients and the application. |
|---|
| 116 | |
|---|
| 117 | For example, in CGI mode, you can write to STDOUT and expect your clients to |
|---|
| 118 | see it, but in mod_perl, you may need to use $r-E<gt>print instead. |
|---|
| 119 | |
|---|
| 120 | Interfaces are the actual layers that does the interaction. HTTP::Engine |
|---|
| 121 | currently supports the following: |
|---|
| 122 | |
|---|
| 123 | # XXX TODO: Update the list |
|---|
| 124 | |
|---|
| 125 | =over 4 |
|---|
| 126 | |
|---|
| 127 | =item HTTP::Engine::Interface::ServerSimple |
|---|
| 128 | |
|---|
| 129 | =item HTTP::Engine::Interface::FastCGI |
|---|
| 130 | |
|---|
| 131 | =item HTTP::Engine::Interface::CGI |
|---|
| 132 | |
|---|
| 133 | =item HTTP::Engine::Interface::Test |
|---|
| 134 | |
|---|
| 135 | for test code interface |
|---|
| 136 | |
|---|
| 137 | =item HTTP::Engine::Interface::ModPerl |
|---|
| 138 | |
|---|
| 139 | experimental |
|---|
| 140 | |
|---|
| 141 | =item HTTP::Engine::Interface::Standalone |
|---|
| 142 | |
|---|
| 143 | old style |
|---|
| 144 | |
|---|
| 145 | =back |
|---|
| 146 | |
|---|
| 147 | Interfaces can be specified as part of the HTTP::Engine constructor: |
|---|
| 148 | |
|---|
| 149 | my $interface = HTTP::Engine::Interface::FastCGI->new( |
|---|
| 150 | handler => ... |
|---|
| 151 | ); |
|---|
| 152 | HTTP::Engine->new( |
|---|
| 153 | interface => $interface |
|---|
| 154 | )->run(); |
|---|
| 155 | |
|---|
| 156 | Or you can let HTTP::Engine instantiate the interface for you: |
|---|
| 157 | |
|---|
| 158 | HTTP::Engine->new( |
|---|
| 159 | interface => { |
|---|
| 160 | module => 'FastCGI', |
|---|
| 161 | args => { |
|---|
| 162 | handler => ... |
|---|
| 163 | } |
|---|
| 164 | } |
|---|
| 165 | )->run(); |
|---|
| 166 | |
|---|
| 167 | =head1 MIDDLEWARES |
|---|
| 168 | |
|---|
| 169 | For all non-core middlewares (consult #codrepos first), use the HTTPEx:: |
|---|
| 170 | namespace. For example, if you have a plugin module named "HTTPEx::Middleware::Foo", |
|---|
| 171 | you could load it as |
|---|
| 172 | |
|---|
| 173 | use HTTP::Engine middlewares => [ qw( +HTTPEx::Plugin::Foo ) ]; |
|---|
| 174 | |
|---|
| 175 | =head1 METHODS |
|---|
| 176 | |
|---|
| 177 | =over 4 |
|---|
| 178 | |
|---|
| 179 | =item load_middleware(middleware) |
|---|
| 180 | |
|---|
| 181 | =item load_middlewares(qw/ middleware middleware /) |
|---|
| 182 | |
|---|
| 183 | Loads the given middleware into the HTTP::Engine. |
|---|
| 184 | |
|---|
| 185 | =back |
|---|
| 186 | |
|---|
| 187 | =head1 AUTHOR |
|---|
| 188 | |
|---|
| 189 | Kazuhiro Osawa E<lt>ko@yappo.ne.jpE<gt> |
|---|
| 190 | |
|---|
| 191 | Daisuke Maki |
|---|
| 192 | |
|---|
| 193 | tokuhirom |
|---|
| 194 | |
|---|
| 195 | nyarla |
|---|
| 196 | |
|---|
| 197 | marcus |
|---|
| 198 | |
|---|
| 199 | hidek |
|---|
| 200 | |
|---|
| 201 | dann |
|---|
| 202 | |
|---|
| 203 | typester (Interface::FCGI) |
|---|
| 204 | |
|---|
| 205 | lopnor |
|---|
| 206 | |
|---|
| 207 | nothingmuch |
|---|
| 208 | |
|---|
| 209 | =head1 SEE ALSO |
|---|
| 210 | |
|---|
| 211 | wiki page L<http://coderepos.org/share/wiki/HTTP%3A%3AEngine> |
|---|
| 212 | |
|---|
| 213 | L<Moose> |
|---|
| 214 | |
|---|
| 215 | =head1 REPOSITORY |
|---|
| 216 | |
|---|
| 217 | svn co http://svn.coderepos.org/share/lang/perl/HTTP-Engine/trunk HTTP-Engine |
|---|
| 218 | |
|---|
| 219 | HTTP::Engine's Subversion repository is hosted at L<http://coderepos.org/share/>. |
|---|
| 220 | patches and collaborators are welcome. |
|---|
| 221 | |
|---|
| 222 | =head1 LICENSE |
|---|
| 223 | |
|---|
| 224 | This library is free software; you can redistribute it and/or modify |
|---|
| 225 | it under the same terms as Perl itself. |
|---|
| 226 | |
|---|
| 227 | =cut |
|---|