XUL/Request.pm

filter: Statementstmt 28.1% 36 / 128 Branchbran 9.2% 5 / 54 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 58.8% 10 / 17 Podpod 11.1% 1 / 9 totaltotal 23.8% 52 / 218 CC 31 SCAR 36.9
CC
31
Cov
22%
parse_post_args
110.0
decode_json
42.0
decode_urlencoded_value
42.0
CRAP 39.9
« XUL/ChangeManager.pm ↑ Summary XUL/Session.pm »
1 package POE::XUL::Request;
2 # $Id: Request.pm 1566 2010-11-03 03:13:32Z fil $
3 # Copyright Philip Gwyn 2007-2010. All rights reserved.
4  
5 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 15 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 15 ▶use HTTP::Status;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 15 ▶use POE::XUL::Logging;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 15 ▶use Unicode::String qw( latin1 utf8 );
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 15 ▶use constant DEBUG => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 15 ▶use base 'POE::Component::Server::HTTP::Request';
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 15 our $VERSION = '0.0600';
18  
19 ##############################################################
20 # Rebless an HTTP::Request to us, so we can add the param argument
21 sub new
22 {
23 0 ▶ my( $package, $req ) = @_;
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
24  
25 0 my $self = bless $req, $package;
26  
27 0 my $rv = $self->parse_args;
28 0 ▶ return $rv if $rv;
Branch: 1 branch
BranchTrueFalseTotal
if $rv 0 0 0
29 0 return $self;
30 }
31  
32 ##############################################################
33 # Get the arguments out of a request
34 sub parse_args
35 {
36 0 ▶ my( $self ) = @_;
Subroutine parse_args: not called CC=5 SCAR=34.0
Pod undocumented
37 0 my $P;
38 0 ▶ return if $self->{P};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"P"} 0 0 0
39  
40 0 local $ENV{QUERY_STRING};
41 0 my $method = $self->method;
42 0 ▶ if( $method eq 'GET' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($method eq 'GET') { } 0 0 0
elsif ($method eq 'POST') { } 0 0 0
43 # TODO: is query UTF-8?
44 0 DEBUG and
45 xdebug "GET: ", $self->uri->query;
46 0 $P = $self->decode_urlencoded( $self->uri->query );
47 }
48 elsif( $method eq 'POST' ) {
49 0 $P = $self->parse_post_args;
50 0 ▶ return $P unless ref $P;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $P 0 0 0
51 }
52 else {
53 0 return RC_METHOD_NOT_ALLOWED;
54 }
55  
56 ####
57 0 $self->{P} = $P;
58 0 return;
59 }
60  
61 sub pre_log
62 {
63 0 ▶ my( $self ) = @_;
Subroutine pre_log: not called CC=1 SCAR=6.9
Pod undocumented
64 0 my $P = $self->{P};
65 0 xwarn "Request=", join( ' ', map { "$_:@{$P->{$_}}" } sort keys %$P), "\n";
66 0 return;
67 }
68  
69 ##############################################################
70 # Get a request parameter. Uses the P hash created in parse_args()
71 sub param
72 {
73 43 ▶ my( $self, $key, $value ) = @_;
Subroutine param: called CC=4 SCAR=14.0
Pod undocumented
74 43 ▶ if( 3==@_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (3 == @_) 8 35 43
75 8 ▶ if( ref $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value) { } 0 8 8
76 0 $self->{P}{$key} = $value;
77 }
78 else {
79 8 $self->{P}{$key} = [ $value ];
80 }
81 }
82 43 my $V = $self->{P}->{$key};
83 43 ▶ return $V->[0] unless wantarray();
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 27 16 43
84 16 return @$V;
85 }
86  
87 sub params
88 {
89 8 ▶ my( $self ) = @_;
Subroutine params: called CC=1 SCAR=0.0
Pod undocumented
90 8 return keys %{$self->{P}};
91 }
92  
93 ##############################################################
94 sub parse_post_args
95 {
96 0 ▶ my( $self ) = @_;
Subroutine parse_post_args: not called CC=10 SCAR=47.0
Pod undocumented
97  
98 # NOTE : this might/will fail if we use a different
99 # content-type. In which case, we have to move to Apache::Request
100 # Also, maybe we should look at $request->dencoded_content;
101  
102 0 my $C = $self->content;
103  
104 0 if( 1 ) {
105 0 my $bad = 0;
106 # This code was to handle over-long requests. But it
107 # turned out the bug was in fact in POE::Filter::HTTPD
108 0 my $l = $self->header('Content-Length');
109 0 ▶ if( $l != length( $C ) ) { # MSIE5.01 does this
Branch: 1 branch
BranchTrueFalseTotal
if ($l != length $C) 0 0 0
110 0 xlog "WRONG LENGTH";
111 0 $C = substr( $C, 0, $l );
112 0 $bad++;
113 }
114 0 ▶ $bad++ if $C =~ s/%0D%0A/%0A/g; # I hate you milkman MSIE!
Branch: 1 branch
BranchTrueFalseTotal
if $C =~ s/%0D%0A/%0A/g 0 0 0
115 0 ▶ $bad++ if $C =~ s/%0D/%0A/g;
Branch: 1 branch
BranchTrueFalseTotal
if $C =~ s/%0D/%0A/g 0 0 0
116 0 ▶ $bad++ if $C =~ s/\r\n/\n/g;
Branch: 1 branch
BranchTrueFalseTotal
if $C =~ s/\r\n/\n/g 0 0 0
117 0 ▶ if( $bad ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bad) 0 0 0
118 0 xlog "Broken User-Agent = ", $self->header('User-Agent');
119 0 $self->content( $C );
120 0 $self->header( 'Content-Length' => length( $C ) );
121 }
122 }
123  
124 0 my $ct = $self->header( 'Content-Type' );
125 0 my $charset = '';
126 0 ▶ if( $ct =~ s/; charset=(.+)// ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ct =~ s/; charset=(.+)//) 0 0 0
127 0 $charset = $1;
128 }
129 0 DEBUG and xdebug "POST ct=$ct -- charset=$charset";
130 0 ▶ if( $ct eq 'application/x-www-form-urlencoded' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ct eq 'application/x-www-form-urlencoded') { } 0 0 0
elsif ($ct eq 'application/json' or $ct eq 'text/json') { } 0 0 0
131 0 return $self->decode_urlencoded( $C, $charset );
132 }
133 ▶ elsif( $ct eq 'application/json' or $ct eq 'text/json' ) {
Condition0%0/3
$ct eq "application/json" or $ct eq "text/json"
l!l&&r!l&&!r
000
MC/DC0%0/2
$ct eq "application/json" or $ct eq "text/json"
$ct eq "application/json" $ct eq "text/json"
Truth table0%0/3
A or B
A= $ct eq "application/json"B= $ct eq "text/json"
ABresult
1X1
011
000
134 # TODO : request might be an array of requests!
135 0 return $self->decode_json ( $C );
136 }
137 0 xwarn "Unable to parse $ct";
138 0 return RC_UNSUPPORTED_MEDIA_TYPE;
139 }
140  
141 ##############################################################
142 sub decode_json
143 {
144 0 ▶ my( $self, $C ) = @_;
Subroutine decode_json: not called CC=6 SCAR=37.4
Pod undocumented
145 0 my $args = eval {
146 0 ▶ if( $JSON::XS::VERSION > 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($JSON::XS::VERSION > 2) { } 0 0 0
147 0 return JSON::XS::decode_json( $C )
148 }
149 else {
150 0 return JSON::XS::from_json( $C )
151 }
152 };
153 0 ▶ if( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
154 0 xwarn "JSON error: $@";
155 0 return RC_BAD_REQUEST;
156 }
157 0 ▶ unless( 'HASH' eq ref $args ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ("HASH" eq ref $args) 0 0 0
158 0 return RC_UNSUPPORTED_MEDIA_TYPE;
159 }
160 0 my $P = {};
161 0 ▶ while( my( $k, $v ) = each %$args ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$args) 0 0 0
162 0 ▶ if( ref $v ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $v) { } 0 0 0
163 0 $P->{$k} = $v;
164 }
165 else {
166 0 $P->{$k} = [ $v ];
167 }
168 }
169 0 return $P;
170 }
171  
172 ##############################################################
173 sub decode_urlencoded
174 {
175 0 ▶ my( $self, $C, $charset ) = @_;
Subroutine decode_urlencoded: not called CC=4 SCAR=30.0
Pod undocumented
176  
177 0 ▶ return $C unless defined $C;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $C 0 0 0
178 0 my $form;
179  
180 0 foreach my $bit ( split /&/, $C ) {
181 0 my( $key, $value ) = split "=", $bit, 2;
182  
183 0 $key = $self->decode_urlencoded_value( $key, $charset );
184 0 $value = $self->decode_urlencoded_value( $value, $charset );
185 0 ▶ unless( exists $form->{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $form->{$key}) { } 0 0 0
186 0 $form->{$key} = [ $value ];
187 } else {
188 0 push @{ $form->{$key} }, $value;
189 }
190 }
191  
192 0 return $form;
193 }
194  
195 ##############################################################
196 sub decode_urlencoded_value
197 {
198 0 ▶ my( $self, $value, $charset ) = @_;
Subroutine decode_urlencoded_value: not called CC=6 SCAR=37.4
Pod undocumented
199 0 ▶ return '' unless defined $value and $value ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and $value ne "" 0 0 0
Condition0%0/3
defined $value and $value ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $value and $value ne ""
defined $value $value ne ""
Truth table0%0/3
A and B
A= defined $valueB= $value ne ""
ABresult
0X0
100
111
200 0 $value =~ tr/+/ /;
201 0 $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/egs;
202  
203 0 ▶ return $value unless $charset;
Branch: 1 branch
BranchTrueFalseTotal
unless $charset 0 0 0
204  
205 0 my $U;
206 0 ▶ if( $charset eq 'UTF-8' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($charset eq "UTF-8") 0 0 0
207 0 $U = utf8( $value );
208 }
209
210 0 ▶ if( defined $U ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $U) 0 0 0
211 0 return $U->latin1;
212 }
213 0 xwarn "Failed to decode $charset string";
214 0 return $value;
215 }
216  
217 15 1;
« XUL/ChangeManager.pm ↑ Summary XUL/Session.pm »