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->next;
29 if (my ($env_delta, @match) = $self->_match_against($env)) {
30 if (my ($result) = $self->_execute_with(@args, @match)) {
31 if ($self->_is_dispatcher($result)) {
32 $next = $result->set_next($next);
33 $env = { %$env, %$env_delta };
39 return $next->dispatch($env, @args);
43 return ({}, $_[1]) unless $_[0]->{match};
44 $_[0]->{match}->($_[1]);
53 my ($class, $data) = @_;
54 my $config = { $class->_default_config, %{($data||{})->{config}||{}} };
55 bless({ config => $config }, $class);
58 sub _setup_default_config {
62 if (${"${class}::_default_config"}{CODE}) {
63 $class->_cannot_call_twice('_setup_default_config', 'default_config');
66 my @defaults = (@_, $class->_default_config);
69 *{"${class}::_default_config"} = sub { @defaults };
73 sub _default_config { () }
79 sub _construct_response_filter {
81 $_[0]->_build_dispatcher({
83 my ($d, $self, $env) = (shift, shift, shift);
84 $self->_run_with_self($code, $d->next->dispatch($env, $self, @_));
89 sub _construct_redispatch {
90 my ($self, $new_path) = @_;
91 $self->_build_dispatcher({
94 my ($self, $env) = @_;
95 $self->_dispatch({ %{$env}, PATH_INFO => $new_path })
100 sub _build_dispatch_parser {
101 require Web::Simple::DispatchParser;
102 return Web::Simple::DispatchParser->new;
105 sub _cannot_call_twice {
106 my ($class, $method, $sub) = @_;
107 my $error = "Cannot call ${method} twice for ${class}";
109 $error .= " - did you call Web::Simple's ${sub} export twice?";
114 sub _setup_dispatcher {
115 my ($class, $dispatch_subs) = @_;
118 if (${"${class}::_dispatcher"}{CODE}) {
119 $class->_cannot_call_twice('_setup_dispatcher', 'dispatch');
122 my $parser = $class->_build_dispatch_parser;
124 foreach my $dispatch_sub (@$dispatch_subs) {
125 my $proto = prototype $dispatch_sub;
128 ? $parser->parse_dispatch_specification($proto)
131 my $new = $class->_build_dispatcher({
134 shift->_run_with_self($dispatch_sub, @_)
138 $last = $last ? $last->next($new) : $new;
140 $last->next($class->_build_final_dispatcher);
143 *{"${class}::_dispatcher"} = sub { $root };
147 sub _build_dispatcher {
148 bless($_[1], 'Web::Simple::Dispatcher');
151 sub _build_final_dispatcher {
152 shift->_build_dispatcher({
155 500, [ 'Content-type', 'text/plain' ],
156 [ 'The management apologises but we have no idea how to handle that' ]
163 my ($self, $env) = @_;
164 $self->_dispatcher->dispatch($env, $self);
168 my ($self, $run, @args) = @_;
169 my $class = ref($self);
171 local *{"${class}::self"} = \$self;
176 return 1 if caller(1); # 1 so we can be the last thing in the file
178 my $self = $class->new;
184 require Web::Simple::HackedPlack;
185 Plack::Server::CGI->run(sub { $self->_dispatch(@_) });
190 if ($ENV{GATEWAY_INTERFACE}) {
191 return $self->_run_cgi;
193 my $path = shift(@ARGV) or die "No path passed - use $0 / for root";
195 require HTTP::Request::AsCGI;
196 require HTTP::Request::Common;
197 local *GET = \&HTTP::Request::Common::GET;
199 my $request = GET($path);
200 my $c = HTTP::Request::AsCGI->new($request)->setup;
203 print $c->response->as_string;