/[cwmp]/google/trunk/lib/CWMP/Request.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /google/trunk/lib/CWMP/Request.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 93 - (hide annotations)
Sat Jun 23 09:20:03 2007 UTC (17 years ago) by dpavlin
Original Path: google/lib/CWMP/Request.pm
File size: 4505 byte(s)
dump ouput only if $debug = 1
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 dpavlin 93 my $debug = 0;
11    
12 dpavlin 31 =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 dpavlin 90 our $state; # FIXME check this!
50 dpavlin 31
51     my $parser = XML::Rules->new(
52     # start_rules => [
53     # '^division_name,fax' => 'skip',
54     # ],
55     namespaces => {
56 dpavlin 62 '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 dpavlin 31 '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 dpavlin 36 'Inform' => sub {
76 dpavlin 71 $state->{_dispatch} = 'InformResponse'; # what reponse to call
77 dpavlin 36 },
78 dpavlin 65 'DeviceId' => sub {
79 dpavlin 31 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 dpavlin 65 'EventStruct' => sub {
88 dpavlin 31 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 dpavlin 65 'ParameterValueStruct' => sub {
96 dpavlin 31 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 dpavlin 65 'MethodList' => sub {
106 dpavlin 31 my ($tag_name, $tag_hash, $context, $parent_data) = @_;
107     $state->{MethodList} = _tag( $tag_hash, 'string' );
108 dpavlin 65 },
109 dpavlin 50 #
110 dpavlin 65 # GetParameterNamesResponse
111     #
112     'ParameterInfoStruct' => sub {
113     my ($tag_name, $tag_hash, $context, $parent_data) = @_;
114 dpavlin 90 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 dpavlin 93 warn "===> $name\n" if $debug;
123 dpavlin 92 $s =~ s/^([^\.]+)/\$state->{ParameterInfo}->{'$1'}/;
124 dpavlin 93 warn "---> $s\n" if $debug;
125 dpavlin 92
126     my $stat;
127     while ( $s =~ s/\.(\d+)/->[$1]/ ) {
128     $stat->{array}++;
129 dpavlin 93 warn "-\@-> $s\n" if $debug;
130 dpavlin 92 }
131     while ( $s =~ s/\.([a-zA-Z0-9_]+)/->{'$1'}/ ) {
132     $stat->{scalar}++;
133 dpavlin 93 warn "-\$-> $s\n" if $debug;
134    
135 dpavlin 92 };
136     $s .= "->{'writable'} = $writable;";
137    
138 dpavlin 93 warn "## $name\n## tree: $s\n## stat: ",dump( $stat ), "\n" if $debug;
139 dpavlin 92
140 dpavlin 90 eval "$s";
141     confess "can't eval $s : $@" if ($@);
142    
143 dpavlin 92 #warn "## state = dump( $state ), "\n";
144 dpavlin 65 },
145     #
146 dpavlin 50 # Fault
147     #
148 dpavlin 65 'Fault' => sub {
149 dpavlin 50 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 dpavlin 31 ]
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 dpavlin 32
175     1;

  ViewVC Help
Powered by ViewVC 1.1.26