| 1 | # ====================================================================== | ||
| 2 | # | ||
| 3 | # Copyright (C) 2000-2001 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 | # $Id: SOAP::Transport::HTTP.pm,v 0.46 2001/01/31 16:30:24 $ | ||
| 8 | # | ||
| 9 | # ====================================================================== | ||
| 10 | |||
| 11 | package SOAP::Transport::HTTPX; | ||
| 12 | |||
| 13 | use strict; | ||
| 14 | use vars qw($VERSION); | ||
| 15 | $VERSION = '0.46'; | ||
| 16 | |||
| 17 | use SOAP::Transport::HTTP; | ||
| 18 | |||
| 19 | # ====================================================================== | ||
| 20 | |||
| 21 | package SOAP::Transport::HTTPX::Client; | ||
| 22 | |||
| 23 | use vars qw(@ISA); | ||
| 24 | @ISA = qw(SOAP::Transport::HTTP::Client); | ||
| 25 | |||
| 26 | use SOAP::Lite; | ||
| 27 | |||
| 28 | my(%redirect, %mpost); | ||
| 29 | |||
| 30 | |||
| 31 | sub send_receive { | ||
| 32 | my($self, %parameters) = @_; | ||
| 33 | my($envelope, $endpoint, $action) = | ||
| 34 | @parameters{qw(envelope endpoint action)}; | ||
| 35 | |||
| 36 | $endpoint ||= $self->endpoint; | ||
| 37 | |||
| 38 | $endpoint =~ s|httpx://|http://|; | ||
| 39 | my $method = 'POST'; | ||
| 40 | my $resp; | ||
| 41 | |||
| 42 | my $redir_count = 0; | ||
| 43 | while (1) { | ||
| 44 | |||
| 45 | # check cache for redirect | ||
| 46 | $endpoint = $redirect{$endpoint} if exists $redirect{$endpoint}; | ||
| 47 | # check cache for M-POST | ||
| 48 | $method = 'M-POST' if exists $mpost{$endpoint}; | ||
| 49 | |||
| 50 | my $req = HTTP::Request->new($method => $endpoint, HTTP::Headers->new, $envelope); | ||
| 51 | $req->proxy_authorization_basic($ENV{'HTTP_proxy_user'}, $ENV{'HTTP_proxy_pass'}) | ||
| 52 | if ($ENV{'HTTP_proxy_user'} && $ENV{'HTTP_proxy_pass'}); # by Murray Nesbitt | ||
| 53 | |||
| 54 | if ($method eq 'M-POST') { | ||
| 55 | my $prefix = sprintf '%04d', int(rand(1000)); | ||
| 56 | $req->header(Man => qq!"$SOAP::Constants::NS_ENV"; ns=$prefix!); | ||
| 57 | $req->header("$prefix-SOAPAction" => $action); | ||
| 58 | } else { | ||
| 59 | $req->header(SOAPAction => $action); | ||
| 60 | } | ||
| 61 | $req->content_type('text/xml'); | ||
| 62 | $req->content_length(length($envelope)); | ||
| 63 | |||
| 64 | SOAP::Trace::transport($req); | ||
| 65 | SOAP::Trace::debug($req->as_string); | ||
| 66 | |||
| 67 | $self->SUPER::env_proxy if $ENV{'HTTP_proxy'}; | ||
| 68 | |||
| 69 | $resp = $self->SUPER::request($req); | ||
| 70 | |||
| 71 | SOAP::Trace::transport($resp); | ||
| 72 | SOAP::Trace::debug($resp->as_string); | ||
| 73 | |||
| 74 | # 100 OK, continue to read? | ||
| 75 | if (($resp->code == 510 || $resp->code == 501) && | ||
| 76 | $method ne 'M-POST') { | ||
| 77 | $mpost{$endpoint} = 1; | ||
| 78 | } elsif ( $resp->code == 301 && $redir_count++ < 10 ) { | ||
| 79 | my $head = $resp->headers; | ||
| 80 | if ( $head->{soapaction} ) { | ||
| 81 | my ($oldclass) = $action =~ m/(.*)#/; | ||
| 82 | my $newaction = $action = $head->{soapaction}; | ||
| 83 | my ($newclass) = $newaction =~ m/(.*)#/; | ||
| 84 | $envelope =~ s/$oldclass/$newclass/; | ||
| 85 | } | ||
| 86 | $endpoint = $head->{location} if ( $head->{location} ); | ||
| 87 | } else { | ||
| 88 | last; | ||
| 89 | } | ||
| 90 | } | ||
| 91 | |||
| 92 | |||
| 93 | $redirect{$endpoint} = $resp->request->url | ||
| 94 | if $resp->previous && $resp->previous->is_redirect; | ||
| 95 | |||
| 96 | $self->code($resp->code); | ||
| 97 | $self->message($resp->message); | ||
| 98 | $self->is_success($resp->is_success); | ||
| 99 | $self->status($resp->status_line); | ||
| 100 | |||
| 101 | join '', $resp->content_type =~ m!^multipart/! ? ($resp->headers_as_string, "\n") : '', | ||
| 102 | $resp->content; | ||
| 103 | } | ||
| 104 | |||
| 105 | # ====================================================================== | ||
| 106 | |||
| 107 | package SOAP::Transport::HTTPX::Server; | ||
| 108 | |||
| 109 | use vars qw(@ISA); | ||
| 110 | @ISA = qw(SOAP::Transport::HTTP::Server); | ||
| 111 | |||
| 112 | # ====================================================================== | ||
| 113 | |||
| 114 | package SOAP::Transport::HTTPX::CGI; | ||
| 115 | |||
| 116 | use vars qw(@ISA); | ||
| 117 | @ISA = qw(SOAP::Transport::HTTP::CGI); | ||
| 118 | |||
| 119 | # ====================================================================== | ||
| 120 | |||
| 121 | package SOAP::Transport::HTTPX::Daemon; | ||
| 122 | |||
| 123 | use vars qw(@ISA); | ||
| 124 | @ISA = qw(SOAP::Transport::HTTP::Daemon); | ||
| 125 | |||
| 126 | # ====================================================================== | ||
| 127 | |||
| 128 | package SOAP::Transport::HTTPX::Apache; | ||
| 129 | |||
| 130 | use vars qw(@ISA %Redirect $hc); | ||
| 131 | @ISA = qw(SOAP::Transport::HTTP::Apache); | ||
| 132 | |||
| 133 | my ( $forward, $redirect ) = ( 0, 1 ); | ||
| 134 | |||
| 135 | sub handler { | ||
| 136 | my $self = shift->new; | ||
| 137 | my $r = shift || Apache->request; | ||
| 138 | |||
| 139 | $self->request(HTTP::Request->new( | ||
| 140 | $r->method => $r->uri, | ||
| 141 | HTTP::Headers->new($r->headers_in), | ||
| 142 | do { my $buf; $r->read($buf, $r->header_in('Content-length')); $buf; } | ||
| 143 | )); | ||
| 144 | |||
| 145 | my $action = my $orig_action = $self->request->header('SOAPAction'); | ||
| 146 | |||
| 147 | $action =~ s|"||g; | ||
| 148 | $action =~ s|(\w+:)(/+)?||; | ||
| 149 | my $scheme = $1.$2; | ||
| 150 | |||
| 151 | my ( $class, $method ) = $action =~ m|(.*?)#(.*)|; | ||
| 152 | $class =~ s|/|::|g; | ||
| 153 | |||
| 154 | unless ( %Redirect ) { | ||
| 155 | foreach ( $self->dispatch_to() ) { | ||
| 156 | push (@INC, $_ ) if m|/|; | ||
| 157 | } | ||
| 158 | eval "use Redirect"; die if $@; | ||
| 159 | } | ||
| 160 | |||
| 161 | if ( exists($Redirect{$class}) ) { | ||
| 162 | my $re_proxy = $Redirect{$class}->[0]; | ||
| 163 | my $re_class = $Redirect{$class}->[1]; | ||
| 164 | |||
| 165 | $re_class = "urn:/$re_class" if ( $re_class && $re_class !~ /^\w+:/ ); | ||
| 166 | my $new_action = ( $re_class ) ? "\"$re_class#$method\"" : $orig_action; | ||
| 167 | |||
| 168 | if ( $Redirect{$class}->[2] == $redirect ) { | ||
| 169 | $r->header_out( 'SOAPAction' => $new_action ); | ||
| 170 | $r->header_out( 'Location' => $re_proxy ); | ||
| 171 | $r->status(301); | ||
| 172 | $r->send_http_header; | ||
| 173 | return 301; | ||
| 174 | } | ||
| 175 | elsif ( exists($Redirect{$class}) && $Redirect{$class}->[2] == $forward ) { | ||
| 176 | my $content = $self->request->content; | ||
| 177 | $content =~ s/$scheme$class/$re_class/ if ( $re_class ); | ||
| 178 | |||
| 179 | $hc ||= SOAP::Transport::HTTP::Client->new; | ||
| 180 | |||
| 181 | my $response = $hc->send_receive ( | ||
| 182 | envelope => $content, | ||
| 183 | endpoint => $re_proxy, | ||
| 184 | action => $new_action, | ||
| 185 | ); | ||
| 186 | |||
| 187 | $response =~ s/$re_class/$class/ if ( $re_class ); | ||
| 188 | |||
| 189 | if ($hc->is_success) { | ||
| 190 | $r->header_out('Content-Length' => length ($response) ); | ||
| 191 | $r->send_http_header($hc->{response}->content_type); | ||
| 192 | $r->print($response); | ||
| 193 | } else { | ||
| 194 | $r->err_header_out('Content-length' => length ($response) ); | ||
| 195 | $r->content_type($hc->{response}->content_type); | ||
| 196 | $r->custom_response($hc->code, $response); | ||
| 197 | } | ||
| 198 | return $hc->code; | ||
| 199 | } | ||
| 200 | } | ||
| 201 | |||
| 202 | |||
| 203 | SOAP::Transport::HTTP::Server::handle ( $self ); | ||
| 204 | |||
| 205 | |||
| 206 | if ($self->response->is_success) { | ||
| 207 | $r->header_out('Content-length' => $self->response->content_length); | ||
| 208 | $r->send_http_header($self->response->content_type); | ||
| 209 | $r->print($self->response->content); | ||
| 210 | } else { | ||
| 211 | $r->err_header_out('Content-length' => $self->response->content_length); | ||
| 212 | $r->content_type($self->response->content_type); | ||
| 213 | $r->custom_response($self->response->code, $self->response->content); | ||
| 214 | } | ||
| 215 | |||
| 216 | $self->response->code; | ||
| 217 | |||
| 218 | } | ||
| 219 | |||
| 220 | # ====================================================================== | ||
| 221 | |||
| 222 | |||
| 223 | 1; | ||
| 224 | |||
| 225 | __END__ |