line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Drogo::Server::Test; |
2
|
1
|
|
|
1
|
|
809
|
use URI::Escape; |
|
1
|
|
|
|
|
1731
|
|
|
1
|
|
|
|
|
69
|
|
3
|
1
|
|
|
1
|
|
7
|
use base 'Drogo::Server'; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
463
|
|
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
707
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
my %SERVER_VARIABLES; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
=head1 NAME |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
Drogo::Server::Test - Bare implementation of a server's methods, for testing. |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
=head1 METHODS |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
=head3 new |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
Create a new server instance. |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
=cut |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub new |
22
|
|
|
|
|
|
|
{ |
23
|
16
|
|
|
16
|
1
|
59
|
my ($class, %params) = @_; |
24
|
16
|
|
|
|
|
36
|
%SERVER_VARIABLES = ( ); |
25
|
16
|
|
|
|
|
66
|
my $self = { %params, output => '' }; |
26
|
|
|
|
|
|
|
|
27
|
16
|
|
|
|
|
24
|
bless($self); |
28
|
|
|
|
|
|
|
|
29
|
16
|
|
|
|
|
50
|
return $self; |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
=head3 variable(key => $value) |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
Returns a persistant server variable. |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
Key without value returns variable. |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
These include variables set by the server configuration, as "user variables" in nginx. |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
=cut |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
sub variable |
43
|
|
|
|
|
|
|
{ |
44
|
120
|
|
|
120
|
1
|
141
|
my ($self, $key, $value) = @_; |
45
|
|
|
|
|
|
|
|
46
|
120
|
100
|
|
|
|
155
|
if ($value) |
47
|
|
|
|
|
|
|
{ |
48
|
89
|
|
|
|
|
308
|
$SERVER_VARIABLES{$key} = $value; |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
else |
51
|
|
|
|
|
|
|
{ |
52
|
31
|
|
|
|
|
88
|
return $SERVER_VARIABLES{$key}; |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
=head3 uri |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
Returns the uri. |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
=cut |
61
|
|
|
|
|
|
|
|
62
|
0
|
|
|
0
|
1
|
0
|
sub uri { shift->{uri} } |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
=head3 args |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
Returns string of arguments. |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
=cut |
69
|
|
|
|
|
|
|
|
70
|
15
|
|
|
15
|
1
|
214
|
sub args { shift->{args} } |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=head3 request_body |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
Returns the request body (used for posts) |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
=cut |
77
|
|
|
|
|
|
|
|
78
|
15
|
|
|
15
|
1
|
79
|
sub request_body { '' } |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
=head3 input |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
Returns input stream. |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
=cut |
85
|
|
|
|
|
|
|
|
86
|
0
|
|
|
0
|
1
|
0
|
sub input { } |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
=head3 request_method |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
Returns the request method (GET or POST) |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
=cut |
93
|
|
|
|
|
|
|
|
94
|
0
|
0
|
|
0
|
1
|
0
|
sub request_method { shift->{request_method} || 'GET' } |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
=head3 remote_addr |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
Returns remote address. |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=cut |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
sub remote_addr |
103
|
|
|
|
|
|
|
{ |
104
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
105
|
|
|
|
|
|
|
|
106
|
0
|
|
0
|
|
|
0
|
return $self->{remote_addr} || '127.0.0.1'; |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
=head3 has_request_body |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
Used by nginx for request body processing. |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
This function is only called when the request method is a post, |
114
|
|
|
|
|
|
|
in an effort to reduce processing time. |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
=cut |
117
|
|
|
|
|
|
|
|
118
|
0
|
|
|
0
|
1
|
0
|
sub has_request_body { } |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
=head3 header_in |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
Returns a request header. |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=cut |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
sub header_in |
127
|
|
|
|
|
|
|
{ |
128
|
0
|
|
|
0
|
1
|
0
|
my ($self, $what) = @_; |
129
|
|
|
|
|
|
|
|
130
|
0
|
|
|
|
|
0
|
return $self->{headers_in}{$what}; |
131
|
|
|
|
|
|
|
} |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
=head3 header_out |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
Sets a header out. |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
=cut |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
sub header_out |
140
|
|
|
|
|
|
|
{ |
141
|
0
|
|
|
0
|
1
|
0
|
my ($self, $header, $value) = @_; |
142
|
|
|
|
|
|
|
|
143
|
0
|
|
|
|
|
0
|
return $self->{headers_out}{$header} = $value; |
144
|
|
|
|
|
|
|
} |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=head3 send_http_header |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
Send the http header. |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=cut |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
sub send_http_header |
153
|
|
|
|
|
|
|
{ |
154
|
15
|
|
|
15
|
1
|
20
|
my ($self, $header) = @_; |
155
|
|
|
|
|
|
|
|
156
|
15
|
|
|
|
|
34
|
$self->{http_header} = $header; |
157
|
|
|
|
|
|
|
} |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
=head3 $self->status(...) |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
Set output status... (200, 404, etc...) |
162
|
|
|
|
|
|
|
If no argument given, returns status. |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
=cut |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
sub status |
167
|
|
|
|
|
|
|
{ |
168
|
15
|
|
|
15
|
1
|
18
|
my ($self, $status) = @_; |
169
|
|
|
|
|
|
|
|
170
|
15
|
50
|
|
|
|
40
|
if ($status) |
171
|
|
|
|
|
|
|
{ |
172
|
15
|
|
|
|
|
47
|
$self->{status} = $status; |
173
|
|
|
|
|
|
|
} |
174
|
|
|
|
|
|
|
else |
175
|
|
|
|
|
|
|
{ |
176
|
0
|
|
|
|
|
0
|
return $self->{status}; |
177
|
|
|
|
|
|
|
} |
178
|
|
|
|
|
|
|
} |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
=head3 print |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
Print stuff to the http stream. |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
=cut |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
sub print { |
187
|
15
|
|
|
15
|
1
|
18
|
my ($self, $line) = @_; |
188
|
|
|
|
|
|
|
|
189
|
15
|
|
|
|
|
37
|
$self->{output} .= $line; |
190
|
|
|
|
|
|
|
} |
191
|
|
|
|
|
|
|
|
192
|
15
|
|
|
15
|
0
|
31
|
sub rflush { } |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
=head3 sleep |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
Sleeps (used by nginx), not needed for other server implementations. |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
=cut |
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
sub sleep |
201
|
|
|
|
|
|
|
{ |
202
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
203
|
0
|
|
|
|
|
0
|
sleep(shift); |
204
|
|
|
|
|
|
|
} |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
=head3 header_only |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
Returns true of only the header was requested. |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
=cut |
211
|
|
|
|
|
|
|
|
212
|
0
|
|
|
0
|
1
|
0
|
sub header_only { 0 } |
213
|
|
|
|
|
|
|
|
214
|
0
|
|
|
0
|
0
|
0
|
sub server_returns_object { 1 } |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
=head3 unescape |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
Unescape an encoded uri. |
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
=cut |
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
sub unescape |
223
|
|
|
|
|
|
|
{ |
224
|
0
|
|
|
0
|
1
|
0
|
my ($self, $string) = @_; |
225
|
|
|
|
|
|
|
|
226
|
0
|
|
|
|
|
0
|
return uri_unescape($string); |
227
|
|
|
|
|
|
|
} |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
=head3 server_return |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
This function defines what is returned to the server at the end of a dispatch. |
232
|
|
|
|
|
|
|
For nginx, this will be a status code, but in this test implementation we're |
233
|
|
|
|
|
|
|
returning the actual server object itself, so we can evaluate it while testing |
234
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
=cut |
236
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
sub server_return |
238
|
|
|
|
|
|
|
{ |
239
|
15
|
|
|
15
|
1
|
20
|
my ($self, $what) = @_; |
240
|
|
|
|
|
|
|
|
241
|
15
|
|
|
|
|
211
|
return $self; |
242
|
|
|
|
|
|
|
} |
243
|
|
|
|
|
|
|
|
244
|
0
|
|
|
0
|
0
|
|
sub close_connection { 1 } |
245
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
=head1 AUTHORS |
247
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
Bizowie |
249
|
|
|
|
|
|
|
|
250
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
251
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
Copyright (C) 2013 Bizowie |
253
|
|
|
|
|
|
|
|
254
|
|
|
|
|
|
|
This library is free software. You can redistribute it and/or modify it under the same terms as Perl itself. |
255
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
=cut |
257
|
|
|
|
|
|
|
|
258
|
|
|
|
|
|
|
1; |