line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package HTTP::Server::PSGI; |
2
|
39
|
|
|
39
|
|
18795
|
use strict; |
|
39
|
|
|
|
|
82
|
|
|
39
|
|
|
|
|
1786
|
|
3
|
39
|
|
|
39
|
|
230
|
use warnings; |
|
39
|
|
|
|
|
77
|
|
|
39
|
|
|
|
|
887
|
|
4
|
|
|
|
|
|
|
|
5
|
39
|
|
|
39
|
|
200
|
use Carp (); |
|
39
|
|
|
|
|
374
|
|
|
39
|
|
|
|
|
1062
|
|
6
|
39
|
|
|
39
|
|
15148
|
use Plack; |
|
39
|
|
|
|
|
77
|
|
|
39
|
|
|
|
|
1127
|
|
7
|
39
|
|
|
39
|
|
12879
|
use Plack::HTTPParser qw( parse_http_request ); |
|
39
|
|
|
|
|
373
|
|
|
39
|
|
|
|
|
3026
|
|
8
|
39
|
|
|
39
|
|
1069
|
use IO::Socket::INET; |
|
39
|
|
|
|
|
28357
|
|
|
39
|
|
|
|
|
1861
|
|
9
|
39
|
|
|
39
|
|
59395
|
use HTTP::Date; |
|
39
|
|
|
|
|
142582
|
|
|
39
|
|
|
|
|
3501
|
|
10
|
39
|
|
|
39
|
|
1059
|
use HTTP::Status; |
|
39
|
|
|
|
|
7902
|
|
|
39
|
|
|
|
|
21026
|
|
11
|
39
|
|
|
39
|
|
309
|
use List::Util qw(max sum); |
|
39
|
|
|
|
|
40
|
|
|
39
|
|
|
|
|
6863
|
|
12
|
39
|
|
|
39
|
|
602
|
use Plack::Util; |
|
39
|
|
|
|
|
78
|
|
|
39
|
|
|
|
|
1928
|
|
13
|
39
|
|
|
39
|
|
934
|
use Stream::Buffered; |
|
39
|
|
|
|
|
11145
|
|
|
39
|
|
|
|
|
1122
|
|
14
|
39
|
|
|
39
|
|
14863
|
use Plack::Middleware::ContentLength; |
|
39
|
|
|
|
|
116
|
|
|
39
|
|
|
|
|
1755
|
|
15
|
39
|
|
|
39
|
|
1029
|
use POSIX qw(EINTR); |
|
39
|
|
|
|
|
10705
|
|
|
39
|
|
|
|
|
1119
|
|
16
|
39
|
|
|
39
|
|
8814
|
use Socket qw(IPPROTO_TCP); |
|
39
|
|
|
|
|
78
|
|
|
39
|
|
|
|
|
2610
|
|
17
|
|
|
|
|
|
|
|
18
|
39
|
|
|
39
|
|
233
|
use Try::Tiny; |
|
39
|
|
|
|
|
125
|
|
|
39
|
|
|
|
|
2247
|
|
19
|
39
|
|
|
39
|
|
1068
|
use Time::HiRes qw(time); |
|
39
|
|
|
|
|
2357
|
|
|
39
|
|
|
|
|
862
|
|
20
|
|
|
|
|
|
|
|
21
|
39
|
|
|
39
|
|
14679
|
use constant TCP_NODELAY => try { Socket::TCP_NODELAY }; |
|
39
|
|
|
|
|
115
|
|
|
39
|
|
|
|
|
456
|
|
|
39
|
|
|
|
|
1134
|
|
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
my $alarm_interval; |
24
|
|
|
|
|
|
|
BEGIN { |
25
|
39
|
50
|
|
39
|
|
3762
|
if ($^O eq 'MSWin32') { |
26
|
0
|
|
|
|
|
0
|
$alarm_interval = 1; |
27
|
|
|
|
|
|
|
} else { |
28
|
39
|
|
|
|
|
156
|
Time::HiRes->import('alarm'); |
29
|
39
|
|
|
|
|
3077
|
$alarm_interval = 0.1; |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
|
33
|
39
|
|
|
39
|
|
233
|
use constant MAX_REQUEST_SIZE => 131072; |
|
39
|
|
|
|
|
78
|
|
|
39
|
|
|
|
|
2183
|
|
34
|
39
|
|
|
39
|
|
234
|
use constant MSWin32 => $^O eq 'MSWin32'; |
|
39
|
|
|
|
|
40
|
|
|
39
|
|
|
|
|
74228
|
|
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub new { |
37
|
37
|
|
|
37
|
0
|
74
|
my($class, %args) = @_; |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
my $self = bless { |
40
|
|
|
|
|
|
|
($args{listen_sock} ? ( |
41
|
|
|
|
|
|
|
listen_sock => $args{listen_sock}, |
42
|
|
|
|
|
|
|
host => $args{listen_sock}->sockhost, |
43
|
|
|
|
|
|
|
port => $args{listen_sock}->sockport, |
44
|
|
|
|
|
|
|
):( |
45
|
|
|
|
|
|
|
host => $args{host} || 0, |
46
|
|
|
|
|
|
|
port => $args{port} || 8080, |
47
|
|
|
|
|
|
|
)), |
48
|
|
|
|
|
|
|
timeout => $args{timeout} || 300, |
49
|
|
|
|
|
|
|
server_software => $args{server_software} || $class, |
50
|
|
|
|
37
|
|
|
server_ready => $args{server_ready} || sub {}, |
51
|
|
|
|
|
|
|
ssl => $args{ssl}, |
52
|
|
|
|
|
|
|
ipv6 => $args{ipv6}, |
53
|
|
|
|
|
|
|
ssl_key_file => $args{ssl_key_file}, |
54
|
|
|
|
|
|
|
ssl_cert_file => $args{ssl_cert_file}, |
55
|
37
|
50
|
50
|
|
|
1036
|
}, $class; |
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
|
50
|
|
|
|
|
56
|
|
|
|
|
|
|
|
57
|
37
|
|
|
|
|
185
|
$self; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
sub run { |
61
|
37
|
|
|
37
|
0
|
74
|
my($self, $app) = @_; |
62
|
37
|
|
|
|
|
111
|
$self->setup_listener(); |
63
|
37
|
|
|
|
|
185
|
$self->accept_loop($app); |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
sub prepare_socket_class { |
67
|
37
|
|
|
37
|
0
|
74
|
my($self, $args) = @_; |
68
|
|
|
|
|
|
|
|
69
|
37
|
0
|
33
|
|
|
111
|
if ($self->{ssl} && $self->{ipv6}) { |
70
|
0
|
|
|
|
|
0
|
Carp::croak("SSL and IPv6 are not supported at the same time (yet). Choose one."); |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
37
|
50
|
|
|
|
148
|
if ($self->{ssl}) { |
|
|
50
|
|
|
|
|
|
74
|
0
|
0
|
|
|
|
0
|
eval { require IO::Socket::SSL; 1 } |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
75
|
|
|
|
|
|
|
or Carp::croak("SSL suport requires IO::Socket::SSL"); |
76
|
0
|
|
|
|
|
0
|
$args->{SSL_key_file} = $self->{ssl_key_file}; |
77
|
0
|
|
|
|
|
0
|
$args->{SSL_cert_file} = $self->{ssl_cert_file}; |
78
|
0
|
|
|
|
|
0
|
return "IO::Socket::SSL"; |
79
|
|
|
|
|
|
|
} elsif ($self->{ipv6}) { |
80
|
0
|
0
|
|
|
|
0
|
eval { require IO::Socket::IP; 1 } |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
81
|
|
|
|
|
|
|
or Carp::croak("IPv6 support requires IO::Socket::IP"); |
82
|
0
|
|
0
|
|
|
0
|
$self->{host} ||= '::'; |
83
|
0
|
|
0
|
|
|
0
|
$args->{LocalAddr} ||= '::'; |
84
|
0
|
|
|
|
|
0
|
return "IO::Socket::IP"; |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
37
|
|
|
|
|
111
|
return "IO::Socket::INET"; |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
sub setup_listener { |
91
|
37
|
|
|
37
|
0
|
74
|
my $self = shift; |
92
|
|
|
|
|
|
|
|
93
|
37
|
|
33
|
|
|
185
|
$self->{listen_sock} ||= do { |
94
|
|
|
|
|
|
|
my %args = ( |
95
|
|
|
|
|
|
|
Listen => SOMAXCONN, |
96
|
|
|
|
|
|
|
LocalPort => $self->{port}, |
97
|
|
|
|
|
|
|
LocalAddr => $self->{host}, |
98
|
37
|
|
|
|
|
555
|
Proto => 'tcp', |
99
|
|
|
|
|
|
|
ReuseAddr => 1, |
100
|
|
|
|
|
|
|
); |
101
|
|
|
|
|
|
|
|
102
|
37
|
|
|
|
|
222
|
my $class = $self->prepare_socket_class(\%args); |
103
|
37
|
50
|
|
|
|
666
|
$class->new(%args) |
104
|
|
|
|
|
|
|
or die "failed to listen to port $self->{port}: $!"; |
105
|
|
|
|
|
|
|
}; |
106
|
|
|
|
|
|
|
|
107
|
37
|
50
|
|
|
|
21053
|
$self->{server_ready}->({ %$self, proto => $self->{ssl} ? 'https' : 'http' }); |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
sub accept_loop { |
111
|
37
|
|
|
37
|
0
|
74
|
my($self, $app) = @_; |
112
|
|
|
|
|
|
|
|
113
|
37
|
|
|
|
|
629
|
$app = Plack::Middleware::ContentLength->wrap($app); |
114
|
|
|
|
|
|
|
|
115
|
37
|
|
|
|
|
74
|
while (1) { |
116
|
740
|
|
|
|
|
12677
|
local $SIG{PIPE} = 'IGNORE'; |
117
|
740
|
50
|
|
|
|
8957
|
if (my $conn = $self->{listen_sock}->accept) { |
118
|
740
|
50
|
|
|
|
4613168
|
if (defined TCP_NODELAY) { |
119
|
740
|
50
|
|
|
|
14927
|
$conn->setsockopt(IPPROTO_TCP, TCP_NODELAY, 1) |
120
|
|
|
|
|
|
|
or die "setsockopt(TCP_NODELAY) failed:$!"; |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
my $env = { |
123
|
|
|
|
|
|
|
SERVER_PORT => $self->{port}, |
124
|
|
|
|
|
|
|
SERVER_NAME => $self->{host}, |
125
|
|
|
|
|
|
|
SCRIPT_NAME => '', |
126
|
|
|
|
|
|
|
REMOTE_ADDR => $conn->peerhost, |
127
|
|
|
|
|
|
|
REMOTE_PORT => $conn->peerport || 0, |
128
|
|
|
|
|
|
|
'psgi.version' => [ 1, 1 ], |
129
|
|
|
|
|
|
|
'psgi.errors' => *STDERR, |
130
|
740
|
50
|
50
|
|
|
22846
|
'psgi.url_scheme' => $self->{ssl} ? 'https' : 'http', |
131
|
|
|
|
|
|
|
'psgi.run_once' => Plack::Util::FALSE, |
132
|
|
|
|
|
|
|
'psgi.multithread' => Plack::Util::FALSE, |
133
|
|
|
|
|
|
|
'psgi.multiprocess' => Plack::Util::FALSE, |
134
|
|
|
|
|
|
|
'psgi.streaming' => Plack::Util::TRUE, |
135
|
|
|
|
|
|
|
'psgi.nonblocking' => Plack::Util::FALSE, |
136
|
|
|
|
|
|
|
'psgix.harakiri' => Plack::Util::TRUE, |
137
|
|
|
|
|
|
|
'psgix.input.buffered' => Plack::Util::TRUE, |
138
|
|
|
|
|
|
|
'psgix.io' => $conn, |
139
|
|
|
|
|
|
|
}; |
140
|
|
|
|
|
|
|
|
141
|
740
|
|
|
|
|
93640
|
$self->handle_connection($env, $conn, $app); |
142
|
703
|
|
|
|
|
15781
|
$conn->close; |
143
|
703
|
50
|
|
|
|
198105
|
last if $env->{'psgix.harakiri.commit'}; |
144
|
|
|
|
|
|
|
} |
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
} |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
sub handle_connection { |
149
|
740
|
|
|
740
|
0
|
2590
|
my($self, $env, $conn, $app) = @_; |
150
|
|
|
|
|
|
|
|
151
|
740
|
|
|
|
|
1401
|
my $buf = ''; |
152
|
740
|
|
|
|
|
2575
|
my $res = [ 400, [ 'Content-Type' => 'text/plain' ], [ 'Bad Request' ] ]; |
153
|
|
|
|
|
|
|
|
154
|
740
|
|
|
|
|
1287
|
while (1) { |
155
|
|
|
|
|
|
|
my $rlen = $self->read_timeout( |
156
|
|
|
|
|
|
|
$conn, \$buf, MAX_REQUEST_SIZE - length($buf), length($buf), |
157
|
|
|
|
|
|
|
$self->{timeout}, |
158
|
740
|
100
|
|
|
|
4305
|
) or return; |
159
|
703
|
|
|
|
|
10208
|
my $reqlen = parse_http_request($buf, $env); |
160
|
703
|
50
|
|
|
|
2492
|
if ($reqlen >= 0) { |
161
|
703
|
|
|
|
|
1432
|
$buf = substr $buf, $reqlen; |
162
|
703
|
100
|
|
|
|
1734
|
if (my $cl = $env->{CONTENT_LENGTH}) { |
163
|
74
|
|
|
|
|
3048
|
my $buffer = Stream::Buffered->new($cl); |
164
|
74
|
|
|
|
|
5726
|
while ($cl > 0) { |
165
|
108
|
|
|
|
|
458
|
my $chunk; |
166
|
108
|
100
|
|
|
|
535
|
if (length $buf) { |
167
|
5
|
|
|
|
|
60
|
$chunk = $buf; |
168
|
5
|
|
|
|
|
35
|
$buf = ''; |
169
|
|
|
|
|
|
|
} else { |
170
|
|
|
|
|
|
|
$self->read_timeout($conn, \$chunk, $cl, 0, $self->{timeout}) |
171
|
103
|
50
|
|
|
|
589
|
or return; |
172
|
|
|
|
|
|
|
} |
173
|
108
|
|
|
|
|
955
|
$buffer->print($chunk); |
174
|
108
|
|
|
|
|
9024
|
$cl -= length $chunk; |
175
|
|
|
|
|
|
|
} |
176
|
74
|
|
|
|
|
866
|
$env->{'psgi.input'} = $buffer->rewind; |
177
|
|
|
|
|
|
|
} else { |
178
|
629
|
|
|
37
|
|
12811
|
open my $input, "<", \$buf; |
|
37
|
|
|
|
|
370
|
|
|
37
|
|
|
|
|
37
|
|
|
37
|
|
|
|
|
777
|
|
179
|
629
|
|
|
|
|
44662
|
$env->{'psgi.input'} = $input; |
180
|
|
|
|
|
|
|
} |
181
|
|
|
|
|
|
|
|
182
|
703
|
|
|
|
|
7705
|
$res = Plack::Util::run_app $app, $env; |
183
|
666
|
|
|
|
|
3147
|
last; |
184
|
|
|
|
|
|
|
} |
185
|
0
|
0
|
|
|
|
0
|
if ($reqlen == -2) { |
|
|
0
|
|
|
|
|
|
186
|
|
|
|
|
|
|
# request is incomplete, do nothing |
187
|
|
|
|
|
|
|
} elsif ($reqlen == -1) { |
188
|
|
|
|
|
|
|
# error, close conn |
189
|
0
|
|
|
|
|
0
|
last; |
190
|
|
|
|
|
|
|
} |
191
|
|
|
|
|
|
|
} |
192
|
|
|
|
|
|
|
|
193
|
666
|
50
|
|
|
|
3619
|
if (ref $res eq 'ARRAY') { |
|
|
0
|
|
|
|
|
|
194
|
666
|
|
|
|
|
7384
|
$self->_handle_response($res, $conn); |
195
|
|
|
|
|
|
|
} elsif (ref $res eq 'CODE') { |
196
|
|
|
|
|
|
|
$res->(sub { |
197
|
0
|
|
|
0
|
|
0
|
$self->_handle_response($_[0], $conn); |
198
|
0
|
|
|
|
|
0
|
}); |
199
|
|
|
|
|
|
|
} else { |
200
|
0
|
|
|
|
|
0
|
die "Bad response $res"; |
201
|
|
|
|
|
|
|
} |
202
|
|
|
|
|
|
|
|
203
|
666
|
|
|
|
|
5928
|
return; |
204
|
|
|
|
|
|
|
} |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
sub _handle_response { |
207
|
666
|
|
|
666
|
|
4392
|
my($self, $res, $conn) = @_; |
208
|
|
|
|
|
|
|
|
209
|
666
|
|
|
|
|
2069
|
my @lines = ( |
210
|
666
|
|
|
|
|
9358
|
"Date: @{[HTTP::Date::time2str()]}\015\012", |
211
|
|
|
|
|
|
|
"Server: $self->{server_software}\015\012", |
212
|
|
|
|
|
|
|
); |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
Plack::Util::header_iter($res->[1], sub { |
215
|
1487
|
|
|
1487
|
|
3979
|
my ($k, $v) = @_; |
216
|
1487
|
|
|
|
|
9187
|
push @lines, "$k: $v\015\012"; |
217
|
666
|
|
|
|
|
42296
|
}); |
218
|
|
|
|
|
|
|
|
219
|
666
|
|
|
|
|
4391
|
unshift @lines, "HTTP/1.0 $res->[0] @{[ HTTP::Status::status_message($res->[0]) ]}\015\012"; |
|
666
|
|
|
|
|
13271
|
|
220
|
666
|
|
|
|
|
12022
|
push @lines, "\015\012"; |
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
$self->write_all($conn, join('', @lines), $self->{timeout}) |
223
|
666
|
50
|
|
|
|
8192
|
or return; |
224
|
|
|
|
|
|
|
|
225
|
666
|
50
|
|
|
|
2157
|
if (defined $res->[2]) { |
226
|
666
|
|
|
|
|
1934
|
my $err; |
227
|
|
|
|
|
|
|
my $done; |
228
|
|
|
|
|
|
|
{ |
229
|
666
|
|
|
|
|
901
|
local $@; |
|
666
|
|
|
|
|
1047
|
|
230
|
666
|
|
|
|
|
1594
|
eval { |
231
|
|
|
|
|
|
|
Plack::Util::foreach( |
232
|
|
|
|
|
|
|
$res->[2], |
233
|
|
|
|
|
|
|
sub { |
234
|
|
|
|
|
|
|
$self->write_all($conn, $_[0], $self->{timeout}) |
235
|
721
|
50
|
|
721
|
|
3690
|
or die "failed to send all data\n"; |
236
|
|
|
|
|
|
|
}, |
237
|
666
|
|
|
|
|
8766
|
); |
238
|
666
|
|
|
|
|
2963
|
$done = 1; |
239
|
|
|
|
|
|
|
}; |
240
|
666
|
|
|
|
|
2666
|
$err = $@; |
241
|
|
|
|
|
|
|
}; |
242
|
666
|
50
|
|
|
|
2670
|
unless ($done) { |
243
|
0
|
0
|
|
|
|
0
|
if ($err =~ /^failed to send all data\n/) { |
244
|
0
|
|
|
|
|
0
|
return; |
245
|
|
|
|
|
|
|
} else { |
246
|
0
|
|
|
|
|
0
|
die $err; |
247
|
|
|
|
|
|
|
} |
248
|
|
|
|
|
|
|
} |
249
|
|
|
|
|
|
|
} else { |
250
|
|
|
|
|
|
|
return Plack::Util::inline_object |
251
|
0
|
|
|
0
|
|
0
|
write => sub { $self->write_all($conn, $_[0], $self->{timeout}) }, |
252
|
0
|
|
|
0
|
|
0
|
close => sub { }; |
253
|
|
|
|
|
|
|
} |
254
|
|
|
|
|
|
|
} |
255
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
# returns 1 if socket is ready, undef on timeout |
257
|
|
|
|
|
|
|
sub do_timeout { |
258
|
2230
|
|
|
2230
|
0
|
8825
|
my ($self, $cb, $timeout) = @_; |
259
|
2230
|
|
|
0
|
|
51854
|
local $SIG{ALRM} = sub {}; |
260
|
2230
|
|
|
|
|
12863
|
my $wait_until = time + $timeout; |
261
|
2230
|
|
|
|
|
17540
|
alarm($timeout); |
262
|
2230
|
|
|
|
|
4077
|
my $ret; |
263
|
2230
|
|
|
|
|
2787
|
while (1) { |
264
|
2230
|
100
|
33
|
|
|
5152
|
if ($ret = $cb->()) { |
|
|
50
|
|
|
|
|
|
265
|
2193
|
|
|
|
|
222944
|
last; |
266
|
|
|
|
|
|
|
} elsif (! (! defined($ret) && $! == EINTR)) { |
267
|
37
|
|
|
|
|
1924
|
undef $ret; |
268
|
37
|
|
|
|
|
111
|
last; |
269
|
|
|
|
|
|
|
} |
270
|
|
|
|
|
|
|
# got EINTR |
271
|
0
|
|
|
|
|
0
|
my $left = $wait_until - time; |
272
|
0
|
0
|
|
|
|
0
|
last if $left <= 0; |
273
|
0
|
|
|
|
|
0
|
alarm($left + $alarm_interval); |
274
|
|
|
|
|
|
|
} |
275
|
2230
|
|
|
|
|
16045
|
alarm(0); |
276
|
2230
|
|
|
|
|
36934
|
$ret; |
277
|
|
|
|
|
|
|
} |
278
|
|
|
|
|
|
|
|
279
|
|
|
|
|
|
|
# returns (positive) number of bytes read, or undef if the socket is to be closed |
280
|
|
|
|
|
|
|
sub read_timeout { |
281
|
843
|
|
|
843
|
0
|
2726
|
my ($self, $sock, $buf, $len, $off, $timeout) = @_; |
282
|
843
|
|
|
843
|
|
9985
|
$self->do_timeout(sub { $sock->sysread($$buf, $len, $off) }, $timeout); |
|
843
|
|
|
|
|
5295
|
|
283
|
|
|
|
|
|
|
} |
284
|
|
|
|
|
|
|
|
285
|
|
|
|
|
|
|
# returns (positive) number of bytes written, or undef if the socket is to be closed |
286
|
|
|
|
|
|
|
sub write_timeout { |
287
|
1387
|
|
|
1387
|
0
|
3667
|
my ($self, $sock, $buf, $len, $off, $timeout) = @_; |
288
|
1387
|
|
|
1387
|
|
11156
|
$self->do_timeout(sub { $sock->syswrite($buf, $len, $off) }, $timeout); |
|
1387
|
|
|
|
|
13924
|
|
289
|
|
|
|
|
|
|
} |
290
|
|
|
|
|
|
|
|
291
|
|
|
|
|
|
|
# writes all data in buf and returns number of bytes written or undef if failed |
292
|
|
|
|
|
|
|
sub write_all { |
293
|
1387
|
|
|
1387
|
0
|
4432
|
my ($self, $sock, $buf, $timeout) = @_; |
294
|
1387
|
50
|
|
|
|
3390
|
return 0 unless defined $buf; |
295
|
1387
|
|
|
|
|
3359
|
_encode($buf); |
296
|
1387
|
|
|
|
|
2809
|
my $off = 0; |
297
|
1387
|
|
|
|
|
5250
|
while (my $len = length($buf) - $off) { |
298
|
1387
|
50
|
|
|
|
5556
|
my $ret = $self->write_timeout($sock, $buf, $len, $off, $timeout) |
299
|
|
|
|
|
|
|
or return; |
300
|
1387
|
|
|
|
|
6825
|
$off += $ret; |
301
|
|
|
|
|
|
|
} |
302
|
1387
|
|
|
|
|
4993
|
return length $buf; |
303
|
|
|
|
|
|
|
} |
304
|
|
|
|
|
|
|
|
305
|
|
|
|
|
|
|
# syswrite() will crash when given wide characters |
306
|
|
|
|
|
|
|
sub _encode { |
307
|
1387
|
50
|
|
1387
|
|
54150
|
if ($_[0] =~ /[^\x00-\xff]/) { |
308
|
0
|
|
|
|
|
0
|
Carp::carp("Wide character outside byte range in response. Encoding data as UTF-8"); |
309
|
0
|
|
|
|
|
0
|
utf8::encode($_[0]); |
310
|
|
|
|
|
|
|
} |
311
|
|
|
|
|
|
|
} |
312
|
|
|
|
|
|
|
|
313
|
|
|
|
|
|
|
1; |
314
|
|
|
|
|
|
|
|
315
|
|
|
|
|
|
|
__END__ |