lib/SOAP/Lite/Packager.pm

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