line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# You may distribute under the terms of either the GNU General Public License |
2
|
|
|
|
|
|
|
# or the Artistic License (the same terms as Perl itself) |
3
|
|
|
|
|
|
|
# |
4
|
|
|
|
|
|
|
# (C) Paul Evans, 2013-2015 -- leonerd@leonerd.org.uk |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
package Net::Async::HTTP::Server::Protocol; |
7
|
|
|
|
|
|
|
|
8
|
12
|
|
|
12
|
|
84
|
use strict; |
|
12
|
|
|
|
|
21
|
|
|
12
|
|
|
|
|
292
|
|
9
|
12
|
|
|
12
|
|
50
|
use warnings; |
|
12
|
|
|
|
|
24
|
|
|
12
|
|
|
|
|
263
|
|
10
|
12
|
|
|
12
|
|
50
|
use base qw( IO::Async::Stream ); |
|
12
|
|
|
|
|
21
|
|
|
12
|
|
|
|
|
2844
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our $VERSION = '0.13'; |
13
|
|
|
|
|
|
|
|
14
|
12
|
|
|
12
|
|
83515
|
use Carp; |
|
12
|
|
|
|
|
24
|
|
|
12
|
|
|
|
|
617
|
|
15
|
12
|
|
|
12
|
|
73
|
use Scalar::Util qw( weaken ); |
|
12
|
|
|
|
|
23
|
|
|
12
|
|
|
|
|
507
|
|
16
|
|
|
|
|
|
|
|
17
|
12
|
|
|
12
|
|
4794
|
use HTTP::Request; |
|
12
|
|
|
|
|
164828
|
|
|
12
|
|
|
|
|
4504
|
|
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
my $CRLF = "\x0d\x0a"; |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub on_read |
22
|
|
|
|
|
|
|
{ |
23
|
54
|
|
|
54
|
1
|
44215
|
my $self = shift; |
24
|
54
|
|
|
|
|
114
|
my ( $buffref, $eof ) = @_; |
25
|
|
|
|
|
|
|
|
26
|
54
|
100
|
|
|
|
146
|
return 0 if $eof; |
27
|
|
|
|
|
|
|
|
28
|
48
|
100
|
|
|
|
500
|
return 0 unless $$buffref =~ s/^(.*?$CRLF$CRLF)//s; |
29
|
25
|
|
|
|
|
96
|
my $header = $1; |
30
|
|
|
|
|
|
|
|
31
|
25
|
|
|
|
|
173
|
my $request = HTTP::Request->parse( $header ); |
32
|
25
|
50
|
33
|
|
|
37157
|
unless( $request and defined $request->protocol and $request->protocol =~ m/^HTTP/ ) { |
|
|
|
33
|
|
|
|
|
33
|
0
|
|
|
|
|
0
|
$self->close_now; |
34
|
0
|
|
|
|
|
0
|
return 0; |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
|
37
|
25
|
|
100
|
|
|
878
|
my $request_body_len = $request->content_length || 0; |
38
|
|
|
|
|
|
|
|
39
|
25
|
|
|
|
|
1328
|
$self->debug_printf( "REQUEST %s %s", $request->method, $request->uri->path ); |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
return sub { |
42
|
26
|
|
|
26
|
|
1657
|
my ( undef, $buffref, $eof ) = @_; |
43
|
|
|
|
|
|
|
|
44
|
26
|
100
|
|
|
|
91
|
return 0 unless length($$buffref) >= $request_body_len; |
45
|
|
|
|
|
|
|
|
46
|
24
|
|
|
|
|
170
|
$request->add_content( substr( $$buffref, 0, $request_body_len, "" ) ); |
47
|
|
|
|
|
|
|
|
48
|
24
|
|
|
|
|
437
|
push @{ $self->{requests} }, my $req = $self->parent->make_request( $self, $request ); |
|
24
|
|
|
|
|
106
|
|
49
|
24
|
|
|
|
|
117
|
weaken( $self->{requests}[-1] ); |
50
|
|
|
|
|
|
|
|
51
|
24
|
|
|
|
|
68
|
$self->parent->_received_request( $req ); |
52
|
|
|
|
|
|
|
|
53
|
24
|
|
|
|
|
237
|
return undef; |
54
|
25
|
|
|
|
|
1200
|
}; |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub on_closed |
58
|
|
|
|
|
|
|
{ |
59
|
8
|
|
|
8
|
1
|
16
|
my $self = shift; |
60
|
|
|
|
|
|
|
|
61
|
8
|
|
33
|
|
|
15
|
$_ and $_->_close for @{ $self->{requests} }; |
|
8
|
|
|
|
|
30
|
|
62
|
8
|
|
|
|
|
13
|
undef @{ $self->{requests} }; |
|
8
|
|
|
|
|
22
|
|
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
sub _flush_requests |
66
|
|
|
|
|
|
|
{ |
67
|
58
|
|
|
58
|
|
130
|
my $self = shift; |
68
|
|
|
|
|
|
|
|
69
|
58
|
|
|
|
|
88
|
my $queue = $self->{requests}; |
70
|
58
|
|
|
|
|
120
|
while( @$queue ) { |
71
|
59
|
|
|
|
|
77
|
my $req = $queue->[0]; |
72
|
59
|
50
|
|
|
|
112
|
$req or shift @$queue, next; |
73
|
|
|
|
|
|
|
|
74
|
59
|
|
|
|
|
117
|
my $is_done = $req->_write_to_stream( $self ); |
75
|
|
|
|
|
|
|
|
76
|
59
|
100
|
|
|
|
278
|
$is_done ? shift @$queue : return; |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
0x55AA; |