ASP/UploadHook.pm untested

filter: Statementstmt 0.0% 0 / 60 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 9 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 97 CC 8 SCAR 32.1
CC
8
Cov
0%
hook
56.0
new
6.0
context
2.0
CRAP 24.7
1  
2 package Apache2::ASP::UploadHook;
3  
4 use strict;
5 use warnings 'all';
6 use Apache2::ASP::HTTPContext;
7 use Carp 'confess';
8 use Time::HiRes 'gettimeofday';
9 use Apache2::ASP::UploadHookArgs;
10  
11  
12 #==============================================================================
13 sub new
14 {
15 my ($class, %args) = @_;
16
17 foreach(qw/ handler_class /)
18 {
19 confess "Required param '$_' was not provided"
20 unless defined($args{$_});
21 }# end foreach()
22 return bless \%args, $class;
23 }# end new()
24  
25  
26 #==============================================================================
27 sub context
28 {
29 Apache2::ASP::HTTPContext->current;
30 }# end context()
31  
32  
33 #==============================================================================
34 sub hook
35 {
36 my ($s, $upload, $data) = @_;
37
38 my $length_received = defined($data) ? length($data) : 0;
39 my $context = $s->context;
40 my $CONTENT_LENGTH = $ENV{CONTENT_LENGTH} || $context->r->pnotes('content_length');
41 my $total_loaded = ($context->r->pnotes('total_loaded') || 0) + $length_received;
42 $context->r->pnotes( total_loaded => $total_loaded);
43 my $percent_complete = sprintf("%.2f", $total_loaded / $CONTENT_LENGTH * 100 );
44
45 # Mark our start time, so we can make our calculations:
46 my $start_time = $context->r->pnotes('upload_start_time');
47 if( ! $start_time )
48 {
49 $start_time = gettimeofday();
50 $context->r->pnotes('upload_start_time' => $start_time);
51 }# end if()
52
53 # Calculate elapsed, total expected and remaining time, etc:
54 my $elapsed_time = gettimeofday() - $start_time;
55 my $bytes_per_second = $context->r->pnotes('total_loaded') / $elapsed_time;
56 $bytes_per_second ||= 1;
57 my $total_expected_time = int( ($CONTENT_LENGTH - $length_received) / $bytes_per_second );
58 my $time_remaining = int( (100 - $percent_complete) * $total_expected_time / 100 );
59 $time_remaining = 0 if $time_remaining < 0;
60
61 # Use an object, not just a hashref:
62 my $Upload = Apache2::ASP::UploadHookArgs->new(
63 upload => $upload,
64 percent_complete => $percent_complete,
65 elapsed_time => $elapsed_time,
66 total_expected_time => $total_expected_time,
67 time_remaining => $time_remaining,
68 length_received => $length_received,
69 data => defined($data) ? $data : undef,
70 );
71
72 # Init the upload:
73 my $did_init = $ENV{did_init};
74 if( ! $did_init )
75 {
76 $ENV{did_init} = 1;
77  
78 $s->{handler_class}->upload_start( $context, $Upload )
79 or return;
80
81 # End the upload if we are done:
82 my $uploadID = $s->_args('uploadID');
83 $context->r->push_handlers(PerlCleanupHandler => sub {
84 delete($context->session->{"upload$uploadID$_"})
85 foreach grep { $_ !~ m/data/i } keys(%$Upload);
86 $context->session->save;
87 });
88 }# end if()
89
90 if( $length_received <= 0 )
91 {
92 $s->{handler_class}->init_asp_objects( $context );
93 $s->{handler_class}->upload_end( $context, $Upload );
94 }
95 else
96 {
97 # Call the hook:
98 $s->{handler_class}->upload_hook( $context, $Upload );
99 }# end if()
100
101 }# end hook()
102  
103
104 #==============================================================================
105 sub _args
106 {
107 my ($s, $key) = @_;
108
109 my %args = map {
110 split /\=/, $_
111 } split /&/, $ENV{QUERY_STRING};
112
113 return $args{$key};
114 }# end _args()
115  
116 1;# return true:
117  
118 =head1 NAME
119  
120 Apache2::ASP::UploadHook - An upload hook for Apache2::Request
121  
122 =head1 SYNOPSIS
123  
124 Internal use only.
125  
126 =head1 DESCRIPTION
127  
128 This class handles interaction with L<Apache2::Request> during file uploads.
129
130 =head1 BUGS
131
132 It's possible that some bugs have found their way into this release.
133
134 Use RT L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Apache2-ASP> to submit bug reports.
135
136 =head1 HOMEPAGE
137
138 Please visit the Apache2::ASP homepage at L<http://www.devstack.com/> to see examples
139 of Apache2::ASP in action.
140
141 =head1 AUTHOR
142
143 John Drago L<mailto:jdrago_999@yahoo.com>
144
145 =head1 COPYRIGHT AND LICENSE
146
147 Copyright 2007 John Drago, All rights reserved.
148
149 This software is free software. It may be used and distributed under the
150 same terms as Perl itself.
151
152 =cut
153