############################################################################### # # This file copyright (c) 2001-2004 by Randy J. Ray , # all rights reserved # # Copying and distribution are permitted under the terms of the Artistic # License as distributed with Perl versions 5.005 and later. See # http://www.opensource.org/licenses/artistic-license.php # ############################################################################### # # $Id: XML.pm,v 1.36 2006/06/30 07:36:29 rjray Exp $ # # Description: This module provides the core XML <-> RPC conversion and # structural management. # # Functions: This module contains many, many subclasses. Better to # examine them individually. # # Libraries: RPC::XML::base64 uses MIME::Base64 # # Global Consts: $VERSION # ############################################################################### package RPC::XML; use 5.005; use strict; use vars qw(@EXPORT @EXPORT_OK %EXPORT_TAGS @ISA $VERSION $ERROR %xmlmap $xmlre $ENCODING $FORCE_STRING_ENCODING); use subs qw(time2iso8601 smart_encode bytelength); # The following is cribbed from SOAP::Lite, tidied up to suit my tastes BEGIN { no strict 'refs'; eval "use bytes"; # Re-worked this passage to continue supporting perl 5.005. It tried to # compile the "use bytes" in the second block even if the conditional never # travelled that path. So, explicit eval strings for everyone. if ($@) { eval 'sub bytelength { length(@_ ? $_[0] : $_) }'; } else { eval 'sub bytelength { use bytes; length(@_ ? $_[0] : $_) }'; } %xmlmap = ( '>' => '>' => '<' => '<' => '&' => '&'); $xmlre = join('', keys %xmlmap); $xmlre = qr/([$xmlre])/; # Default encoding: $ENCODING = 'us-ascii'; # force strings? $FORCE_STRING_ENCODING = 0; } require Exporter; @ISA = qw(Exporter); @EXPORT_OK = qw(time2iso8601 smart_encode bytelength RPC_BOOLEAN RPC_INT RPC_I4 RPC_DOUBLE RPC_DATETIME_ISO8601 RPC_BASE64 RPC_STRING $ENCODING $FORCE_STRING_ENCODING); %EXPORT_TAGS = (types => [ qw(RPC_BOOLEAN RPC_INT RPC_I4 RPC_DOUBLE RPC_STRING RPC_DATETIME_ISO8601 RPC_BASE64) ], all => [ @EXPORT_OK ]); $VERSION = do { my @r=(q$Revision: 1.36 $=~/\d+/g); sprintf "%d."."%02d"x$#r,@r }; # Global error string $ERROR = ''; # All of the RPC_* functions are convenience-encoders sub RPC_STRING ( $ ) { RPC::XML::string->new($_[0]) } sub RPC_BOOLEAN ( $ ) { RPC::XML::boolean->new($_[0]) } sub RPC_INT ( $ ) { RPC::XML::int->new($_[0]) } sub RPC_I4 ( $ ) { RPC::XML::i4->new($_[0]) } sub RPC_DOUBLE ( $ ) { RPC::XML::double->new($_[0]) } sub RPC_DATETIME_ISO8601 ( $ ) { RPC::XML::datetime_iso8601->new($_[0]) } sub RPC_BASE64 ( $ ) { RPC::XML::base64->new($_[0]) } # This is a dead-simple ISO8601-from-UNIX-time stringifier. Always expresses # time in UTC. sub time2iso8601 { my $time = shift || time; my $zone = shift || ''; my @time = gmtime($time); $time = sprintf("%4d%02d%02dT%02d:%02d:%02dZ", $time[5] + 1900, $time[4] + 1, @time[3, 2, 1, 0]); if ($zone) { my $char = $zone > 0 ? '+' : '-'; chop $time; # Lose the Z if we're specifying a zone $time .= $char . sprintf('%02d:00', abs($zone)); } $time; } # This is a (futile?) attempt to provide a "smart" encoding method that will # take a Perl scalar and promote it to the appropriate RPC::XML::_type_. { my $MaxInt = 256**4; my $MinInt = $MaxInt * -1; my $MaxDouble = 1e37; my $MinDouble = $MaxDouble * -1; sub smart_encode { my @values = @_; my $type; @values = map { if (!defined $_) { $type = RPC::XML::string->new(''); } elsif (ref $_) { # Skip any that have already been encoded if (UNIVERSAL::isa($_, 'RPC::XML::datatype')) { $type = $_; } elsif (UNIVERSAL::isa($_, 'HASH')) { $type = RPC::XML::struct->new($_); } elsif (UNIVERSAL::isa($_, 'ARRAY')) { $type = RPC::XML::array->new($_); } elsif (UNIVERSAL::isa($_, 'SCALAR')) { # This is a rare excursion into recursion, since the scalar # nature (de-refed from the object, so no longer magic) # will prevent further recursing. $type = smart_encode($$_); } else { # ??? Don't know what else to do, so skip it for now next; } } # You have to check ints first, because they match the # next pattern too # make sure not to encode digits that are larger than i4 elsif (! $FORCE_STRING_ENCODING and /^[-+]?\d+$/ and $_ > $MinInt and $_ < $MaxInt) { $type = RPC::XML::int->new($_); } # Pattern taken from perldata(1) elsif (! $FORCE_STRING_ENCODING and /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/ and $_ > $MinDouble and $_ < $MaxDouble) { $type = RPC::XML::double->new($_); } else { $type = RPC::XML::string->new($_); } $type; } @values; return (wantarray ? @values : $values[0]); } } # This is a (mostly) empty class used as a common superclass for simple and # complex types, so that their derivatives may be universally type-checked. package RPC::XML::datatype; use vars qw(@ISA); @ISA = (); sub type { my $class = ref($_[0]) || $_[0]; $class =~ s/.*://; $class } sub is_fault { 0 } ############################################################################### # # Package: RPC::XML::simple_type # # Description: A base class for the simpler type-classes to inherit from, # for default constructor, stringification, etc. # ############################################################################### package RPC::XML::simple_type; use strict; use vars qw(@ISA); @ISA = qw(RPC::XML::datatype); # new - a generic constructor that presumes the value being stored is scalar sub new { my $class = shift; my $value = shift; $RPC::XML::ERROR = ''; $class = ref($class) || $class; if (ref $value) { # If it is a scalar reference, just deref if (UNIVERSAL::isa($value, 'SCALAR')) { $value = $$value; } else { # We can only manage scalar references (or blessed scalar refs) $RPC::XML::ERROR = "${class}::new: Cannot instantiate from a " . 'reference not derived from scalar'; } } bless \$value, $class; } # value - a generic accessor sub value { my $self = shift; $$self; } # as_string - return the value as an XML snippet sub as_string { my $self = shift; my $class; return unless ($class = ref($self)); $class =~ s/^.*\://; $class =~ s/_/./g; substr($class, 0, 8) = 'dateTime' if (substr($class, 0, 8) eq 'datetime'); "<$class>$$self"; } # Serialization for simple types is just a matter of sending as_string over sub serialize { my ($self, $fh) = @_; print $fh $self->as_string; } # The switch to serialization instead of in-memory strings means having to # calculate total size in bytes for Content-Length headers: sub length { my $self = shift; length($self->as_string); } ############################################################################### # # Package: RPC::XML::int # # Description: Data-type class for integers # ############################################################################### package RPC::XML::int; use strict; use vars qw(@ISA); @ISA = qw(RPC::XML::simple_type); ############################################################################### # # Package: RPC::XML::i4 # # Description: Data-type class for i4. Forces data into an int object. # ############################################################################### package RPC::XML::i4; use strict; use vars qw(@ISA); @ISA = qw(RPC::XML::simple_type); ############################################################################### # # Package: RPC::XML::double # # Description: The "double" type-class # ############################################################################### package RPC::XML::double; use strict; use vars qw(@ISA); @ISA = qw(RPC::XML::simple_type); ############################################################################### # # Package: RPC::XML::string # # Description: The "string" type-class # ############################################################################### package RPC::XML::string; use strict; use vars qw(@ISA); @ISA = qw(RPC::XML::simple_type); # as_string - return the value as an XML snippet sub as_string { my $self = shift; my ($class, $value); return unless ($class = $self->type); ($value = defined $$self ? $$self : '' ) =~ s/$RPC::XML::xmlre/$RPC::XML::xmlmap{$1}/ge; "<$class>$value"; } # Overloaded from simple_type, so that we can apply bytelength to the body sub length { my $self = shift; RPC::XML::bytelength($self->as_string); } ############################################################################### # # Package: RPC::XML::boolean # # Description: The type-class for boolean data. Handles some "extra" cases # ############################################################################### package RPC::XML::boolean; use strict; use vars qw(@ISA); @ISA = qw(RPC::XML::simple_type); # This constructor allows any of true, false, yes or no to be specified sub new { my $class = shift; my $value = shift || 0; $RPC::XML::ERROR = ''; if ($value =~ /true|yes|1/i) { $value = 1; } elsif ($value =~ /false|no|0/i) { $value = 0; } else { $class = ref($class) || $class; $RPC::XML::ERROR = "${class}::new: Value must be one of yes, no, " . 'true, false, 1, 0 (case-insensitive)'; return undef; } bless \$value, $class; } ############################################################################### # # Package: RPC::XML::datetime_iso8601 # # Description: This is the class to manage ISO8601-style date/time values # ############################################################################### package RPC::XML::datetime_iso8601; use strict; use vars qw(@ISA); @ISA = qw(RPC::XML::simple_type); sub type { 'dateTime.iso8601' }; ############################################################################### # # Package: RPC::XML::array # # Description: This class encapsulates the array data type. Each element # within the array should be one of the datatype classes. # ############################################################################### package RPC::XML::array; use strict; use vars qw(@ISA); @ISA = qw(RPC::XML::datatype); # The constructor for this class mainly needs to sanity-check the value data sub new { my $class = shift; my @args = (UNIVERSAL::isa($_[0], 'ARRAY')) ? @{$_[0]} : @_; # First ensure that each argument passed in is itself one of the data-type # class instances. for (@args) { $_ = RPC::XML::smart_encode($_) unless (UNIVERSAL::isa($_, 'RPC::XML::datatype')); } bless \@args, $class; } # This became more complex once it was shown that there may be a need to fetch # the value while preserving the underlying objects. sub value { my $self = shift; my $no_recurse = shift || 0; my $ret; if ($no_recurse) { $ret = [ @$self ]; } else { $ret = [ map { $_->value } @$self ]; } $ret; } sub as_string { my $self = shift; join('', '', (map { ('', $_->as_string(), '') } (@$self)), ''); } # Serialization for arrays is not as straight-forward as it is for simple # types. One or more of the elements may be a base64 object, which has a # non-trivial serialize() method. Thus, rather than just sending the data from # as_string down the pipe, instead call serialize() recursively on all of the # elements. sub serialize { my ($self, $fh) = @_; print $fh ''; for (@$self) { print $fh ''; $_->serialize($fh); print $fh ''; } print $fh ''; } # Length calculation starts to get messy here, due to recursion sub length { my $self = shift; # Start with the constant components in the text my $len = 28; # That the part for (@$self) { $len += (15 + $_->length) } # 15 is for $len; } ############################################################################### # # Package: RPC::XML::struct # # Description: This is the "struct" data class. The struct is like Perl's # hash, with the constraint that all values are instances # of the datatype classes. # ############################################################################### package RPC::XML::struct; use strict; use vars qw(@ISA); @ISA = qw(RPC::XML::datatype); # The constructor for this class mainly needs to sanity-check the value data sub new { my $class = shift; my %args = (UNIVERSAL::isa($_[0], 'HASH')) ? %{$_[0]} : @_; # First ensure that each argument passed in is itself one of the data-type # class instances. for (keys %args) { $args{$_} = RPC::XML::smart_encode($args{$_}) unless (UNIVERSAL::isa($args{$_}, 'RPC::XML::datatype')); } bless \%args, $class; } # This became more complex once it was shown that there may be a need to fetch # the value while preserving the underlying objects. sub value { my $self = shift; my $no_recurse = shift || 0; my %value; if ($no_recurse) { %value = map { $_, $self->{$_} } (keys %$self); } else { %value = map { $_, $self->{$_}->value } (keys %$self); } \%value; } sub as_string { my $self = shift; my $key; join('', '', (map { ($key = $_) =~ s/$RPC::XML::xmlre/$RPC::XML::xmlmap{$1}/ge; ("$key", $self->{$_}->as_string, '') } (keys %$self)), ''); } # As with the array type, serialization here isn't cut and dried, since one or # more values may be base64. sub serialize { my ($self, $fh) = @_; my $key; print $fh ''; for (keys %$self) { ($key = $_) =~ s/$RPC::XML::xmlre/$RPC::XML::xmlmap{$1}/ge; print $fh "$key"; $self->{$_}->serialize($fh); print $fh ''; } print $fh ''; } # Length calculation is a real pain here. But not as bad as base64 promises sub length { my $self = shift; my $len = 17; # for (keys %$self) { $len += 45; # For all the constant XML presence $len += length($_); $len += $self->{$_}->length; } $len; } ############################################################################### # # Package: RPC::XML::base64 # # Description: This is the base64-encoding type. Plain data is passed in, # plain data is returned. Plain is always returned. All the # encoding/decoding is done behind the scenes. # ############################################################################### package RPC::XML::base64; use strict; use vars qw(@ISA); @ISA = qw(RPC::XML::datatype); sub new { require MIME::Base64; my ($class, $value, $encoded) = @_; my $self = {}; $RPC::XML::ERROR = ''; $self->{encoded} = $encoded ? 1 : 0; # Is this already Base-64? $self->{inmem} = 0; # To signal in-memory vs. filehandle # First, determine if the call sent actual data, a reference to actual # data, or an open filehandle. if (ref($value) and UNIVERSAL::isa($value, 'GLOB')) { # This is a seekable filehandle (or acceptable substitute thereof). # This assignment increments the ref-count, and prevents destruction # in other scopes. binmode $value; $self->{value_fh} = $value; $self->{fh_pos} = tell($value); } else { # Not a filehandle. Might be a scalar ref, but other than that it's # in-memory data. $self->{inmem}++; $self->{value} = ref($value) ? $$value : $value; unless (defined $value and length $value) { $class = ref($class) || $class; $RPC::XML::ERROR = "${class}::new: Must be called with non-null " . 'data or an open, seekable filehandle'; return undef; } # We want in-memory data to always be in the clear, to reduce the tests # needed in value(), below. if ($self->{encoded}) { local($^W) = 0; # Disable warnings in case the data is underpadded $self->{value} = MIME::Base64::decode_base64($self->{value}); $self->{encoded} = 0; } } bless $self, $class; } sub value { my $self = shift; my $as_base64 = (defined $_[0] and $_[0]) ? 1 : 0; # There are six cases here, based on whether or not the data exists in # Base-64 or clear form, and whether the data is in-memory or needs to be # read from a filehandle. if ($self->{inmem}) { # This is simplified into two cases (rather than four) since we always # keep in-memory data as cleartext return $as_base64 ? MIME::Base64::encode_base64($self->{value}, '') : $self->{value}; } else { # This is trickier with filehandle-based data, since we chose not to # change the state of the data. Thus, the behavior is dependant not # only on $as_base64, but also on $self->{encoded}. This is why we # took pains to explicitly set $as_base64 to either 0 or 1, rather than # just accept whatever non-false value the caller sent. It makes this # first test possible. my ($accum, $pos, $res); $accum = ''; $self->{fh_pos} = tell($self->{value_fh}); seek($self->{value_fh}, 0, 0); if ($as_base64 == $self->{encoded}) { $pos = 0; while ($res = read($self->{value_fh}, $accum, 1024, $pos)) { $pos += $res; } } else { if ($as_base64) { # We're reading cleartext and converting it to Base-64. Read in # multiples of 57 bytes for best Base-64 calculation. The # choice of 60 for the multiple is purely arbitrary. $res = ''; while (read($self->{value_fh}, $res, 60*57)) { $accum .= MIME::Base64::encode_base64($res, ''); } } else { # Reading Base-64 and converting it back to cleartext. If the # Base-64 data doesn't have any line-breaks, no telling how # much memory this will eat up. local($^W) = 0; # Disable padding-length warnings $pos = $self->{value_fh}; while (defined($res = <$pos>)) { $accum .= MIME::Base64::decode_base64($res); } } } seek($self->{value_fh}, $self->{fh_pos}, 0); return $accum; } } # The value needs to be encoded before being output sub as_string { my $self = shift; '' . $self->value('encoded') . ''; } # If it weren't for Tellme and their damnable WAV files, and ViAir and their # half-baked XML-RPC server, I wouldn't have to do any of this... sub serialize { my ($self, $fh) = @_; # If the data is in-memory, just call as_string and pass it down the pipe if ($self->{inmem}) { print $fh $self->as_string; } else { # If it's a filehandle, at least we take comfort in knowing that we # always want Base-64 at this level. my $buf = ''; $self->{fh_pos} = tell($self->{value_fh}); seek($self->{value_fh}, 0, 0); print $fh ''; if ($self->{encoded}) { # Easy-- just use read() to send it down in palatably-sized chunks while (read($self->{value_fh}, $buf, 4096)) { print $fh $buf; } } else { # This actually requires work. As with value(), the 60*57 is based # on ideal Base-64 chunks, with the 60 part being arbitrary. while (read($self->{value_fh}, $buf, 60*57)) { print $fh &MIME::Base64::encode_base64($buf, ''); } } print $fh ''; seek($self->{value_fh}, $self->{fh_pos}, 0); } } # This promises to be a big enough pain that I seriously considered opening # an anon-temp file (one that's unlinked for security, and survives only as # long as the FH is open) and passing that to serialize just to -s on the FH. # But I'll do this the "right" way instead... sub length { my $self = shift; # Start with the constant bits my $len = 17; # if ($self->{inmem}) { # If it's in-memory, it's cleartext. Size the encoded version $len += length(MIME::Base64::encode_base64($self->{value}, '')); } else { if ($self->{encoded}) { # We're lucky, it's already encoded in the file, and -s will do $len += -s $self->{value_fh}; } else { # Oh bugger. We have to encode it. my $buf = ''; my $cnt = 0; $self->{fh_pos} = tell($self->{value_fh}); seek($self->{value_fh}, 0, 0); while ($cnt = read($self->{value_fh}, $buf, 60*57)) { $len += length(MIME::Base64::encode_base64($buf, '')); } seek($self->{value_fh}, $self->{fh_pos}, 0); } } $len; } # This allows writing the decoded data to an arbitrary file. It's useful when # an application has gotten a RPC::XML::base64 object back from a request, and # knows that it needs to go straight to a file without being completely read # into memory, first. sub to_file { my ($self, $file) = @_; my ($fh, $buf, $do_close, $count) = (undef, '', 0, 0); if (ref $file and UNIVERSAL::isa($file, 'GLOB')) { $fh = $file; } else { require Symbol; $fh = Symbol::gensym(); unless (open($fh, "> $file")) { $RPC::XML::ERROR = $!; return -1; } binmode $fh; $do_close++; } # If all the data is in-memory, then we know that it's clear, and we # don't have to jump through hoops in moving it to the filehandle. if ($self->{inmem}) { print $fh $self->{value}; $count = CORE::length($self->{value}); } else { # Filehandle-to-filehandle transfer. # Now determine if the data can be copied over directly, or if we have # to decode it along the way. $self->{fh_pos} = tell($self->{value_fh}); seek($self->{value_fh}, 0, 0); if ($self->{encoded}) { # As with the caveat in value(), if the base-64 data doesn't have # any line-breaks, no telling how much memory this will eat up. local($^W) = 0; # Disable padding-length warnings my $tmp_fh = $self->{value_fh}; while (defined($_ = <$tmp_fh>)) { $buf = MIME::Base64::decode_base64($_); print $fh $buf; $count += CORE::length($buf); } } else { my $size; while ($size = read($self->{value_fh}, $buf, 4096)) { print $fh $buf; $count += $size; } } seek($self->{value_fh}, $self->{fh_pos}, 0); } close($fh) if $do_close; return $count; } ############################################################################### # # Package: RPC::XML::fault # # Description: This is the class that encapsulates the data for a RPC # fault-response. Like the others, it takes the relevant # information and maintains it internally. This is put # at the end of the datum types, though it isn't really a # data type in the sense that it cannot be passed in to a # request. But it is separated so as to better generalize # responses. # ############################################################################### package RPC::XML::fault; use strict; use vars qw(@ISA); @ISA = qw(RPC::XML::struct); # For our new(), we only need to ensure that we have the two required members sub new { my $class = shift; my @args = @_; my ($self, %args); $RPC::XML::ERROR = ''; if (ref($args[0]) and UNIVERSAL::isa($args[0], 'RPC::XML::struct')) { # Take the keys and values from the struct object as our own %args = %{$args[0]->value('shallow')}; } elsif (@args == 2) { # This is a special convenience-case to make simple new() calls clearer %args = (faultCode => RPC::XML::int->new($args[0]), faultString => RPC::XML::string->new($args[1])); } else { %args = @args; } unless ($args{faultCode} and $args{faultString}) { $class = ref($class) || $class; $RPC::XML::ERROR = "${class}::new: Missing required struct fields"; return undef; } if (scalar(keys %args) > 2) { $class = ref($class) || $class; $RPC::XML::ERROR = "${class}::new: Extra struct fields not allowed"; return undef; } $self = $class->SUPER::new(%args); } # This only differs from the display of a struct in that it has some extra # wrapped around it. Let the superclass as_string method do most of the work. sub as_string { my $self = shift; '' . $self->SUPER::as_string . ''; } # Because of the slight diff above, length() has to be different from struct sub length { my $self = shift; my $len = 30; # Constant XML content $len += $self->SUPER::length; $len; } # Convenience methods: sub code { $_[0]->{faultCode}->value } sub string { $_[0]->{faultString}->value } # This is the only one to override this method, for obvious reasons sub is_fault { 1 } ############################################################################### # # Package: RPC::XML::request # # Description: This is the class that encapsulates the data for a RPC # request. It takes the relevant information and maintains # it internally until asked to stringify. Only then is the # XML generated, encoding checked, etc. This allows for # late-selection of or as a # containing tag. # # This class really only needs a constructor and a method # to stringify. # ############################################################################### package RPC::XML::request; use strict; use vars qw(@ISA); ############################################################################### # # Sub Name: new # # Description: Creating a new request object, in this (reference) case, # means checking the list of arguments for sanity and # packaging it up for later use. # # Arguments: NAME IN/OUT TYPE DESCRIPTION # $class in scalar Class/ref to bless into # @argz in list The exact disposition of the # arguments is based on the # type of the various elements # # Returns: Success: object ref # Failure: undef, error in $RPC::XML::ERROR # ############################################################################### sub new { my $class = shift; my @argz = @_; my ($self, $name); $class = ref($class) || $class; $RPC::XML::ERROR = ''; unless (@argz) { $RPC::XML::ERROR = 'RPC::XML::request::new: At least a method name ' . 'must be specified'; return undef; } if (UNIVERSAL::isa($argz[0], 'RPC::XML::request')) { # Maybe this will be a clone operation } else { # This is the method name to be called $name = shift(@argz); # All the remaining args must be data. @argz = RPC::XML::smart_encode(@argz); $self = { args => [ @argz ], name => $name }; bless $self, $class; } $self; } # Accessor methods sub name { shift->{name} } sub args { shift->{args} || [] } ############################################################################### # # Sub Name: as_string # # Description: This is a fair bit more complex than the simple as_string # methods for the datatypes. Express the invoking object as # a well-formed XML document. # # Arguments: NAME IN/OUT TYPE DESCRIPTION # $self in ref Invoking object # $indent in scalar Indention level for output # # Returns: Success: text # Failure: undef # ############################################################################### sub as_string { my $self = shift; my $text; $RPC::XML::ERROR = ''; $text = qq(); $text .= "$self->{name}"; for (@{$self->{args}}) { $text .= '' . $_->as_string . ''; } $text .= ''; $text; } # The difference between stringifying and serializing a request is much like # the difference was for structs and arrays. The boilerplate is the same, but # the destination is different in a sensitive way. sub serialize { my ($self, $fh) = @_; print $fh qq(); print $fh "$self->{name}"; for (@{$self->{args}}) { print $fh ''; $_->serialize($fh); print $fh ''; } print $fh ''; } # Compared to base64, length-calculation here is pretty easy, much like struct sub length { my $self = shift; my $len = 100 + length($RPC::XML::ENCODING); # All the constant XML present $len += length($self->{name}); for (@{$self->{args}}) { $len += 30; # Constant XML $len += $_->length; } $len; } ############################################################################### # # Package: RPC::XML::response # # Description: This is the class that encapsulates the data for a RPC # response. As above, it takes the information and maintains # it internally until asked to stringify. Only then is the # XML generated, encoding checked, etc. This allows for # late-selection of or # as above. # ############################################################################### package RPC::XML::response; use strict; use vars qw(@ISA); ############################################################################### # # Sub Name: new # # Description: Creating a new response object, in this (reference) case, # means checking the outgoing parameter(s) for sanity. # # Arguments: NAME IN/OUT TYPE DESCRIPTION # $class in scalar Class/ref to bless into # @argz in list The exact disposition of the # arguments is based on the # type of the various elements # # Returns: Success: object ref # Failure: undef, error in $RPC::XML::ERROR # ############################################################################### sub new { my $class = shift; my @argz = @_; my ($self, %extra, %attr); $class = ref($class) || $class; $RPC::XML::ERROR = ''; if (! @argz) { $RPC::XML::ERROR = 'RPC::XML::response::new: One of a datatype, ' . 'value or a fault object must be specified'; } elsif (UNIVERSAL::isa($argz[0], 'RPC::XML::response')) { # This will eventually be a clone-operation. For now, just return in $self = $argz[0]; } elsif (@argz > 1) { $RPC::XML::ERROR = 'RPC::XML::response::new: Responses may take ' . 'only one argument'; } else { $argz[0] = RPC::XML::smart_encode($argz[0]); $self = { value => $argz[0] }; bless $self, $class; } $self; } # Accessor/status methods sub value { $_[0]->{value} } sub is_fault { $_[0]->{value}->is_fault } ############################################################################### # # Sub Name: as_string # # Description: This is a fair bit more complex than the simple as_string # methods for the datatypes. Express the invoking object as # a well-formed XML document. # # Arguments: NAME IN/OUT TYPE DESCRIPTION # $self in ref Invoking object # $indent in scalar Indention level for output # # Returns: Success: text # Failure: undef # ############################################################################### sub as_string { my $self = shift; my $text; $RPC::XML::ERROR = ''; $text = qq(); $text .= ''; if ($self->{value}->isa('RPC::XML::fault')) { $text .= $self->{value}->as_string; } else { $text .= '' . $self->{value}->as_string . ''; } $text .= ''; $text; } # See the comment for serialize() above in RPC::XML::request sub serialize { my ($self, $fh) = @_; print $fh qq(); print $fh ''; if ($self->{value}->isa('RPC::XML::fault')) { # This also bypasses a un-needed call to serialize in the struct # package, since we know by definition that there is no base64 data # in a fault. print $fh $self->{value}->as_string; } else { print $fh ''; $self->{value}->serialize($fh); print $fh ''; } print $fh ''; } # Compared to base64, length-calculation here is pretty easy, much like struct sub length { my $self = shift; my $len = 66 + length($RPC::XML::ENCODING); # All the constant XML present # This boilerplate XML is only present when it is NOT a fault $len += 47 unless ($self->{value}->isa('RPC::XML::fault')); $len += $self->{value}->length; $len; } 1; __END__ =head1 NAME RPC::XML - A set of classes for core data, message and XML handling =head1 SYNOPSIS use RPC::XML; $req = RPC::XML::request->new('fetch_prime_factors', RPC::XML::int->new(985120528)); ... $resp = RPC::XML::Parser->new()->parse(STREAM); if (ref($resp)) { return $resp->value->value; } else { die $resp; } =head1 DESCRIPTION The B package is an implementation of the B standard. The package provides a set of classes for creating values to pass to the constructors for requests and responses. These are lightweight objects, most of which are implemented as tied scalars so as to associate specific type information with the value. Classes are also provided for requests, responses, faults (errors) and a parser based on the L package from CPAN. This module does not actually provide any transport implementation or server basis. For these, see L and L, respectively. =head1 EXPORTABLE FUNCTIONS At present, three simple functions are available for import. They must be explicitly imported as part of the C statement, or with a direct call to C: =over 4 =item time2iso8601([$time]) Convert the integer time value in C<$time> (which defaults to calling the built-in C