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 |
=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 |
# 'http://schemas.xmlsoap.org/soap/envelope/' => 'soapenv', |
55 |
# 'http://schemas.xmlsoap.org/soap/encoding/' => 'soap', |
56 |
'urn:dslforum-org:cwmp-1-0' => '', |
57 |
}, |
58 |
rules => [ |
59 |
#_default => 'content trim', |
60 |
x_default => sub { |
61 |
my ($tag_name, $tag_hash, $context, $parent_data) = @_; |
62 |
warn dump( $tag_name, $tag_hash, $context ); |
63 |
}, |
64 |
'ID' => sub { |
65 |
my ($tag_name, $tag_hash, $context, $parent_data) = @_; |
66 |
$state->{ID} = $tag_hash->{_content}; |
67 |
}, |
68 |
# |
69 |
# Inform |
70 |
# |
71 |
qr/DeviceId/ => sub { |
72 |
my ($tag_name, $tag_hash, $context, $parent_data) = @_; |
73 |
foreach my $name ( keys %$tag_hash ) { |
74 |
next if $name eq '_content'; |
75 |
my $key = $name; |
76 |
$key =~ s/^\w+://; # stip namespace |
77 |
$state->{DeviceID}->{ $key } = _tag( $tag_hash, $name, '_content' ); |
78 |
} |
79 |
}, |
80 |
qr/EventStruct/ => sub { |
81 |
my ($tag_name, $tag_hash, $context, $parent_data) = @_; |
82 |
push @{ $state->{EventStruct} }, $tag_hash->{EventCode}->{_content}; |
83 |
}, |
84 |
qr/(MaxEnvelopes|CurrentTime|RetryCount)/ => sub { |
85 |
my ($tag_name, $tag_hash, $context, $parent_data) = @_; |
86 |
$state->{$tag_name} = $tag_hash->{_content}; |
87 |
}, |
88 |
qr/ParameterValueStruct/ => sub { |
89 |
my ($tag_name, $tag_hash, $context, $parent_data) = @_; |
90 |
# Name/Value tags must be case insnesitive |
91 |
my $value = (grep( /value/i, keys %$tag_hash ))[0]; |
92 |
$state->{Parameter}->{ _tag($tag_hash, 'Name', '_content') } = _tag($tag_hash, 'Value', '_content' ); |
93 |
}, |
94 |
# |
95 |
# GetRPCMethodsResponse |
96 |
# |
97 |
qr/^(?:^\w+:)*string$/ => 'content array', |
98 |
qr/MethodList/ => sub { |
99 |
my ($tag_name, $tag_hash, $context, $parent_data) = @_; |
100 |
$state->{MethodList} = _tag( $tag_hash, 'string' ); |
101 |
}, |
102 |
] |
103 |
); |
104 |
|
105 |
=head2 parse |
106 |
|
107 |
my $state = CWMP::Request->parse( "<soap>request</soap>" ); |
108 |
|
109 |
=cut |
110 |
|
111 |
sub parse { |
112 |
my $self = shift; |
113 |
|
114 |
my $xml = shift || confess "no xml?"; |
115 |
|
116 |
$state = {}; |
117 |
$parser->parsestring( $xml ); |
118 |
return $state; |
119 |
} |