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

  ViewVC Help
Powered by ViewVC 1.1.26