HTTPServer/Request.pm

filter: Statementstmt 82.1% 106 / 129 Branchbran 75.0% 33 / 44 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 78.2% 18 / 23 Podpod 83.3% 10 / 12 totaltotal 77.5% 169 / 218 CC 29 SCAR 16.7
CC
29
Cov
79%
_parse
11.7
_chroot
8.8
Response
6.0
CRAP 5.3
« HTTPServer/Response.pm ↑ Summary HTTPServer/CaptureSTDOUT.pm »
1 ##############################################################################
2 #
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU Library General Public
5 # License as published by the Free Software Foundation; either
6 # version 2 of the License, or (at your option) any later version.
7 #
8 # This library is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 # Library General Public License for more details.
12 #
13 # You should have received a copy of the GNU Library General Public
14 # License along with this library; if not, write to the
15 # Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 # Boston, MA 02111-1307, USA.
17 #
18 # Copyright (C) 2003-2005 Ryan Eatmon
19 #
20 ##############################################################################
21 package Net::HTTPServer::Request;
22  
23 =head1 NAME
24  
25 Net::HTTPServer::Request
26  
27 =head1 SYNOPSIS
28  
29 Net::HTTPServer::Request handles the parsing of a request.
30
31 =head1 DESCRIPTION
32  
33 Net::HTTPServer::Request takes a full request, parses it, and then provides
34 a nice OOP interface to pulling out the information you want from a request.
35  
36 =head1 METHODS
37  
38 =head2 Cookie([cookie])
39  
40 Returns a hash reference of cookie/value pairs. If you specify a cookie,
41 then it returns the value for that cookie, or undef if it does not exist.
42  
43 =head2 Env([var])
44  
45 Returns a hash reference of variable/value pairs. If you specify a
46 variable, then it returns the value for that variable, or undef if it does
47 not exist.
48  
49 =head2 Header([header])
50  
51 Returns a hash reference of header/value pairs. If you specify a header,
52 then it returns the value for that header, or undef if it does not exist.
53  
54 =head2 Method()
55  
56 Returns the method of the request (GET,POST,etc...)
57  
58 =head2 Path()
59  
60 Returns the path portion of the URL. Does not include any query
61 strings.
62  
63 =head2 Procotol()
64  
65 Returns the name and revision that the request came in with.
66  
67 =head2 Query()
68  
69 Returns the query portion of the URL (if any). You can combine the Path
70 and the Query with a ? to get the real URL that the client requested.
71  
72 =head2 Request()
73  
74 Returns the entire request as a string.
75  
76 =head2 Response()
77  
78 Returns a Net::HTTPServer::Response object with various bits prefilled
79 in. If you have created session via the Session() method, then the
80 session will already be registered with the response.
81  
82 =head2 Session()
83  
84 Create a new Net::HTTPServer::Session object. If the cookie value is set,
85 then the previous state values are loaded, otherwise a new session is
86 started.
87  
88 =head2 URL()
89  
90 Returns the URL of the request.
91  
92 =head1 AUTHOR
93  
94 Ryan Eatmon
95  
96 =head1 COPYRIGHT
97  
98 Copyright (c) 2003-2005 Ryan Eatmon <reatmon@mail.com>. All rights
99 reserved. This program is free software; you can redistribute it
100 and/or modify it under the same terms as Perl itself.
101  
102 =cut
103
104 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
105 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
106 4 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
107 4 ▶use URI::QueryParam;
Subroutine BEGIN: called CC=1 SCAR=0.0
108 4 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
109  
110 4 ▶use vars qw ( $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
111  
112 4 $VERSION = "1.0.3";
113  
114 sub new
115 6 ▶{
Subroutine new: called CC=3 SCAR=11.0
Pod undocumented
116 6 my $proto = shift;
117 6 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
060
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
118 6 my $self = { };
119
120 6 bless($self, $proto);
121  
122 6 my (%args) = @_;
123  
124 6 $self->{ARGS} = \%args;
125  
126 6 $self->{HEADERS} = {};
127 6 $self->{ENV} = {};
128 6 $self->{COOKIES} = {};
129 6 $self->{FAILURE} = "";
130 6 $self->{CHROOT} = $self->_arg("chroot",1);
131 6 $self->{REQUEST} = $self->_arg("request",undef);
132 6 $self->{SERVER} = $self->_arg("server",undef);
133  
134 6 ▶ $self->_parse() if defined($self->{REQUEST});
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"REQUEST"} 5 1 6
135  
136 6 return $self;
137 }
138  
139  
140 sub Cookie
141 9 ▶{
Subroutine Cookie: called CC=3 SCAR=11.0
Pod documented
142 9 my $self = shift;
143 9 my $cookie = shift;
144  
145 9 ▶ return $self->{COOKIES} unless defined($cookie);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cookie 6 3 9
146 3 ▶ return unless exists($self->{COOKIES}->{$cookie});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"COOKIES"}{$cookie} 1 2 3
147 2 return $self->{COOKIES}->{$cookie};
148 }
149  
150  
151 sub Env
152 12 ▶{
Subroutine Env: called CC=3 SCAR=11.0
Pod documented
153 12 my $self = shift;
154 12 my $env = shift;
155  
156 12 ▶ return $self->{ENV} unless defined($env);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $env 6 6 12
157 6 ▶ return unless exists($self->{ENV}->{$env});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"ENV"}{$env} 2 4 6
158 4 return $self->{ENV}->{$env};
159 }
160  
161  
162 sub Header
163 10 ▶{
Subroutine Header: called CC=3 SCAR=11.0
Pod documented
164 10 my $self = shift;
165 10 my $header = shift;
166  
167 10 ▶ return $self->{HEADERS} unless defined($header);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $header 6 4 10
168 4 ▶ return unless exists($self->{HEADERS}->{lc($header)});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"HEADERS"}{lc $header} 1 3 4
169 3 return $self->{HEADERS}->{lc($header)};
170 }
171  
172  
173 sub Method
174 6 ▶{
Subroutine Method: called CC=1 SCAR=0.0
Pod documented
175 6 my $self = shift;
176  
177 6 return $self->{METHOD};
178 }
179  
180  
181 sub Path
182 6 ▶{
Subroutine Path: called CC=1 SCAR=0.0
Pod documented
183 6 my $self = shift;
184  
185 6 return $self->{PATH};
186 }
187  
188  
189 sub Protocol
190 0 ▶{
Subroutine Protocol: not called CC=1 SCAR=6.9
Pod undocumented
191 0 my $self = shift;
192  
193 0 return $self->{PROTOCOL};
194 }
195  
196  
197 sub Query
198 0 ▶{
Subroutine Query: not called CC=1 SCAR=6.9
Pod documented
199 0 my $self = shift;
200  
201 0 return $self->{QUERY};
202 }
203  
204  
205 sub Request
206 6 ▶{
Subroutine Request: called CC=1 SCAR=0.0
Pod documented
207 6 my $self = shift;
208  
209 6 return $self->{REQUEST};
210 }
211  
212  
213 sub Response
214 0 ▶{
Subroutine Response: not called CC=2 SCAR=17.9
Pod documented
215 0 my $self = shift;
216  
217 0 my $response = new Net::HTTPServer::Response();
218  
219 0 ▶ if (exists($self->{SESSION}))
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"SESSION"}) 0 0 0
220 {
221 0 $response->Session($self->{SESSION});
222 }
223  
224 0 return $response;
225 }
226  
227  
228 sub Session
229 1 ▶{
Subroutine Session: called CC=3 SCAR=11.2
Pod documented
230 1 my $self = shift;
231  
232 1 ▶ return unless $self->{SERVER}->{CFG}->{SESSIONS};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"SERVER"}{"CFG"}{"SESSIONS"} 0 1 1
233
234 1 ▶ if (!exists($self->{SESSION}))
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"SESSION"}) 1 0 1
235 {
236 1 my $cookie = $self->Cookie("NETHTTPSERVERSESSION");
237
238 $self->{SESSION} =
239 new Net::HTTPServer::Session(key=>$cookie,
240 server=>$self->{SERVER},
241 1 );
242 }
243  
244 1 return $self->{SESSION};
245 }
246  
247  
248 sub URL
249 6 ▶{
Subroutine URL: called CC=1 SCAR=0.0
Pod documented
250 6 my $self = shift;
251  
252 6 return $self->{URL};
253 }
254  
255  
256 ###############################################################################
257 #
258 # _arg - if the arg exists then use it, else use the default.
259 #
260 ###############################################################################
261 sub _arg
262 18 ▶{
Subroutine _arg: called CC=2 SCAR=6.9
263 18 my $self = shift;
264 18 my $arg = shift;
265 18 my $default = shift;
266  
267 18 ▶ return (exists($self->{ARGS}->{$arg}) ? $self->{ARGS}->{$arg} : $default);
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'ARGS'}{$arg} ? : 10 8 18
268 }
269  
270  
271 ###############################################################################
272 #
273 # _chroot - take the path and if we are running under chroot, massage it so
274 # that is cannot leave DOCROOT.
275 #
276 ###############################################################################
277 sub _chroot
278 5 ▶{
Subroutine _chroot: called CC=7 SCAR=21.8
279 5 my $self = shift;
280 5 my $url = shift;
281  
282 5 ▶ return $url unless $self->{CHROOT};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"CHROOT"} 0 5 5
283
284 5 my $change = 1;
285 5 ▶ while( $change )
Branch: 1 branch
BranchTrueFalseTotal
if ($change) 5 5 10
286 {
287 5 $change = 0;
288
289 #-----------------------------------------------------------------
290 # Look for multiple / in a row and make them one /
291 #-----------------------------------------------------------------
292 0 ▶ while( $url =~ s/\/\/+/\// ) { $change = 1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($url =~ s[//+][/]) 0 5 5
293
294 #-----------------------------------------------------------------
295 # look for something/.. and remove it
296 #-----------------------------------------------------------------
297 0 ▶ while( $url =~ s/[^\/]+\/\.\.(\/|$)// ) { $change = 1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($url =~ s[[^/]+/\.\.(/|$)][]) 0 5 5
298  
299 #-----------------------------------------------------------------
300 # Look for ^/.. and remove it
301 #-----------------------------------------------------------------
302 0 ▶ while( $url =~ s/^\/?\.\.(\/|$)/\// ) { $change = 1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($url =~ s[^/?\.\.(/|$)][/]) 0 5 5
303
304 #-----------------------------------------------------------------
305 # Look for /.../ and make it /
306 #-----------------------------------------------------------------
307 0 ▶ while( $url =~ s/(^|\/)\.+(\/|$)/\// ) { $change = 1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($url =~ s[(^|/)\.+(/|$)][/]) 0 5 5
308 }
309  
310 5 return $url;
311 }
312  
313  
314 sub _failure
315 0 ▶{
Subroutine _failure: not called CC=1 SCAR=6.9
316 0 my $self = shift;
317  
318 0 return $self->{FAILURE};
319 }
320  
321  
322 sub _env
323 0 ▶{
Subroutine _env: not called CC=1 SCAR=6.9
324 0 my $self = shift;
325 0 my $env = shift;
326 0 my $value = shift;
327  
328 0 $self->{ENV}->{$env} = $value;
329 }
330  
331  
332 sub _parse
333 5 ▶{
Subroutine _parse: called CC=11 SCAR=24.6
334 5 my $self = shift;
335  
336 5 ($self->{METHOD},$self->{URL},$self->{PROTOCOL}) = ($self->{REQUEST} =~ /(\S+)\s+(\S+)\s+(.+?)\015?\012/s);
337
338 5 my $uri = new URI($self->{URL},"http");
339  
340 #-------------------------------------------------------------------------
341 # What did they ask for?
342 #-------------------------------------------------------------------------
343 5 $self->{PATH} = $self->_chroot($uri->path());
344  
345 5 my ($headers,$body) = ($self->{REQUEST} =~ /^(.+?)\015?\012\015?\012(.*?)$/s);
346
347 5 my $last_header = "";
348 5 foreach my $header (split(/[\r\n]+/,$headers))
349 {
350 52 my $folded;
351 my $key;
352 52 my $value;
353
354 52 ($folded,$value) = ($header =~ /^(\s*)(.+?)\s*$/);
355 52 ▶ if ($folded ne "")
Branch: 1 branch
BranchTrueFalseTotal
if ($folded ne "") 0 52 52
356 {
357 0 $self->{HEADERS}->{lc($last_header)} .= $value;
358 0 next;
359 }
360
361 52 ($key,$value) = ($header =~ /^([^\:]+?)\s*\:\s*(.+?)\s*$/);
362 52 ▶ next unless defined($key);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 5 47 52
363  
364 47 $last_header = $key;
365
366 47 $self->{HEADERS}->{lc($key)} = $value;
367  
368 47 ▶ if ((lc($key) eq "expect") && ($value ne "100-continue"))
Branch: 1 branch
BranchTrueFalseTotal
if (lc $key eq "expect" and $value ne "100-continue") 0 47 47
Condition33%1/3
lc $key eq "expect" and $value ne "100-continue"
!ll&&!rl&&r
4700
MC/DC0%0/2
lc $key eq "expect" and $value ne "100-continue"
lc $key eq "expect" $value ne "100-continue"
Truth table33%1/3
A and B
A= lc $key eq "expect"B= $value ne "100-continue"
ABresult
0X0
100
111
369 {
370 0 $self->{FAILURE} = "expect";
371 0 return;
372 }
373 }
374  
375 #-------------------------------------------------------------------------
376 # Did they send any ?xxx=yy on the URL?
377 #-------------------------------------------------------------------------
378 5 $self->{QUERY} = $uri->query();
379 5 foreach my $key ($uri->query_param())
380 {
381 2 $self->{ENV}->{$key} = $uri->query_param($key);
382 }
383  
384 #-------------------------------------------------------------------------
385 # If this was POST, then the body contains more xxx=yyyy
386 #-------------------------------------------------------------------------
387 5 ▶ if ($self->{METHOD} eq "POST")
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"METHOD"} eq "POST") 1 4 5
388 {
389 1 my $post_uri = new URI("?$body","http");
390  
391 1 foreach my $key ($post_uri->query_param())
392 {
393 2 $self->{ENV}->{$key} = $post_uri->query_param($key);
394 }
395 }
396  
397 #-------------------------------------------------------------------------
398 # Finally, parse out any cookies.
399 #-------------------------------------------------------------------------
400 5 ▶ if (exists($self->{HEADERS}->{cookie}))
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"HEADERS"}{"cookie"}) 3 2 5
401 {
402 3 foreach my $cookie ( split( /\s*;\s*/,$self->{HEADERS}->{cookie}) )
403 {
404 3 my ($name,$value) = split("=",$cookie,2);
405 3 $self->{COOKIES}->{$name} = uri_unescape($value);
406 }
407 }
408 }
409  
410  
411  
412 4 1;
413  
« HTTPServer/Response.pm ↑ Summary HTTPServer/CaptureSTDOUT.pm »