HTTPServer/Response.pm

filter: Statementstmt 78.2% 90 / 115 Branchbran 59.5% 25 / 42 Conditioncond 22.2% 2 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 83.3% 15 / 18 Podpod 100% 11 / 11 totaltotal 71.1% 143 / 201 CC 28 SCAR 18.8
CC
28
Cov
70%
Session
12.0
ProcessSTDOUT
12.0
_build
11.6
CRAP 6.5
« HTTPServer.pm ↑ Summary HTTPServer/Request.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::Response;
22  
23 =head1 NAME
24  
25 Net::HTTPServer::Response
26  
27 =head1 SYNOPSIS
28  
29 Net::HTTPServer::Response handles formatting the response to the client.
30
31 =head1 DESCRIPTION
32  
33 Net::HTTPServer::Response provides a nice OOP interface for easy control
34 of headers, cookies, sessions, and/or the content that will be sent to
35 the requesting client.
36  
37 =head1 EXAMPLES
38  
39 my $response = new Net::HTTPServer::Response();
40  
41  
42  
43 my $response = new Net::HTTPServer::Response(code=>200,
44 headers=>{
45 );
46  
47 my $response = $request->Response();
48  
49 =head1 METHODS
50  
51 =head2 new(%cfg)
52  
53 Given a config hash, return a server object that you can start, process,
54 and stop. The config hash takes the options:
55  
56 body => string - The contents of the response.
57 ( Default: "" )
58  
59 code => int - The return code of this reponse.
60 ( Default: 200 )
61
62 cookies => hashref - Hash reference to a set of cookies to send.
63 Most people should just use the Cookie method
64 to set these.
65 ( Default: {} )
66  
67 headers => hashref - Hash reference to the headers to send. Most
68 people should just use the Header method.
69 ( Default: {} )
70  
71 =head2 Body([string])
72  
73 Returns the current value of the response body. Sets the content of
74 the response if a value is specified.
75  
76 =head2 Clear()
77  
78 Reset the body to "".
79  
80 =head2 Code(int)
81  
82 Returns the current value of the response code. Set the status code
83 of the response if a value is specified.
84  
85 =head2 Cookie(name[,value[,%options]])
86  
87 Returns the cookie value for the specified name, or undef if it is
88 not set. If the value is also specified, then the cookie is set
89 to the value. The optional hash options that you can provide to
90 the cookie are:
91  
92 domain => string - If specified, the client will return the
93 cookie for any hostname that is part of
94 the domain.
95  
96 expires => string - When should the cookie expire. Must be
97 formatted according to the rules:
98 Wednesday, 30-June-2004 18:14:24 GMT
99 Optionally you can specify "now" which
100 will resolve to the current time.
101
102 path => string - The path on the server that the client should
103 return the cookie for.
104  
105 secure => 0|1 - The client will only return the cookie over
106 an HTTPS connection.
107  
108 =head2 Header(name[,value])
109  
110 Returns the header value for the specified name, or undef if it is not
111 set. If the value is specified, then the header is set to the value.
112  
113 =head2 Print(arg1[,arg2,...,argN])
114  
115 Appends the arguments to the end of the body.
116  
117 =head2 Redirect(url)
118  
119 Redirect the client to the specified URL.
120  
121 =head2 Session(object)
122  
123 Register the Net::HTTPServer::Session object with the response. When
124 the server builds the actual reponse to the client it will set the
125 appropriate cookie and save the session. If the response is
126 created from the request object, and there was a session created
127 from the request object then this, will be prepopulated with that
128 session.
129  
130 =head2 CaptureSTDOUT()
131  
132 If you use the CGI perl module then it wants to print everything to
133 STDOUT. CaptureSTDOUT() will put the Reponse object into a mode
134 where it will capture all the output from the module. See
135 ProcessSTDOUT() for more information.
136  
137 =head2 ProcessSTDOUT([%args])
138  
139 This will harvest all of the data printed to STDOUT and put it into
140 the Response object via a Print() call. This will also stop
141 monitoring STDOUT and release it. You can specify some options:
142  
143 strip_header => 0|1 - If you use the CGI module and you
144 print the headers then ProcessSTDOUT()
145 can try to strip those out. The best
146 plan is not to print them.
147
148 See CaptureSTDOUT() for more information.
149  
150 =head1 AUTHOR
151  
152 Ryan Eatmon
153  
154 =head1 COPYRIGHT
155  
156 Copyright (c) 2003-2005 Ryan Eatmon <reatmon@mail.com>. All rights
157 reserved. This program is free software; you can redistribute it
158 and/or modify it under the same terms as Perl itself.
159  
160 =cut
161
162 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
163 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
164 5 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
165 5 ▶use Net::HTTPServer::CaptureSTDOUT;
Subroutine BEGIN: called CC=1 SCAR=0.0
166  
167 5 ▶use vars qw ( $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
168  
169 5 $VERSION = "1.0.3";
170  
171 sub new
172 2 ▶{
Subroutine new: called CC=2 SCAR=7.0
Pod documented
173 2 my $proto = shift;
174 2 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
020
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
175 2 my $self = { };
176
177 2 bless($self, $proto);
178  
179 2 my (%args) = @_;
180  
181 2 $self->{ARGS} = \%args;
182  
183 2 $self->{CODE} = $self->_arg("code","200");
184 2 $self->{HEADERS} = $self->_arg("headers",{});
185 2 $self->{COOKIES} = $self->_arg("cookies",{});
186 2 $self->{BODY} = $self->_arg("body","");
187  
188 2 return $self;
189 }
190  
191  
192 sub Body
193 8 ▶{
Subroutine Body: called CC=2 SCAR=6.9
Pod documented
194 8 my $self = shift;
195 8 my $body = shift;
196  
197 8 ▶ return $self->{BODY} unless defined($body);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $body 7 1 8
198 1 $self->{BODY} = $body;
199 }
200  
201  
202 sub Clear
203 2 ▶{
Subroutine Clear: called CC=1 SCAR=0.0
Pod documented
204 2 my $self = shift;
205
206 2 $self->{BODY} = "";
207 }
208  
209  
210 sub Code
211 5 ▶{
Subroutine Code: called CC=2 SCAR=6.9
Pod documented
212 5 my $self = shift;
213 5 my $code = shift;
214  
215 5 ▶ return $self->{CODE} unless defined($code);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $code 3 2 5
216 2 $self->{CODE} = $code;
217 }
218  
219  
220 sub Cookie
221 2 ▶{
Subroutine Cookie: called CC=8 SCAR=21.1
Pod documented
222 2 my $self = shift;
223 2 my $cookie = shift;
224 2 my $value = shift;
225 2 my (%args) = @_;
226  
227 2 ▶ return unless (defined($cookie) && defined($value));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cookie and defined $value 0 2 2
Condition33%1/3
defined $cookie and defined $value
!ll&&!rl&&r
002
MC/DC0%0/2
defined $cookie and defined $value
defined $cookie defined $value
Truth table33%1/3
A and B
A= defined $cookieB= defined $value
ABresult
0X0
100
111
228
229 2 $self->{COOKIES}->{$cookie}->{value} = $value;
230 2 ▶ if (exists($args{expires}))
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"expires"}) 1 1 2
231 {
232 1 $self->{COOKIES}->{$cookie}->{expires} = $args{expires};
233 $self->{COOKIES}->{$cookie}->{expires} = &Net::HTTPServer::_date()
234 1 ▶ if ($args{expires} eq "now");
Branch: 1 branch
BranchTrueFalseTotal
if $args{"expires"} eq "now" 0 1 1
235 }
236 $self->{COOKIES}->{$cookie}->{domain} = $args{domain}
237 2 ▶ if exists($args{domain});
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"domain"} 1 1 2
238 $self->{COOKIES}->{$cookie}->{path} = $args{path}
239 2 ▶ if exists($args{path});
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"path"} 1 1 2
240 $self->{COOKIES}->{$cookie}->{secure} = $args{secure}
241 2 ▶ if exists($args{secure});
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"secure"} 1 1 2
242 }
243  
244  
245 sub Header
246 4 ▶{
Subroutine Header: called CC=4 SCAR=14.0
Pod documented
247 4 my $self = shift;
248 4 my $header = shift;
249 4 my $value = shift;
250  
251 4 ▶ return unless defined($header);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $header 0 4 4
252 4 ▶ $self->{HEADERS}->{$header} = $value if defined($value);
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 2 2 4
253 4 ▶ return unless exists($self->{HEADERS}->{$header});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"HEADERS"}{$header} 0 4 4
254 4 return $self->{HEADERS}->{$header};
255 }
256  
257  
258 sub Print
259 2 ▶{
Subroutine Print: called CC=1 SCAR=0.0
Pod documented
260 2 my $self = shift;
261
262 2 $self->{BODY} .= join("",@_);
263 }
264  
265  
266 sub Redirect
267 1 ▶{
Subroutine Redirect: called CC=1 SCAR=0.0
Pod documented
268 1 my $self = shift;
269 1 my $url = shift;
270  
271 1 $self->Code(307);
272 1 $self->Clear();
273 1 $self->Header("Location",$url);
274 }
275  
276  
277 sub Session
278 0 ▶{
Subroutine Session: not called CC=3 SCAR=24.8
Pod documented
279 0 my $self = shift;
280 0 my $session = shift;
281  
282 0 ▶ $self->{SESSION} = $session if defined($session);
Branch: 1 branch
BranchTrueFalseTotal
if defined $session 0 0 0
283 0 ▶ return unless exists($self->{SESSION});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"SESSION"} 0 0 0
284 0 return $self->{SESSION};
285 }
286  
287  
288 sub CaptureSTDOUT
289 0 ▶{
Subroutine CaptureSTDOUT: not called CC=2 SCAR=17.9
Pod documented
290 0 my $self = shift;
291  
292 0 ▶ if (tied *STDOUT)
Branch: 1 branch
BranchTrueFalseTotal
if (tied *STDOUT) 0 0 0
293 {
294 0 croak("You cannot call CaptureSTDOUT more than once without calling ProcessSTDOUT");
295 }
296  
297 0 tie(*STDOUT, "Net::HTTPServer::CaptureSTDOUT");
298 }
299  
300  
301 sub ProcessSTDOUT
302 0 ▶{
Subroutine ProcessSTDOUT: not called CC=3 SCAR=24.8
Pod documented
303 0 my $self = shift;
304 0 my (%args) = @_;
305  
306 0 my $output = join("",<STDOUT>);
307  
308 #--------------------------------------------------------------------------
309 # Try and strip out the headers if the user printed any...
310 #--------------------------------------------------------------------------
311 0 ▶ if (exists($args{strip_header}) && ($args{strip_header} == 1))
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"strip_header"} and $args{"strip_header"} == 1) 0 0 0
Condition0%0/3
exists $args{"strip_header"} and $args{"strip_header"} == 1
!ll&&!rl&&r
000
MC/DC0%0/2
exists $args{"strip_header"} and $args{"strip_header"} == 1
exists $args{"strip_header"} $args{"strip_header"} == 1
Truth table0%0/3
A and B
A= exists $args{"strip_header"}B= $args{"strip_header"} == 1
ABresult
0X0
100
111
312 {
313 0 $output =~ s/^.+\015?\012\015?\012//;
314 }
315  
316 0 $self->Print($output);
317  
318 0 untie(*STDOUT);
319 }
320  
321 ###############################################################################
322 #
323 # _arg - if the arg exists then use it, else use the default.
324 #
325 ###############################################################################
326 sub _arg
327 8 ▶{
Subroutine _arg: called CC=2 SCAR=7.0
328 8 my $self = shift;
329 8 my $arg = shift;
330 8 my $default = shift;
331  
332 8 ▶ return (exists($self->{ARGS}->{$arg}) ? $self->{ARGS}->{$arg} : $default);
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'ARGS'}{$arg} ? : 0 8 8
333 }
334  
335  
336 sub _build
337 9 ▶{
Subroutine _build: called CC=9 SCAR=24.5
338 9 my $self = shift;
339  
340 #-------------------------------------------------------------------------
341 # Format the return headers
342 #-------------------------------------------------------------------------
343 9 my $header = "HTTP/1.1 ".$self->{CODE}."\n";
344 0 foreach my $key (sort {$a cmp $b} keys(%{$self->{HEADERS}}))
345 {
346 8 $header .= "$key: ".$self->{HEADERS}->{$key}."\n";
347 }
348  
349 #-------------------------------------------------------------------------
350 # Session in, cookie out
351 #-------------------------------------------------------------------------
352 9 ▶ if (exists($self->{SESSION}))
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"SESSION"}) 0 9 9
353 {
354 0 my $value = $self->{SESSION}->_key();
355 0 my $delta = 4*60*60; # 4 hours from now
356  
357 0 ▶ if ($self->{SESSION}->_valid())
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'SESSION'}->_valid) { } 0 0 0
358 {
359 0 $self->{SESSION}->_save();
360 }
361 else
362 {
363 0 $value = "";
364 0 $delta = -(100*24*60*60); # 100 days ago
365 }
366  
367 0 $self->Cookie("NETHTTPSERVERSESSION",
368 $value,
369 expires=>&Net::HTTPServer::_date(time,$delta),
370 );
371 }
372  
373 #-------------------------------------------------------------------------
374 # Mmmm.... Cookies....
375 #-------------------------------------------------------------------------
376 0 foreach my $cookie (sort {$a cmp $b} keys(%{$self->{COOKIES}}))
377 {
378 6 my $value = uri_escape($self->{COOKIES}->{$cookie}->{value});
379
380 6 $header .= "Set-Cookie: $cookie=$value";
381
382 6 foreach my $key (sort {$a cmp $b} keys(%{$self->{COOKIES}->{$cookie}}))
383 {
384 26 ▶ next if ($key eq "value");
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "value" 6 20 26
385 20 ▶ if ($key eq "secure")
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq 'secure') { } 5 15 20
386 {
387 5 ▶ if ($self->{COOKIES}->{$cookie}->{$key} == 1)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"COOKIES"}{$cookie}{$key} == 1) 5 0 5
388 {
389 5 $header .= ";$key";
390 }
391 }
392 else
393 {
394 15 $header .= ";$key=".$self->{COOKIES}->{$cookie}->{$key};
395 }
396 }
397  
398 6 $header .= "\n";
399 }
400  
401 9 chomp($header);
402 9 $header .= "\r\n\r\n";
403  
404 9 return ($header,$self->{BODY});
405 }
406  
407  
408  
409  
410 5 1;
411  
« HTTPServer.pm ↑ Summary HTTPServer/Request.pm »