[r389]: SOAP-Transport-TCP / trunk / lib / SOAP / Transport / TCP.pm  Maximize  Restore  History

Download this file

312 lines (218 with data), 9.7 kB

  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
# ======================================================================
#
# Copyright (C) 2000-2001 Paul Kulchenko (paulclinger@yahoo.com)
# SOAP::Lite is free software; you can redistribute it
# and/or modify it under the same terms as Perl itself.
#
# $Id: TCP.pm 384 2011-08-16 17:08:08Z kutterma $
#
# ======================================================================
package SOAP::Transport::TCP;
use strict;
our $VERSION = 0.715;
use URI;
use IO::Socket;
use IO::Select;
use IO::SessionData;
# ======================================================================
package URI::tcp; # ok, let's do 'tcp://' scheme
our $VERSION = 0.715;
require URI::_server;
@URI::tcp::ISA=qw(URI::_server);
# ======================================================================
package SOAP::Transport::TCP::Client;
our $VERSION = 0.715;
use vars qw(@ISA);
require SOAP::Lite;
@ISA = qw(SOAP::Client);
sub DESTROY { SOAP::Trace::objects('()') }
sub new {
my $self = shift;
unless (ref $self) {
my $class = ref($self) || $self;
my(@params, @methods);
while (@_) { $class->can($_[0]) ? push(@methods, shift() => shift) : push(@params, shift) }
$self = bless {@params} => $class;
while (@methods) { my($method, $params) = splice(@methods,0,2);
$self->$method(ref $params eq 'ARRAY' ? @$params : $params)
}
# use SSL if there is any parameter with SSL_* in the name
$self->SSL(1) if !$self->SSL && grep /^SSL_/, keys %$self;
SOAP::Trace::objects('()');
}
return $self;
}
sub SSL {
my $self = shift->new;
@_ ? ($self->{_SSL} = shift, return $self) : return $self->{_SSL};
}
sub io_socket_class { shift->SSL ? 'IO::Socket::SSL' : 'IO::Socket::INET' }
sub syswrite {
my($self, $sock, $data) = @_;
my $timeout = $sock->timeout;
my $select = IO::Select->new($sock);
my $len = length $data;
while (length $data > 0) {
return unless $select->can_write($timeout);
local $SIG{PIPE} = 'IGNORE';
# added length() to make it work on Mac. Thanks to Robin Fuller <rfuller@broadjump.com>
my $wc = syswrite($sock, $data, length($data));
if (defined $wc) {
substr($data, 0, $wc) = '';
} elsif (!IO::SessionData::WOULDBLOCK($!)) {
return;
}
}
return $len;
}
sub sysread {
my($self, $sock) = @_;
my $timeout = $sock->timeout;
my $select = IO::Select->new($sock);
my $result = '';
my $data;
while (1) {
return unless $select->can_read($timeout);
my $rc = sysread($sock, $data, 4096);
if ($rc) {
$result .= $data;
} elsif (defined $rc) {
return $result;
} elsif (!IO::SessionData::WOULDBLOCK($!)) {
return;
}
}
}
sub send_receive {
my($self, %parameters) = @_;
my($envelope, $endpoint, $action) =
@parameters{qw(envelope endpoint action)};
$endpoint ||= $self->endpoint;
warn "URLs with 'tcp:' scheme are deprecated. Use 'tcp://'. Still continue\n"
if $endpoint =~ s!^tcp:(//)?!tcp://!i && !$1;
my $uri = URI->new($endpoint);
local($^W, $@, $!);
my $socket = $self->io_socket_class;
eval "require $socket" or Carp::croak $@ unless UNIVERSAL::can($socket => 'new');
my $sock = $socket->new (
PeerAddr => $uri->host, PeerPort => $uri->port, Proto => $uri->scheme, %$self
);
SOAP::Trace::debug($envelope);
# bytelength hack. See SOAP::Transport::HTTP.pm for details.
my $bytelength = SOAP::Utils::bytelength($envelope);
$envelope = pack('C0A*', $envelope)
if !$SOAP::Constants::DO_NOT_USE_LWP_LENGTH_HACK && length($envelope) != $bytelength;
my $result;
if ($sock) {
$sock->blocking(0);
$self->syswrite($sock, $envelope) and
$sock->shutdown(1) and # stop writing
$result = $self->sysread($sock);
}
SOAP::Trace::debug($result);
my $code = $@ || $!;
$self->code($code);
$self->message($code);
$self->is_success(!defined $code || $code eq '');
$self->status($code);
return $result;
}
# ======================================================================
package SOAP::Transport::TCP::Server;
use IO::SessionSet;
use Carp ();
use vars qw($AUTOLOAD @ISA);
@ISA = qw(SOAP::Server);
our $VERSION = 0.715;
sub DESTROY { SOAP::Trace::objects('()') }
sub new {
my $self = shift;
unless (ref $self) {
my $class = ref($self) || $self;
my(@params, @methods);
while (@_) { $class->can($_[0]) ? push(@methods, shift() => shift) : push(@params, shift) }
$self = $class->SUPER::new(@methods);
# use SSL if there is any parameter with SSL_* in the name
$self->SSL(1) if !$self->SSL && grep /^SSL_/, @params;
my $socket = $self->io_socket_class;
eval "require $socket" or Carp::croak $@ unless UNIVERSAL::can($socket => 'new');
$self->{_socket} = $socket->new(Proto => 'tcp', @params)
or Carp::croak "Can't open socket: $!";
SOAP::Trace::objects('()');
}
return $self;
}
sub SSL {
my $self = shift->new;
@_ ? ($self->{_SSL} = shift, return $self) : return $self->{_SSL};
}
sub io_socket_class { shift->SSL ? 'IO::Socket::SSL' : 'IO::Socket::INET' }
sub AUTOLOAD {
my $method = substr($AUTOLOAD, rindex($AUTOLOAD, '::') + 2);
return if $method eq 'DESTROY';
no strict 'refs';
*$AUTOLOAD = sub { shift->{_socket}->$method(@_) };
goto &$AUTOLOAD;
}
sub handle {
my $self = shift->new;
my $sock = $self->{_socket};
my $session_set = IO::SessionSet->new($sock);
my %data;
while (1) {
my @ready = $session_set->wait($sock->timeout);
for my $session (@ready) {
my $data;
if (my $rc = $session->read($data, 4096)) {
$data{$session} .= $data if $rc > 0;
} else {
$session->write($self->SUPER::handle(delete $data{$session}));
$session->close;
}
}
}
}
# ======================================================================
1;
__END__
=head1 NAME
SOAP::Transport::TCP - TCP Transport Support for SOAP::Lite
=head2 SOAP::Transport::TCP
The classes provided by this module implement direct TCP/IP communications methods for both clients and servers.
The connections don't use HTTP or any other higher-level protocol. These classes are selected when the client or server object being created uses an endpoint URI that starts with tcp://. Both client and server classes support using Secure Socket Layer if it is available. If any of the parameters to a new method from either of the classes begins with SSL_ (such as SSL_server in place of Server), the class attempts to load the IO::Socket::SSL package and use it to create socket objects.
Both of the following classes catch methods that are intended for the socket objects and pass them along, allowing calls such as $client->accept( ) without including the socket class in the inheritance tree.
=head3 SOAP::Transport::TCP::Client
Inherits from: L<SOAP::Client>.
The TCP client class defines only two relevant methods beyond new and send_receive. These methods are:
=over
=item SSL(I<optional new boolean value>)
if ($client->SSL) # Execute only if in SSL mode
Reflects the attribute that denotes whether the client object is using SSL sockets for communications.
=item io_socket_class
($client->io_socket_class)->new(%options);
Returns the name of the class to use when creating socket objects for internal use in communications. As implemented, it returns one of IO::Socket::INET or IO::Socket::SSL, depending on the return value of the previous SSL method.
=back
If an application creates a subclass that inherits from this client class, either method is a likely target for overloading.
The new method behaves identically to most other classes, except that it detects the presence of SSL-targeted values in the parameter list and sets the SSL method appropriately if they are present.
The send_receive method creates a socket of the appropriate class and connects to the configured endpoint. It then sets the socket to nonblocking I/O, sends the message, shuts down the client end of the connection (preventing further writing), and reads the response back from the server. The socket object is discarded after the response and
appropriate status codes are set on the client object.
=head3 SOAP::Transport::TCP::Server
Inherits from: L<SOAP::Server>.
The server class also defines the same two additional methods as in the client class:
=over
=item SSL(I<optional new boolean value>)
if ($client->SSL) # Execute only if in SSL mode
Reflects the attribute that denotes whether the client object is using SSL sockets for communications.
=item io_socket_class
($client->io_socket_class)->new(%options);
Returns the name of the class to use when creating socket objects for internal use in communications. As implemented, it returns one of IO::Socket::INET or IO::Socket::SSL, depending on the return value of the previous SSL method. The new method also manages the automatic selection of SSL in the same fashion as the client class does.
The handle method in this server implementation isn't designed to be called once with each new request. Rather, it is called with no arguments, at which time it enters into an infinite loop of waiting for a connection, reading the request, routing the request and sending back the serialized response. This continues until the process itself is interrupted by an untrapped signal or similar means.
=back
=head1 COPYRIGHT
Copyright (C) 2000-2004 Paul Kulchenko. All rights reserved.
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=head1 AUTHORS
Written by Paul Kulchenko.
Split from SOAP::Lite and SOAP-Transport-TCP packaging by Martin Kutter
=cut

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks