MrGamoo/API/Get.pm untested

filter: Statementstmt 0.0% 0 / 52 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 72 CC 3 SCAR 11.7
CC
3
Cov
0%
handler
6.0
_get_file
6.0
BEGIN
2.0
CRAP 3.2
1 # -*- perl -*-
2  
3 # Copyright (c) 2010 AdCopy
4 # Author: Jeff Weisberg
5 # Created: 2010-Jan-19 18:21 (EST)
6 # Function:
7 #
8 # $Id: Get.pm,v 1.1 2010/11/01 18:41:51 jaw Exp $
9  
10 package AC::MrGamoo::API::Get;
11 use AC::MrGamoo::Debug 'api_get';
12 use AC::MrGamoo::Config;
13 use AC::MrGamoo::API::Simple;
14 use AC::MrGamoo::Protocol;
15 use AC::MrGamoo::Scriblr;
16 use AC::SHA1File;
17 use Fcntl;
18 use POSIX;
19  
20 use strict;
21  
22 sub handler {
23 my $class = shift;
24 my $io = shift;
25 my $proto = shift;
26 my $req = shift;
27 my $content = shift;
28  
29 return unless $proto->{want_reply};
30 in_background( \&_get_file, $io, $proto, $req, $content );
31 }
32  
33 sub _get_file {
34 my $io = shift;
35 my $proto = shift;
36 my $req = shift;
37 my $content = shift;
38  
39 my $file = filename($req->{filename});
40 my $fd = $io->{fd};
41 fcntl($fd, F_SETFL, 0); # unset nbio
42  
43 return nbfd_reply(404, "not found", $fd, $proto, $req) unless -f $file;
44 open(F, $file) || return nbfd_reply(500, 'error', $fd, $proto, $req);
45 my $size = (stat($file))[7];
46 my $sha1 = sha1_file($file);
47  
48 debug("get file '$file' size $size");
49  
50 # send header
51 my $gb = ACPScriblReply->encode( { status_code => 200, status_message => 'OK', hash_sha1 => $sha1 } );
52 my $hdr = AC::MrGamoo::Protocol->encode_header(
53 type => $proto->{type},
54 msgidno => $proto->{msgidno},
55 is_reply => 1,
56 data_length => length($gb),
57 content_length => $size,
58 );
59 my $buf = $hdr . $gb;
60  
61 syswrite( $fd, $buf );
62  
63 # stream
64 AC::MrGamoo::Protocol->sendfile($fd, \*F, $size);
65 }
66  
67  
68 1;