blib/lib/XML/Compile/Transport.pm untested

filter: Statementstmt 0.0% 0 / 87 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 16 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 151 SCAR 50.5
CC
12
Cov
0%
compileClient
110.0
init
6.0
address
6.0
CRAP 156.0
1 # Copyrights 2007-2021 by [Mark Overmeer <markov@cpan.org>].
2 # For other contributors see ChangeLog.
3 # See the manual pages for details on the licensing terms.
4 # Pod stripped from pm file by OODoc 2.02.
5 # This code is part of distribution XML-Compile-SOAP. Meta-POD processed
6 # with OODoc into POD and HTML manual-pages. See README.md
7 # Copyright Mark Overmeer. Licensed under the same terms as Perl itself.
8  
9 package XML::Compile::Transport;
10 use vars '$VERSION';
11 $VERSION = '3.27';
12  
13 use base 'XML::Compile::SOAP::Extension';
14  
15 use warnings;
16 use strict;
17  
18 use Log::Report 'xml-compile-soap';
19  
20 use Log::Report::Exception ();
21  
22 use XML::LibXML ();
23 use Time::HiRes qw/time/;
24  
25  
26 sub init($)
27 { my ($self, $args) = @_;
28 $self->SUPER::init($args);
29 $self->{charset} = $args->{charset} || 'UTF-8';
30  
31 my $addr = $args->{address} || 'http://localhost';
32 my @addrs = ref $addr eq 'ARRAY' ? @$addr : $addr;
33  
34 $self->{addrs} = \@addrs;
35 $self;
36 }
37  
38 #-------------------------------------
39  
40  
41 sub charset() {shift->{charset}}
42  
43  
44 sub addresses() { @{shift->{addrs}} }
45  
46  
47 sub address()
48 { my $addrs = shift->{addrs};
49 @$addrs==1 ? $addrs->[0] : $addrs->[rand @$addrs];
50 }
51  
52 #-------------------------------------
53  
54  
55 sub compileClient(@)
56 { my ($self, %args) = @_;
57 my $call = $self->_prepare_call(\%args);
58 my $kind = $args{kind} || 'request-response';
59 my $format = $args{xml_format} || 0;
60  
61 sub
62 { my ($xmlout, $trace, $mtom) = @_;
63 my $start = time;
64 my $textout = ref $xmlout ? $xmlout->toString($format) : $xmlout;
65 #warn $xmlout->toString(1); # show message sent
66  
67 my $stringify = time;
68 $trace->{stringify_elapse} = $stringify - $start;
69 $trace->{transport_start} = $start;
70  
71 my ($textin, $xops) = try { $call->(\$textout, $trace, $mtom) };
72 my $connected = time;
73 $trace->{connect_elapse} = $connected - $stringify;
74 if($@)
75 { $trace->{errors} = [ $@->wasFatal ];
76 return;
77 }
78  
79 my $xmlin;
80 if($textin)
81 { $xmlin = try { XML::LibXML->load_xml(string => $$textin) };
82 if($@) { $trace->{errors} = [ $@->wasFatal ] }
83 else { $trace->{response_dom} = $xmlin }
84 }
85  
86 my $answer = $xmlin;
87 if($kind eq 'one-way')
88 { my $response = $trace->{http_response};
89 my $code = defined $response ? $response->code : -1;
90 if($code==202) { $answer ||= {} }
91 else
92 { push @{$trace->{errors}}, Log::Report::Exception->new
93 (reason => 'error', message => __"call failed with code $code")
94 }
95 }
96 elsif(!$xmlin)
97 { push @{$trace->{errors}}, Log::Report::Exception->new
98 (reason => 'error', message => __"no xml as answer");
99 }
100  
101 my $end = $trace->{transport_end} = time;
102  
103 $trace->{parse_elapse} = $end - $connected;
104 $trace->{transport_elapse} = $end - $start;
105  
106 wantarray || ! keys %$xops
107 or warning "loosing received XOPs";
108  
109 wantarray ? ($answer, $xops) : $answer;
110 }
111 }
112  
113 sub _prepare_call($) { panic "not implemented" }
114  
115 #--------------------------------------
116  
117  
118 { my %registered;
119 sub register($) { my ($class, $uri) = @_; $registered{$uri} = $class }
120 sub plugin($) { my ($class, $uri) = @_; $registered{$uri} }
121 sub registered($) { values %registered }
122 }
123  
124 #--------------------------------------
125  
126  
127 1;