fix a memory leak introduced by 1f8cad5e5a1875de94d63ac91d8ded4d2282c62e
[catagits/Web-Simple.git] / lib / Web / Dispatch.pm
CommitLineData
4ed4fb42 1package Web::Dispatch;
2
3use Sub::Quote;
4use Scalar::Util qw(blessed);
1f4dd6f9 5
6sub MAGIC_MIDDLEWARE_KEY { __PACKAGE__.'.middleware' }
7
4ed4fb42 8use Moo;
9use Web::Dispatch::Parser;
10use Web::Dispatch::Node;
11
12with 'Web::Dispatch::ToApp';
13
e5250d96 14has dispatch_app => (
15 is => 'lazy', builder => sub { shift->dispatch_object->to_app }
16);
1f727762 17has dispatch_object => (is => 'ro', required => 0, weak_ref => 1);
4ed4fb42 18has parser_class => (
19 is => 'ro', default => quote_sub q{ 'Web::Dispatch::Parser' }
20);
21has node_class => (
22 is => 'ro', default => quote_sub q{ 'Web::Dispatch::Node' }
23);
4ed4fb42 24has _parser => (is => 'lazy');
25
e5250d96 26after BUILDARGS => sub {
27 my ( $self, %args ) = @_;
28 die "Either dispatch_app or dispatch_object need to be supplied."
29 if !$args{dispatch_app} and !$args{dispatch_object}
30};
31
4ed4fb42 32sub _build__parser {
33 my ($self) = @_;
34 $self->parser_class->new;
35}
36
37sub call {
38 my ($self, $env) = @_;
e5250d96 39 my $res = $self->_dispatch($env, $self->dispatch_app);
1a0ea82a 40 return $res->[0] if ref($res) eq 'ARRAY' and @{$res} == 1 and ref($res->[0]) eq 'CODE';
41 return $res;
4ed4fb42 42}
43
44sub _dispatch {
45 my ($self, $env, @match) = @_;
59ccc1e8 46 while (defined(my $try = shift @match)) {
75ad66d6 47
48 return $try if ref($try) eq 'ARRAY';
4ed4fb42 49 if (ref($try) eq 'HASH') {
d96756e8 50 $env = { 'Web::Dispatch.original_env' => $env, %$env, %$try };
4ed4fb42 51 next;
4ed4fb42 52 }
75ad66d6 53
69aaa28a 54 my @result = $self->_to_try($try, \@match)->($env, @match);
4ed4fb42 55 next unless @result and defined($result[0]);
75ad66d6 56
57 my $first = $result[0];
e4c7f3b4 58
6cf1d73a 59 if (my $res = $self->_have_result($first, \@result, \@match, $env)) {
e4c7f3b4 60
61 return $res;
62 }
75ad66d6 63
64 # make a copy so we don't screw with it assigning further up
65 my $env = $env;
66 unshift @match, sub { $self->_dispatch($env, @result) };
4ed4fb42 67 }
75ad66d6 68
4ed4fb42 69 return;
70}
71
e4c7f3b4 72sub _have_result {
6cf1d73a 73 my ($self, $first, $result, $match, $env) = @_;
6bd99619 74
6cf1d73a 75 if (ref($first) eq 'ARRAY') {
1a0ea82a 76 return $first;
6bd99619 77 }
6cf1d73a 78 elsif (blessed($first) && $first->isa('Plack::Middleware')) {
79 return $self->_uplevel_middleware($first, $result);
6bd99619 80 }
6cf1d73a 81 elsif (ref($first) eq 'HASH' and $first->{+MAGIC_MIDDLEWARE_KEY}) {
82 return $self->_redispatch_with_middleware($first, $match, $env);
6bd99619 83 }
481da1e2 84 elsif (
85 blessed($first) &&
86 not($first->can('to_app')) &&
87 not($first->isa('Web::Dispatch::Matcher'))
88 ) {
6bd99619 89 return $first;
90 }
6bd99619 91 return;
92}
93
e4c7f3b4 94sub _uplevel_middleware {
6cf1d73a 95 my ($self, $match, $results) = @_;
75ad66d6 96 die "Multiple results but first one is a middleware ($match)"
97 if @{$results} > 1;
98 # middleware needs to uplevel exactly once to wrap the rest of the
99 # level it was created for - next elsif unwraps it
100 return { MAGIC_MIDDLEWARE_KEY, $match };
101}
102
e4c7f3b4 103sub _redispatch_with_middleware {
6cf1d73a 104 my ($self, $first, $match, $env) = @_;
75ad66d6 105
106 my $mw = $first->{+MAGIC_MIDDLEWARE_KEY};
107
108 $mw->app(sub { $self->_dispatch($_[0], @{$match}) });
109
110 return $mw->to_app->($env);
111}
112
4ed4fb42 113sub _to_try {
69aaa28a 114 my ($self, $try, $more) = @_;
c398ce1a 115
116 # sub (<spec>) {} becomes a dispatcher
117 # sub {} is a PSGI app and can be returned as is
118 # '<spec>' => sub {} becomes a dispatcher
456dc2bb 119 # $obj isa WD:Predicates::Matcher => sub { ... } - become a dispatcher
c398ce1a 120 # $obj w/to_app method is a Plack::App-like thing - call it to get a PSGI app
481da1e2 121 #
c398ce1a 122
4ed4fb42 123 if (ref($try) eq 'CODE') {
124 if (defined(my $proto = prototype($try))) {
79dda56f 125 $self->_construct_node(match => $proto, run => $try);
4ed4fb42 126 } else {
127 $try
128 }
b2e37c62 129 } elsif (!ref($try)
130 and (ref($more->[0]) eq 'CODE'
131 or (!ref($more->[0]) and $self->dispatch_object
132 and $self->dispatch_object->can($more->[0])))
133 ) {
79dda56f 134 $self->_construct_node(match => $try, run => shift(@$more));
481da1e2 135 } elsif (
136 (blessed($try) && $try->isa('Web::Dispatch::Matcher'))
137 and (ref($more->[0]) eq 'CODE')
138 ) {
79dda56f 139 $self->_construct_node(match => $try, run => shift(@$more));
4ed4fb42 140 } elsif (blessed($try) && $try->can('to_app')) {
141 $try->to_app;
142 } else {
143 die "No idea how we got here with $try";
144 }
145}
146
147sub _construct_node {
148 my ($self, %args) = @_;
79dda56f 149 $args{match} = $self->_parser->parse($args{match}) if !ref $args{match};
1f8cad5e 150 if ( my $obj = $self->dispatch_object) {
151 # if possible, call dispatchers as methods of the app object
152 my $dispatch_sub = $args{run};
153 $args{run} = sub { $obj->$dispatch_sub(@_) };
154 }
155 $self->node_class->new(\%args)->to_app;
4ed4fb42 156}
157
1581;