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")
75 if do { no strict 'refs'; defined *{"${targ}::BUILD"}{CODE} };
76 my @targ_isa = do { no strict 'refs'; @{"${targ}::ISA"} };
77 die "${targ} uses Multiple Inheritance: ISA is: ".join ', ', @targ_isa
81 $self->$_($data) for reverse @targ;
85 sub _setup_default_config {
89 if (${"${class}::_default_config"}{CODE}) {
90 $class->_cannot_call_twice('_setup_default_config', 'default_config');
93 my @defaults = (@_, $class->_default_config);
96 *{"${class}::_default_config"} = sub { @defaults };
100 sub _default_config { () }
106 sub _construct_response_filter {
108 $_[0]->_build_dispatcher({
110 my ($d, $self, $env) = (shift, shift, shift);
111 my @next = $d->next->dispatch($env, $self, @_);
113 $self->_run_with_self($code, @next);
118 sub _construct_redispatch {
119 my ($self, $new_path) = @_;
120 $self->_build_dispatcher({
123 my ($self, $env) = @_;
124 $self->_dispatch({ %{$env}, PATH_INFO => $new_path })
129 sub _build_dispatch_parser {
130 require Web::Simple::DispatchParser;
131 return Web::Simple::DispatchParser->new;
134 sub _cannot_call_twice {
135 my ($class, $method, $sub) = @_;
136 my $error = "Cannot call ${method} twice for ${class}";
138 $error .= " - did you call Web::Simple's ${sub} export twice?";
143 sub _setup_dispatcher {
144 my ($class, $dispatch_specs) = @_;
147 if (${"${class}::_dispatcher"}{CODE}) {
148 $class->_cannot_call_twice('_setup_dispatcher', 'dispatch');
151 my $chain = $class->_build_dispatch_chain(
152 [ @$dispatch_specs, $class->_build_final_dispatcher ]
156 *{"${class}::_dispatcher"} = sub { $chain };
160 sub _construct_subdispatch {
161 my ($class, $dispatch_spec) = @_;
162 my $disp = $class->_build_dispatcher_from_spec($dispatch_spec);
163 my $call = $disp->call;
165 my @res = $call->(@_);
167 my $chain = $class->_build_dispatch_chain(@res);
168 return $class->_build_dispatcher({
170 my ($d, $self, $env) = (shift, shift, shift); pop; # lose trailing $env
171 return $chain->dispatch($env, $self, @_);
175 return $class->_build_dispatcher({
177 my ($d, $self, $env) = (shift, shift, shift); pop; # lose trailing $env
178 my @sub = $disp->dispatch($env, $self, @_);
180 return unless (my $next = $d->next);
181 return $next->dispatch($env, $self, @_);
186 sub _build_dispatcher_from_spec {
187 my ($class, $spec) = @_;
188 return $spec unless ref($spec) eq 'CODE';
189 my $proto = prototype $spec;
190 my $parser = $class->_build_dispatch_parser;
192 defined($proto) && length($proto)
193 ? $parser->parse_dispatch_specification($proto)
194 : sub { ({}, $_[1]) }
196 return $class->_build_dispatcher({
199 shift->_run_with_self($spec, @_)
204 sub _build_dispatch_chain {
205 my ($class, $dispatch_specs) = @_;
207 foreach my $dispatch_spec (@$dispatch_specs) {
208 my $new = $class->_build_dispatcher_from_spec($dispatch_spec);
210 $last = $last ? $last->next($new) : $new;
215 sub _build_dispatcher {
216 bless($_[1], 'Web::Simple::Dispatcher');
219 sub _build_final_dispatcher {
220 shift->_build_dispatcher({
223 404, [ 'Content-type', 'text/plain' ],
231 my ($self, $env) = @_;
232 $self->_dispatcher->dispatch($env, $self);
236 my ($self, $run, @args) = @_;
237 my $class = ref($self);
239 local *{"${class}::self"} = \$self;
244 # ->as_psgi_app is true for require() but also works for plackup
245 return $_[0]->as_psgi_app if caller(1);
246 my $self = ref($_[0]) ? $_[0] : $_[0]->new;
252 require Plack::Server::CGI;
253 Plack::Server::CGI->run($self->as_psgi_app);
258 require Plack::Server::FCGI;
259 Plack::Server::FCGI->run($self->as_psgi_app);
263 my $self = ref($_[0]) ? $_[0] : $_[0]->new;
264 sub { $self->_dispatch(@_) };
269 if ($ENV{PHP_FCGI_CHILDREN} || $ENV{FCGI_ROLE} || $ENV{FCGI_SOCKET_PATH}) {
270 return $self->_run_fcgi;
271 } elsif ($ENV{GATEWAY_INTERFACE}) {
272 return $self->_run_cgi;
274 unless (@ARGV && $ARGV[0] =~ m{^/}) {
275 return $self->_run_cli;
278 my $path = shift @ARGV;
280 require HTTP::Request::Common;
282 local *GET = \&HTTP::Request::Common::GET;
284 my $request = GET($path);
286 Plack::Test::test_psgi($self->as_psgi_app, sub { $response = shift->($request) });
287 print $response->as_string;
292 die $self->_cli_usage;
296 "To run this script in CGI test mode, pass a URL path beginning with /:\n".