line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Dancer2::Core::Dispatcher; |
2
|
|
|
|
|
|
|
# ABSTRACT: Class for dispatching request to the appropriate route handler |
3
|
|
|
|
|
|
|
$Dancer2::Core::Dispatcher::VERSION = '1.0.0'; |
4
|
143
|
|
|
143
|
|
1113
|
use Moo; |
|
143
|
|
|
|
|
495
|
|
|
143
|
|
|
|
|
1078
|
|
5
|
|
|
|
|
|
|
|
6
|
143
|
|
|
143
|
|
50640
|
use Dancer2::Core::Types; |
|
143
|
|
|
|
|
519
|
|
|
143
|
|
|
|
|
1122
|
|
7
|
143
|
|
|
143
|
|
1844691
|
use Dancer2::Core::Request; |
|
143
|
|
|
|
|
466
|
|
|
143
|
|
|
|
|
4444
|
|
8
|
143
|
|
|
143
|
|
70640
|
use Dancer2::Core::Response; |
|
143
|
|
|
|
|
658
|
|
|
143
|
|
|
|
|
52964
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
has apps => ( |
11
|
|
|
|
|
|
|
is => 'rw', |
12
|
|
|
|
|
|
|
isa => ArrayRef, |
13
|
|
|
|
|
|
|
default => sub { [] }, |
14
|
|
|
|
|
|
|
); |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
has apps_psgi => ( |
17
|
|
|
|
|
|
|
is => 'ro', |
18
|
|
|
|
|
|
|
isa => ArrayRef, |
19
|
|
|
|
|
|
|
lazy => 1, |
20
|
|
|
|
|
|
|
builder => '_build_apps_psgi', |
21
|
|
|
|
|
|
|
); |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
sub _build_apps_psgi { |
24
|
34
|
|
|
34
|
|
535
|
my $self = shift; |
25
|
34
|
|
|
|
|
90
|
return [ map +( $_->name, $_->to_app ), @{ $self->apps } ]; |
|
34
|
|
|
|
|
639
|
|
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
sub dispatch { |
29
|
129
|
|
|
129
|
1
|
30025
|
my ( $self, $env ) = @_; |
30
|
129
|
|
|
|
|
220
|
my @apps = @{ $self->apps_psgi }; |
|
129
|
|
|
|
|
3018
|
|
31
|
|
|
|
|
|
|
|
32
|
129
|
|
|
|
|
1538
|
DISPATCH: while (1) { |
33
|
146
|
|
|
|
|
563
|
for ( my $i = 0; $i < @apps; $i += 2 ) { |
34
|
188
|
|
|
|
|
673
|
my ( $app_name, $app ) = @apps[ $i, $i + 1 ]; |
35
|
|
|
|
|
|
|
|
36
|
188
|
|
|
|
|
542
|
my $response = $app->($env); |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
# check for an internal request |
39
|
188
|
100
|
|
|
|
8500
|
delete Dancer2->runner->{'internal_forward'} |
40
|
|
|
|
|
|
|
and next DISPATCH; |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
# the app raised a flag saying it couldn't match anything |
43
|
|
|
|
|
|
|
# which is different than "I matched and it's a 404" |
44
|
|
|
|
|
|
|
delete Dancer2->runner->{'internal_404'} |
45
|
171
|
100
|
|
|
|
435
|
or do { |
46
|
120
|
|
|
|
|
314
|
delete Dancer2->runner->{'internal_request'}; |
47
|
120
|
|
|
|
|
521
|
return $response; |
48
|
|
|
|
|
|
|
}; |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
# don't run anymore |
52
|
9
|
|
|
|
|
50
|
delete Dancer2->runner->{'internal_request'}; |
53
|
9
|
|
|
|
|
115
|
last; |
54
|
|
|
|
|
|
|
} # while |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
# a 404 on all apps, using the first app |
57
|
9
|
|
|
|
|
211
|
my $default_app = $self->apps->[0]; |
58
|
9
|
|
|
|
|
139
|
my $request = $default_app->build_request($env); |
59
|
9
|
|
|
|
|
74
|
return $default_app->response_not_found($request)->to_psgi; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
1; |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
__END__ |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
=pod |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
=encoding UTF-8 |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
=head1 NAME |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
Dancer2::Core::Dispatcher - Class for dispatching request to the appropriate route handler |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=head1 VERSION |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
version 1.0.0 |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
=head1 SYNOPSIS |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
use Dancer2::Core::Dispatcher; |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
# Create an instance of dispatcher |
83
|
|
|
|
|
|
|
my $dispatcher = Dancer2::Core::Dispatcher->new( apps => [$app] ); |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
# Dispatch a request |
86
|
|
|
|
|
|
|
my $resp = $dispatcher->dispatch($env)->to_psgi; |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
# Capture internal error of a response (if any) after a dispatch |
89
|
|
|
|
|
|
|
$dispatcher->response_internal_error($app, $error); |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
# Capture response not found for an application the after dispatch |
92
|
|
|
|
|
|
|
$dispatcher->response_not_found($env); |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
=head1 ATTRIBUTES |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
=head2 apps |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
The apps is an array reference to L<Dancer2::Core::App>. |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=head2 default_content_type |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
The default_content_type is a string which represents the context of the |
103
|
|
|
|
|
|
|
request. This attribute is read-only. |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
=head1 METHODS |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
=head2 dispatch |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
The C<dispatch> method accepts the list of applications, hash reference for |
110
|
|
|
|
|
|
|
the B<env> attribute of L<Dancer2::Core::Request> and optionally the request |
111
|
|
|
|
|
|
|
object and an env as input arguments. |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
C<dispatch> returns a response object of L<Dancer2::Core::Response>. |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
Any before hook and matched route code is wrapped to allow DSL keywords such |
116
|
|
|
|
|
|
|
as forward and redirect to short-circuit remaining code, returning across |
117
|
|
|
|
|
|
|
multiple stack frames without having to throw an exception. |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
=head2 response_internal_error |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
The C<response_internal_error> takes as input the list of applications and |
122
|
|
|
|
|
|
|
a variable error and returns an object of L<Dancer2::Core::Error>. |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=head2 response_not_found |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
The C<response_not_found> consumes as input the list of applications and an |
127
|
|
|
|
|
|
|
object of type L<Dancer2::Core::App> and returns an object |
128
|
|
|
|
|
|
|
L<Dancer2::Core::Error>. |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
=head1 AUTHOR |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
Dancer Core Developers |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
This software is copyright (c) 2023 by Alexis Sukrieh. |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
139
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
=cut |