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