blib/lib/Apache/Request/Redirect.pm

filter: Statementstmt 27.9% 43 / 154 Branchbran 2.0% 1 / 50 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 50.0% 11 / 22 Podpod 71.4% 5 / 7 totaltotal 24.6% 60 / 243 CC 26 SCAR 34.0
CC
26
Cov
21%
new
90.0
_built_content
56.0
apachereq
20.0
CRAP 29.9
↑ Summary
1 package Apache::Request::Redirect;
2  
3 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use vars qw(@ISA @EXPORT $LOG_REQUEST $LOG_QUERYSTRING $LOG_RESPONSE);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 @ISA = qw(Exporter);
12 1 @EXPORT = qw($LOG_REQUEST $LOG_QUERYSTRING $LOG_RESPONSE);
13  
14 1 ▶use HTTP::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use HTTP::Headers;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 1 $Apache::Request::Redirect::VERSION = '0.05';
21  
22 1 $Apache::Request::Redirect::LOG = 0;
23  
24 1 $LOG_REQUEST = 0b0001;
25 1 $LOG_QUERYSTRING = 0b0010;
26 1 $LOG_RESPONSE = 0b0100;
27  
28  
29 1 my $MOD_PERL = 0;
30 # Turn on special checking for Doug MacEachern's modperl
31 1 ▶if (exists $ENV{MOD_PERL}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $ENV{"MOD_PERL"}) 0 1 1
32 0 eval "require mod_perl";
33 # mod_perl handlers may run system() on scripts using CGI.pm;
34 # Make sure so we don't get fooled by inherited $ENV{MOD_PERL}
35 0 ▶ if (defined $mod_perl::VERSION) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $mod_perl::VERSION) 0 0 0
36 0 ▶ if ($mod_perl::VERSION >= 1.99) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mod_perl::VERSION >= 1.99) { } 0 0 0
37 0 $MOD_PERL = 2;
38 0 require Apache::RequestRec;
39 0 require Apache::RequestUtil;
40 0 require APR::Pool;
41 } else {
42 0 $MOD_PERL = 1;
43 0 require Apache;
44 }
45 }
46 }
47  
48  
49 1 my %fields = (
50 apachereq => '',
51 host => 'localhost',
52 url => '/',
53 args => {},
54 use_http10 => 0,
55 );
56  
57 0 ▶sub new {
Subroutine new: not called CC=9 SCAR=45.0
Pod undocumented
58 0 my ($proto,%options) = @_;
59 0 ▶ my $class = ref($proto) || $proto;
Condition0%0/3
ref $proto || $proto
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table0%0/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
60 0 my $self = { };
61 0 ▶ while (my ($key,$value) = each(%options)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %options) 0 0 0
62 0 ▶ if (exists($fields{$key})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $fields{$key}) { } 0 0 0
63 0 $self->{$key} = $value;
64 } else {
65 0 die $class . "::new: invalid option '$key'\n";
66 }
67 }
68 #foreach (keys %fields) {
69 # die $class . "::new: omitted required option '$_'\n"
70 # if (!defined $self->{$_});
71 #}
72
73 0 bless $self, $class;
74
75 # attivo apachereq direttamente da Apache
76 0 ▶ if ($MOD_PERL) {
Branch: 1 branch
BranchTrueFalseTotal
if ($MOD_PERL) 0 0 0
77 0 ▶ $self->apachereq(Apache->request) unless $self->apachereq;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->apachereq 0 0 0
78 0 my $apachereq = $self->apachereq;
79 0 ▶ if ($MOD_PERL == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($MOD_PERL == 1) { } 0 0 0
80 #$apacheref->register_cleanup(\&CGI::_reset_globals);
81 } else {
82 # XXX: once we have the new API
83 # will do a real PerlOptions -SetupEnv check
84 #$apacheref->subprocess_env unless exists $ENV{REQUEST_METHOD};
85 #$apacheref->pool->cleanup_register(\&CGI::_reset_globals);
86 }
87 }
88  
89 0 ▶ if ($Apache::Request::Redirect::LOG != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Apache::Request::Redirect::LOG != 0) 0 0 0
90 0 eval {
91 0 require "Log/FileSimple.pm";
92 };
93 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
94 0 warn "Warning: Logging disabled...cannot find Log::FileSimple module";
95 0 $Apache::Request::Redirect::LOG = 0;
96 } else {
97 0 $self->{log} = new Log::FileSimple(
98 name=> "Apache::Request::Redirect",
99 file=> '/tmp/Apache-Request-Redirect.log',
100 mask=> $Apache::Request::Redirect::LOG,
101 );
102 }
103 }
104 0 return $self;
105 }
106  
107 0 ▶sub redirect() {
Subroutine redirect: not called CC=1 SCAR=6.9
Pod documented
108 # passare un riferimento ad hash con
109 # i parametri della query in quanto la query string (GET)
110 # o il content (POST) deve essere ricostruito
111 # (Mason si mangia il content)
112 0 my $self = shift;
113 0 my $request = $self->_prepare_request();
114 0 $self->_log(message => "Request:\n" . $request->as_string , id => $LOG_REQUEST);
115 0 my $response = $self->_send_request($request);
116 0 my $response_text = $response->as_string;
117 0 $self->_log(id => $LOG_RESPONSE, message => "Response:\n" .
118 $response_text);
119 0 return $response;
120 }
121  
122 0 ▶sub _prepare_request() {
Subroutine _prepare_request: not called CC=2 SCAR=17.9
123 0 my $self = shift;
124 0 my $request_args = $self->{args};
125  
126 # Costruisco l'header della richiesta da quello originale
127 0 my $headers = new HTTP::Headers(%{$self->{apachereq}->headers_in});
128 # modifico l'host per impostarlo a quello che andro' realmente a
129 # chiamare
130 0 $headers->header('Host',$self->{host});
131 # dato che questo modulo e' fatto per post processare
132 # l'html ottenuto...non posso permettere che mi ritorni
133 # html compresso
134 0 $headers->remove_header('Accept-Encoding');
135 #$self->_log(id => $LOG_REQUEST, message => 'HTTP::Headers',objects=>[$headers]);
136 # costruisco l'url ed il content
137 0 my $uri = URI->new();
138 0 $uri->scheme('http');
139 0 $uri->host($self->{host});
140 0 $uri->path($self->{url});
141 0 $uri->query_form(%$request_args);
142 0 my $content;
143 0 ▶ if ($self->{apachereq}->method eq 'POST') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'apachereq'}->method eq 'POST') { } 0 0 0
144 # costruisco il content
145 0 $content = $self->_built_content();
146 # nel post la query string totale la metto nel
147 # content e non nell'url
148 0 $content .= $uri->query;
149 # nell'url ci lasciamo la sola query_string originale (00.04)
150 0 $uri->query(scalar($self->{apachereq}->args));
151 # imposto la lunghezza del content nell'header
152 0 $headers->header('Content-Length' => length($content));
153 } else {
154 # nel get il content non c'e' (sara' vero ? :-)
155 0 $headers->remove_header('Content-Length');
156 }
157 # costruisco la nuova richiesta per il recupero dell'url
158 my $request = new HTTP::Request($self->{apachereq}->method,
159 0 $uri,
160 $headers,
161 $content
162 );
163 0 return $request;
164 }
165  
166 0 ▶sub _send_request() {
Subroutine _send_request: not called CC=2 SCAR=17.9
167 0 my $self = shift;
168 0 my $request = shift;
169  
170 0 ▶ if ($self->{use_http10}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"use_http10"}) 0 0 0
171 0 require LWP::Protocol::http10;
172 0 LWP::Protocol::implementor('http', 'LWP::Protocol::http10');
173 }
174 0 my $ua = new LWP::UserAgent;
175 0 my $response = $ua->send_request($request);
176 0 return $response;
177 }
178  
179 0 ▶sub _log() {
Subroutine _log: not called CC=2 SCAR=17.9
180 0 my $self = shift;
181 0 ▶ $self->{log} && $self->{log}->log(@_);
Branch: 1 branch
BranchTrueFalseTotal
$self->{"log"} and $self->{"log"}->log(@_) 0 0 0
182 }
183  
184 0 ▶sub _built_content() {
Subroutine _built_content: not called CC=7 SCAR=40.3
185 0 my $self = shift;
186 0 my $request_args = $self->{args};
187  
188 0 my $request = $self->{apachereq};
189 0 my $content;
190 my $boundary;
191 0 ▶ if ($request->header_in("Content-type") =~
Branch: 1 branch
BranchTrueFalseTotal
if ($request->header_in("Content-type") =~ qr"^multipart/form-data; boundary=(.+?)$"i) 0 0 0
192 qr|^multipart/form-data; boundary=(.+?)$|i) {
193 0 $boundary = "--$1";
194 0 for my $upload ($self->{apachereq}->upload) {
195 0 $self->_log(message => 'Upload object',
196 objects=>[$upload], id => $LOG_REQUEST);
197 0 $content .= "$boundary\r\n";
198 0 my $info = $upload->info;
199 0 ▶ while (my($key, $val) = each %$info) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$info) 0 0 0
200 0 ▶ if ($key ne 'Content-Type') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key ne "Content-Type") 0 0 0
201 0 $content .= "$key: $val; ";
202 }
203 # rimuovo l'ultimo ;
204 0 chop($content);
205 }
206 0 $content .= "\r\nContent-Type: " .
207 $upload->info("Content-Type") . "\r\n\r\n";
208 0 my $fh = $upload->fh;
209 0 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 0 0 0
210 0 $content .= $_;
211 }
212 # lo rimuovo da args
213 0 delete $request_args->{$upload->name};
214 }
215 # aggiungo gli args
216 0 ▶ while (my ($key,$val) = each(%$request_args)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$request_args) 0 0 0
217 0 $content .= qq|\r\n$boundary\r\nContent-Disposition: | .
218 qq|form-data; name="$key"\r\n\r\n$val|;
219 }
220 0 $content .= "\r\n$boundary--\r\n";
221 }
222
223 0 return $content;
224 }
225  
226 # read-write property
227  
228 0 ▶sub apachereq {
Subroutine apachereq: not called CC=4 SCAR=30.0
Pod documented
229 0 my $s = shift;
230 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
231 0 ▶ die "apachereq must be a reference to Apache or Apache::Request object"
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] ne "Apache" and ref $_[0] ne "Apache::Request" 0 0 0
Condition0%0/3
ref $_[0] ne "Apache" and ref $_[0] ne "Apache::Request"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $_[0] ne "Apache" and ref $_[0] ne "Apache::Request"
ref $_[0] ne "Apache" ref $_[0] ne "Apache::Request"
Truth table0%0/3
A and B
A= ref $_[0] ne "Apache"B= ref $_[0] ne "Apache::Request"
ABresult
0X0
100
111
232 if (ref($_[0]) ne "Apache" && ref($_[0]) ne "Apache::Request");
233 0 $s->{apachereq} = shift;
234 }
235 0 return $s->{apachereq};
236 }
237  
238 0 ▶sub host { my $s = shift; if (@_) { $s->{host} = shift; } return $s->{host}; }
Subroutine host: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
239 0 ▶sub url { my $s = shift; if (@_) { $s->{url} = shift; } return $s->{url}; }
Subroutine url: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
240 0 ▶sub use_http10 { my $s = shift; if (@_) { $s->{use_http10} = shift; } return $s->{use_http10}; }
Subroutine use_http10: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
241  
242 0 ▶sub args {
Subroutine args: not called CC=3 SCAR=24.8
Pod documented
243 0 my $s = shift;
244 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
245 0 ▶ die "args must be a reference to a hash insteed of " . ref($_[0])
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] ne "HASH" 0 0 0
246 if (ref($_[0]) ne "HASH");
247 0 $s->{args} = shift;
248 }
249 0 return $s->{args};
250 }
251  
252 1 1;
253 __END__
↑ Summary