lib/OpenFrame/Segment/HTTP/Request.pm

filter: Statementstmt 62.9% 73 / 116 Branchbran 17.6% 6 / 34 Conditioncond 16.6% 1 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 94.4% 17 / 18 Podpod 25.0% 2 / 8 totaltotal 53.2% 99 / 186 CC 22 SCAR 35.0
CC
22
Cov
51%
parse_multipart_data
110.0
req2args
20.8
req2cookie_tin
3.6
CRAP 33.0
↑ Summary blib/lib/OpenFrame/Cookie.pm »
1 package OpenFrame::Segment::HTTP::Request;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use CGI;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Pipeline::Segment;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use OpenFrame::Object;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use OpenFrame::Cookies;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use OpenFrame::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use OpenFrame::Argument::Blob;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use OpenFrame::Segment::HTTP::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 ▶use File::Temp qw ( tempfile );
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 ▶use base qw ( Pipeline::Segment OpenFrame::Object );
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 1 our $VERSION=3.05;
18  
19 1 ▶sub init {
Subroutine init: called CC=1 SCAR=0.0
Pod documented
20 1 my $self = shift;
21 1 $self->respond( 1 );
22 1 $self->SUPER::init( @_ );
23 }
24  
25 3 ▶sub respond {
Subroutine respond: called CC=2 SCAR=6.9
Pod undocumented
26 3 my $self = shift;
27 3 my $respond = shift;
28 3 ▶ if (defined( $respond )) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $respond) { } 2 1 3
29 2 $self->{respond} = $respond;
30 2 return $self;
31 } else {
32 1 return $self->{respond};
33 }
34 }
35  
36 1 ▶sub dispatch {
Subroutine dispatch: called CC=3 SCAR=11.3
Pod documented
37 1 my $self = shift;
38 1 my $pipe = shift;
39
40 1 my $store = $pipe->store();
41
42 1 my $httpr = $store->get('HTTP::Request');
43  
44 1 ▶ return undef unless $httpr;
Branch: 1 branch
BranchTrueFalseTotal
unless $httpr 0 1 1
45  
46 1 my ($ofr,$cookies) = $self->req2ofr( $httpr );
47  
48 1 ▶ if ($self->respond) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->respond) { } 1 0 1
49 1 return ($ofr, $cookies, OpenFrame::Segment::HTTP::Response->new());
50 } else {
51 0 return ($ofr, $cookies);
52 }
53 }
54  
55 ##
56 ## turns an HTTP::Request object into an OpenFrame::Request object
57 ##
58 1 ▶sub req2ofr {
Subroutine req2ofr: called CC=1 SCAR=0.0
Pod undocumented
59 1 my $self = shift;
60 1 my $r = shift;
61 1 my $uri = $r->uri();
62 1 my $args = $self->req2args( $r );
63 1 my $ctin = $self->req2cookie_tin( $r );
64  
65 1 my $ofr = OpenFrame::Request->new()
66 ->arguments( $args )
67 ->uri( $uri );
68  
69 1 return ($ofr,$ctin);
70 }
71  
72 1 ▶sub req2cookie_tin {
Subroutine req2cookie_tin: called CC=3 SCAR=12.7
Pod undocumented
73 1 my $self = shift;
74 1 my $r = shift;
75 1 my $ctin = OpenFrame::Cookies->new();
76  
77 1 ▶ if ($r->header('Cookie')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->header("Cookie")) 0 1 1
78 0 foreach my $ctext (split(/; ?/, $r->header('Cookie'))) {
79 0 my ($cname, $cvalue) = split /=/, $ctext;
80 0 $ctin->set( $cname, $cvalue );
81 }
82 }
83  
84 1 return $ctin;
85 }
86  
87 1 ▶sub params2hash {
Subroutine params2hash: called CC=2 SCAR=11.6
Pod undocumented
88 1 my $self = shift;
89 1 my $data = shift;
90 1 my $cgi = CGI->new($data);
91 return {
92 map {
93 0 my $return;
94 0 my @results = $cgi->param($_);
95 0 ▶ if (scalar(@results) > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @results > 1) { } 0 0 0
96 0 $return = [@results];
97 } else {
98 0 $return = $results[0];
99 }
100 0 ($_, $return)
101 } $cgi->param()
102 };
103 }
104  
105  
106 ##
107 ## shameless copied from acme's original
108 ##
109 1 ▶sub req2args {
Subroutine req2args: called CC=7 SCAR=30.3
Pod undocumented
110 1 my $self = shift;
111 1 my $r = shift;
112 1 my $args = {};
113  
114 1 my $method = $r->method;
115  
116 1 ▶ if ($method eq 'GET' || $method eq 'HEAD') {
Branch: 2 branches
BranchTrueFalseTotal
if ($method eq 'GET' or $method eq 'HEAD') { } 1 0 1
elsif ($method eq 'POST') { } 0 0 0
Condition33%1/3
$method eq "GET" or $method eq "HEAD"
l!l&&r!l&&!r
100
MC/DC0%0/2
$method eq "GET" or $method eq "HEAD"
$method eq "GET" $method eq "HEAD"
Truth table33%1/3
A or B
A= $method eq "GET"B= $method eq "HEAD"
ABresult
1X1
011
000
117 1 $args = $self->params2hash($r->uri->equery);
118 1 $r->uri->query(undef);
119 } elsif ($method eq 'POST') {
120 0 my $content_type = $r->content_type;
121 0 ▶ if (!$content_type || $content_type eq "application/x-www-form-urlencoded") {
Branch: 2 branches
BranchTrueFalseTotal
if (not $content_type or $content_type eq 'application/x-www-form-urlencoded') { } 0 0 0
elsif ($content_type eq 'multipart/form-data') { } 0 0 0
Condition0%0/3
not $content_type or $content_type eq "application/x-www-form-urlencoded"
l!l&&r!l&&!r
000
MC/DC0%0/2
not $content_type or $content_type eq "application/x-www-form-urlencoded"
not $content_type $content_type eq "application/x-www-form-urlencoded"
Truth table0%0/3
not(A) or B
A= not $content_typeB= $content_type eq "application/x-www-form-urlencoded"
ABresult
1X1
011
000
122 0 $args = $self->params2hash($r->content);
123 0 $r->uri->query(undef);
124 } elsif ($content_type eq "multipart/form-data") {
125 0 $args = $self->parse_multipart_data($r);
126 } else {
127 0 $self->emit("invalid content type: $content_type");
128 }
129 } else {
130 0 $self->emit("unsupported method: $method");
131 }
132  
133 1 return $args;
134 }
135  
136 0 ▶sub parse_multipart_data {
Subroutine parse_multipart_data: not called CC=10 SCAR=47.0
Pod undocumented
137 0 my $self = shift;
138 0 my $r = shift;
139 0 my $args = {};
140  
141 0 my($boundary) = $r->headers->header("Content-Type") =~ /boundary=(\S+)$/;
142  
143 0 foreach my $part (split(/-?-?$boundary-?-?/, $r->content)) {
144 0 $part =~ s|^\r\n||g;
145 0 ▶ next unless $part;
Branch: 1 branch
BranchTrueFalseTotal
unless $part 0 0 0
146 0 my %headers;
147 0 my @lines = split /\r\n/, $part;
148 0 ▶ while (@lines) {
Branch: 1 branch
BranchTrueFalseTotal
if (@lines) 0 0 0
149 0 my $line = shift @lines;
150 0 ▶ last unless $line;
Branch: 1 branch
BranchTrueFalseTotal
unless $line 0 0 0
151 0 ▶ $headers{type} = $1 if $line =~ /^content-type: (.+)$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^content-type: (.+)$/i 0 0 0
152 0 ▶ $headers{disposition} = $1 if $line =~ /^content-disposition: (.+)$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^content-disposition: (.+)$/i 0 0 0
153 }
154  
155 0 ▶ my $name = $1 if $headers{disposition} =~ /name="(.+?)"/;
Branch: 1 branch
BranchTrueFalseTotal
if $headers{"disposition"} =~ /name="(.+?)"/ 0 0 0
156  
157 0 ▶ if ($headers{disposition} =~ /filename="(.+)?"/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($headers{'disposition'} =~ /filename="(.+)?"/) { } 0 0 0
158 0 my $filename = $1;
159  
160  
161 0 my $fh = tempfile(DIR => "/tmp/", UNLINK => 1);
162 0 print $fh join("\r\n", @lines);
163 0 $fh->seek(0, 0);
164 0 my $blob = OpenFrame::Argument::Blob->new()
165 ->name( $name )
166 ->filehandle( $fh );
167  
168 0 ▶ if ($filename) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filename) 0 0 0
169 0 $blob->filename( $filename );
170 }
171  
172 0 $args->{$name} = $blob;
173 } else {
174 0 $args->{$name} = join("\n", @lines);
175 }
176 }
177  
178 0 return $args;
179 }
180  
181 1 1;
182  
183 =head1 NAME
184  
185 OpenFrame::Segment::HTTP::Request - creates an OpenFrame::Request object from an HTTP::Request
186  
187 =head1 SYNOPSIS
188  
189 use OpenFrame::Segment::HTTP::Request;
190 my $response_creator = OpenFrame::Segment::HTTP::Request->new();
191 $pipeline->add_segment( $request );
192  
193 =head1 DESCRIPTION
194  
195 OpenFrame::Segment::HTTP::Request inherits from Pipeline::Segment and is used to turn an
196 HTTP::Request object into an OpenFrame::Request. Additionally it provides the C<respond()>
197 method that acts as a get/set method to decide whether or not OpenFrame::Segment::HTTP::Request
198 places an OpenFrame::Segment::HTTP::Response segment on the pipeline cleanup list. By default
199 C<respond()> is set to a true value.
200  
201 =head1 AUTHOR
202  
203 James A. Duncan <jduncan@fotango.com>
204  
205 =head1 SEE ALSO
206  
207 OpenFrame::Segment::HTTP::Response, Pipeline::Segment
208  
209 =cut
210  
↑ Summary blib/lib/OpenFrame/Cookie.pm »