line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Starman::Server; |
2
|
74
|
|
|
74
|
|
60192
|
use strict; |
|
74
|
|
|
|
|
696
|
|
|
74
|
|
|
|
|
5534
|
|
3
|
74
|
|
|
74
|
|
1125
|
use base 'Net::Server::PreFork'; |
|
74
|
|
|
|
|
532
|
|
|
74
|
|
|
|
|
64277
|
|
4
|
|
|
|
|
|
|
|
5
|
74
|
|
|
74
|
|
2088708
|
use Data::Dump qw(dump); |
|
74
|
|
|
|
|
349210
|
|
|
74
|
|
|
|
|
5125
|
|
6
|
74
|
|
|
74
|
|
575
|
use Socket qw(IPPROTO_TCP TCP_NODELAY); |
|
74
|
|
|
|
|
196
|
|
|
74
|
|
|
|
|
6155
|
|
7
|
74
|
|
|
74
|
|
485
|
use IO::Socket qw(:crlf); |
|
74
|
|
|
|
|
304
|
|
|
74
|
|
|
|
|
1587
|
|
8
|
74
|
|
|
74
|
|
51379
|
use HTTP::Parser::XS qw(parse_http_request); |
|
74
|
|
|
|
|
80855
|
|
|
74
|
|
|
|
|
5583
|
|
9
|
74
|
|
|
74
|
|
1087
|
use HTTP::Status qw(status_message); |
|
74
|
|
|
|
|
4161
|
|
|
74
|
|
|
|
|
28998
|
|
10
|
74
|
|
|
74
|
|
33366
|
use HTTP::Date qw(time2str); |
|
74
|
|
|
|
|
265986
|
|
|
74
|
|
|
|
|
4769
|
|
11
|
74
|
|
|
74
|
|
533
|
use POSIX qw(EINTR EPIPE ECONNRESET); |
|
74
|
|
|
|
|
160
|
|
|
74
|
|
|
|
|
519
|
|
12
|
74
|
|
|
74
|
|
8286
|
use Symbol; |
|
74
|
|
|
|
|
148
|
|
|
74
|
|
|
|
|
10060
|
|
13
|
|
|
|
|
|
|
|
14
|
74
|
|
|
74
|
|
1164
|
use Plack::Util; |
|
74
|
|
|
|
|
2686
|
|
|
74
|
|
|
|
|
1904
|
|
15
|
74
|
|
|
74
|
|
79159
|
use Plack::TempBuffer; |
|
74
|
|
|
|
|
376665
|
|
|
74
|
|
|
|
|
2793
|
|
16
|
|
|
|
|
|
|
|
17
|
74
|
|
50
|
74
|
|
545
|
use constant DEBUG => $ENV{STARMAN_DEBUG} || 0; |
|
74
|
|
|
|
|
148
|
|
|
74
|
|
|
|
|
5360
|
|
18
|
74
|
|
|
74
|
|
441
|
use constant CHUNKSIZE => 64 * 1024; |
|
74
|
|
|
|
|
93
|
|
|
74
|
|
|
|
|
4423
|
|
19
|
|
|
|
|
|
|
|
20
|
74
|
|
|
74
|
|
517
|
my $null_io = do { open my $io, "<", \""; $io }; |
|
74
|
|
|
|
|
154
|
|
|
74
|
|
|
|
|
2709
|
|
21
|
|
|
|
|
|
|
|
22
|
74
|
|
|
74
|
|
449
|
use Net::Server::SIG qw(register_sig); |
|
74
|
|
|
|
|
147
|
|
|
74
|
|
|
|
|
65789
|
|
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
# Override Net::Server's HUP handling - just restart all the workers and that's about it |
25
|
|
|
|
|
|
|
sub sig_hup { |
26
|
0
|
|
|
0
|
0
|
0
|
my $self = shift; |
27
|
0
|
|
|
|
|
0
|
$self->hup_children; |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
sub run { |
31
|
73
|
|
|
73
|
1
|
1058
|
my($self, $app, $options) = @_; |
32
|
|
|
|
|
|
|
|
33
|
73
|
|
|
|
|
370
|
$self->{app} = $app; |
34
|
73
|
|
|
|
|
140
|
$self->{options} = $options; |
35
|
|
|
|
|
|
|
|
36
|
73
|
|
|
|
|
109
|
my %extra = (); |
37
|
|
|
|
|
|
|
|
38
|
73
|
50
|
|
|
|
389
|
if ($options->{net_server_args}) { |
39
|
0
|
|
|
|
|
0
|
%extra = %{ $options->{net_server_args} }; |
|
0
|
|
|
|
|
0
|
|
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
73
|
50
|
|
|
|
456
|
if ( $options->{pid} ) { |
43
|
0
|
|
|
|
|
0
|
$extra{pid_file} = $options->{pid}; |
44
|
|
|
|
|
|
|
} |
45
|
73
|
50
|
|
|
|
213
|
if ( $options->{daemonize} ) { |
46
|
0
|
|
|
|
|
0
|
$extra{setsid} = $extra{background} = 1; |
47
|
|
|
|
|
|
|
} |
48
|
73
|
50
|
|
|
|
182
|
if ( $options->{error_log} ) { |
49
|
0
|
|
|
|
|
0
|
$extra{log_file} = $options->{error_log}; |
50
|
|
|
|
|
|
|
} |
51
|
73
|
|
|
|
|
97
|
if ( DEBUG ) { |
52
|
|
|
|
|
|
|
$extra{log_level} = 4; |
53
|
|
|
|
|
|
|
} |
54
|
73
|
50
|
|
|
|
255
|
if ( $options->{ssl_cert} ) { |
55
|
0
|
|
|
|
|
0
|
$extra{SSL_cert_file} = $options->{ssl_cert}; |
56
|
|
|
|
|
|
|
} |
57
|
73
|
50
|
|
|
|
293
|
if ( $options->{ssl_key} ) { |
58
|
0
|
|
|
|
|
0
|
$extra{SSL_key_file} = $options->{ssl_key}; |
59
|
|
|
|
|
|
|
} |
60
|
73
|
50
|
|
|
|
213
|
if (! exists $options->{keepalive}) { |
61
|
73
|
|
|
|
|
146
|
$options->{keepalive} = 1; |
62
|
|
|
|
|
|
|
} |
63
|
73
|
50
|
|
|
|
176
|
if (! exists $options->{keepalive_timeout}) { |
64
|
73
|
|
|
|
|
146
|
$options->{keepalive_timeout} = 1; |
65
|
|
|
|
|
|
|
} |
66
|
73
|
50
|
|
|
|
213
|
if (! exists $options->{read_timeout}) { |
67
|
73
|
|
|
|
|
146
|
$options->{read_timeout} = 5; |
68
|
|
|
|
|
|
|
} |
69
|
73
|
50
|
|
|
|
200
|
if (! exists $options->{proctitle}) { |
70
|
73
|
|
|
|
|
115
|
$options->{proctitle} = 1; |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
73
|
|
|
|
|
140
|
my @port; |
74
|
73
|
50
|
|
|
|
235
|
for my $listen (@{$options->{listen} || [ "$options->{host}:$options->{port}" ]}) { |
|
73
|
|
|
|
|
621
|
|
75
|
73
|
|
|
|
|
134
|
my %listen; |
76
|
73
|
50
|
|
|
|
304
|
if ($listen =~ /:/) { |
77
|
73
|
|
|
|
|
345
|
my($h, $p, $opt) = split /:/, $listen, 3; |
78
|
73
|
50
|
|
|
|
323
|
$listen{host} = $h if $h; |
79
|
73
|
|
|
|
|
170
|
$listen{port} = $p; |
80
|
73
|
50
|
33
|
|
|
334
|
$listen{proto} = 'ssl' if defined $opt && lc $opt eq 'ssl'; |
81
|
|
|
|
|
|
|
} else { |
82
|
0
|
|
|
|
|
0
|
%listen = ( |
83
|
|
|
|
|
|
|
host => 'localhost', |
84
|
|
|
|
|
|
|
port => $listen, |
85
|
|
|
|
|
|
|
proto => 'unix', |
86
|
|
|
|
|
|
|
); |
87
|
|
|
|
|
|
|
} |
88
|
73
|
|
|
|
|
212
|
push @port, \%listen; |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
73
|
|
50
|
|
|
371
|
my $workers = $options->{workers} || 5; |
92
|
73
|
|
|
|
|
224
|
local @ARGV = (); |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
$self->SUPER::run( |
95
|
|
|
|
|
|
|
port => \@port, |
96
|
|
|
|
|
|
|
host => '*', # default host |
97
|
|
|
|
|
|
|
proto => $options->{ssl} ? 'ssl' : 'tcp', # default proto |
98
|
|
|
|
|
|
|
serialize => ( $^O =~ m!(linux|darwin|bsd|cygwin)$! ) ? 'none' : 'flock', |
99
|
|
|
|
|
|
|
min_servers => $options->{min_servers} || $workers, |
100
|
|
|
|
|
|
|
min_spare_servers => $options->{min_spare_servers} || $workers - 1, |
101
|
|
|
|
|
|
|
max_spare_servers => $options->{max_spare_servers} || $workers - 1, |
102
|
|
|
|
|
|
|
max_servers => $options->{max_servers} || $workers, |
103
|
|
|
|
|
|
|
max_requests => $options->{max_requests} || 1000, |
104
|
|
|
|
|
|
|
user => $options->{user} || $>, |
105
|
|
|
|
|
|
|
group => $options->{group} || $), |
106
|
73
|
50
|
33
|
|
|
141806
|
listen => $options->{backlog} || 1024, |
|
|
50
|
33
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
|
50
|
|
|
|
|
107
|
|
|
|
|
|
|
check_for_waiting => 1, |
108
|
|
|
|
|
|
|
no_client_stdout => 1, |
109
|
|
|
|
|
|
|
%extra |
110
|
|
|
|
|
|
|
); |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
sub pre_loop_hook { |
114
|
73
|
|
|
73
|
1
|
350994
|
my $self = shift; |
115
|
|
|
|
|
|
|
|
116
|
73
|
|
|
|
|
212
|
my $port = $self->{server}->{port}->[0]; |
117
|
|
|
|
|
|
|
my $proto = $port->{proto} eq 'ssl' ? 'https' : |
118
|
73
|
50
|
|
|
|
371
|
$port->{proto} eq 'unix' ? 'unix' : |
|
|
50
|
|
|
|
|
|
119
|
|
|
|
|
|
|
'http'; |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
$self->{options}{server_ready}->({ |
122
|
|
|
|
|
|
|
host => $port->{host}, |
123
|
|
|
|
|
|
|
port => $port->{port}, |
124
|
|
|
|
|
|
|
proto => $proto, |
125
|
|
|
|
|
|
|
server_software => 'Starman', |
126
|
73
|
50
|
|
|
|
267
|
}) if $self->{options}{server_ready}; |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
register_sig( |
129
|
0
|
|
|
0
|
|
0
|
TTIN => sub { $self->{server}->{$_}++ for qw( min_servers max_servers ) }, |
130
|
0
|
|
|
0
|
|
0
|
TTOU => sub { $self->{server}->{$_}-- for qw( min_servers max_servers ) }, |
131
|
0
|
|
|
0
|
|
0
|
QUIT => sub { $self->server_close(1) }, |
132
|
73
|
|
|
|
|
572
|
); |
133
|
|
|
|
|
|
|
} |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
sub server_close { |
136
|
10
|
|
|
10
|
1
|
2074575
|
my($self, $quit) = @_; |
137
|
|
|
|
|
|
|
|
138
|
10
|
50
|
|
|
|
205
|
if ($quit) { |
139
|
0
|
|
|
|
|
0
|
$self->log(2, $self->log_time . " Received QUIT. Running a graceful shutdown\n"); |
140
|
0
|
|
|
|
|
0
|
$self->{server}->{$_} = 0 for qw( min_servers max_servers ); |
141
|
0
|
|
|
|
|
0
|
$self->hup_children; |
142
|
0
|
|
|
|
|
0
|
while (1) { |
143
|
0
|
|
|
|
|
0
|
Net::Server::SIG::check_sigs(); |
144
|
0
|
|
|
|
|
0
|
$self->coordinate_children; |
145
|
0
|
0
|
|
|
|
0
|
last if !keys %{$self->{server}{children}}; |
|
0
|
|
|
|
|
0
|
|
146
|
0
|
|
|
|
|
0
|
sleep 1; |
147
|
|
|
|
|
|
|
} |
148
|
0
|
|
|
|
|
0
|
$self->log(2, $self->log_time . " Worker processes cleaned up\n"); |
149
|
|
|
|
|
|
|
} |
150
|
|
|
|
|
|
|
|
151
|
10
|
|
|
|
|
509
|
$self->SUPER::server_close(); |
152
|
|
|
|
|
|
|
} |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
sub run_parent { |
155
|
23
|
|
|
23
|
0
|
132131
|
my $self = shift; |
156
|
23
|
50
|
|
|
|
10975
|
$0 = "starman master " . join(" ", @{$self->{options}{argv} || []}) |
157
|
23
|
50
|
|
|
|
714
|
if $self->{options}{proctitle}; |
158
|
74
|
|
|
74
|
|
559
|
no warnings 'redefine'; |
|
74
|
|
|
|
|
153
|
|
|
74
|
|
|
|
|
110825
|
|
159
|
|
|
|
|
|
|
local *Net::Server::PreFork::register_sig = sub { |
160
|
23
|
|
|
23
|
|
5301
|
my %args = @_; |
161
|
23
|
|
|
|
|
197
|
delete $args{QUIT}; |
162
|
23
|
|
|
|
|
662
|
Net::Server::SIG::register_sig(%args); |
163
|
23
|
|
|
|
|
2101
|
}; |
164
|
23
|
|
|
|
|
1194
|
$self->SUPER::run_parent(@_); |
165
|
|
|
|
|
|
|
} |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
# The below methods run in the child process |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
sub child_init_hook { |
170
|
63
|
|
|
63
|
1
|
10004254
|
my $self = shift; |
171
|
63
|
|
|
|
|
3385
|
srand(); |
172
|
63
|
50
|
|
|
|
2001
|
if ($self->{options}->{psgi_app_builder}) { |
173
|
0
|
|
|
|
|
0
|
DEBUG && warn "[$$] Initializing the PSGI app\n"; |
174
|
0
|
|
|
|
|
0
|
$self->{app} = $self->{options}->{psgi_app_builder}->(); |
175
|
|
|
|
|
|
|
} |
176
|
63
|
50
|
|
|
|
59337
|
$0 = "starman worker " . join(" ", @{$self->{options}{argv} || []}) |
177
|
63
|
50
|
|
|
|
2037
|
if $self->{options}{proctitle}; |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
} |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
sub post_accept_hook { |
182
|
59
|
|
|
59
|
1
|
586356
|
my $self = shift; |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
$self->{client} = { |
185
|
59
|
|
|
|
|
1862
|
headerbuf => '', |
186
|
|
|
|
|
|
|
inputbuf => '', |
187
|
|
|
|
|
|
|
keepalive => 1, |
188
|
|
|
|
|
|
|
}; |
189
|
|
|
|
|
|
|
} |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
sub dispatch_request { |
192
|
85
|
|
|
85
|
0
|
719
|
my ($self, $env) = @_; |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
# Run PSGI apps |
195
|
85
|
|
|
|
|
1019
|
my $res = Plack::Util::run_app($self->{app}, $env); |
196
|
|
|
|
|
|
|
|
197
|
85
|
100
|
|
|
|
14895
|
if (ref $res eq 'CODE') { |
198
|
5
|
|
|
5
|
|
90
|
$res->(sub { $self->_finalize_response($env, $_[0]) }); |
|
5
|
|
|
|
|
266
|
|
199
|
|
|
|
|
|
|
} else { |
200
|
80
|
|
|
|
|
483
|
$self->_finalize_response($env, $res); |
201
|
|
|
|
|
|
|
} |
202
|
|
|
|
|
|
|
} |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
sub process_request { |
205
|
59
|
|
|
59
|
1
|
3585
|
my $self = shift; |
206
|
59
|
|
|
|
|
418
|
my $conn = $self->{server}->{client}; |
207
|
|
|
|
|
|
|
|
208
|
59
|
50
|
|
|
|
749
|
if ($conn->NS_proto eq 'TCP') { |
209
|
59
|
50
|
|
|
|
1501
|
setsockopt($conn, IPPROTO_TCP, TCP_NODELAY, 1) |
210
|
|
|
|
|
|
|
or die $!; |
211
|
|
|
|
|
|
|
} |
212
|
|
|
|
|
|
|
|
213
|
59
|
|
|
|
|
627
|
while ( $self->{client}->{keepalive} ) { |
214
|
126
|
50
|
|
|
|
162266
|
last if !$conn->connected; |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
# Read until we see all headers |
217
|
126
|
100
|
|
|
|
2950
|
last if !$self->_read_headers; |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
my $env = { |
220
|
|
|
|
|
|
|
REMOTE_ADDR => $self->{server}->{peeraddr}, |
221
|
|
|
|
|
|
|
REMOTE_HOST => $self->{server}->{peerhost} || $self->{server}->{peeraddr}, |
222
|
|
|
|
|
|
|
REMOTE_PORT => $self->{server}->{peerport} || 0, |
223
|
|
|
|
|
|
|
SERVER_NAME => $self->{server}->{sockaddr} || 0, # XXX: needs to be resolved? |
224
|
|
|
|
|
|
|
SERVER_PORT => $self->{server}->{sockport} || 0, |
225
|
|
|
|
|
|
|
SCRIPT_NAME => '', |
226
|
|
|
|
|
|
|
'psgi.version' => [ 1, 1 ], |
227
|
|
|
|
|
|
|
'psgi.errors' => *STDERR, |
228
|
|
|
|
|
|
|
'psgi.url_scheme' => ($conn->NS_proto eq 'SSL' ? 'https' : 'http'), |
229
|
|
|
|
|
|
|
'psgi.nonblocking' => Plack::Util::FALSE, |
230
|
|
|
|
|
|
|
'psgi.streaming' => Plack::Util::TRUE, |
231
|
|
|
|
|
|
|
'psgi.run_once' => Plack::Util::FALSE, |
232
|
|
|
|
|
|
|
'psgi.multithread' => Plack::Util::FALSE, |
233
|
|
|
|
|
|
|
'psgi.multiprocess' => Plack::Util::TRUE, |
234
|
|
|
|
|
|
|
'psgix.io' => $conn, |
235
|
|
|
|
|
|
|
'psgix.input.buffered' => Plack::Util::TRUE, |
236
|
|
|
|
|
|
|
'psgix.harakiri' => Plack::Util::TRUE, |
237
|
1
|
|
|
1
|
|
65
|
'psgix.informational' => sub { _write_informational($conn, @_) }, |
238
|
85
|
50
|
33
|
|
|
1905
|
}; |
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
239
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
# Parse headers |
241
|
85
|
|
|
|
|
6084
|
my $reqlen = parse_http_request(delete $self->{client}->{headerbuf}, $env); |
242
|
85
|
50
|
|
|
|
461
|
if ( $reqlen == -1 ) { |
243
|
|
|
|
|
|
|
# Bad request |
244
|
0
|
|
|
|
|
0
|
DEBUG && warn "[$$] Bad request\n"; |
245
|
0
|
|
|
|
|
0
|
$self->_http_error(400, { SERVER_PROTOCOL => "HTTP/1.0" }); |
246
|
0
|
|
|
|
|
0
|
last; |
247
|
|
|
|
|
|
|
} |
248
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
# Initialize PSGI environment |
250
|
|
|
|
|
|
|
# Determine whether we will keep the connection open after the request |
251
|
85
|
|
|
|
|
203
|
my $connection = delete $env->{HTTP_CONNECTION}; |
252
|
85
|
|
|
|
|
197
|
my $proto = $env->{SERVER_PROTOCOL}; |
253
|
85
|
50
|
33
|
|
|
1077
|
if ( $proto && $proto eq 'HTTP/1.0' ) { |
|
|
50
|
33
|
|
|
|
|
254
|
0
|
0
|
0
|
|
|
0
|
if ( $connection && $connection =~ /^keep-alive$/i ) { |
255
|
|
|
|
|
|
|
# Keep-alive only with explicit header in HTTP/1.0 |
256
|
0
|
|
|
|
|
0
|
$self->{client}->{keepalive} = 1; |
257
|
|
|
|
|
|
|
} |
258
|
|
|
|
|
|
|
else { |
259
|
0
|
|
|
|
|
0
|
$self->{client}->{keepalive} = 0; |
260
|
|
|
|
|
|
|
} |
261
|
|
|
|
|
|
|
} |
262
|
|
|
|
|
|
|
elsif ( $proto && $proto eq 'HTTP/1.1' ) { |
263
|
85
|
50
|
33
|
|
|
635
|
if ( $connection && $connection =~ /^close$/i ) { |
264
|
0
|
|
|
|
|
0
|
$self->{client}->{keepalive} = 0; |
265
|
|
|
|
|
|
|
} |
266
|
|
|
|
|
|
|
else { |
267
|
|
|
|
|
|
|
# Keep-alive assumed in HTTP/1.1 |
268
|
85
|
|
|
|
|
214
|
$self->{client}->{keepalive} = 1; |
269
|
|
|
|
|
|
|
} |
270
|
|
|
|
|
|
|
|
271
|
|
|
|
|
|
|
# Do we need to send 100 Continue? |
272
|
85
|
100
|
|
|
|
294
|
if ( $env->{HTTP_EXPECT} ) { |
273
|
1
|
50
|
|
|
|
3
|
if ( lc $env->{HTTP_EXPECT} eq '100-continue' ) { |
274
|
1
|
|
|
|
|
19
|
_syswrite($conn, \('HTTP/1.1 100 Continue' . $CRLF . $CRLF)); |
275
|
1
|
|
|
|
|
2
|
DEBUG && warn "[$$] Sent 100 Continue response\n"; |
276
|
|
|
|
|
|
|
} |
277
|
|
|
|
|
|
|
else { |
278
|
0
|
|
|
|
|
0
|
DEBUG && warn "[$$] Invalid Expect header, returning 417\n"; |
279
|
0
|
|
|
|
|
0
|
$self->_http_error( 417, $env ); |
280
|
0
|
|
|
|
|
0
|
last; |
281
|
|
|
|
|
|
|
} |
282
|
|
|
|
|
|
|
} |
283
|
|
|
|
|
|
|
|
284
|
85
|
50
|
|
|
|
745
|
unless ($env->{HTTP_HOST}) { |
285
|
|
|
|
|
|
|
# No host, bad request |
286
|
0
|
|
|
|
|
0
|
DEBUG && warn "[$$] Bad request, HTTP/1.1 without Host header\n"; |
287
|
0
|
|
|
|
|
0
|
$self->_http_error( 400, $env ); |
288
|
0
|
|
|
|
|
0
|
last; |
289
|
|
|
|
|
|
|
} |
290
|
|
|
|
|
|
|
} |
291
|
|
|
|
|
|
|
|
292
|
85
|
50
|
|
|
|
374
|
unless ($self->{options}->{keepalive}) { |
293
|
0
|
|
|
|
|
0
|
DEBUG && warn "[$$] keep-alive is disabled. Closing the connection after this request\n"; |
294
|
0
|
|
|
|
|
0
|
$self->{client}->{keepalive} = 0; |
295
|
|
|
|
|
|
|
} |
296
|
|
|
|
|
|
|
|
297
|
85
|
|
|
|
|
2020
|
$self->_prepare_env($env); |
298
|
|
|
|
|
|
|
|
299
|
85
|
|
|
|
|
825
|
$self->dispatch_request($env); |
300
|
|
|
|
|
|
|
|
301
|
85
|
|
|
|
|
174
|
DEBUG && warn "[$$] Request done\n"; |
302
|
|
|
|
|
|
|
|
303
|
85
|
100
|
|
|
|
455
|
if ( $self->{client}->{keepalive} ) { |
304
|
|
|
|
|
|
|
# If we still have data in the input buffer it may be a pipelined request |
305
|
67
|
50
|
|
|
|
183
|
if ( $self->{client}->{inputbuf} ne '' ) { |
306
|
0
|
0
|
|
|
|
0
|
if ( $self->{client}->{inputbuf} =~ /^(?:GET|HEAD)/ ) { |
307
|
0
|
|
|
|
|
0
|
if ( DEBUG ) { |
308
|
|
|
|
|
|
|
warn "Pipelined GET/HEAD request in input buffer: " |
309
|
|
|
|
|
|
|
. dump( $self->{client}->{inputbuf} ) . "\n"; |
310
|
|
|
|
|
|
|
} |
311
|
|
|
|
|
|
|
|
312
|
|
|
|
|
|
|
# Continue processing the input buffer |
313
|
0
|
|
|
|
|
0
|
next; |
314
|
|
|
|
|
|
|
} |
315
|
|
|
|
|
|
|
else { |
316
|
|
|
|
|
|
|
# Input buffer just has junk, clear it |
317
|
0
|
|
|
|
|
0
|
if ( DEBUG ) { |
318
|
|
|
|
|
|
|
warn "Clearing junk from input buffer: " |
319
|
|
|
|
|
|
|
. dump( $self->{client}->{inputbuf} ) . "\n"; |
320
|
|
|
|
|
|
|
} |
321
|
|
|
|
|
|
|
|
322
|
0
|
|
|
|
|
0
|
$self->{client}->{inputbuf} = ''; |
323
|
|
|
|
|
|
|
} |
324
|
|
|
|
|
|
|
} |
325
|
|
|
|
|
|
|
|
326
|
67
|
|
|
|
|
83
|
DEBUG && warn "[$$] Waiting on previous connection for keep-alive request...\n"; |
327
|
|
|
|
|
|
|
|
328
|
67
|
|
|
|
|
12561
|
my $sel = IO::Select->new($conn); |
329
|
67
|
50
|
|
|
|
4373
|
last unless $sel->can_read($self->{options}->{keepalive_timeout}); |
330
|
|
|
|
|
|
|
} |
331
|
|
|
|
|
|
|
} |
332
|
|
|
|
|
|
|
|
333
|
59
|
|
|
|
|
322
|
DEBUG && warn "[$$] Closing connection\n"; |
334
|
|
|
|
|
|
|
} |
335
|
|
|
|
|
|
|
|
336
|
|
|
|
|
|
|
sub _read_headers { |
337
|
126
|
|
|
126
|
|
439
|
my $self = shift; |
338
|
|
|
|
|
|
|
|
339
|
126
|
|
|
|
|
25395
|
eval { |
340
|
126
|
|
|
0
|
|
3948
|
local $SIG{ALRM} = sub { die "Timed out\n"; }; |
|
0
|
|
|
|
|
0
|
|
341
|
|
|
|
|
|
|
|
342
|
126
|
|
|
|
|
1326
|
alarm( $self->{options}->{read_timeout} ); |
343
|
|
|
|
|
|
|
|
344
|
126
|
|
|
|
|
421
|
while (1) { |
345
|
|
|
|
|
|
|
# Do we have a full header in the buffer? |
346
|
|
|
|
|
|
|
# This is before sysread so we don't read if we have a pipelined request |
347
|
|
|
|
|
|
|
# waiting in the buffer |
348
|
212
|
100
|
100
|
|
|
29456
|
last if $self->{client}->{inputbuf} ne '' && $self->{client}->{inputbuf} =~ /$CR?$LF$CR?$LF/s; |
349
|
|
|
|
|
|
|
|
350
|
|
|
|
|
|
|
# If not, read some data |
351
|
127
|
|
|
|
|
11689
|
my $read = sysread $self->{server}->{client}, my $buf, CHUNKSIZE; |
352
|
|
|
|
|
|
|
|
353
|
127
|
100
|
66
|
|
|
1758
|
if ( !defined $read || $read == 0 ) { |
354
|
41
|
|
|
|
|
1017
|
die "Read error: $!\n"; |
355
|
|
|
|
|
|
|
} |
356
|
|
|
|
|
|
|
|
357
|
86
|
|
|
|
|
219
|
if ( DEBUG ) { |
358
|
|
|
|
|
|
|
warn "[$$] Read $read bytes: " . dump($buf) . "\n"; |
359
|
|
|
|
|
|
|
} |
360
|
|
|
|
|
|
|
|
361
|
86
|
|
|
|
|
488
|
$self->{client}->{inputbuf} .= $buf; |
362
|
|
|
|
|
|
|
} |
363
|
|
|
|
|
|
|
}; |
364
|
|
|
|
|
|
|
|
365
|
126
|
|
|
|
|
1056
|
alarm(0); |
366
|
|
|
|
|
|
|
|
367
|
126
|
100
|
|
|
|
495
|
if ( $@ ) { |
368
|
41
|
50
|
|
|
|
454
|
if ( $@ =~ /Timed out/ ) { |
369
|
0
|
|
|
|
|
0
|
DEBUG && warn "[$$] Client connection timed out\n"; |
370
|
0
|
|
|
|
|
0
|
return; |
371
|
|
|
|
|
|
|
} |
372
|
|
|
|
|
|
|
|
373
|
41
|
50
|
|
|
|
471
|
if ( $@ =~ /Read error/ ) { |
374
|
41
|
|
|
|
|
88
|
DEBUG && warn "[$$] Read error: $!\n"; |
375
|
41
|
|
|
|
|
238
|
return; |
376
|
|
|
|
|
|
|
} |
377
|
|
|
|
|
|
|
} |
378
|
|
|
|
|
|
|
|
379
|
|
|
|
|
|
|
# Pull out the complete header into a new buffer |
380
|
85
|
|
|
|
|
376
|
$self->{client}->{headerbuf} = $self->{client}->{inputbuf}; |
381
|
|
|
|
|
|
|
|
382
|
|
|
|
|
|
|
# Save any left-over data, possibly body data or pipelined requests |
383
|
85
|
|
|
|
|
1923
|
$self->{client}->{inputbuf} =~ s/.*?$CR?$LF$CR?$LF//s; |
384
|
|
|
|
|
|
|
|
385
|
85
|
|
|
|
|
649
|
return 1; |
386
|
|
|
|
|
|
|
} |
387
|
|
|
|
|
|
|
|
388
|
|
|
|
|
|
|
sub _http_error { |
389
|
0
|
|
|
0
|
|
0
|
my ( $self, $code, $env ) = @_; |
390
|
|
|
|
|
|
|
|
391
|
0
|
|
0
|
|
|
0
|
my $status = $code || 500; |
392
|
0
|
|
|
|
|
0
|
my $msg = status_message($status); |
393
|
|
|
|
|
|
|
|
394
|
0
|
|
|
|
|
0
|
my $res = [ |
395
|
|
|
|
|
|
|
$status, |
396
|
|
|
|
|
|
|
[ 'Content-Type' => 'text/plain', 'Content-Length' => length($msg) ], |
397
|
|
|
|
|
|
|
[ $msg ], |
398
|
|
|
|
|
|
|
]; |
399
|
|
|
|
|
|
|
|
400
|
0
|
|
|
|
|
0
|
$self->{client}->{keepalive} = 0; |
401
|
0
|
|
|
|
|
0
|
$self->_finalize_response($env, $res); |
402
|
|
|
|
|
|
|
} |
403
|
|
|
|
|
|
|
|
404
|
|
|
|
|
|
|
sub _prepare_env { |
405
|
86
|
|
|
86
|
|
398
|
my($self, $env) = @_; |
406
|
|
|
|
|
|
|
|
407
|
|
|
|
|
|
|
my $get_chunk = sub { |
408
|
11
|
100
|
|
11
|
|
48
|
if ($self->{client}->{inputbuf} ne '') { |
409
|
3
|
|
|
|
|
111
|
my $chunk = delete $self->{client}->{inputbuf}; |
410
|
3
|
|
|
|
|
15
|
return ($chunk, length $chunk); |
411
|
|
|
|
|
|
|
} |
412
|
8
|
|
|
|
|
40817
|
my $read = sysread $self->{server}->{client}, my($chunk), CHUNKSIZE; |
413
|
8
|
|
|
|
|
116
|
return ($chunk, $read); |
414
|
86
|
|
|
|
|
871
|
}; |
415
|
|
|
|
|
|
|
|
416
|
74
|
|
|
74
|
|
599
|
my $chunked = do { no warnings; lc delete $env->{HTTP_TRANSFER_ENCODING} eq 'chunked' }; |
|
74
|
|
|
|
|
148
|
|
|
74
|
|
|
|
|
97418
|
|
|
86
|
|
|
|
|
193
|
|
|
86
|
|
|
|
|
496
|
|
417
|
|
|
|
|
|
|
|
418
|
86
|
100
|
|
|
|
456
|
if (my $cl = $env->{CONTENT_LENGTH}) { |
|
|
100
|
|
|
|
|
|
419
|
4
|
|
|
|
|
103
|
my $buf = Plack::TempBuffer->new($cl); |
420
|
4
|
|
|
|
|
536
|
while ($cl > 0) { |
421
|
5
|
|
|
|
|
170
|
my($chunk, $read) = $get_chunk->(); |
422
|
|
|
|
|
|
|
|
423
|
5
|
50
|
33
|
|
|
53
|
if ( !defined $read || $read == 0 ) { |
424
|
0
|
|
|
|
|
0
|
die "Read error: $!\n"; |
425
|
|
|
|
|
|
|
} |
426
|
|
|
|
|
|
|
|
427
|
5
|
|
|
|
|
9
|
$cl -= $read; |
428
|
5
|
|
|
|
|
76
|
$buf->print($chunk); |
429
|
|
|
|
|
|
|
} |
430
|
4
|
|
|
|
|
190
|
$env->{'psgi.input'} = $buf->rewind; |
431
|
|
|
|
|
|
|
} elsif ($chunked) { |
432
|
2
|
|
|
|
|
52
|
my $buf = Plack::TempBuffer->new; |
433
|
2
|
|
|
|
|
262
|
my $chunk_buffer = ''; |
434
|
2
|
|
|
|
|
4
|
my $length; |
435
|
|
|
|
|
|
|
|
436
|
|
|
|
|
|
|
DECHUNK: |
437
|
2
|
|
|
|
|
3
|
while (1) { |
438
|
6
|
|
|
|
|
100
|
my($chunk, $read) = $get_chunk->(); |
439
|
6
|
|
|
|
|
80
|
$chunk_buffer .= $chunk; |
440
|
|
|
|
|
|
|
|
441
|
6
|
|
|
|
|
81
|
while ( $chunk_buffer =~ s/^(([0-9a-fA-F]+).*\015\012)// ) { |
442
|
20
|
|
|
|
|
53
|
my $trailer = $1; |
443
|
20
|
|
|
|
|
47
|
my $chunk_len = hex $2; |
444
|
|
|
|
|
|
|
|
445
|
20
|
100
|
|
|
|
44
|
if ($chunk_len == 0) { |
|
|
100
|
|
|
|
|
|
446
|
2
|
|
|
|
|
6
|
last DECHUNK; |
447
|
|
|
|
|
|
|
} elsif (length $chunk_buffer < $chunk_len + 2) { |
448
|
1
|
|
|
|
|
3
|
$chunk_buffer = $trailer . $chunk_buffer; |
449
|
1
|
|
|
|
|
2
|
last; |
450
|
|
|
|
|
|
|
} |
451
|
|
|
|
|
|
|
|
452
|
17
|
|
|
|
|
62
|
$buf->print(substr $chunk_buffer, 0, $chunk_len, ''); |
453
|
17
|
|
|
|
|
296
|
$chunk_buffer =~ s/^\015\012//; |
454
|
|
|
|
|
|
|
|
455
|
17
|
|
|
|
|
63
|
$length += $chunk_len; |
456
|
|
|
|
|
|
|
} |
457
|
|
|
|
|
|
|
|
458
|
4
|
50
|
33
|
|
|
25
|
last unless $read && $read > 0; |
459
|
|
|
|
|
|
|
} |
460
|
|
|
|
|
|
|
|
461
|
2
|
|
|
|
|
15
|
$env->{CONTENT_LENGTH} = $length; |
462
|
2
|
|
|
|
|
8
|
$env->{'psgi.input'} = $buf->rewind; |
463
|
|
|
|
|
|
|
} else { |
464
|
80
|
|
|
|
|
963
|
$env->{'psgi.input'} = $null_io; |
465
|
|
|
|
|
|
|
} |
466
|
|
|
|
|
|
|
} |
467
|
|
|
|
|
|
|
|
468
|
|
|
|
|
|
|
sub _finalize_response { |
469
|
85
|
|
|
85
|
|
204
|
my($self, $env, $res) = @_; |
470
|
|
|
|
|
|
|
|
471
|
85
|
100
|
|
|
|
463
|
if ($env->{'psgix.harakiri.commit'}) { |
472
|
18
|
|
|
|
|
102
|
$self->{client}->{keepalive} = 0; |
473
|
18
|
|
|
|
|
167
|
$self->{client}->{harakiri} = 1; |
474
|
|
|
|
|
|
|
} |
475
|
|
|
|
|
|
|
|
476
|
85
|
|
|
|
|
474
|
my $protocol = $env->{SERVER_PROTOCOL}; |
477
|
85
|
|
|
|
|
166
|
my $status = $res->[0]; |
478
|
85
|
|
|
|
|
960
|
my $message = status_message($status); |
479
|
|
|
|
|
|
|
|
480
|
85
|
|
|
|
|
792
|
my(@headers, %headers); |
481
|
85
|
|
|
|
|
344
|
push @headers, "$protocol $status $message"; |
482
|
|
|
|
|
|
|
|
483
|
|
|
|
|
|
|
# Switch on Transfer-Encoding: chunked if we don't know Content-Length. |
484
|
85
|
|
|
|
|
132
|
my $chunked; |
485
|
85
|
|
|
|
|
163
|
my $headers = $res->[1]; |
486
|
85
|
|
|
|
|
362
|
for (my $i = 0; $i < @$headers; $i += 2) { |
487
|
97
|
|
|
|
|
167
|
my $k = $headers->[$i]; |
488
|
97
|
|
|
|
|
196
|
my $v = $headers->[$i + 1]; |
489
|
97
|
50
|
|
|
|
227
|
next if $k eq 'Connection'; |
490
|
97
|
|
|
|
|
246
|
push @headers, "$k: $v"; |
491
|
97
|
|
|
|
|
619
|
$headers{lc $k} = $v; |
492
|
|
|
|
|
|
|
} |
493
|
|
|
|
|
|
|
|
494
|
85
|
50
|
|
|
|
272
|
if ( $protocol eq 'HTTP/1.1' ) { |
495
|
85
|
100
|
|
|
|
350
|
if ( !exists $headers{'content-length'} ) { |
|
|
50
|
|
|
|
|
|
496
|
81
|
100
|
100
|
|
|
1203
|
if ( $status !~ /^1\d\d|[23]04$/ && $env->{REQUEST_METHOD} ne 'HEAD' ) { |
497
|
79
|
|
|
|
|
124
|
DEBUG && warn "[$$] Using chunked transfer-encoding to send unknown length body\n"; |
498
|
79
|
|
|
|
|
313
|
push @headers, 'Transfer-Encoding: chunked'; |
499
|
79
|
|
|
|
|
227
|
$chunked = 1; |
500
|
|
|
|
|
|
|
} |
501
|
|
|
|
|
|
|
} |
502
|
|
|
|
|
|
|
elsif ( my $te = $headers{'transfer-encoding'} ) { |
503
|
0
|
0
|
|
|
|
0
|
if ( $te eq 'chunked' ) { |
504
|
0
|
|
|
|
|
0
|
DEBUG && warn "[$$] Chunked transfer-encoding set for response\n"; |
505
|
0
|
|
|
|
|
0
|
$chunked = 1; |
506
|
|
|
|
|
|
|
} |
507
|
|
|
|
|
|
|
} |
508
|
|
|
|
|
|
|
} else { |
509
|
0
|
0
|
|
|
|
0
|
if ( !exists $headers{'content-length'} ) { |
510
|
0
|
|
|
|
|
0
|
DEBUG && warn "[$$] Disabling keep-alive after sending unknown length body on $protocol\n"; |
511
|
0
|
|
|
|
|
0
|
$self->{client}->{keepalive} = 0; |
512
|
|
|
|
|
|
|
} |
513
|
|
|
|
|
|
|
} |
514
|
|
|
|
|
|
|
|
515
|
85
|
50
|
|
|
|
249
|
if ( ! $headers{date} ) { |
516
|
85
|
|
|
|
|
1002
|
push @headers, "Date: " . time2str( time() ); |
517
|
|
|
|
|
|
|
} |
518
|
|
|
|
|
|
|
|
519
|
|
|
|
|
|
|
# Should we keep the connection open? |
520
|
85
|
100
|
|
|
|
2914
|
if ( $self->{client}->{keepalive} ) { |
521
|
67
|
|
|
|
|
159
|
push @headers, 'Connection: keep-alive'; |
522
|
|
|
|
|
|
|
} else { |
523
|
18
|
|
|
|
|
181
|
push @headers, 'Connection: close'; |
524
|
|
|
|
|
|
|
} |
525
|
|
|
|
|
|
|
|
526
|
85
|
|
|
|
|
205
|
my $conn = $self->{server}->{client}; |
527
|
|
|
|
|
|
|
|
528
|
|
|
|
|
|
|
# Buffer the headers so they are sent with the first write() call |
529
|
|
|
|
|
|
|
# This reduces the number of TCP packets we are sending |
530
|
85
|
|
|
|
|
824
|
_syswrite($conn, \(join( $CRLF, @headers, '' ) . $CRLF)); |
531
|
|
|
|
|
|
|
|
532
|
85
|
100
|
|
|
|
438
|
if (defined $res->[2]) { |
533
|
|
|
|
|
|
|
Plack::Util::foreach($res->[2], sub { |
534
|
82
|
|
|
82
|
|
1810
|
my $buffer = $_[0]; |
535
|
82
|
100
|
|
|
|
199
|
if ($chunked) { |
536
|
78
|
|
|
|
|
137
|
my $len = length $buffer; |
537
|
78
|
50
|
|
|
|
196
|
return unless $len; |
538
|
78
|
|
|
|
|
641
|
$buffer = sprintf( "%x", $len ) . $CRLF . $buffer . $CRLF; |
539
|
|
|
|
|
|
|
} |
540
|
82
|
|
|
|
|
300
|
_syswrite($conn, \$buffer); |
541
|
81
|
|
|
|
|
1167
|
}); |
542
|
81
|
100
|
|
|
|
3909
|
_syswrite($conn, \"0$CRLF$CRLF") if $chunked; |
543
|
|
|
|
|
|
|
} else { |
544
|
|
|
|
|
|
|
return Plack::Util::inline_object |
545
|
|
|
|
|
|
|
write => sub { |
546
|
6
|
|
|
6
|
|
383
|
my $buffer = $_[0]; |
547
|
6
|
50
|
|
|
|
23
|
if ($chunked) { |
548
|
6
|
|
|
|
|
29
|
my $len = length $buffer; |
549
|
6
|
100
|
|
|
|
17
|
return unless $len; |
550
|
5
|
|
|
|
|
33
|
$buffer = sprintf( "%x", $len ) . $CRLF . $buffer . $CRLF; |
551
|
|
|
|
|
|
|
} |
552
|
5
|
|
|
|
|
16
|
_syswrite($conn, \$buffer); |
553
|
|
|
|
|
|
|
}, |
554
|
|
|
|
|
|
|
close => sub { |
555
|
4
|
50
|
|
4
|
|
224
|
_syswrite($conn, \"0$CRLF$CRLF") if $chunked; |
556
|
4
|
|
|
|
|
152
|
}; |
557
|
|
|
|
|
|
|
} |
558
|
|
|
|
|
|
|
} |
559
|
|
|
|
|
|
|
|
560
|
|
|
|
|
|
|
sub _syswrite { |
561
|
253
|
|
|
253
|
|
565
|
my ($conn, $buffer_ref) = @_; |
562
|
|
|
|
|
|
|
|
563
|
253
|
|
|
|
|
418
|
my $amount = length $$buffer_ref; |
564
|
253
|
|
|
|
|
323
|
my $offset = 0; |
565
|
|
|
|
|
|
|
|
566
|
253
|
|
|
|
|
609
|
while ($amount > 0) { |
567
|
253
|
|
|
|
|
10235
|
my $len = syswrite($conn, $$buffer_ref, $amount, $offset); |
568
|
|
|
|
|
|
|
|
569
|
253
|
50
|
|
|
|
1116
|
if (not defined $len) { |
570
|
0
|
0
|
|
|
|
0
|
return if $! == EPIPE; |
571
|
0
|
0
|
|
|
|
0
|
return if $! == ECONNRESET; |
572
|
0
|
0
|
|
|
|
0
|
redo if $! == EINTR; |
573
|
0
|
|
|
|
|
0
|
die "write error: $!"; |
574
|
|
|
|
|
|
|
} |
575
|
|
|
|
|
|
|
|
576
|
253
|
|
|
|
|
353
|
$amount -= $len; |
577
|
253
|
|
|
|
|
457
|
$offset += $len; |
578
|
|
|
|
|
|
|
|
579
|
253
|
|
|
|
|
1414
|
DEBUG && warn "[$$] Wrote $len byte", ($len == 1 ? '' : 's'), "\n"; |
580
|
|
|
|
|
|
|
} |
581
|
|
|
|
|
|
|
} |
582
|
|
|
|
|
|
|
|
583
|
|
|
|
|
|
|
sub _write_informational { |
584
|
1
|
|
|
1
|
|
134
|
my ($conn, $code, $headers) = @_; |
585
|
1
|
|
|
|
|
35
|
my $message = HTTP::Status::status_message($code); |
586
|
1
|
|
|
|
|
26
|
my @lines = "HTTP/1.1 $code $message"; |
587
|
1
|
|
|
|
|
61
|
for (my $i = 0; $i < @$headers; $i += 2) { |
588
|
1
|
|
|
|
|
9
|
my $k = $headers->[$i]; |
589
|
1
|
|
|
|
|
3
|
my $v = $headers->[$i + 1]; |
590
|
1
|
|
|
|
|
8
|
push @lines, "$k: $v" ; |
591
|
|
|
|
|
|
|
} |
592
|
1
|
|
|
|
|
25
|
_syswrite($conn, \join($CRLF, @lines, $CRLF)); |
593
|
|
|
|
|
|
|
|
594
|
1
|
|
|
|
|
6
|
DEBUG && warn "[$$] Sent $code $message response\n"; |
595
|
|
|
|
|
|
|
} |
596
|
|
|
|
|
|
|
|
597
|
|
|
|
|
|
|
sub post_client_connection_hook { |
598
|
59
|
|
|
59
|
1
|
8170
|
my $self = shift; |
599
|
59
|
100
|
|
|
|
376
|
if ($self->{client}->{harakiri}) { |
600
|
18
|
|
|
|
|
3965
|
exit; |
601
|
|
|
|
|
|
|
} |
602
|
|
|
|
|
|
|
} |
603
|
|
|
|
|
|
|
|
604
|
|
|
|
|
|
|
1; |