lib/SOAP/Lite/Packager.pm

filter: Statementstmt 30.5% 63 / 206 Branchbran 0.0% 0 / 68 Conditioncond 0.0% 0 / 19 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 48.6% 18 / 37 Podpod 71.4% 5 / 7 totaltotal 24.7% 86 / 348 CC 48 SCAR 38.0
CC
48
Cov
22%
process_related
182.0
unpackage
90.0
package
31.9
CRAP 44.8
« lib/SOAP/Transport/POP3.pm ↑ Summary lib/SOAP/Lite.pm »
1 # ======================================================================
2 #
3 # Copyright (C) 2000-2004 Paul Kulchenko (paulclinger@yahoo.com)
4 # SOAP::Lite is free software; you can redistribute it
5 # and/or modify it under the same terms as Perl itself.
6 #
7 # ======================================================================
8  
9 package SOAP::Lite::Packager;
10  
11 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 our $VERSION = '1.27'; # VERSION
14  
15 1 ▶use vars;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 1 ▶use vars qw($SUPPORTED_TYPES);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 $SUPPORTED_TYPES = { };
19  
20 1 ▶sub BEGIN {
Subroutine BEGIN: called CC=2 SCAR=9.6
21 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 for my $method ( qw(parser headers_http persist_parts) ) {
23 3 my $field = '_' . $method;
24 0 ▶ *$method = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
25 0 my $self = shift;
26 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
27 0 $self->{$field} = shift;
28 0 return $self
29 }
30 0 return $self->{$field};
31 }
32 3 }
33 }
34  
35 2 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
36 2 my($class) = shift;
37 2 my(%params) = @_;
38 2 bless {
39 "_parts" => [ ],
40 "_parser" => undef,
41 "_persist_parts" => 0,
42 }, $class;
43 }
44  
45 1 ▶sub is_supported_part {
Subroutine is_supported_part: called CC=1 SCAR=0.0
Pod undocumented
46 1 my $self = shift;
47 1 return $SUPPORTED_TYPES->{ref $_[0]};
48 }
49  
50 0 ▶sub parts {
Subroutine parts: not called CC=2 SCAR=17.9
Pod documented
51 0 my $self = shift;
52 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
53 0 $self->{'_parts'} = shift;
54 }
55 0 return $self->{'_parts'};
56 }
57  
58 # This is a static method that helps find the right Packager
59 0 ▶sub find_packager {
Subroutine find_packager: not called CC=1 SCAR=0.0
Pod undocumented
60 # TODO - Input:
61 # * the mimetype of the data to be decoded raw data that needs
62 # * the data to be decoded
63 # Returns:
64 # * the proper SOAP::Lite::Packager instance
65 }
66  
67 0 ▶sub push_part {
Subroutine push_part: not called CC=1 SCAR=6.9
Pod documented
68 0 my $self = shift;
69 0 my ($part) = @_;
70 0 push @{$self->{'_parts'}}, $part;
71 }
72  
73 0 ▶sub package {
Subroutine package: not called CC=1 SCAR=6.9
Pod documented
74 # do nothing
75 0 die "SOAP::Lite::Packager::package() must be implemented";
76 }
77  
78 0 ▶sub unpackage {
Subroutine unpackage: not called CC=2 SCAR=17.9
Pod documented
79 0 my $self = shift;
80 0 ▶ $self->{'_parts'} = [] if !$self->persist_parts; # experimental
Branch: 1 branch
BranchTrueFalseTotal
if not $self->persist_parts 0 0 0
81 }
82  
83 # ======================================================================
84  
85 package SOAP::Lite::Packager::MIME;
86  
87 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
88 1 ▶use vars qw(@ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
89 1 @ISA = qw(SOAP::Lite::Packager);
90  
91 1 ▶sub BEGIN {
Subroutine BEGIN: called CC=2 SCAR=9.6
92 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
93 1 for my $method ( qw(transfer_encoding env_id env_location) ) {
94 3 my $field = '_' . $method;
95 0 ▶ *$method = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
96 0 my $self = shift;
97 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
98 0 $self->{$field} = shift;
99 0 return $self
100 }
101 0 return $self->{$field};
102 }
103 3 }
104 }
105  
106 1 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
107 1 my ($classname) = @_;
108 1 my $self = SOAP::Lite::Packager::new(@_);
109 1 $self->{'_content_encoding'} = '8bit';
110 1 $self->{'_env_id'} = '<main_envelope>';
111 1 $self->{'_env_location'} = '/main_envelope';
112 1 bless $self, $classname;
113 1 $SOAP::Lite::Packager::SUPPORTED_TYPES->{"MIME::Entity"} = 1;
114 1 return $self;
115 }
116  
117 0 ▶sub initialize_parser {
Subroutine initialize_parser: not called CC=2 SCAR=17.9
118 0 my $self = shift;
119 0 eval "require MIME::Parser;";
120 0 ▶ die "Could not find MIME::Parser - is MIME::Tools installed? Aborting." if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
121 0 $self->{'_parser'} = MIME::Parser->new;
122 0 $self->{'_parser'}->output_to_core('ALL');
123 0 $self->{'_parser'}->tmp_to_core(1);
124 0 $self->{'_parser'}->ignore_errors(1);
125 0 $self->{'_parser'}->extract_nested_messages(0);
126 }
127  
128 0 ▶sub generate_random_string {
Subroutine generate_random_string: not called CC=2 SCAR=17.9
129 0 my ($self,$len) = @_;
130 0 my @chars=('a'..'z','A'..'Z','0'..'9','_');
131 0 my $random_string;
132 0 foreach (1..$len) {
133 0 $random_string .= $chars[rand @chars];
134 }
135 0 return $random_string;
136 }
137  
138 0 ▶sub get_multipart_id {
Subroutine get_multipart_id: not called CC=3 SCAR=24.8
139 0 my ($id) = shift;
140 0 ▶ ($id || '') =~ /^<?([^>]+)>?$/; $1 || '';
Branch: 1 branch
BranchTrueFalseTotal
$1 or "" 0 0 0
Condition0%0/2
$id || ''
l!l
00
MC/DC0%0/1
$id || ''
$id
Truth table0%0/2
A
A= $id
Aresult
11
00
141 }
142  
143 0 ▶sub package {
Subroutine package: not called CC=5 SCAR=30.9
144 0 my $self = shift;
145 0 my ($envelope,$context) = @_;
146 0 ▶ return $envelope if (!$self->parts); # if there are no parts,
Branch: 1 branch
BranchTrueFalseTotal
unless $self->parts 0 0 0
147 # then there is nothing to do
148 0 require MIME::Entity;
149 0 local $MIME::Entity::BOUNDARY_DELIMITER = "\r\n";
150 0 my $top = MIME::Entity->build('Type' => "Multipart/Related");
151 0 ▶ my $soapversion = defined($context) ? $context->soapversion : '1.1';
Branch: 1 branch
BranchTrueFalseTotal
defined $context ? : 0 0 0
152 0 ▶ $top->attach('Type' => $soapversion == 1.1 ? "text/xml" : "application/soap+xml",
Branch: 1 branch
BranchTrueFalseTotal
$soapversion == 1.1 ? : 0 0 0
153 'Content-Transfer-Encoding' => $self->transfer_encoding(),
154 'Content-Location' => $self->env_location(),
155 'Content-ID' => $self->env_id(),
156 'Data' => $envelope );
157 # consume the attachments that come in as input by 'shift'ing
158 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
159 0 ▶ while (my $part = shift(@{$self->parts})) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $part = shift @{$self->parts;}) 0 0 0
160 0 $top->add_part($part);
161 }
162 # determine MIME boundary
163 0 my $boundary = $top->head->multipart_boundary;
164 0 $self->headers_http({ 'Content-Type' => 'Multipart/Related; type="text/xml"; start="<main_envelope>"; boundary="'.$boundary.'"'});
165 0 return $top->stringify_body;
166 }
167  
168 0 ▶sub unpackage {
Subroutine unpackage: not called CC=9 SCAR=45.0
169 0 my $self = shift;
170 0 my ($raw_input,$context) = @_;
171 0 $self->SUPER::unpackage();
172  
173 # Parse the raw input into a MIME::Entity structure.
174 # - fail if the raw_input is not MIME formatted
175 0 ▶ $self->initialize_parser() if !defined($self->parser);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->parser 0 0 0
176 0 ▶ my $entity = eval { $self->parser->parse_data($raw_input) }
Branch: 1 branch
BranchTrueFalseTotal
my $entity = eval { do { $self->parser->parse_data($raw_input) } } or die "Something wrong with MIME message: @{[$@ || $self->parser->last_error];}\n" 0 0 0
177 0 ▶ or die "Something wrong with MIME message: @{[$@ || $self->parser->last_error]}\n";
Condition0%0/3
$@ || $self->parser->last_error
l!l&&r!l&&!r
000
MC/DC0%0/2
$@ || $self->parser->last_error
$@ $self->parser->last_error
Truth table0%0/3
A || B
A= $@B= $self->parser->last_error
ABresult
1X1
011
000
178  
179 0 my $env = undef;
180 # major memory bloat below! TODO - fix!
181 0 ▶ if (lc($entity->head->mime_type) eq 'multipart/form-data') {
Branch: 3 branches
BranchTrueFalseTotal
if (lc $entity->head->mime_type eq 'multipart/form-data') { } 0 0 0
elsif (lc $entity->head->mime_type eq 'multipart/related') { } 0 0 0
elsif (lc $entity->head->mime_type eq 'text/xml') { } 0 0 0
182 0 $env = $self->process_form_data($entity);
183 }
184 elsif (lc($entity->head->mime_type) eq 'multipart/related') {
185 0 $env = $self->process_related($entity);
186 }
187 elsif (lc($entity->head->mime_type) eq 'text/xml') {
188 # I don't think this ever gets called.
189 # warn "I am somewhere in the SOAP::Lite::Packager::MIME code I didn't know I would be in!";
190 0 $env = $entity->bodyhandle->as_string;
191 }
192 else {
193 0 die "Can't handle MIME messsage with specified type (@{[$entity->head->mime_type]})\n";
194 }
195  
196 # return the envelope
197 0 ▶ if ($env) {
Branch: 2 branches
BranchTrueFalseTotal
if ($env) { } 0 0 0
elsif ($entity->bodyhandle->as_string) { } 0 0 0
198 0 return $env;
199 }
200 elsif ($entity->bodyhandle->as_string) {
201 0 return $entity->bodyhandle->as_string;
202 }
203 else {
204 0 die "No content in MIME message\n";
205 }
206 }
207  
208 0 ▶sub process_form_data {
Subroutine process_form_data: not called CC=3 SCAR=24.8
209 0 my ($self, $entity) = @_;
210 0 my $env = undef;
211 0 foreach my $part ($entity->parts()) {
212 0 my $name = $part->head()->mime_attr('content-disposition.name');
213 0 ▶ $name eq 'payload' ?
Branch: 1 branch
BranchTrueFalseTotal
$name eq 'payload' ? : 0 0 0
214 $env = $part->bodyhandle()->as_string()
215 : $self->push_part($part);
216 }
217 0 return $env;
218 }
219  
220 0 ▶sub process_related {
Subroutine process_related: not called CC=13 SCAR=52.0
221 0 my $self = shift;
222 0 my ($entity) = @_;
223 0 ▶ die "Multipart MIME messages MUST declare Multipart/Related content-type"
Branch: 1 branch
BranchTrueFalseTotal
unless $entity->head->mime_attr("content-type") =~ m[^multipart/related]i 0 0 0
224 if ($entity->head->mime_attr('content-type') !~ /^multipart\/related/i);
225 # As it turns out, the Content-ID and start parameters are optional
226 # according to the MIME and SOAP specs. In the event that the head cannot
227 # be found, the head/root entity is used as a starting point.
228 0 my $start = get_multipart_id($entity->head->mime_attr('content-type.start'));
229 0 ▶ if (!defined($start) || $start eq "") {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $start or $start eq "") 0 0 0
Condition0%0/3
not defined $start or $start eq ""
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $start or $start eq ""
not defined $start $start eq ""
Truth table0%0/3
not(A) or B
A= not defined $startB= $start eq ""
ABresult
1X1
011
000
230 0 $start = $self->generate_random_string(10);
231 0 $entity->parts(0)->head->add('content-id',$start);
232 }
233 0 ▶ my $location = $entity->head->mime_attr('content-location')
Condition0%0/2
$entity->head->mime_attr('content-location') || 'thismessage:/'
l!l
00
MC/DC0%0/1
$entity->head->mime_attr('content-location') || 'thismessage:/'
$entity->head->mime_attr('content-location')
Truth table0%0/2
A
A= $entity->head->mime_attr('content-location')
Aresult
11
00
234 || 'thismessage:/';
235 0 my $env;
236 0 foreach my $part ($entity->parts) {
237 0 ▶ next if !UNIVERSAL::isa($part => "MIME::Entity");
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($part, "MIME::Entity") 0 0 0
238  
239 # Weird, the following use of head->get(SCALAR[,INDEX]) doesn't work as
240 # expected. Work around is to eliminate the INDEX.
241 0 my $pid = get_multipart_id($part->head->mime_attr('content-id'));
242  
243 # If Content-ID is not supplied, then generate a random one (HACK - because
244 # MIME::Entity does not do this as it should... content-id is required
245 # according to MIME specification)
246 0 ▶ $pid = $self->generate_random_string(10) if $pid eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $pid eq "" 0 0 0
247 0 my $type = $part->head->mime_type;
248  
249 # If a Content-Location header cannot be found, this will look for an
250 # alternative in the following MIME Header attributes
251 0 ▶ my $plocation = $part->head->get('content-location')
Condition0%0/6
$part->head->get('content-location') || $part->head->mime_attr('Content-Disposition.filename') || $part->head->mime_attr('Content-Type.name')
l!l&&r!l&&!r
000
$part->head->get("content-location") or $part->head->mime_attr("Content-Disposition.filename")
l!l&&r!l&&!r
000
MC/DC0%0/3
$part->head->get('content-location') || $part->head->mime_attr('Content-Disposition.filename') || $part->head->mime_attr('Content-Type.name')
$part->head->get("content-location") $part->head->mime_attr("Content-Disposition.filename") $part->head->mime_attr('Content-Type.name')
Truth table0%0/4
A or B || C
A= $part->head->get("content-location")B= $part->head->mime_attr("Content-Disposition.filename")C= $part->head->mime_attr('Content-Type.name')
ABCresult
1XX1
01X1
0011
0000
252 || $part->head->mime_attr('Content-Disposition.filename')
253 || $part->head->mime_attr('Content-Type.name');
254 0 ▶ if ($start && $pid eq $start) {
Branch: 1 branch
BranchTrueFalseTotal
if ($start and $pid eq $start) { } 0 0 0
Condition0%0/3
$start and $pid eq $start
!ll&&!rl&&r
000
MC/DC0%0/2
$start and $pid eq $start
$start $pid eq $start
Truth table0%0/3
A and B
A= $startB= $pid eq $start
ABresult
0X0
100
111
255 0 $env = $part->bodyhandle->as_string;
256 }
257 else {
258 0 ▶ $self->push_part($part) if (defined($part->bodyhandle));
Branch: 1 branch
BranchTrueFalseTotal
if defined $part->bodyhandle 0 0 0
259 }
260 }
261  
262 0 return $env;
263 }
264  
265 # ======================================================================
266  
267 package SOAP::Lite::Packager::DIME;
268  
269 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
270 1 ▶use vars qw(@ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
271 1 @ISA = qw(SOAP::Lite::Packager);
272  
273 1 ▶sub BEGIN {
Subroutine BEGIN: called CC=2 SCAR=9.6
274 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
275 1 for my $method ( qw(foo) ) {
276 1 my $field = '_' . $method;
277 0 ▶ *$method = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
278 0 my $self = shift;
279 0 ▶ if (@_) { $self->{$field} = shift; return $self }
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
280 0 return $self->{$field};
281 }
282 1 }
283 }
284  
285 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
286 0 my ($classname) = @_;
287 0 my $self = SOAP::Lite::Packager::new(@_);
288 0 bless $self, $classname;
289 0 $SOAP::Lite::Packager::SUPPORTED_TYPES->{"DIME::Payload"} = 1;
290 0 return $self;
291 }
292  
293 0 ▶sub initialize_parser {
Subroutine initialize_parser: not called CC=2 SCAR=17.9
294 0 my $self = shift;
295 0 print STDERR "Initializing parser\n";
296 0 eval "require DIME::Parser;";
297 0 ▶ die "Could not find DIME::Parser - is DIME::Tools installed? Aborting." if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
298 0 $self->{'_parser'} = DIME::Parser->new;
299 }
300  
301 0 ▶sub package {
Subroutine package: not called CC=6 SCAR=34.6
302 0 my $self = shift;
303 0 my ($envelope,$context) = @_;
304 0 ▶ return $envelope if (!$self->parts); # if there are no parts,
Branch: 1 branch
BranchTrueFalseTotal
unless $self->parts 0 0 0
305 # then there is nothing to do
306 0 require DIME::Message;
307 0 require DIME::Payload;
308 0 my $message = DIME::Message->new;
309 0 my $top = DIME::Payload->new;
310 0 ▶ my $soapversion = defined($context) ? $context->soapversion : '1.1';
Branch: 1 branch
BranchTrueFalseTotal
defined $context ? : 0 0 0
311 0 ▶ $top->attach('MIMEType' => $soapversion == 1.1 ?
Branch: 1 branch
BranchTrueFalseTotal
$soapversion == 1.1 ? : 0 0 0
312 "http://schemas.xmlsoap.org/soap/envelope/" : "application/soap+xml",
313 'Data' => $envelope );
314 0 $message->add_payload($top);
315 # consume the attachments that come in as input by 'shift'ing
316 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
317 0 ▶ while (my $part = shift(@{$self->parts})) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $part = shift @{$self->parts;}) 0 0 0
318 0 ▶ die "You are only allowed to add parts of type DIME::Payload to a DIME::Message"
Branch: 1 branch
BranchTrueFalseTotal
unless $part->isa("DIME::Payload") 0 0 0
319 if (!$part->isa('DIME::Payload'));
320 # print STDERR "Adding payload to DIME message: ".ref($part)."\n";
321 0 $message->add_payload($part);
322 # print STDERR "Payload's payload is: ".${$part->print_content_data}."\n";
323 }
324 0 $self->headers_http({ 'Content-Type' => 'application/dime' });
325 0 return $message->print_data;
326 }
327  
328 0 ▶sub unpackage {
Subroutine unpackage: not called CC=4 SCAR=30.0
329 0 my $self = shift;
330 0 my ($raw_input,$context) = @_;
331 0 $self->SUPER::unpackage();
332  
333 # Parse the raw input into a DIME::Message structure.
334 # - fail if the raw_input is not DIME formatted
335 0 print STDERR "raw_data: $raw_input\n";
336 0 ▶ $self->initialize_parser() if !defined($self->parser);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->parser 0 0 0
337 0 ▶ my $message = eval { $self->parser->parse_data(\$raw_input) }
Branch: 1 branch
BranchTrueFalseTotal
my $message = eval { do { $self->parser->parse_data(\$raw_input) } } or die "Something wrong with DIME message: @{[$@];}\n" 0 0 0
338 0 or die "Something wrong with DIME message: @{[$@]}\n";
339  
340 # The first payload is always the SOAP Message
341 # TODO - Error check
342 0 my @payloads = @{$message->{'_PAYLOADS'}};
343 0 my $env = shift(@payloads);
344 0 my $env_str = $env->print_content_data;
345 0 print STDERR "Received this envelope: ".$env_str."\n";
346 0 ▶ while (my $p = shift(@payloads)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $p = shift @payloads) 0 0 0
347 0 print STDERR "Adding part to Packager\n";
348 0 $self->push_part($p);
349 }
350 0 return $env_str;
351 }
352  
353 1 1;
354 __END__
« lib/SOAP/Transport/POP3.pm ↑ Summary lib/SOAP/Lite.pm »