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 |
use Class::Trigger; |
10 |
|
11 |
#use Devel::LeakTrace::Fast; |
12 |
|
13 |
=head1 NAME |
14 |
|
15 |
CWMP::Request - parse SOAP request metods |
16 |
|
17 |
=head1 CPE metods |
18 |
|
19 |
All methods described below call triggers with same name |
20 |
|
21 |
=cut |
22 |
|
23 |
our $state; # FIXME check this! |
24 |
|
25 |
our $rules = [ |
26 |
#_default => 'content trim', |
27 |
x_default => sub { |
28 |
my ($tag_name, $tag_hash, $context, $parent_data) = @_; |
29 |
warn dump( $tag_name, $tag_hash, $context ); |
30 |
}, |
31 |
'ID' => sub { |
32 |
my ($tag_name, $tag_hash, $context, $parent_data) = @_; |
33 |
$state->{ID} = $tag_hash->{_content}; |
34 |
}, |
35 |
|
36 |
'DeviceId' => sub { |
37 |
my ($tag_name, $tag_hash, $context, $parent_data) = @_; |
38 |
foreach my $name ( keys %$tag_hash ) { |
39 |
next if $name eq '_content'; |
40 |
my $key = $name; |
41 |
$key =~ s/^\w+://; # stip namespace |
42 |
$state->{DeviceID}->{ $key } = _tag( $tag_hash, $name, '_content' ); |
43 |
} |
44 |
}, |
45 |
'EventStruct' => sub { |
46 |
my ($tag_name, $tag_hash, $context, $parent_data) = @_; |
47 |
push @{ $state->{EventStruct} }, $tag_hash->{EventCode}->{_content}; |
48 |
}, |
49 |
qr/(MaxEnvelopes|CurrentTime|RetryCount)/ => sub { |
50 |
my ($tag_name, $tag_hash, $context, $parent_data) = @_; |
51 |
$state->{$tag_name} = $tag_hash->{_content}; |
52 |
}, |
53 |
'ParameterValueStruct' => sub { |
54 |
my ($tag_name, $tag_hash, $context, $parent_data) = @_; |
55 |
# Name/Value tags must be case insnesitive |
56 |
my $value = (grep( /value/i, keys %$tag_hash ))[0]; |
57 |
$state->{Parameter}->{ _tag($tag_hash, 'Name', '_content') } = _tag($tag_hash, 'Value', '_content' ); |
58 |
$state->{_trigger} = 'ParameterValue'; |
59 |
}, |
60 |
|
61 |
]; |
62 |
|
63 |
=head2 Inform |
64 |
|
65 |
Generate InformResponse to CPE |
66 |
|
67 |
=cut |
68 |
|
69 |
push @$rules, |
70 |
'Inform' => sub { |
71 |
$state->{_dispatch} = 'InformResponse'; # what reponse to call |
72 |
$state->{_trigger} = 'Inform'; |
73 |
}; |
74 |
|
75 |
=head2 GetRPCMethodsResponse |
76 |
|
77 |
=cut |
78 |
|
79 |
push @$rules, |
80 |
qr/^(?:^\w+:)*string$/ => 'content array', |
81 |
'MethodList' => sub { |
82 |
my ($tag_name, $tag_hash, $context, $parent_data) = @_; |
83 |
$state->{MethodList} = _tag( $tag_hash, 'string' ); |
84 |
$state->{_trigger} = 'GetRPCMethodsResponse'; |
85 |
}; |
86 |
|
87 |
=head2 GetParameterNamesResponse |
88 |
|
89 |
=cut |
90 |
|
91 |
push @$rules, |
92 |
'ParameterInfoStruct' => sub { |
93 |
my ($tag_name, $tag_hash, $context, $parent_data) = @_; |
94 |
my $name = _tag($tag_hash, 'Name', '_content'); |
95 |
my $writable = _tag($tag_hash, 'Writable', '_content' ); |
96 |
|
97 |
confess "need state" unless ( $state ); # don't remove! |
98 |
|
99 |
$state->{ParameterInfo}->{$name} = $writable; |
100 |
|
101 |
#warn "## state = dump( $state ), "\n"; |
102 |
|
103 |
$state->{_trigger} = 'GetParameterNamesResponse'; |
104 |
}; |
105 |
|
106 |
=head2 GetParameterAttributesResponse |
107 |
|
108 |
=cut |
109 |
|
110 |
push @$rules, |
111 |
'GetParameterAttributesResponse' => sub { |
112 |
my ($tag_name, $tag_hash, $context, $parent_data) = @_; |
113 |
warn dump( $tag_name, $tag_hash, $context ); |
114 |
$state->{_trigger} = 'GetParameterAttributesResponse'; |
115 |
}; |
116 |
|
117 |
=head2 Fault |
118 |
|
119 |
=cut |
120 |
|
121 |
push @$rules, |
122 |
'Fault' => sub { |
123 |
my ($tag_name, $tag_hash, $context, $parent_data) = @_; |
124 |
$state->{Fault} = { |
125 |
FaultCode => _tag( $tag_hash, 'FaultCode', '_content' ), |
126 |
FaultString => _tag( $tag_hash, 'FaultString', '_content' ), |
127 |
}; |
128 |
warn "FAULT: ", $state->{Fault}->{FaultCode}, " ", $state->{Fault}->{FaultString}, "\n"; |
129 |
$state->{_trigger} = 'Fault'; |
130 |
}; |
131 |
|
132 |
=head1 METHODS |
133 |
|
134 |
=head2 parse |
135 |
|
136 |
my $state = CWMP::Request->parse( "<soap>request</soap>" ); |
137 |
|
138 |
=cut |
139 |
|
140 |
sub parse { |
141 |
my $self = shift; |
142 |
|
143 |
my $xml = shift || confess "no xml?"; |
144 |
|
145 |
$state = {}; |
146 |
|
147 |
my $parser = XML::Rules->new( |
148 |
# start_rules => [ |
149 |
# '^division_name,fax' => 'skip', |
150 |
# ], |
151 |
namespaces => { |
152 |
'http://schemas.xmlsoap.org/soap/envelope/' => 'soapenv', |
153 |
'http://schemas.xmlsoap.org/soap/encoding/' => 'soap', |
154 |
'http://www.w3.org/2001/XMLSchema' => 'xsd', |
155 |
'http://www.w3.org/2001/XMLSchema-instance' => 'xsi', |
156 |
'urn:dslforum-org:cwmp-1-0' => '', |
157 |
}, |
158 |
rules => $rules, |
159 |
); |
160 |
|
161 |
# warn "## created $parser\n"; |
162 |
|
163 |
$parser->parsestring( $xml ); |
164 |
|
165 |
undef $parser; |
166 |
|
167 |
if ( my $trigger = $state->{_trigger} ) { |
168 |
warn "### call_trigger( $trigger )\n"; |
169 |
$self->call_trigger( $trigger, $state ); |
170 |
} |
171 |
# XXX propagate _trigger (useful for symlinks) |
172 |
|
173 |
return $state; |
174 |
} |
175 |
|
176 |
=head2 _tag |
177 |
|
178 |
Get value of tag. Tag name is case insensitive (don't ask why), |
179 |
we ignore namespaces and can take optional C<sub_key> |
180 |
(usually C<_content>). |
181 |
|
182 |
_tag( $tag_hash, $name, $sub_key ) |
183 |
|
184 |
=cut |
185 |
|
186 |
sub _tag { |
187 |
my ( $tag_hash, $name, $sub_key ) = @_; |
188 |
confess "need hash as first argument" unless ( ref $tag_hash eq 'HASH' ); |
189 |
$name = (grep { m/^(?:\w+:)*$name$/i } keys %$tag_hash )[0]; |
190 |
# $name =~ s/^\w+://; |
191 |
if ( defined $tag_hash->{$name} ) { |
192 |
if ( ! defined $sub_key ) { |
193 |
return $tag_hash->{$name}; |
194 |
} elsif ( defined $tag_hash->{$name}->{$sub_key} ) { |
195 |
return $tag_hash->{$name}->{$sub_key}; |
196 |
} else { |
197 |
return if ( $name =~ m/^value$/i ); |
198 |
warn "can't find '$name/$sub_key' in ", dump( $tag_hash ); |
199 |
return; |
200 |
} |
201 |
} else { |
202 |
warn "can't find '$name' in ", dump( $tag_hash ); |
203 |
return; |
204 |
} |
205 |
} |
206 |
|
207 |
1; |