1 |
package CWMP::Request; |
2 |
|
3 |
use warnings; |
4 |
use strict; |
5 |
|
6 |
use XML::Rules; |
7 |
use Data::Dump qw/dump/; |
8 |
use Carp qw/confess cluck/; |
9 |
|
10 |
my $debug = 0; |
11 |
|
12 |
=head1 NAME |
13 |
|
14 |
CWMP::Request - parse SOAP request |
15 |
|
16 |
=head1 METHODS |
17 |
|
18 |
=head2 _tag |
19 |
|
20 |
Get value of tag. Tag name is case insensitive (don't ask why), |
21 |
we ignore namespaces and can take optional C<sub_key> |
22 |
(usually C<_content>). |
23 |
|
24 |
_tag( $tag_hash, $name, $sub_key ) |
25 |
|
26 |
=cut |
27 |
|
28 |
sub _tag { |
29 |
my ( $tag_hash, $name, $sub_key ) = @_; |
30 |
confess "need hash as first argument" unless ( ref $tag_hash eq 'HASH' ); |
31 |
$name = (grep { m/^(?:\w+:)*$name$/i } keys %$tag_hash )[0]; |
32 |
# $name =~ s/^\w+://; |
33 |
if ( defined $tag_hash->{$name} ) { |
34 |
if ( ! defined $sub_key ) { |
35 |
return $tag_hash->{$name}; |
36 |
} elsif ( defined $tag_hash->{$name}->{$sub_key} ) { |
37 |
return $tag_hash->{$name}->{$sub_key}; |
38 |
} else { |
39 |
return if ( $name =~ m/^value$/i ); |
40 |
warn "can't find '$name/$sub_key' in ", dump( $tag_hash ); |
41 |
return; |
42 |
} |
43 |
} else { |
44 |
warn "can't find '$name' in ", dump( $tag_hash ); |
45 |
return; |
46 |
} |
47 |
} |
48 |
|
49 |
our $state; # FIXME check this! |
50 |
|
51 |
my $parser = XML::Rules->new( |
52 |
# start_rules => [ |
53 |
# '^division_name,fax' => 'skip', |
54 |
# ], |
55 |
namespaces => { |
56 |
'http://schemas.xmlsoap.org/soap/envelope/' => 'soapenv', |
57 |
'http://schemas.xmlsoap.org/soap/encoding/' => 'soap', |
58 |
'http://www.w3.org/2001/XMLSchema' => 'xsd', |
59 |
'http://www.w3.org/2001/XMLSchema-instance' => 'xsi', |
60 |
'urn:dslforum-org:cwmp-1-0' => '', |
61 |
}, |
62 |
rules => [ |
63 |
#_default => 'content trim', |
64 |
x_default => sub { |
65 |
my ($tag_name, $tag_hash, $context, $parent_data) = @_; |
66 |
warn dump( $tag_name, $tag_hash, $context ); |
67 |
}, |
68 |
'ID' => sub { |
69 |
my ($tag_name, $tag_hash, $context, $parent_data) = @_; |
70 |
$state->{ID} = $tag_hash->{_content}; |
71 |
}, |
72 |
# |
73 |
# Inform |
74 |
# |
75 |
'Inform' => sub { |
76 |
$state->{_dispatch} = 'InformResponse'; # what reponse to call |
77 |
}, |
78 |
'DeviceId' => sub { |
79 |
my ($tag_name, $tag_hash, $context, $parent_data) = @_; |
80 |
foreach my $name ( keys %$tag_hash ) { |
81 |
next if $name eq '_content'; |
82 |
my $key = $name; |
83 |
$key =~ s/^\w+://; # stip namespace |
84 |
$state->{DeviceID}->{ $key } = _tag( $tag_hash, $name, '_content' ); |
85 |
} |
86 |
}, |
87 |
'EventStruct' => sub { |
88 |
my ($tag_name, $tag_hash, $context, $parent_data) = @_; |
89 |
push @{ $state->{EventStruct} }, $tag_hash->{EventCode}->{_content}; |
90 |
}, |
91 |
qr/(MaxEnvelopes|CurrentTime|RetryCount)/ => sub { |
92 |
my ($tag_name, $tag_hash, $context, $parent_data) = @_; |
93 |
$state->{$tag_name} = $tag_hash->{_content}; |
94 |
}, |
95 |
'ParameterValueStruct' => sub { |
96 |
my ($tag_name, $tag_hash, $context, $parent_data) = @_; |
97 |
# Name/Value tags must be case insnesitive |
98 |
my $value = (grep( /value/i, keys %$tag_hash ))[0]; |
99 |
$state->{Parameter}->{ _tag($tag_hash, 'Name', '_content') } = _tag($tag_hash, 'Value', '_content' ); |
100 |
}, |
101 |
# |
102 |
# GetRPCMethodsResponse |
103 |
# |
104 |
qr/^(?:^\w+:)*string$/ => 'content array', |
105 |
'MethodList' => sub { |
106 |
my ($tag_name, $tag_hash, $context, $parent_data) = @_; |
107 |
$state->{MethodList} = _tag( $tag_hash, 'string' ); |
108 |
}, |
109 |
# |
110 |
# GetParameterNamesResponse |
111 |
# |
112 |
'ParameterInfoStruct' => sub { |
113 |
my ($tag_name, $tag_hash, $context, $parent_data) = @_; |
114 |
my $name = _tag($tag_hash, 'Name', '_content'); |
115 |
my $writable = _tag($tag_hash, 'Writable', '_content' ); |
116 |
|
117 |
confess "need state" unless ( $state ); # don't remove! |
118 |
|
119 |
# XXX dragons ahead: convert name to tree rewriting it into perl |
120 |
|
121 |
my $s = $name; |
122 |
warn "===> $name\n" if $debug; |
123 |
$s =~ s/^([^\.]+)/\$state->{ParameterInfo}->{'$1'}/; |
124 |
warn "---> $s\n" if $debug; |
125 |
|
126 |
my $stat; |
127 |
while ( $s =~ s/\.(\d+)/->[$1]/ ) { |
128 |
$stat->{array}++; |
129 |
warn "-\@-> $s\n" if $debug; |
130 |
} |
131 |
while ( $s =~ s/\.([a-zA-Z0-9_]+)/->{'$1'}/ ) { |
132 |
$stat->{scalar}++; |
133 |
warn "-\$-> $s\n" if $debug; |
134 |
|
135 |
}; |
136 |
$s .= "->{'writable'} = $writable;"; |
137 |
|
138 |
warn "## $name\n## tree: $s\n## stat: ",dump( $stat ), "\n" if $debug; |
139 |
|
140 |
eval "$s"; |
141 |
confess "can't eval $s : $@" if ($@); |
142 |
|
143 |
#warn "## state = dump( $state ), "\n"; |
144 |
}, |
145 |
# |
146 |
# Fault |
147 |
# |
148 |
'Fault' => sub { |
149 |
my ($tag_name, $tag_hash, $context, $parent_data) = @_; |
150 |
$state->{Fault} = { |
151 |
FaultCode => _tag( $tag_hash, 'FaultCode', '_content' ), |
152 |
FaultString => _tag( $tag_hash, 'FaultString', '_content' ), |
153 |
}; |
154 |
warn "FAULT: ", $state->{Fault}->{FaultCode}, " ", $state->{Fault}->{FaultString}, "\n"; |
155 |
} |
156 |
] |
157 |
); |
158 |
|
159 |
=head2 parse |
160 |
|
161 |
my $state = CWMP::Request->parse( "<soap>request</soap>" ); |
162 |
|
163 |
=cut |
164 |
|
165 |
sub parse { |
166 |
my $self = shift; |
167 |
|
168 |
my $xml = shift || confess "no xml?"; |
169 |
|
170 |
$state = {}; |
171 |
$parser->parsestring( $xml ); |
172 |
return $state; |
173 |
} |
174 |
|
175 |
1; |