1 package Web::Simple::Application;
4 use warnings FATAL => 'all';
7 package Web::Simple::Dispatcher;
11 and "$_[1]" =~ /\w+=[A-Z]/
12 and $_[1]->isa(__PACKAGE__);
17 ? $_[0]->{next} = $_[1]
22 $_[0]->{next} = $_[1];
27 my ($self, $env, @args) = @_;
28 my $next = $self->_has_match ? $self->next : undef;
29 if (my ($env_delta, @match) = $self->_match_against($env)) {
30 if (my ($result) = $self->_execute_with(@args, @match, $env)) {
31 if ($self->_is_dispatcher($result)) {
32 $next = $result->set_next($next);
33 $env = { %$env, %$env_delta };
39 return () unless $next;
40 return $next->dispatch($env, @args);
45 ? $_[0]->{call} = $_[1]
49 sub _has_match { $_[0]->{match} }
52 return ({}, $_[1]) unless $_[0]->{match};
53 $_[0]->{match}->($_[1]);
62 my ($class, $data) = @_;
63 my $config = { $class->_default_config, %{($data||{})->{config}||{}} };
64 my $new = bless({ config => $config }, $class);
65 $new->BUILDALL($data);
70 my ($self, $data) = @_;
71 my $targ = ref($self);
73 while ($targ->isa(__PACKAGE__) and $targ ne __PACKAGE__) {
74 push(@targ, "${targ}::BUILD")
76 no strict 'refs'; no warnings 'once';
77 defined *{"${targ}::BUILD"}{CODE}
79 my @targ_isa = do { no strict 'refs'; @{"${targ}::ISA"} };
80 die "${targ} uses Multiple Inheritance: ISA is: ".join ', ', @targ_isa
84 $self->$_($data) for reverse @targ;
88 sub _setup_default_config {
92 if (${"${class}::_default_config"}{CODE}) {
93 $class->_cannot_call_twice('_setup_default_config', 'default_config');
96 my @defaults = (@_, $class->_default_config);
99 *{"${class}::_default_config"} = sub { @defaults };
103 sub _default_config { () }
109 sub _construct_response_filter {
111 $_[0]->_build_dispatcher({
113 my ($d, $self, $env) = (shift, shift, shift);
114 my @next = $d->next->dispatch($env, $self, @_);
116 $self->_run_with_self($code, @next);
121 sub _construct_redispatch {
122 my ($self, $new_path) = @_;
123 $self->_build_dispatcher({
126 my ($self, $env) = @_;
127 $self->_dispatch({ %{$env}, PATH_INFO => $new_path })
132 sub _build_dispatch_parser {
133 require Web::Simple::DispatchParser;
134 return Web::Simple::DispatchParser->new;
137 sub _cannot_call_twice {
138 my ($class, $method, $sub) = @_;
139 my $error = "Cannot call ${method} twice for ${class}";
141 $error .= " - did you call Web::Simple's ${sub} export twice?";
146 sub _setup_dispatcher {
147 my ($class, $dispatch_specs) = @_;
150 if (${"${class}::_dispatcher"}{CODE}) {
151 $class->_cannot_call_twice('_setup_dispatcher', 'dispatch');
154 my $chain = $class->_build_dispatch_chain(
155 [ @$dispatch_specs, $class->_build_final_dispatcher ]
159 *{"${class}::_dispatcher"} = sub { $chain };
163 sub _construct_subdispatch {
164 my ($class, $dispatch_spec) = @_;
165 my $disp = $class->_build_dispatcher_from_spec($dispatch_spec);
166 my $call = $disp->call;
168 my @res = $call->(@_);
170 my $chain = $class->_build_dispatch_chain(@res);
171 return $class->_build_dispatcher({
173 my ($d, $self, $env) = (shift, shift, shift); pop; # lose trailing $env
174 return $chain->dispatch($env, $self, @_);
178 return $class->_build_dispatcher({
180 my ($d, $self, $env) = (shift, shift, shift); pop; # lose trailing $env
181 my @sub = $disp->dispatch($env, $self, @_);
183 return unless (my $next = $d->next);
184 return $next->dispatch($env, $self, @_);
189 sub _build_dispatcher_from_spec {
190 my ($class, $spec) = @_;
191 return $spec unless ref($spec) eq 'CODE';
192 my $proto = prototype $spec;
193 my $parser = $class->_build_dispatch_parser;
195 defined($proto) && length($proto)
196 ? $parser->parse_dispatch_specification($proto)
197 : sub { ({}, $_[1]) }
199 return $class->_build_dispatcher({
202 shift->_run_with_self($spec, @_)
207 sub _build_dispatch_chain {
208 my ($class, $dispatch_specs) = @_;
210 foreach my $dispatch_spec (@$dispatch_specs) {
211 my $new = $class->_build_dispatcher_from_spec($dispatch_spec);
213 $last = $last ? $last->next($new) : $new;
218 sub _build_dispatcher {
219 bless($_[1], 'Web::Simple::Dispatcher');
222 sub _build_final_dispatcher {
223 shift->_build_dispatcher({
226 404, [ 'Content-type', 'text/plain' ],
234 my ($self, $env) = @_;
235 $self->_dispatcher->dispatch($env, $self);
239 my ($self, $run, @args) = @_;
240 my $class = ref($self);
242 local *{"${class}::self"} = \$self;
247 # ->as_psgi_app is true for require() but also works for plackup
248 return $_[0]->as_psgi_app if caller(1);
249 my $self = ref($_[0]) ? $_[0] : $_[0]->new;
255 require Plack::Server::CGI;
256 Plack::Server::CGI->run($self->as_psgi_app);
261 require Plack::Server::FCGI;
262 Plack::Server::FCGI->run($self->as_psgi_app);
266 my $self = ref($_[0]) ? $_[0] : $_[0]->new;
267 sub { $self->_dispatch(@_) };
272 if ($ENV{PHP_FCGI_CHILDREN} || $ENV{FCGI_ROLE} || $ENV{FCGI_SOCKET_PATH}) {
273 return $self->_run_fcgi;
274 } elsif ($ENV{GATEWAY_INTERFACE}) {
275 return $self->_run_cgi;
277 unless (@ARGV && $ARGV[0] =~ m{^/}) {
278 return $self->_run_cli(@ARGV);
281 my $path = shift @ARGV;
283 require HTTP::Request::Common;
285 local *GET = \&HTTP::Request::Common::GET;
287 my $request = GET($path);
289 Plack::Test::test_psgi($self->as_psgi_app, sub { $response = shift->($request) });
290 print $response->as_string;
295 die $self->_cli_usage;
299 "To run this script in CGI test mode, pass a URL path beginning with /:\n".