blib/lib/CGI/XMLPost.pm

filter: Statementstmt 47.7% 32 / 67 Branchbran 31.2% 5 / 16 Conditioncond 10.5% 2 / 19 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 61.5% 8 / 13 Podpod 100% 10 / 10 totaltotal 41.9% 57 / 136 CC 15 SCAR 23.8
CC
15
Cov
38%
response
30.0
encoding
12.0
header
12.0
CRAP 10.8
↑ Summary
1 package CGI::XMLPost;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 our $VERSION = '1.6';
9  
10 # Ripped off from CGI.pm
11  
12 2 our $CRLF;
13  
14 2 my $EBCDIC = "\t" ne "\011";
15  
16 2 ▶if ($^O eq 'VMS')
Branch: 2 branches
BranchTrueFalseTotal
if ($^O eq 'VMS') { } 0 2 2
elsif ($EBCDIC) { } 0 2 2
17 {
18 0 $CRLF = "\n";
19 }
20 elsif ($EBCDIC)
21 {
22 0 $CRLF= "\r\n";
23 }
24 else
25 {
26 2 $CRLF = "\015\012";
27 }
28  
29 =head1 NAME
30  
31 CGI::XMLPost - receive XML file as an HTTP POST
32  
33 =head1 SYNOPSIS
34  
35 use CGI::XMLPost;
36  
37 my $xmlpost = CGI::XMLPost->new();
38  
39 my $xml = $xmlpost->data();
40  
41 # ... do something with $xml
42  
43 =head1 DESCRIPTION
44  
45 CGI::XMLPost is a lightweight module for receiving XML documents in the
46 body of an HTTP request. It provides some utility methods that make it
47 easier to work in a CGI environment without requiring any further modules.
48  
49 =head1 METHODS
50  
51  
52 =over 4
53  
54 =cut
55  
56  
57 =item new
58  
59 This is the constructor of the class. If it succeeds in reading the POST
60 data correct it will return a a blessed object - otherwise undef.
61  
62 The arguments are in the form of a hash reference - the keys are :
63  
64 =over 2
65  
66 =item strict
67  
68 If this is set to a true value then the HTTP request method and content type
69 are checked. If the first is not POST and the second does not match 'xml$'
70 then the method will return undef.
71  
72 =back
73  
74 =cut
75  
76 sub new
77 1 ▶{
Subroutine new: called CC=6 SCAR=20.8
Pod documented
78 1 my ( $proto, $args ) = @_;
79  
80 1 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
81  
82
83 1 my $self = bless {}, $class;
84  
85 1 ▶ if ( $args->{strict} )
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"strict"}) 1 0 1
86 {
87 1 ▶ if ( $self->request_method() ne 'POST' or $self->content_type !~ /xml$/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->request_method ne "POST" or not $self->content_type =~ /xml$/) 0 1 1
Condition33%1/3
$self->request_method ne "POST" or not $self->content_type =~ /xml$/
l!l&&r!l&&!r
001
MC/DC0%0/2
$self->request_method ne "POST" or not $self->content_type =~ /xml$/
$self->request_method ne "POST" not $self->content_type =~ /xml$/
Truth table33%1/3
A or not(B)
A= $self->request_method ne "POST"B= not $self->content_type =~ /xml$/
ABresult
1X1
011
000
88 {
89 0 return undef;
90 }
91 }
92  
93 1 my $cl = $self->content_length();
94  
95 1 ▶ if ( sysread( STDIN, $self->{_data}, $cl) == $cl )
Branch: 1 branch
BranchTrueFalseTotal
if (sysread(STDIN, $self->{"_data"}, $cl) == $cl) 1 0 1
96 {
97 1 return $self;
98 }
99 }
100  
101 =item content_type
102  
103 Returns the content type of the HTTP request.
104  
105 =cut
106  
107 sub content_type
108 2 ▶{
Subroutine content_type: called CC=1 SCAR=0.0
Pod documented
109 2 my ( $self ) = @_;
110  
111 2 return $ENV{CONTENT_TYPE};
112 }
113  
114 =item request_method
115  
116 Returns the request method of the HTTP request.
117  
118 =cut
119  
120 sub request_method
121 1 ▶{
Subroutine request_method: called CC=1 SCAR=0.0
Pod documented
122 1 my ( $self ) = @_;
123  
124 1 return $ENV{REQUEST_METHOD};
125 }
126  
127  
128 =item content_length
129  
130 Returns the content length of the request.
131  
132 =cut
133  
134 sub content_length
135 2 ▶{
Subroutine content_length: called CC=1 SCAR=0.0
Pod documented
136 2 my ( $self ) = @_;
137  
138 2 return $ENV{CONTENT_LENGTH};
139 }
140  
141 =item data
142  
143 Returns the data as read from the body of the HTTP request.
144  
145 =cut
146  
147 sub data
148 1 ▶{
Subroutine data: called CC=1 SCAR=0.0
Pod documented
149 1 my ( $self ) = @_;
150  
151 1 return $self->{_data};
152 }
153  
154 =item encoding
155  
156 Gets or sets the encoding used in the response. The default is utf-8
157  
158 =cut
159  
160 sub encoding
161 0 ▶{
Subroutine encoding: not called CC=3 SCAR=24.8
Pod documented
162 0 my ( $self, $encoding ) = @_;
163  
164 0 ▶ if ( defined $encoding )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $encoding) 0 0 0
165 {
166 0 $self->{_encoding} = $encoding;
167 }
168  
169 0 ▶ return $self->{_encoding} || 'utf-8';
Condition0%0/2
$self->{'_encoding'} || 'utf-8'
l!l
00
MC/DC0%0/1
$self->{'_encoding'} || 'utf-8'
$self->{'_encoding'}
Truth table0%0/2
A
A= $self->{'_encoding'}
Aresult
11
00
170 }
171  
172 =item header
173  
174 Returns a header suitable to be used in an HTTP response. The arguments are
175 in the form of key/value pairs - valid keys are :
176  
177 =over 2
178  
179 =item status
180  
181 The HTTP status code to be returned - the default is 200 (OK).
182  
183 =item type
184  
185 The content type of the response - the default is 'application/xml'.
186  
187 =back
188  
189 =cut
190  
191 sub header
192 0 ▶{
Subroutine header: not called CC=3 SCAR=24.8
Pod documented
193 0 my ( $self, %args ) = @_;
194  
195 0 my @header;
196  
197 0 ▶ $self->{status} = $args{status} || 200;
Condition0%0/2
$args{'status'} || 200
l!l
00
MC/DC0%0/1
$args{'status'} || 200
$args{'status'}
Truth table0%0/2
A
A= $args{'status'}
Aresult
11
00
198  
199 0 push @header, "Status: $self->{status}";
200  
201 0 ▶ $self->{type} = $args{type} || 'application/xml';
Condition0%0/2
$args{'type'} || 'application/xml'
l!l
00
MC/DC0%0/1
$args{'type'} || 'application/xml'
$args{'type'}
Truth table0%0/2
A
A= $args{'type'}
Aresult
11
00
202  
203 0 my $charset = $self->encoding();
204  
205 0 push @header, "Content-Type: $self->{type}; charset=$charset";
206  
207 0 my $header = join $CRLF, @header;
208  
209 0 $header .= $CRLF x 2;
210  
211 0 return $header;
212  
213 }
214  
215 2 my %status_codes = (
216 200 => "OK",
217 405 => "Method Not Allowed",
218 415 => "Unsupported Media Type",
219 400 => "Bad Request",
220 );
221  
222 =item response
223  
224 Returns a string that is suitable to be sent in the body of the response.
225 The default is to return an XML string of the form :
226  
227 <?xml version="1.0" encoding="iso-8859-1"?>
228 <Response>
229 <Code>$status</Code>
230 <Text>$text</Text>
231 </Response>
232
233 Where $status is the status code used in the header as described above and
234 $text is the desciptive text for that status. If a different text is required
235 this can be supplied with the argument key 'text'.
236  
237 =cut
238  
239 sub response
240 0 ▶{
Subroutine response: not called CC=5 SCAR=34.0
Pod documented
241 0 my ( $self, %args ) = @_;
242  
243 0 ▶ my $status = $self->{status} || 200;
Condition0%0/2
$self->{'status'} || 200
l!l
00
MC/DC0%0/1
$self->{'status'} || 200
$self->{'status'}
Truth table0%0/2
A
A= $self->{'status'}
Aresult
11
00
244 0 ▶ my $text = $args{text} || $status_codes{$status};
Condition0%0/3
$args{'text'} || $status_codes{$status}
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{'text'} || $status_codes{$status}
$args{'text'} $status_codes{$status}
Truth table0%0/3
A || B
A= $args{'text'}B= $status_codes{$status}
ABresult
1X1
011
000
245  
246 0 ▶ my $type = $self->{type} || 'application/xml';
Condition0%0/2
$self->{'type'} || 'application/xml'
l!l
00
MC/DC0%0/1
$self->{'type'} || 'application/xml'
$self->{'type'}
Truth table0%0/2
A
A= $self->{'type'}
Aresult
11
00
247  
248 0 my $response;
249  
250 0 my $encoding = $self->encoding();
251  
252 0 ▶ if ( $type =~ /xml$/i )
Branch: 1 branch
BranchTrueFalseTotal
if ($type =~ /xml$/i) { } 0 0 0
253 {
254 0 $response =<<EOX;
255 <?xml version="1.0" encoding="$encoding"?>
256 <Response>
257 <Code>$status</Code>
258 <Text>$text</Text>
259 </Response>
260 EOX
261 }
262 else
263 {
264 0 $response = $text;
265 }
266 0 return $response;
267 }
268  
269 =item remote_address
270  
271 Remotes the address of the remote peer if it is known.
272  
273 =cut
274  
275 sub remote_address
276 0 ▶{
Subroutine remote_address: not called CC=1 SCAR=6.9
Pod documented
277 0 my ( $self ) = @_;
278 0 return $ENV{REMOTE_ADDRESS};
279 }
280  
281 =item as_xpath
282  
283 Returns an XML::XPath object inititialized with the received XML or a false
284 value if XML::XPath is not present or the parse failed.
285  
286 =cut
287  
288 sub as_xpath
289 0 ▶{
Subroutine as_xpath: not called CC=2 SCAR=17.9
Pod documented
290 0 my ( $self ) = @_;
291  
292 0 my $got_xpath = undef;
293  
294 eval
295 0 {
296 0 require XML::XPath;
297 0 $got_xpath = 1;
298 };
299  
300 0 ▶ return $got_xpath ? XML::XPath->new(xml => $self->data()) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$got_xpath ? : 0 0 0
301 }
302  
303 2 1;
304 __END__
↑ Summary