lib/SOAP/Clean/Internal.pm untested

filter: Statementstmt 0.0% 0 / 167 Branchbran 0.0% 0 / 62 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 30 Podpod 0.0% 0 / 21 totaltotal 0.0% 0 / 285 CC 32 SCAR 38.2
CC
32
Cov
0%
arg_decode_to_string
156.0
arg_encode
56.0
_comm
42.0
CRAP 45.6
1 # Copyright (c) 2003, Cornell University
2 # See the file COPYING for the status of this software
3  
4 ########################################################################
5 # Some internal routines
6 ########################################################################
7  
8 package SOAP::Clean::Internal;
9  
10 use strict;
11 use warnings;
12  
13 use File::Temp qw/ :POSIX /;
14 use MIME::Base64;
15 use Data::Dumper;
16  
17 use SOAP::Clean::Misc;
18 use SOAP::Clean::XML;
19  
20 BEGIN {
21 use Exporter ();
22 our (@ISA, @EXPORT);
23  
24 @ISA = qw(Exporter);
25 @EXPORT = qw(
26 &arg_decode_to_xml
27 &arg_decode_to_string
28 &arg_encode
29 &arg_encode_raw
30 &arg_encode_bool
31 &arg_encode_float
32 &arg_encode_int
33 &arg_encode_string
34 &arg_encode_url
35 &arg_encode_xml
36 &arg_strip_white
37 &is_type
38  
39 $SOAP_ENC
40 $SOAP_ENV
41 $soaphttp
42 $ds
43 $wsdl
44 $wsdl_http
45 $wsdl_mime
46 $wsdl_soap
47 $wsse
48 $xenc
49 $xsd
50 $xsi
51 );
52  
53 }
54  
55 ########################################################################
56 # Global variable initialization
57 ########################################################################
58  
59 # initialize package globals, first exported ones
60  
61 our $SOAP_ENC = "http://schemas.xmlsoap.org/soap/encoding/";
62 our $SOAP_ENV = "http://schemas.xmlsoap.org/soap/envelope/";
63 our $soaphttp = "http://schemas.xmlsoap.org/soap/http";
64 our $ds = "http://www.w3.org/2000/09/xmldsig#";
65 our $wsdl = "http://schemas.xmlsoap.org/wsdl/";
66 our $wsdl_http = "http://schemas.xmlsoap.org/wsdl/http/";
67 our $wsdl_mime = "http://schemas.xmlsoap.org/wsdl/mime/";
68 our $wsdl_soap = "http://schemas.xmlsoap.org/wsdl/soap/";
69 our $wsse = "http://schemas.xmlsoap.org/ws/2002/04/secext";
70 our $xenc = "http://www.w3.org/2001/04/xmlenc#";
71 our $xsd = "http://www.w3.org/2001/XMLSchema";
72 our $xsi = "http://www.w3.org/2001/XMLSchema-instance";
73  
74  
75 # non-exported package globals go here
76 # our $var = "...";
77  
78 ########################################################################
79  
80 sub arg_encode {
81 my ($type,$name,$value) = @_;
82  
83 if ( $type eq 'bool' ) {
84 return arg_encode_bool($name,$value);
85 } elsif ( $type eq 'int' ) {
86 return arg_encode_int($name,$value);
87 } elsif ( $type eq 'float' ) {
88 return arg_encode_float($name,$value);
89 } elsif ( $type eq 'string' ) {
90 return arg_encode_string($name,$value);
91 } elsif ( $type eq 'raw' ) {
92 return arg_encode_raw($name,$value);
93 } elsif ( $type eq 'xml' ) {
94 return arg_encode_xml($name,$value);
95 } else {
96 assert(0,"Can't deal with type ".$type);
97 }
98 }
99  
100 sub arg_encode_bool {
101 my ($name,$val) = @_;
102 return element($name,
103 attr("xsi:type","xsd:boolean"),
104 text($val ? "true" : "false"));
105 }
106  
107 sub arg_encode_float {
108 my ($name,$val) = @_;
109 return element($name,
110 attr("xsi:type","xsd:float"),
111 text($val));
112 }
113  
114 sub arg_encode_int {
115 my ($name,$val) = @_;
116 return element($name,
117 attr("xsi:type","xsd:int"),
118 text($val));
119 }
120  
121 sub arg_encode_string {
122 my ($name,$val) = @_;
123 return element($name,
124 attr("xsi:type","xsd:string"),
125 text($val));
126 }
127  
128 sub arg_encode_url {
129 my ($name,$val) = @_;
130 return element($name,
131 attr("xsi:type","xsd:anyURI"),
132 text($val));
133 }
134  
135 sub arg_encode_raw {
136 my ($name,$val) = @_;
137 return element($name,
138 attr("xsi:type","xsd:base64Binary"),
139 text(encode_base64($val)));
140 }
141  
142 sub arg_encode_xml {
143 my ($name,$val) = @_;
144 if (!ref($val)) {
145 # It's a string. Parse it.
146 $val = xml_from_string($val);
147 }
148 if (xml_is_document($val)) {
149 ($val) = xml_get_children($val);
150 }
151 # fixme: Make sure that any surrounding default namespaces do
152 # not capture unqualified attributes in the raw XML.
153 return element($name, $val);
154 }
155  
156 ########################################################################
157  
158 # does $n have type ($target_url,$target_local)?
159 sub is_type {
160 my ($target_url,$target_local,$n) = @_;
161 my $type = xml_get_attr($n,$xsi,"type");
162 if ( !defined($type) ) { return 0; }
163 return xml_same_names($target_url,$target_local,
164 xml_fix_name($n,$type));
165 }
166  
167  
168 sub arg_strip_white {
169 my ($s) = @_;
170 $s =~ s/[\n\t]/ /g;
171 $s =~ s/ +/ /g;
172 $s =~ s/^ +//;
173 $s =~ s/ +$//;
174 return $s;
175 }
176  
177 sub arg_decode_to_string {
178 my ($n) = @_;
179 if (!(ref $n)) {
180 # It's already a "string"
181 return $n;
182 } elsif (is_type($xsd,"boolean",$n)) {
183 my $result = arg_strip_white(xml_get_text($n));
184 chomp($result);
185 return 1 if ( $result eq "true" );
186 return 0 if ( $result eq "false" );
187 assert(0,"\"".$result."\" is not a valid boolean value");
188 } elsif (is_type($xsd,"float",$n)) {
189 my $result = arg_strip_white(xml_get_text($n));
190 chomp($result);
191 return $result;
192 } elsif (is_type($xsd,"int",$n)) {
193 my $result = arg_strip_white(xml_get_text($n));
194 chomp($result);
195 return $result;
196 } elsif (is_type($xsd,"string",$n)) {
197 return xml_get_text($n);
198 } elsif (is_type($xsd,"anyURI",$n)) {
199 my $result = arg_strip_white(xml_get_text($n));
200 chomp($result);
201 return $result;
202 } elsif (is_type($xsd,"base64Binary",$n)) {
203 return decode_base64(xml_get_text($n));
204 } else {
205 my @kids = xml_get_children($n);
206 if ( $#kids == 0 ) {
207 # If this argument element one element children, then decode the
208 # child as unparsed XML.
209 return xml_to_string(arg_decode_to_xml($n));
210 } elsif ( $#kids == -1 ) {
211 # Otherwise, the argument element has no element children then
212 # it had better have text children!
213 return xml_get_text($n);
214 } else {
215 # Ack! Multiple children elements?! How are we suppose to handle
216 # that?!
217 assert(0,"Cannot convert multiple children element to a string.");
218 }
219 }
220 }
221  
222 sub arg_decode_to_xml {
223 my ($n) = @_;
224 if (!(ref $n)) {
225 # It's already a "string"
226 return xml_from_string($n);
227 } elsif (is_type($xsd,"string",$n)) {
228 return xml_from_string(xml_get_text($n));
229 } elsif (is_type($xsd,"base64Binary",$n)) {
230 return xml_from_string(decode_base64(xml_get_text($n)));
231 } else {
232 # Some arbitrary XML element.
233 my @kids = xml_get_children($n);
234 if ( $#kids != 0 ) {
235 assert(0,"Argument value is not an xml element: ".Dumper($n));
236 }
237 return xml_extract_and_close_child($kids[0]);
238 }
239 }
240  
241 ########################################################################
242  
243 package SOAP::Clean::Internal::Actor;
244  
245 use SOAP::Clean::Misc;
246  
247 # Inheritance
248 our @ISA = qw(SOAP::Clean::Misc::Object);
249  
250 sub initialize {
251 my ($self) = @_;
252 $self->{is_server} = 0;
253 $self->{verbose} = 0;
254 $self->{request_count} = 0;
255 $self->{response_count} = 0;
256 }
257  
258 sub counts {
259 my ($self) = @_;
260 return ($self->{request_count},$self->{response_count});
261 }
262  
263 sub verbose {
264 my ($self,$verbose_level) = @_;
265 $self->{verbose} = $verbose_level;
266 return $self;
267 }
268  
269 sub dsig_keys{
270 my ($self,$dsigcl,$key_file,$cert_file,$tmpl_file,$appl) = @_;
271 $self->{dsig} = $dsigcl;
272 $self->{key} = $key_file;
273 $self->{cert} = $cert_file;
274 $self->{tmpl} = $tmpl_file;
275 $self->{appl} = $appl;
276 return $self;
277 }
278  
279 sub enc_dec_params{
280 my ($self,$enccl,$privkey_file_enc,$pubkey_file_enc,$tmpl_file,$appl) = @_;
281 $self->{enc} = $enccl;
282 $self->{privkeyenc} = $privkey_file_enc;
283 $self->{pubkeyenc} = $pubkey_file_enc;
284 $self->{enctmpl} = $tmpl_file;
285 $self->{appl} = $appl;
286 return $self;
287 }
288  
289 sub _print {
290 my $self = shift;
291 my $level = shift;
292  
293 if ( $self->{verbose} > $level ) {
294 print(@_);
295 }
296 }
297  
298 # Do the web communication
299 sub _comm {
300 my ($self,$tag, $server_url,$request_method,$request_headers,$request_str)
301 = @_;
302  
303 # Set up the request
304 my $ua = LWP::UserAgent->new;
305 my $request = HTTP::Request->new($request_method => $server_url);
306 if (defined($request_headers)) {
307 foreach my $k ( keys %$request_headers ) {
308 $request->header($k,$$request_headers{$k});
309 }
310 }
311 if (defined($request_str) && $request_str) {
312 $request->content($request_str);
313 }
314  
315 # Messages and statistics before sending
316 if ( $self->{verbose} ) {
317 $self->_print(1,"##################################################\n");
318 if ($tag) {
319 $self->_print(0,"Invoking ",$tag," at ",$server_url,"...\n");
320 } else {
321 $self->_print(0,"Invoking ",$server_url,"...\n");
322 }
323 my $request_str = $request->as_string();
324 $self->_print(1,$request_str);
325 $self->{request_count} += length($request_str);
326 }
327  
328 # Send the request - receive the response.
329 my $response = $ua->request($request);
330  
331 # Messages and statistics after receiving
332 if ( $self->{verbose} ) {
333 $self->_print(1,"##################################################\n");
334 $self->_print(1,,"Response:\n");
335 my $response_str = $response->as_string();
336 $self->_print(1,$response_str);
337 $self->_print(1,"##################################################\n");
338 $self->{response_count} += length($response_str);
339 }
340  
341 return $response;
342 }
343  
344 ########################################################################
345  
346 END { } # module clean-up code here (global destructor)
347  
348