Perlbal/ClientHTTP.pm

filter: Statementstmt 92.1% 248 / 269 Branchbran 68.5% 85 / 124 Conditioncond 61.8% 34 / 55 MC/DCmcdc 40.6% 13 / 32 Subroutinesub 97.3% 36 / 37 Podpod 18.7% 3 / 16 totaltotal 78.6% 419 / 533 CC 82 SCAR 21.2
CC
82
Cov
82%
handle_put
23.5
__ANON__
11.1
put_writeout
9.5
CRAP 8.3
« Perlbal/Plugin/Vhosts.pm ↑ Summary Perlbal/Plugin/Include.pm »
1 ######################################################################
2 # HTTP Connection from a reverse proxy client. GET/HEAD only.
3 # most functionality is implemented in the base class.
4 #
5 # Copyright 2004, Danga Interactive, Inc.
6 # Copyright 2005-2007, Six Apart, Ltd.
7 #
8  
9 package Perlbal::ClientHTTP;
10 22 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 22 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 22 ▶no warnings qw(deprecated);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 22 ▶use base "Perlbal::ClientHTTPBase";
Subroutine BEGIN: called CC=1 SCAR=0.0
15 22 ▶use Perlbal::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 22 ▶use fields ('put_in_progress', # 1 when we're currently waiting for an async job to return
Subroutine BEGIN: called CC=1 SCAR=0.0
18 'put_fh', # file handle to use for writing data
19 'put_fh_filename', # filename of put_fh
20 'put_final_name', # final pathname of put_fh
21 'put_pos', # file offset to write next data at
22  
23 'content_length', # length of document being transferred
24 'content_length_remain', # bytes remaining to be read
25 'chunked_upload_state', # bool/obj: if processing a chunked upload, Perlbal::ChunkedUploadState object, else undef
26 'md5_ctx', # Digest::MD5 used to verify Content-MD5
27 22 );
28  
29 22 ▶use HTTP::Date ();
Subroutine BEGIN: called CC=1 SCAR=0.0
30 22 ▶use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 22 ▶use Errno qw( EPIPE );
Subroutine BEGIN: called CC=1 SCAR=0.0
33 22 ▶use POSIX qw( O_CREAT O_TRUNC O_WRONLY O_RDONLY O_EXCL ENOENT EEXIST );
Subroutine BEGIN: called CC=1 SCAR=0.0
34 22 ▶use Digest::MD5;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 # class list of directories we know exist
37 22 our (%VerifiedDirs);
38  
39 44 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
40 44 my $class = shift;
41  
42 44 my $self = fields::new($class);
43 44 $self->SUPER::new(@_);
44 44 $self->init;
45 44 return $self;
46 }
47  
48 # upcasting a generic ClientHTTPBase (from a service selector) to a
49 # "full-fledged" ClientHTTP.
50 7 ▶sub new_from_base {
Subroutine new_from_base: called CC=1 SCAR=0.0
Pod undocumented
51 7 my $class = shift;
52 7 my Perlbal::ClientHTTPBase $cb = shift; # base object
53 7 Perlbal::Util::rebless($cb, $class);
54 7 $cb->init;
55  
56 7 $cb->watch_read(1); # enable our reads, so we can get PUT/POST data
57 7 $cb->handle_request; # this will disable reads, if GET/HEAD/etc
58 7 return $cb;
59 }
60  
61 51 ▶sub init {
Subroutine init: called CC=1 SCAR=0.0
Pod undocumented
62 51 my Perlbal::ClientHTTP $self = shift;
63 51 $self->{put_in_progress} = 0;
64 51 $self->{put_fh} = undef;
65 51 $self->{put_pos} = 0;
66 51 $self->{chunked_upload_state} = undef;
67 51 $self->{md5_ctx} = undef;
68 51 $self->{put_final_name} = undef;
69 }
70  
71 42 ▶sub close {
Subroutine close: called CC=2 SCAR=7.0
Pod documented
72 42 my Perlbal::ClientHTTP $self = shift;
73  
74 # don't close twice
75 42 ▶ return if $self->{closed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"closed"} 0 42 42
76  
77 42 $self->{put_fh} = undef;
78 42 $self->SUPER::close(@_);
79 }
80  
81 72 ▶sub setup_keepalive {
Subroutine setup_keepalive: called CC=3 SCAR=11.0
Pod undocumented
82 72 my Perlbal::ClientHTTP $self = $_[0];
83 72 ▶ my $not_done_reading = defined $self->{content_length_remain} && $self->{content_length_remain} > 0;
Condition100%3/3
defined $self->{'content_length_remain'} && $self->{'content_length_remain'} > 0
!ll&&!rl&&r
59121
MC/DC100%2/2
defined $self->{'content_length_remain'} && $self->{'content_length_remain'} > 0
defined $self->{'content_length_remain'} $self->{'content_length_remain'} > 0
Truth table100%3/3
A && B
A= defined $self->{'content_length_remain'}B= $self->{'content_length_remain'} > 0
ABresult
0X0
100
111
84  
85 72 ▶ return $self->SUPER::setup_keepalive($_[1], $not_done_reading ? 0 : undef);
Branch: 1 branch
BranchTrueFalseTotal
$not_done_reading ? : 1 71 72
86 }
87  
88 87 ▶sub event_read {
Subroutine event_read: called CC=4 SCAR=14.0
Pod documented
89 87 my Perlbal::ClientHTTP $self = shift;
90 87 $self->{alive_time} = $Perlbal::tick_time;
91  
92 # see if we have headers?
93 87 ▶ if ($self->{req_headers}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"req_headers"}) 18 69 87
94 18 ▶ if ($self->{req_headers}->request_method eq 'PUT') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'req_headers'}->request_method eq 'PUT') { } 18 0 18
95 18 $self->event_read_put;
96 } else {
97 # since we have headers and we're not doing any special
98 # handling above, let's just disable read notification, because
99 # we won't do anything with the data
100 0 $self->watch_read(0);
101 }
102 18 return;
103 }
104  
105 # try and get the headers, if they're all here
106 69 ▶ my $hd = $self->read_request_headers
Branch: 1 branch
BranchTrueFalseTotal
my $hd = $self->read_request_headers or return 65 4 69
107 or return;
108  
109 65 $self->handle_request;
110 }
111  
112 # one-time routing of new request to the right handlers
113 72 ▶sub handle_request {
Subroutine handle_request: called CC=7 SCAR=19.6
Pod undocumented
114 72 my Perlbal::ClientHTTP $self = shift;
115 72 my $hd = $self->{req_headers};
116  
117 72 $self->check_req_headers;
118  
119 # fully formed request received
120 72 $self->{requests}++;
121  
122 # notify that we're about to serve
123 72 ▶ return if $self->{service}->run_hook('start_web_request', $self);
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"service"}->run_hook("start_web_request", $self) 0 72 72
124 72 ▶ return if $self->{service}->run_hook('start_http_request', $self);
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"service"}->run_hook("start_http_request", $self) 1 71 72
125  
126 # GET/HEAD requests (local, from disk)
127 71 ▶ if ($hd->request_method eq 'GET' || $hd->request_method eq 'HEAD') {
Branch: 1 branch
BranchTrueFalseTotal
if ($hd->request_method eq "GET" or $hd->request_method eq "HEAD") 54 17 71
Condition100%3/3
$hd->request_method eq "GET" or $hd->request_method eq "HEAD"
l!l&&r!l&&!r
53117
MC/DC100%2/2
$hd->request_method eq "GET" or $hd->request_method eq "HEAD"
$hd->request_method eq "GET" $hd->request_method eq "HEAD"
Truth table100%3/3
A or B
A= $hd->request_method eq "GET"B= $hd->request_method eq "HEAD"
ABresult
1X1
011
000
128 # and once we have it, start serving
129 54 $self->watch_read(0);
130 54 return $self->_serve_request($hd);
131 }
132  
133 # PUT requests
134 17 ▶ return $self->handle_put if $hd->request_method eq 'PUT';
Branch: 1 branch
BranchTrueFalseTotal
if $hd->request_method eq "PUT" 14 3 17
135  
136 # DELETE requests
137 3 ▶ return $self->handle_delete if $hd->request_method eq 'DELETE';
Branch: 1 branch
BranchTrueFalseTotal
if $hd->request_method eq "DELETE" 3 0 3
138  
139 # else, bad request
140 0 return $self->send_response(400);
141 }
142  
143 14 ▶sub handle_put {
Subroutine handle_put: called CC=19 SCAR=31.6
Pod undocumented
144 14 my Perlbal::ClientHTTP $self = shift;
145 14 my $hd = $self->{req_headers};
146  
147 14 ▶ return $self->send_response(403) unless $self->{service}->{enable_put};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"service"}{"enable_put"} 1 13 14
148  
149 13 ▶ $self->{md5_ctx} = $self->{service}->{enable_md5} && $hd->header('Content-MD5') ? Digest::MD5->new : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'service'}{'enable_md5'} && $hd->header('Content-MD5') ? : 2 11 13
Condition100%3/3
$self->{"service"}{"enable_md5"} and $hd->header("Content-MD5")
!ll&&!rl&&r
1102
MC/DC100%2/2
$self->{"service"}{"enable_md5"} and $hd->header("Content-MD5")
$self->{"service"}{"enable_md5"} $hd->header("Content-MD5")
Truth table100%3/3
A and B
A= $self->{"service"}{"enable_md5"}B= $hd->header("Content-MD5")
ABresult
0X0
100
111
150  
151 13 ▶ return if $self->handle_put_chunked;
Branch: 1 branch
BranchTrueFalseTotal
if $self->handle_put_chunked 1 12 13
152  
153 # they want to put something, so let's setup and wait for more reads
154 my $clen =
155 $self->{content_length} =
156 $self->{content_length_remain} =
157 12 $hd->header('Content-length') + 0;
158  
159 # return a 400 (bad request) if we got no content length or if it's
160 # bigger than any specified max put size
161 return $self->send_response(400, "Content-length of $clen is invalid.")
162 if ! defined($clen) ||
163 $clen < 0 ||
164 ($self->{service}->{max_put_size} &&
165 12 ▶ $clen > $self->{service}->{max_put_size});
Branch: 1 branch
BranchTrueFalseTotal
if not defined $clen or $clen < 0 or $self->{"service"}{"max_put_size"} and $clen > $self->{"service"}{"max_put_size"} 0 12 12
Condition33%3/9
not defined $clen or $clen < 0 or $self->{"service"}{"max_put_size"} and $clen > $self->{"service"}{"max_put_size"}
l!l&&r!l&&!r
0012
not defined $clen or $clen < 0
l!l&&r!l&&!r
0012
$self->{"service"}{"max_put_size"} and $clen > $self->{"service"}{"max_put_size"}
!ll&&!rl&&r
1200
MC/DC0%0/4
not defined $clen or $clen < 0 or $self->{"service"}{"max_put_size"} and $clen > $self->{"service"}{"max_put_size"}
not defined $clen $clen < 0 $self->{"service"}{"max_put_size"} $clen > $self->{"service"}{"max_put_size"}
Truth table20%1/5
not(A) or B or C and D
A= not defined $clenB= $clen < 0C= $self->{"service"}{"max_put_size"}D= $clen > $self->{"service"}{"max_put_size"}
ABCDresult
1XXX1
01XX1
00111
000X0
00100
166  
167 # if we are supposed to read data and have some data already from a header over-read, note it
168 12 ▶ if ($clen && defined $self->{read_ahead} && $self->{read_ahead} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($clen and defined $self->{"read_ahead"} and $self->{"read_ahead"} > 0) 0 12 12
Condition66%4/6
$clen and defined $self->{"read_ahead"} and $self->{"read_ahead"} > 0
!ll&&!rl&&r
1110
$clen and defined $self->{"read_ahead"}
!ll&&!rl&&r
1011
MC/DC0%0/3
$clen and defined $self->{"read_ahead"} and $self->{"read_ahead"} > 0
$clen defined $self->{"read_ahead"} $self->{"read_ahead"} > 0
Truth table50%2/4
A and B and C
A= $clenB= defined $self->{"read_ahead"}C= $self->{"read_ahead"} > 0
ABCresult
0XX0
10X0
1100
1111
169 0 $self->{content_length_remain} -= $self->{read_ahead};
170 }
171  
172 12 ▶ return if $self->{service}->run_hook('handle_put', $self);
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"service"}->run_hook("handle_put", $self) 0 12 12
173  
174 # error in filename? (any .. is an error)
175 12 my $uri = $self->{req_headers}->request_uri;
176 12 ▶ return $self->send_response(400, 'Invalid filename')
Branch: 1 branch
BranchTrueFalseTotal
if $uri =~ /\.\./ 0 12 12
177 if $uri =~ /\.\./;
178  
179 # now we want to get the URI
180 12 ▶ return $self->send_response(400, 'Invalid filename')
Branch: 1 branch
BranchTrueFalseTotal
unless $uri =~ m[^ ((?:/[\w\-\.]+)*) # $1: zero+ path components of /FOO where foo is # one+ conservative characters / # path separator ([\w\-\.]+) # $2: and the filename, one+ conservative characters $]x 0 12 12
181 unless $uri =~ m!^
182 ((?:/[\w\-\.]+)*) # $1: zero+ path components of /FOO where foo is
183 # one+ conservative characters
184 / # path separator
185 ([\w\-\.]+) # $2: and the filename, one+ conservative characters
186 $!x;
187  
188 # sanitize uri into path and file into a disk path and filename
189 12 ▶ my ($path, $filename) = ($1 || '', $2);
Condition100%2/2
$1 || ''
l!l
93
MC/DC100%1/1
$1 || ''
$1
Truth table100%2/2
A
A= $1
Aresult
11
00
190  
191 # the final action we'll be taking, eventually, is to start an async
192 # file open of the requested disk path. but we might need to verify
193 # the min_put_directory first.
194 11 ▶ my $start_open = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
195 11 my $disk_path = $self->{service}->{docroot} . '/' . $path;
196 11 $self->start_put_open($disk_path, $filename);
197 12 };
198  
199 # verify minput if necessary
200 12 ▶ if ($self->{service}->{min_put_directory}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'service'}{'min_put_directory'}) { } 3 9 12
201 3 ▶ my @elems = grep { defined $_ && length $_ } split '/', $path;
Condition66%2/3
defined $_ && length $_
!ll&&!rl&&r
036
MC/DC50%1/2
defined $_ && length $_
defined $_ length $_
Truth table66%2/3
A && B
A= defined $_B= length $_
ABresult
0X0
100
111
202 return $self->send_response(400, 'Does not meet minimum directory requirement')
203 3 ▶ unless scalar(@elems) >= $self->{service}->{min_put_directory};
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @elems >= $self->{"service"}{"min_put_directory"} 0 3 3
204 3 my $req_path = '/' . join('/', splice(@elems, 0, $self->{service}->{min_put_directory}));
205 3 my $extra_path = '/' . join('/', @elems);
206 3 $self->validate_min_put_directory($req_path, $extra_path, $filename, $start_open);
207 } else {
208 9 $start_open->();
209 }
210  
211 12 return;
212 }
213  
214 13 ▶sub handle_put_chunked {
Subroutine handle_put_chunked: called CC=8 SCAR=22.0
Pod undocumented
215 13 my Perlbal::ClientHTTP $self = shift;
216 13 my $req_hd = $self->{req_headers};
217 13 my $te = $req_hd->header("Transfer-Encoding");
218 13 ▶ return unless $te && $te eq "chunked";
Branch: 1 branch
BranchTrueFalseTotal
unless $te and $te eq "chunked" 12 1 13
Condition66%2/3
$te and $te eq "chunked"
!ll&&!rl&&r
1201
MC/DC50%1/2
$te and $te eq "chunked"
$te $te eq "chunked"
Truth table66%2/3
A and B
A= $teB= $te eq "chunked"
ABresult
0X0
100
111
219  
220 1 my $eh = $req_hd->header("Expect");
221 1 ▶ if ($eh && $eh =~ /\b100-continue\b/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($eh and $eh =~ /\b100-continue\b/) 1 0 1
Condition33%1/3
$eh and $eh =~ /\b100-continue\b/
!ll&&!rl&&r
001
MC/DC0%0/2
$eh and $eh =~ /\b100-continue\b/
$eh $eh =~ /\b100-continue\b/
Truth table33%1/3
A and B
A= $ehB= $eh =~ /\b100-continue\b/
ABresult
0X0
100
111
222 1 $self->write(\ "HTTP/1.1 100 Continue\r\n\r\n");
223 }
224  
225 1 my $max_size = $self->{service}{max_chunked_request_size};
226  
227 # error in filename? (any .. is an error)
228 1 my $uri = $self->{req_headers}->request_uri;
229 1 ▶ return $self->send_response(400, 'Invalid filename')
Branch: 1 branch
BranchTrueFalseTotal
if $uri =~ /\.\./ 0 1 1
230 if $uri =~ /\.\./;
231  
232 # now we want to get the URI
233 1 ▶ return $self->send_response(400, 'Invalid filename')
Branch: 1 branch
BranchTrueFalseTotal
unless $uri =~ m[^ ((?:/[\w\-\.]+)*) # $1: zero+ path components of /FOO where foo is # one+ conservative characters / # path separator ([\w\-\.]+) # $2: and the filename, one+ conservative characters $]x 0 1 1
234 unless $uri =~ m!^
235 ((?:/[\w\-\.]+)*) # $1: zero+ path components of /FOO where foo is
236 # one+ conservative characters
237 / # path separator
238 ([\w\-\.]+) # $2: and the filename, one+ conservative characters
239 $!x;
240  
241 # sanitize uri into path and file into a disk path and filename
242 1 ▶ my ($path, $filename) = ($1 || '', $2);
Condition50%1/2
$1 || ''
l!l
01
MC/DC0%0/1
$1 || ''
$1
Truth table50%1/2
A
A= $1
Aresult
11
00
243  
244 1 my $disk_path = $self->{service}->{docroot} . '/' . $path;
245  
246 1 $self->{chunked_upload_state} = Perlbal::ChunkedUploadState->new(%{{
247 12 ▶ on_new_chunk => sub {
Subroutine __ANON__: called CC=5 SCAR=17.4
248 12 my $cref = shift;
249 12 my $len = length($$cref);
250 12 push @{$self->{read_buf}}, $cref;
251  
252 12 $self->{read_ahead} += $len;
253 12 $self->{content_length} += $len;
254  
255 # if too large, disconnect them...
256 12 ▶ if ($max_size && $self->{content_length} > $max_size) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max_size and $self->{"content_length"} > $max_size) 0 12 12
Condition33%1/3
$max_size and $self->{"content_length"} > $max_size
!ll&&!rl&&r
0120
MC/DC0%0/2
$max_size and $self->{"content_length"} > $max_size
$max_size $self->{"content_length"} > $max_size
Truth table33%1/3
A and B
A= $max_sizeB= $self->{"content_length"} > $max_size
ABresult
0X0
100
111
257 # TODO: delete file at this point? we're disconnecting them
258 # to prevent them from writing more, but do we care to keep
259 # what they already wrote?
260 0 $self->close;
261 0 return;
262 }
263  
264 # Reading too far ahead of our AIO subsystem will cause us to buffer it in memory.
265 12 ▶ $self->watch_read(0) if $self->{read_ahead} >= 1024 * 1024; # arbitrary
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"read_ahead"} >= 1048576 0 12 12
266 # ->put_writeout clears {read_ahead}, so we run it after we need that
267 12 ▶ $self->put_writeout if $self->{read_ahead} >= 8192; # arbitrary
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"read_ahead"} >= 8192 7 5 12
268 },
269 0 ▶ on_disconnect => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
270 0 warn "Disconnect during chunked PUT.\n";
271  
272 # TODO: do we unlink the file here, since it wasn't a proper close
273 # ending in a zero-length chunk? perhaps a config option? for
274 # now we'll just leave it on disk with what we've got so far:
275 0 $self->close('remote_closure_during_chunked_put');
276 },
277 1 ▶ on_zero_chunk => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
278 1 $self->{chunked_upload_state} = undef;
279 1 $self->watch_read(0);
280  
281 # kick off any necessary aio writes:
282 1 $self->put_writeout;
283 # this will do nothing, if a put is already in progress:
284 1 $self->put_close;
285 },
286 1 }});
287  
288 1 $self->start_put_open($disk_path, $filename);
289  
290 1 return 1;
291 }
292  
293 # called when we're requested to do a delete
294 3 ▶sub handle_delete {
Subroutine handle_delete: called CC=4 SCAR=14.3
Pod undocumented
295 3 my Perlbal::ClientHTTP $self = shift;
296  
297 3 ▶ return $self->send_response(403) unless $self->{service}->{enable_delete};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"service"}{"enable_delete"} 1 2 3
298  
299 2 $self->watch_read(0);
300  
301 # error in filename? (any .. is an error)
302 2 my $uri = $self->{req_headers}->request_uri;
303 2 ▶ return $self->send_response(400, 'Invalid filename')
Branch: 1 branch
BranchTrueFalseTotal
if $uri =~ /\.\./ 0 2 2
304 if $uri =~ /\.\./;
305  
306 # now we want to get the URI
307 2 ▶ if ($uri =~ m!^(?:/[\w\-\.]+)+$!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uri =~ m[^(?:/[\w\-\.]+)+$]) { } 2 0 2
308 # now attempt the unlink
309 2 ▶ Perlbal::AIO::aio_unlink($self->{service}->{docroot} . '/' . $uri, sub {
Subroutine __ANON__: called CC=3 SCAR=11.3
310 2 my $err = shift;
311 2 ▶ if ($err == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($err == 0) { } 1 1 2
elsif ($! == 2) { } 1 0 1
312 # delete was successful
313 1 return $self->send_response(204);
314 } elsif ($! == ENOENT) {
315 # no such file
316 1 return $self->send_response(404);
317 } else {
318 # failure...
319 0 return $self->send_response(400, "$!");
320 }
321 2 });
322 } else {
323 # bad URI, don't accept the delete
324 0 return $self->send_response(400, 'Invalid filename');
325 }
326 }
327  
328 18 ▶sub event_read_put {
Subroutine event_read_put: called CC=6 SCAR=20.1
Pod undocumented
329 18 my Perlbal::ClientHTTP $self = shift;
330  
331 18 ▶ if (my $cus = $self->{chunked_upload_state}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cus = $self->{"chunked_upload_state"}) 8 10 18
332 8 $cus->on_readable($self);
333 8 return;
334 }
335  
336 # read in data and shove it on the read buffer
337 10 my $dataref = $self->read($self->{content_length_remain});
338  
339 # unless they disconnected prematurely
340 10 ▶ unless (defined $dataref) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $dataref) 0 10 10
341 0 $self->close('remote_closure');
342 0 return;
343 }
344  
345 # got some data
346 10 push @{$self->{read_buf}}, $dataref;
347 10 my $clen = length($$dataref);
348 10 $self->{read_size} += $clen;
349 10 $self->{read_ahead} += $clen;
350 10 $self->{content_length_remain} -= $clen;
351  
352 10 ▶ if ($self->{content_length_remain}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'content_length_remain'}) { } 0 10 10
353 # Reading too far ahead of our AIO subsystem will cause us to buffer it in memory.
354 0 ▶ $self->watch_read(0) if $self->{read_ahead} >= 1024 * 1024; # arbitrary
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"read_ahead"} >= 1048576 0 0 0
355 # ->put_writeout clears {read_ahead}, so we run it after we need that
356 0 ▶ $self->put_writeout if $self->{read_ahead} >= 8192; # arbitrary
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"read_ahead"} >= 8192 0 0 0
357 } else {
358 # now, if we've filled the content of this put, we're done
359 10 $self->watch_read(0);
360 10 $self->put_writeout;
361 }
362 }
363  
364 # verify that a minimum put directory exists. if/when it's verified,
365 # perhaps cached, the provided callback will be run.
366 3 ▶sub validate_min_put_directory {
Subroutine validate_min_put_directory: called CC=2 SCAR=6.9
Pod undocumented
367 3 my Perlbal::ClientHTTP $self = shift;
368 3 my ($req_path, $extra_path, $filename, $callback) = @_;
369  
370 3 my $disk_dir = $self->{service}->{docroot} . '/' . $req_path;
371 3 ▶ return $callback->() if $VerifiedDirs{$disk_dir};
Branch: 1 branch
BranchTrueFalseTotal
if $Perlbal::ClientHTTP::VerifiedDirs{$disk_dir} 1 2 3
372  
373 2 $self->{put_in_progress} = 1;
374  
375 2 ▶ Perlbal::AIO::aio_open($disk_dir, O_RDONLY, 0755, sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
376 2 my $fh = shift;
377 2 $self->{put_in_progress} = 0;
378  
379 # if error return failure
380 2 ▶ return $self->send_response(404, "Base directory does not exist") unless $fh;
Branch: 1 branch
BranchTrueFalseTotal
unless $fh 1 1 2
381 1 CORE::close($fh);
382  
383 # mindir existed, mark it as so and start the open for the rest of the path
384 1 $VerifiedDirs{$disk_dir} = 1;
385 1 $callback->();
386 2 });
387 }
388  
389 # attempt to open a file being PUT for writing to disk
390 13 ▶sub start_put_open {
Subroutine start_put_open: called CC=2 SCAR=7.1
Pod undocumented
391 13 my Perlbal::ClientHTTP $self = shift;
392 13 my ($path, $file) = @_;
393 13 my ($fs_path, $open_flags);
394  
395 13 $self->{put_in_progress} = 1;
396 13 ▶ if ($self->{md5_ctx}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'md5_ctx'}) { } 2 11 13
397 2 $fs_path = "$path/$file.$$." . int(rand(0xffffffff)) . '.tmp';
398 2 $self->{put_final_name} = "$path/$file";
399 2 $open_flags = O_CREAT | O_EXCL | O_WRONLY;
400 } else {
401 11 $fs_path = "$path/$file";
402 11 $open_flags = O_CREAT | O_TRUNC | O_WRONLY;
403 }
404  
405 13 ▶ Perlbal::AIO::aio_open($fs_path, $open_flags, 0644, sub {
Subroutine __ANON__: called CC=9 SCAR=24.1
406 # get the fd
407 13 my $fh = shift;
408  
409 # verify file was opened
410 13 $self->{put_in_progress} = 0;
411  
412 13 ▶ if (! $fh) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh) 1 12 13
413 1 ▶ if ($! == ENOENT) {
Branch: 2 branches
BranchTrueFalseTotal
if ($! == 2) { } 1 0 1
elsif ($! == 17 and $self->{'put_final_name'}) { } 0 0 0
414 # directory doesn't exist, so let's manually create it
415 1 eval { File::Path::mkpath($path, 0, 0755); };
416 1 ▶ return $self->system_error("Unable to create directory", "path = $path, file = $file") if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
417  
418 # should be created, call self recursively to try
419 1 return $self->start_put_open($path, $file);
420 ▶ } elsif ($! == EEXIST && $self->{put_final_name}) {
Condition0%0/3
$! == 17 and $self->{"put_final_name"}
!ll&&!rl&&r
000
MC/DC0%0/2
$! == 17 and $self->{"put_final_name"}
$! == 17 $self->{"put_final_name"}
Truth table0%0/3
A and B
A= $! == 17B= $self->{"put_final_name"}
ABresult
0X0
100
111
421 # temp name collision, bail hard because this should be near impossible already
422 0 Perlbal::log('crit', "Failure to open exclusively $fs_path as temp file in PUT");
423 0 return $self->_simple_response(500);
424 } else {
425 0 return $self->system_error("Internal error", "error = $!, path = $path, file = $file");
426 }
427 }
428  
429 12 $self->{put_fh} = $fh;
430 12 $self->{put_pos} = 0;
431 12 $self->{put_fh_filename} = $fs_path;
432  
433 # We just opened the file, haven't read_ahead any bytes, are expecting 0 bytes for read and we're
434 # not in chunked mode, so close the file immediately, we're done.
435 12 ▶ unless ($self->{read_ahead} || $self->{content_length_remain} || $self->{chunked_upload_state}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"read_ahead"} or $self->{"content_length_remain"} or $self->{"chunked_upload_state"}) 1 11 12
Condition83%5/6
$self->{"read_ahead"} or $self->{"content_length_remain"} or $self->{"chunked_upload_state"}
l!l&&r!l&&!r
1011
$self->{"read_ahead"} or $self->{"content_length_remain"}
l!l&&r!l&&!r
0102
MC/DC66%2/3
$self->{"read_ahead"} or $self->{"content_length_remain"} or $self->{"chunked_upload_state"}
$self->{"read_ahead"} $self->{"content_length_remain"} $self->{"chunked_upload_state"}
Truth table75%3/4
A or B or C
A= $self->{"read_ahead"}B= $self->{"content_length_remain"}C= $self->{"chunked_upload_state"}
ABCresult
1XX1
01X1
0011
0000
436 # FIXME this should be done through AIO
437 1 $self->put_close;
438 1 return;
439 }
440  
441 11 $self->put_writeout;
442 13 });
443 }
444  
445 # called when we've got some put data to write out
446 29 ▶sub put_writeout {
Subroutine put_writeout: called CC=9 SCAR=22.6
Pod undocumented
447 29 my Perlbal::ClientHTTP $self = shift;
448 29 ▶ Carp::confess("wrong class for $self") unless ref $self eq "Perlbal::ClientHTTP";
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self eq "Perlbal::ClientHTTP" 0 29 29
449  
450 29 ▶ return if $self->{service}->run_hook('put_writeout', $self);
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"service"}->run_hook("put_writeout", $self) 0 29 29
451 29 ▶ return if $self->{put_in_progress};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"put_in_progress"} 0 29 29
452 29 ▶ return unless $self->{put_fh};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"put_fh"} 0 29 29
453 29 ▶ return unless $self->{read_ahead};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"read_ahead"} 12 17 29
454  
455 17 my $data = join("", map { $$_ } @{$self->{read_buf}});
456 17 my $count = length $data;
457 17 my $md5_ctx = $self->{md5_ctx};
458 17 ▶ $md5_ctx->add($data) if $md5_ctx;
Branch: 1 branch
BranchTrueFalseTotal
if $md5_ctx 2 15 17
459  
460 # reset our input buffer
461 17 $self->{read_buf} = [];
462 17 $self->{read_ahead} = 0;
463  
464 # After copying out and clearing the buffer, turn reads back on again to fill up another buffer.
465 17 ▶ $self->watch_read(1) if $self->{content_length_remain} || $self->{chunked_upload_state};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"content_length_remain"} or $self->{"chunked_upload_state"} 7 10 17
Condition66%2/3
$self->{"content_length_remain"} or $self->{"chunked_upload_state"}
l!l&&r!l&&!r
0710
MC/DC50%1/2
$self->{"content_length_remain"} or $self->{"chunked_upload_state"}
$self->{"content_length_remain"} $self->{"chunked_upload_state"}
Truth table66%2/3
A or B
A= $self->{"content_length_remain"}B= $self->{"chunked_upload_state"}
ABresult
1X1
011
000
466  
467 # okay, file is open, write some data
468 17 $self->{put_in_progress} = 1;
469  
470 17 Perlbal::AIO::set_file_for_channel($self->{put_fh_filename});
471 17 ▶ Perlbal::AIO::aio_write($self->{put_fh}, $self->{put_pos}, $count, $data, sub {
Subroutine __ANON__: called CC=5 SCAR=17.1
472 17 ▶ return if $self->{closed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"closed"} 0 17 17
473  
474 # see how many bytes written
475 17 my $bytes = shift() + 0;
476  
477 17 $self->{put_pos} += $bytes;
478 17 $self->{put_in_progress} = 0;
479  
480 # now recursively call ourselves?
481 17 ▶ if ($self->{read_ahead}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"read_ahead"}) 0 17 17
482 0 $self->put_writeout;
483 0 return;
484 }
485  
486 17 ▶ return if $self->{content_length_remain} || $self->{chunked_upload_state};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"content_length_remain"} or $self->{"chunked_upload_state"} 7 10 17
Condition66%2/3
$self->{"content_length_remain"} or $self->{"chunked_upload_state"}
l!l&&r!l&&!r
0710
MC/DC50%1/2
$self->{"content_length_remain"} or $self->{"chunked_upload_state"}
$self->{"content_length_remain"} $self->{"chunked_upload_state"}
Truth table66%2/3
A or B
A= $self->{"content_length_remain"}B= $self->{"chunked_upload_state"}
ABresult
1X1
011
000
487  
488 # we're done putting this file, so close it.
489 # FIXME this should be done through AIO
490 10 $self->put_close;
491 17 });
492 }
493  
494 2 ▶sub put_check_md5 {
Subroutine put_check_md5: called CC=2 SCAR=7.0
Pod undocumented
495 2 my Perlbal::ClientHTTP $self = shift;
496  
497 2 my $actual = $self->{md5_ctx}->b64digest;
498 2 my $expect = $self->{req_headers}->header("Content-MD5");
499 2 $expect =~ s/=+\s*\z//;
500 2 ▶ if ($actual eq $expect) {
Branch: 1 branch
BranchTrueFalseTotal
if ($actual eq $expect) { } 1 1 2
501 1 ▶ Perlbal::AIO::aio_rename($self->{put_fh_filename}, $self->{put_final_name}, sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
502 1 my $err = shift;
503 1 $self->{put_fh_filename} = undef;
504 1 $self->{put_final_name} = undef;
505 1 ▶ if ($err == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err == 0) { } 1 0 1
506 1 return $self->send_response(201);
507 } else {
508 0 return $self->system_error("Error renaming file", "error in rename: $!");
509 }
510 1 });
511 } else {
512 1 ▶ Perlbal::AIO::aio_unlink($self->{put_fh_filename}, sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
513 1 my $err = shift;
514 1 $self->{put_fh_filename} = undef;
515 1 $self->{put_final_name} = undef;
516 1 ▶ if ($err == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err == 0) { } 1 0 1
517 1 return $self->send_response(400,
518 "Content-MD5 mismatch, expected: $expect actual: $actual");
519 } else {
520 0 return $self->system_error("Error unlinking file", "error in unlink: $!");
521 }
522 1 });
523 }
524 }
525  
526 12 ▶sub put_close {
Subroutine put_close: called CC=5 SCAR=16.8
Pod undocumented
527 12 my Perlbal::ClientHTTP $self = shift;
528 12 ▶ return if $self->{put_in_progress};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"put_in_progress"} 0 12 12
529 12 ▶ return unless $self->{put_fh};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"put_fh"} 0 12 12
530  
531 12 ▶ if (CORE::close($self->{put_fh})) {
Branch: 1 branch
BranchTrueFalseTotal
if (CORE::close $self->{'put_fh'}) { } 12 0 12
532 12 $self->{put_fh} = undef;
533  
534 12 ▶ return $self->put_check_md5 if $self->{md5_ctx};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"md5_ctx"} 2 10 12
535 10 return $self->send_response(200);
536 } else {
537 0 return $self->system_error("Error saving file", "error in close: $!");
538 }
539 }
540  
541 22 1;
542  
543 # Local Variables:
544 # mode: perl
545 # c-basic-indent: 4
546 # indent-tabs-mode: nil
547 # End:
« Perlbal/Plugin/Vhosts.pm ↑ Summary Perlbal/Plugin/Include.pm »