1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
|
# Copyrights 2007-2022 by [Mark Overmeer <markov@cpan.org>].
# For other contributors see ChangeLog.
# See the manual pages for details on the licensing terms.
# Pod stripped from pm file by OODoc 2.03.
# This code is part of distribution XML-Compile-SOAP. Meta-POD processed
# with OODoc into POD and HTML manual-pages. See README.md
# Copyright Mark Overmeer. Licensed under the same terms as Perl itself.
package XML::Compile::Transport::SOAPHTTP;
use vars '$VERSION';
$VERSION = '3.28';
use base 'XML::Compile::Transport';
use warnings;
use strict;
use Log::Report 'xml-compile-soap';
use XML::Compile::SOAP::Util qw/SOAP11ENV SOAP11HTTP/;
use XML::Compile ();
use LWP ();
use LWP::UserAgent ();
use HTTP::Request ();
use HTTP::Headers ();
use Encode;
# (Microsofts HTTP Extension Framework)
my $http_ext_id = SOAP11ENV;
my $mime_xop = 'application/xop+xml';
__PACKAGE__->register(SOAP11HTTP);
sub init($)
{ my ($self, $args) = @_;
$self->SUPER::init($args);
$self->userAgent
( $args->{user_agent}
, keep_alive => (exists $args->{keep_alive} ? $args->{keep_alive} : 1)
, timeout => ($args->{timeout} || 180)
, ssl_opts => $args->{ssl_opts}
);
$self;
}
sub initWSDL11($)
{ my ($class, $wsdl) = @_;
trace "initialize SOAPHTTP transporter for WSDL11";
}
#-------------------------------------------
my $default_ua;
sub userAgent(;$)
{ my ($self, $agent) = (shift, shift);
return $self->{user_agent} = $agent
if defined $agent;
$self->{user_agent} ||= $default_ua ||= LWP::UserAgent->new
( requests_redirectable => [ qw/GET HEAD POST M-POST/ ]
, protocols_allowed => [ qw/http https/ ]
, parse_head => 0
, @_
);
}
sub defaultUserAgent() { $default_ua }
#-------------------------------------------
# SUPER::compileClient() calls this method to do the real work
sub _prepare_call($)
{ my ($self, $args) = @_;
my $method = $args->{method} || 'POST';
my $soap = $args->{soap} || 'SOAP11';
my $version = ref $soap ? $soap->version : $soap;
my $mpost_id = $args->{mpost_id} || 42;
my $action = $args->{action};
my $mime = $args->{mime};
my $kind = $args->{kind} || 'request-response';
my $expect = $kind ne 'one-way' && $kind ne 'notification-operation';
my $charset = $self->charset;
my $ua = $self->userAgent;
# Prepare header
my $header = $args->{header} || HTTP::Headers->new;
$self->headerAddVersions($header);
# There is probably never a real HTTP server on the other side, but
# HTTP/1.1 requires this.
$header->header(Host => $1)
if +($args->{endpoint} // '') =~ m!^\w+\://([^/:]+)!;
my $content_type;
if($version eq 'SOAP11')
{ $mime ||= 'text/xml';
$content_type = qq{$mime; charset=$charset};
}
elsif($version eq 'SOAP12')
{ $mime ||= 'application/soap+xml';
my $sa = defined $action ? qq{; action="$action"} : '';
$content_type = qq{$mime; charset=$charset$sa};
$header->header(Accept => $mime); # not the HTML answer
}
else
{ error "SOAP version {version} not implemented", version => $version;
}
if($method eq 'POST')
{ # should only be used by SOAP11, but you never know. So, SOAP12
# will have the action both ways.
$header->header(SOAPAction => qq{"$action"})
if defined $action;
}
elsif($method eq 'M-POST')
{ $header->header(Man => qq{"$http_ext_id"; ns=$mpost_id});
$header->header("$mpost_id-SOAPAction", qq{"$action"})
if $version eq 'SOAP11';
}
else
{ error "SOAP method must be POST or M-POST, not {method}"
, method => $method;
}
# Prepare request
# Ideally, we should change server when one fails, and stick to that
# one as long as possible.
my $server = $self->address;
# Create handler
my ($create_message, $parse_message)
= exists $INC{'XML/Compile/XOP.pm'}
? $self->_prepare_xop_call($content_type)
: $self->_prepare_simple_call($content_type);
$parse_message = $self->_prepare_for_no_answer($parse_message)
unless $expect;
my $hook = $args->{hook};
$hook
? sub # hooked code
{ my $trace = $_[1];
my $request = HTTP::Request->new($method => $server, $header);
$request->protocol('HTTP/1.1');
$create_message->($request, $_[0], $_[2]);
$trace->{http_request} = $request;
$trace->{action} = $action;
$trace->{soap_version} = $version;
$trace->{server} = $server;
$trace->{user_agent} = $ua;
$trace->{hooked} = 1;
my $response = $hook->($request, $trace, $self)
or return undef;
UNIVERSAL::isa($response, 'HTTP::Response')
or error __x"transport_hook must produce a HTTP::Response, got {resp}"
, resp => $response;
$trace->{http_response} = $response;
if($response->is_error)
{ error $response->message
if $response->header('Client-Warning');
warning $response->message;
# still try to parse the response for Fault blocks
}
$parse_message->($response);
}
: sub # real call
{ my $trace = $_[1];
my $request = HTTP::Request->new($method => $server, $header);
$request->protocol('HTTP/1.1');
$create_message->($request, $_[0], $_[2]);
$trace->{http_request} = $request;
my $response = $ua->request($request)
or return undef;
$trace->{http_response} = $response;
if($response->is_error)
{ error $response->message
if $response->header('Client-Warning');
warning $response->message;
# still try to parse the response for Fault blocks
}
$parse_message->($response);
};
}
sub _prepare_simple_call($)
{ my ($self, $content_type) = @_;
my $create = sub
{ my ($request, $content) = @_;
$request->header(Content_Type => $content_type);
$request->content_ref($content); # already bytes (not utf-8)
use bytes; $request->header('Content-Length' => length $$content);
};
my $parse = sub
{ my $response = shift;
UNIVERSAL::isa($response, 'HTTP::Response')
or error __x"no response object received";
my $ct = $response->content_type || '';
lc($ct) ne 'multipart/related'
or error __x"remote system uses XOP, use XML::Compile::XOP";
trace "received ".$response->status_line;
$ct =~ m,[/+]xml$,i
or error __x"answer is not xml but `{type}'", type => $ct;
# HTTP::Message::decoded_content() does not work for old Perls
my $content = $response->decoded_content(ref => 1)
|| $response->content(ref => 1);
($content, {});
};
($create, $parse);
}
sub _prepare_xop_call($)
{ my ($self, $content_type) = @_;
my ($simple_create, $simple_parse)
= $self->_prepare_simple_call($content_type);
my $charset = $self->charset;
my $create = sub
{ my ($request, $content, $mtom) = @_;
$mtom ||= [];
@$mtom or return $simple_create->($request, $content);
my $bound = "MIME-boundary-".int rand 10000;
(my $start_cid = $mtom->[0]->cid) =~ s/^.*\@/xml@/;
my $si = "$content_type";
$si =~ s/\"/\\"/g;
$request->header(Content_Type => <<__CT);
multipart/related;
boundary="$bound";
type="$mime_xop";
start="<$start_cid>";
start-info="$si"
__CT
my $base = HTTP::Message->new
( [ Content_Type => qq{$mime_xop; charset="$charset"; type="$si"}
, Content_Transfer_Encoding => '8bit'
, Content_ID => "<$start_cid>"
] );
$base->content_ref($content); # already bytes (not utf-8)
my @parts = ($base, map $_->mimePart, @$mtom);
$request->parts(@parts); #$base, map $_->mimePart, @$mtom);
$request;
};
my $parse = sub
{ my ($response, $mtom) = @_;
my $ct = $response->header('Content-Type') || '';
$ct =~ m!^\s*multipart/related\s*\;!i
or return $simple_parse->($response);
my (@parts, %parts);
foreach my $part ($response->parts)
{ my $include = XML::Compile::XOP::Include->fromMime($part)
or next;
$parts{$include->cid} = $include;
push @parts, $include;
}
@parts
or error "no parts in response multi-part for XOP";
my $root;
if($ct =~ m!start\=(["']?)\<([^"']*)\>\1!)
{ my $startid = $2;
$root = delete $parts{$startid};
defined $root
or warning __x"cannot find root node id in parts `{id}'"
, id => $startid;
}
unless($root)
{ $root = shift @parts;
delete $parts{$root->cid};
}
($root->content(1), \%parts);
};
($create, $parse);
}
sub _prepare_for_no_answer($)
{ my $self = shift;
sub
{ my $response = shift;
my $ct = $response->content_type || '';
trace "received ".$response->status_line;
my $content = '';
if($ct =~ m,[/+]xml$,i)
{ # HTTP::Message::decoded_content() does not work for old Perls
$content = $] >= 5.008 ? $response->decoded_content(ref => 1)
: $response->content(ref => 1);
}
($content, {});
};
}
sub headerAddVersions($)
{ my ($thing, $h) = @_;
foreach my $pkg (qw/XML::Compile XML::Compile::Cache
XML::Compile::SOAP XML::LibXML LWP/)
{ no strict 'refs';
my $version = ${"${pkg}::VERSION"} || 'undef';
(my $field = "X-$pkg-Version") =~ s/\:\:/-/g;
$h->header($field => $version);
}
}
1;
|