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