| 1 | package XMLRPC::Fast; | ||
| 2 | |||
| 3 | 3 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 3 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | 3 | ▶ | use B qw< svref_2object SVf_IOK SVf_NOK >; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 3 | ▶ | use Encode; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 3 | ▶ | use Exporter qw< import >; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 3 | ▶ | use MIME::Base64; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 0 | ▶ | use XML::Parser; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 11 | |||
| 12 | |||
| 13 | our $VERSION = "0.11"; | ||
| 14 | |||
| 15 | our @EXPORT = qw< | ||
| 16 | decode_xmlrpc encode_xmlrpc | ||
| 17 | encode_xmlrpc_request encode_xmlrpc_response encode_xmlrpc_fault | ||
| 18 | >; | ||
| 19 | |||
| 20 | |||
| 21 | my $utf8 = find_encoding("UTF-8"); | ||
| 22 | |||
| 23 | |||
| 24 | |||
| 25 | # | ||
| 26 | # encode_xmlrpc_request() | ||
| 27 | # --------------------- | ||
| 28 | sub encode_xmlrpc_request { | ||
| 29 | encode_xmlrpc(method => @_) | ||
| 30 | } | ||
| 31 | |||
| 32 | |||
| 33 | # | ||
| 34 | # encode_xmlrpc_response() | ||
| 35 | # ---------------------- | ||
| 36 | sub encode_xmlrpc_response { | ||
| 37 | encode_xmlrpc(response => "", @_) | ||
| 38 | } | ||
| 39 | |||
| 40 | |||
| 41 | # | ||
| 42 | # encode_xmlrpc_fault() | ||
| 43 | # ------------------- | ||
| 44 | sub encode_xmlrpc_fault { | ||
| 45 | encode_xmlrpc(fault => "", $_[0], $_[1]) | ||
| 46 | } | ||
| 47 | |||
| 48 | |||
| 49 | # | ||
| 50 | # encode_xmlrpc() | ||
| 51 | # ------------- | ||
| 52 | sub encode_xmlrpc { | ||
| 53 | my ($type, $method, @args) = @_; | ||
| 54 | |||
| 55 | my $tag = $type eq "method" ? "methodCall" : "methodResponse"; | ||
| 56 | |||
| 57 | my $xml = q{<?xml version="1.0" encoding="UTF-8"?>}; | ||
| 58 | $xml .= "<$tag>"; | ||
| 59 | $xml .= "<methodName>$method</methodName>" if $type eq "method"; | ||
| 60 | |||
| 61 | if ($type eq "fault") { | ||
| 62 | $args[0] = "" unless defined $args[0]; | ||
| 63 | $args[1] = "" unless defined $args[1]; | ||
| 64 | |||
| 65 | $xml .= "<fault><value><struct><member><name>faultCode</name>" | ||
| 66 | . "<value><int>$args[0]</int></value></member>" | ||
| 67 | . "<member><name>faultString</name>" | ||
| 68 | . "<value><string>$args[1]</string></value></member>" | ||
| 69 | . "</struct></value></fault>" | ||
| 70 | } | ||
| 71 | else { | ||
| 72 | if (@args) { | ||
| 73 | $xml .= "<params>"; | ||
| 74 | $xml .= "<param><value>".encode_xmlrpc_thing($_)."</value></param>" | ||
| 75 | for @args; | ||
| 76 | $xml .= "</params>"; | ||
| 77 | } | ||
| 78 | } | ||
| 79 | |||
| 80 | $xml .= "</$tag>"; | ||
| 81 | } | ||
| 82 | |||
| 83 | |||
| 84 | # | ||
| 85 | # encode_xmlrpc_thing() | ||
| 86 | # ------------------- | ||
| 87 | sub encode_xmlrpc_thing { | ||
| 88 | if (ref $_[0]) { | ||
| 89 | # handle structures and objects | ||
| 90 | my $struct = $_[0]; | ||
| 91 | |||
| 92 | if (ref $struct eq "ARRAY") { | ||
| 93 | return join "", | ||
| 94 | "<array><data>", | ||
| 95 | (map encode_xmlrpc_thing($_), @$struct), | ||
| 96 | "</data></array>" | ||
| 97 | } | ||
| 98 | elsif (ref $struct eq "HASH") { | ||
| 99 | return join "", | ||
| 100 | "<struct>", | ||
| 101 | (map "<member><name>$_</name><value>" | ||
| 102 | . encode_xmlrpc_thing($struct->{$_}) | ||
| 103 | . "</value></member>", | ||
| 104 | keys %$struct), | ||
| 105 | "</struct>" | ||
| 106 | } | ||
| 107 | elsif (ref $struct eq "DateTime") { | ||
| 108 | my $date = $struct->strftime("%Y-%m-%dT%H:%M:%S"); | ||
| 109 | return "<dateTime.iso8601>$date</dateTime.iso8601>" | ||
| 110 | } | ||
| 111 | elsif (ref $struct eq "DateTime::Tiny") { | ||
| 112 | my $date = $struct->as_string; | ||
| 113 | return "<dateTime.iso8601>$date</dateTime.iso8601>" | ||
| 114 | } | ||
| 115 | } | ||
| 116 | else { | ||
| 117 | # handle scalar values | ||
| 118 | return "<nil/>" if not defined $_[0]; | ||
| 119 | |||
| 120 | my $copy = $_[0]; | ||
| 121 | my $sv = svref_2object(\$_[0]); | ||
| 122 | |||
| 123 | return "<double>$copy</double>" if $sv->FLAGS & SVf_NOK; | ||
| 124 | return "<int>$copy</int>" if $sv->FLAGS & SVf_IOK; | ||
| 125 | |||
| 126 | if (Encode::is_utf8($_[0])) { | ||
| 127 | $copy = $utf8->encode($_[0]); | ||
| 128 | } | ||
| 129 | |||
| 130 | if ($copy ne $_[0] or $copy =~ /[^\x09\x0a\x0d\x20-\x7f]/) { | ||
| 131 | return "<base64>" . encode_base64($copy, "") . "</base64>" | ||
| 132 | } | ||
| 133 | else { | ||
| 134 | $copy =~ s/&/&/g; | ||
| 135 | $copy =~ s/</</g; | ||
| 136 | $copy =~ s/>/>/g; | ||
| 137 | return "<string>$copy</string>" | ||
| 138 | } | ||
| 139 | } | ||
| 140 | } | ||
| 141 | |||
| 142 | |||
| 143 | # | ||
| 144 | # decode_xmlrpc() | ||
| 145 | # ------------- | ||
| 146 | sub decode_xmlrpc { | ||
| 147 | my ($xml) = shift; | ||
| 148 | |||
| 149 | # parse the XML document | ||
| 150 | my $parser = XML::Parser->new(Style => "Tree"); | ||
| 151 | my $tree = $parser->parse($xml); | ||
| 152 | my $root = $tree->[1]; | ||
| 153 | my %struct; | ||
| 154 | |||
| 155 | # detect the message type | ||
| 156 | if ($tree->[0] eq "methodCall") { | ||
| 157 | $struct{type} = "request"; | ||
| 158 | } | ||
| 159 | elsif ($tree->[0] eq "methodResponse") { | ||
| 160 | $struct{type} = "response"; | ||
| 161 | } | ||
| 162 | else { | ||
| 163 | die "unknown type of message"; | ||
| 164 | } | ||
| 165 | |||
| 166 | # handle first-level elements + detect if fault message | ||
| 167 | while (defined (my $e = shift @$root)) { | ||
| 168 | next if ref $e eq "HASH"; # skip attributes | ||
| 169 | shift @$root and next if $e eq "0"; # skip text outside elements | ||
| 170 | |||
| 171 | if ($e eq "params") { | ||
| 172 | $struct{params} = [ decode_node(shift @$root) ]; | ||
| 173 | } | ||
| 174 | elsif ($e eq "methodName") { | ||
| 175 | $struct{methodName} = (shift @$root)->[2]; | ||
| 176 | } | ||
| 177 | elsif ($e eq "fault") { | ||
| 178 | %struct = ( | ||
| 179 | type => "fault", | ||
| 180 | fault => decode_node(shift @$root), | ||
| 181 | ); | ||
| 182 | } | ||
| 183 | } | ||
| 184 | |||
| 185 | return \%struct; | ||
| 186 | } | ||
| 187 | |||
| 188 | |||
| 189 | # | ||
| 190 | # decode_node() | ||
| 191 | # ----------- | ||
| 192 | sub decode_node { | ||
| 193 | my ($node) = shift; | ||
| 194 | my @result; | ||
| 195 | |||
| 196 | while (defined (my $e = shift @$node)) { | ||
| 197 | next if ref $e eq "HASH"; # skip attributes | ||
| 198 | shift @$node and next if $e eq "0"; # skip text outside elements | ||
| 199 | |||
| 200 | if ($e eq "value") { | ||
| 201 | # small dance to correctly handle empty values, which must | ||
| 202 | # generate an undef in order to keep things balanced | ||
| 203 | my $v = shift @$node; | ||
| 204 | push @result, @$v > 1 ? decode_node($v) : undef; | ||
| 205 | } | ||
| 206 | elsif ($e eq "data" or $e eq "member" or $e eq "param") { | ||
| 207 | push @result, decode_node(shift @$node); | ||
| 208 | } | ||
| 209 | elsif ($e eq "array") { | ||
| 210 | push @result, [ decode_node(shift @$node) ]; | ||
| 211 | } | ||
| 212 | elsif ($e eq "struct") { | ||
| 213 | push @result, { decode_node(shift @$node) }; | ||
| 214 | } | ||
| 215 | elsif ($e eq "int" or $e eq "i4" or $e eq "boolean") { | ||
| 216 | push @result, int((shift @$node)->[2]); | ||
| 217 | } | ||
| 218 | elsif ($e eq "double") { | ||
| 219 | push @result, (shift @$node)->[2] / 1.0; | ||
| 220 | } | ||
| 221 | elsif ($e eq "string" or $e eq "name" or $e eq "dateTime.iso8601") { | ||
| 222 | push @result, (shift @$node)->[2]; | ||
| 223 | } | ||
| 224 | elsif ($e eq "base64") { | ||
| 225 | push @result, decode_base64((shift @$node)->[2]); | ||
| 226 | } | ||
| 227 | elsif ($e eq "nil") { | ||
| 228 | push @result, undef; | ||
| 229 | } | ||
| 230 | } | ||
| 231 | |||
| 232 | return @result | ||
| 233 | } | ||
| 234 | |||
| 235 | |||
| 236 | |||
| 237 | __END__ | ||