FileHash/GET.pm untested

filter: Statementstmt 0.0% 0 / 63 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 90 CC 6 SCAR 20.8
CC
6
Cov
0%
handler
20.0
file_handler
12.0
BEGIN
2.0
CRAP 8.0
1 package Apache2::FileHash::GET;
2  
3 use strict;
4 use warnings;
5  
6 use Carp;
7 use Digest::MD5;
8 use Math::BigInt;
9 use File::Temp;
10 use File::Copy;
11  
12 use Apache2::RequestRec ();
13 use Apache2::Const -compile => qw(DECLINED OK REDIRECT);
14  
15 use constant METHOD => "GET";
16  
17 our $Config = {
18 METHOD => {
19 re_path => qr(^/getFile/(.*)),
20 re_hashed => qr(^/getFile/(0x[a-zA-Z0-9]{32}\.(.*))),
21 },
22 };
23  
24 sub handler
25 {
26 my $r = shift;
27  
28 return(Apache2::Const::DECLINED) unless $r->method() eq METHOD;
29  
30 $r->handler("perl-script");
31 $r->push_handlers(PerlResponseHandler => \&file_handler);
32  
33 # First GET
34 my $uri = $r->uri();
35 unless ($uri =~ m#$Config->{METHOD}{re_hashed}#) {
36 if ($uri =~ m#$Config->{METHOD}{re_path}#) {
37 my $path = $1;
38 my $filename = &Apache2::FileHash::hashing_function($r, $path);
39  
40 my $netloc = &Apache2::FileHash::netloc($r, $filename);
41  
42 $r->headers_out->set(Location => $netloc);
43 $r->status(Apache2::Const::REDIRECT);
44  
45 return Apache2::Const::REDIRECT;
46 }
47 }
48  
49 return Apache2::Const::OK;
50 }
51  
52 sub file_handler
53 {
54 my $r = shift;
55  
56 my $uri = $r->uri();
57  
58 # Second pass GET
59 if ($uri =~ m#$Config->{METHOD}{re_hashed}#) {
60 my $file = $1;
61 my $extension = $2;
62  
63 my $newfile = $Apache2::FileHash::Config->[0]{GLOBALS}{base_dir} . "/$file";
64  
65 my $types = MIME::Types->new();
66 my $mime = $types->mimeTypeOf($extension);
67 $r->content_type($mime);
68 open(my $fh, $newfile) or die("error: open: $newfile: $!\n");
69  
70 my $buffer;
71 my $len = 1024;
72 while (read($fh, $buffer, $len)) {
73 last unless $len;
74 $r->print($buffer);
75 }
76  
77 close($fh);
78  
79 return(Apache2::Const::OK);
80 }
81 }
82  
83 1;