/[cwmp]/google/trunk/lib/CWMP/Session.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/Session.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 83 - (hide annotations)
Fri Jun 22 15:54:43 2007 UTC (16 years, 11 months ago) by dpavlin
Original Path: google/lib/CWMP/Session.pm
File size: 5974 byte(s)
create again CWMP::Server which spawns CWMP::Session for each CPE
1 dpavlin 30 # Dobrica Pavlinusic, <dpavlin@rot13.org> 06/18/07 10:19:50 CEST
2 dpavlin 82 package CWMP::Session;
3 dpavlin 30
4     use strict;
5     use warnings;
6    
7 dpavlin 40 use base qw/Class::Accessor/;
8 dpavlin 30 __PACKAGE__->mk_accessors( qw/
9     debug
10 dpavlin 77 store_path
11    
12 dpavlin 41 sock
13 dpavlin 49 state
14 dpavlin 50 queue
15 dpavlin 77 store
16 dpavlin 30 / );
17    
18 dpavlin 40 use IO::Socket::INET;
19 dpavlin 30 use Data::Dump qw/dump/;
20 dpavlin 77 use Carp qw/confess cluck croak/;
21    
22 dpavlin 35 use CWMP::Request;
23     use CWMP::Response;
24 dpavlin 77 use CWMP::Store;
25 dpavlin 30
26 dpavlin 34 =head1 NAME
27    
28 dpavlin 82 CWMP::Session - implement logic of CWMP protocol
29 dpavlin 34
30     =head1 METHODS
31    
32 dpavlin 40 =head2 new
33 dpavlin 34
34 dpavlin 82 my $server = CWMP::Session->new({
35 dpavlin 83 sock => $io_socket_object,
36 dpavlin 77 store_path => 'state.db',
37 dpavlin 83 queue => [ qw/GetRPCMethods GetParameterNames/ ],
38 dpavlin 77 debug => 1,
39     });
40 dpavlin 34
41 dpavlin 40
42 dpavlin 34 =cut
43    
44 dpavlin 83 sub new {
45     my $class = shift;
46     my $self = $class->SUPER::new( @_ );
47 dpavlin 30
48 dpavlin 83 confess "need sock" unless $self->sock;
49 dpavlin 40
50 dpavlin 83 warn "created ", __PACKAGE__, "(", dump( @_ ), ") for ", $self->sock->peerhost, "\n" if $self->debug;
51 dpavlin 40
52 dpavlin 77 $self->store( CWMP::Store->new({
53     debug => $self->debug,
54     path => $self->store_path,
55     }) );
56 dpavlin 83
57 dpavlin 77 croak "can't open ", $self->store_path, ": $!" unless $self->store;
58    
59 dpavlin 83 return $self;
60 dpavlin 40 }
61    
62 dpavlin 48 =head2 process_request
63    
64     One request from client/response from server cycle. Call multiple times to
65     facilitate brain-dead concept of adding state to stateless protocol like
66     HTTP.
67    
68     =cut
69    
70 dpavlin 40 sub process_request {
71     my $self = shift;
72    
73 dpavlin 42 my $sock = $self->sock || die "no sock?";
74 dpavlin 40
75     die "not IO::Socket::INET but ", ref( $sock ) unless ( ref($sock) eq 'IO::Socket::INET' );
76    
77 dpavlin 42 if ( ! $sock->connected ) {
78 dpavlin 53 warn "SOCKET NOT CONNECTED\n";
79 dpavlin 42 return 0;
80     }
81    
82 dpavlin 40 $sock->autoflush( 1 );
83     $sock->blocking( 1 );
84    
85     ### read the first line of response
86 dpavlin 49 my $line = $sock->getline;
87     return $self->error(400, "No Data") unless ( defined $line );
88 dpavlin 40
89     $line =~ s/[\r\n]+$//;
90     if ($line !~ /^ (\w+) \ + (\S+) \ + (HTTP\/1.\d) $ /x) {
91 dpavlin 49 warn "ERROR: $line\n";
92 dpavlin 40 return $self->error(400, "Bad request");
93     }
94     my ($method, $req, $protocol) = ($1, $2, $3);
95 dpavlin 49 warn "<<<< ", $sock->peerhost, " - - [" . localtime() . "] \"$method $req $protocol\"\n";
96 dpavlin 40
97     ### read in other headers
98 dpavlin 42 $self->read_headers || return $self->error(400, "Strange headers");
99 dpavlin 40
100     ### do we support the type
101     # if ($method !~ /GET|POST|HEAD/) {
102     if ($method !~ /POST/) {
103     return $self->error(400, "Unsupported Method");
104     }
105    
106 dpavlin 30 my $chunk;
107     my $transfer_encoding = $self->header('Transfer-Encoding');
108    
109     if ( $transfer_encoding && $transfer_encoding =~ qr/^chunked/i ) {
110    
111 dpavlin 31 my $len = 0;
112 dpavlin 40
113 dpavlin 30 do {
114    
115 dpavlin 67 warn "get chunk len\n" if $self->debug > 1;
116 dpavlin 40
117     my $hex;
118     do {
119     $hex = $sock->getline;
120     $hex =~ s/[\n\r]+$//;
121     } until ( $hex ne '' );
122 dpavlin 30
123 dpavlin 40 die "chunk size not valid hex: $hex" unless ( $hex =~ m/^[0-9a-f]+$/i);
124     $len = hex( $hex );
125 dpavlin 30
126 dpavlin 67 warn "getting chunk of $len bytes\n" if $self->debug > 1;
127 dpavlin 30
128 dpavlin 40 $sock->read( my $buff, $len );
129     $chunk .= $buff;
130 dpavlin 30
131 dpavlin 67 warn "--- $len bytes: --=>||$buff||<=--\n" if $self->debug > 1;
132 dpavlin 30
133 dpavlin 40 } while ( $len > 0 );
134 dpavlin 49 my $sep = $sock->getline;
135     die "expected separator, not ", dump( $sep ) if ( $sep !~ m/^[\n\r]+$/ );
136 dpavlin 30
137 dpavlin 40 } else {
138     die "right now, we support only Transfer-Encoding: chunked";
139     }
140 dpavlin 34
141 dpavlin 49 my $size = length( $chunk );
142 dpavlin 34
143 dpavlin 72 warn "<<<< " . $sock->peerhost . " [" . localtime() . "] request $size bytes\n";
144 dpavlin 34
145 dpavlin 36 my $state;
146    
147 dpavlin 49 if ( $size > 0 ) {
148 dpavlin 30
149 dpavlin 49 die "no SOAPAction header in ",dump($chunk) unless defined ( $self->header('SOAPAction') );
150 dpavlin 34
151 dpavlin 49
152     if ( $chunk ) {
153     warn "## request chunk: ",length($chunk)," bytes\n$chunk\n" if $self->debug;
154    
155     $state = CWMP::Request->parse( $chunk );
156    
157 dpavlin 70 warn "## acquired state = ", dump( $state ), "\n";
158 dpavlin 49
159     $self->state( $state );
160 dpavlin 79 $self->store->update_state( $state->{ID} => $state );
161 dpavlin 49
162     } else {
163 dpavlin 70 warn "## empty request\n";
164 dpavlin 49 }
165    
166 dpavlin 36 } else {
167 dpavlin 49 $state = $self->state;
168 dpavlin 67 warn "last request state = ", dump( $state ), "\n" if $self->debug > 1;
169 dpavlin 34 }
170    
171    
172 dpavlin 44 $sock->send(join("\r\n",
173     'HTTP/1.1 200 OK',
174     'Content-Type: text/xml; charset="utf-8"',
175     'Server: AcmeCWMP/42',
176 dpavlin 49 'SOAPServer: AcmeCWMP/42'
177 dpavlin 54 )."\r\n");
178 dpavlin 30
179 dpavlin 43 $sock->send( "Set-Cookie: ID=" . $state->{ID} . "; path=/\r\n" ) if ( $state->{ID} );
180 dpavlin 40
181     my $xml = '';
182    
183 dpavlin 36 if ( my $dispatch = $state->{_dispatch} ) {
184 dpavlin 50 $xml = $self->dispatch( $dispatch );
185     } elsif ( $dispatch = shift @{ $self->queue } ) {
186     $xml = $self->dispatch( $dispatch );
187     } elsif ( $size == 0 ) {
188     warn ">>> closing connection\n";
189     return 0;
190 dpavlin 36 } else {
191     warn ">>> empty response\n";
192 dpavlin 50 $state->{NoMoreRequests} = 1;
193     $xml = $self->dispatch( 'xml', sub {} );
194 dpavlin 36 }
195 dpavlin 40
196 dpavlin 43 $sock->send( "Content-Length: " . length( $xml ) . "\r\n\r\n" );
197 dpavlin 44 $sock->send( $xml ) or die "can't send response";
198 dpavlin 40
199 dpavlin 72 warn ">>>> " . $sock->peerhost . " [" . localtime() . "] sent ", length( $xml )," bytes\n";
200    
201 dpavlin 70 warn "### request over\n" if $self->debug;
202 dpavlin 40
203 dpavlin 72 return 1; # next request
204 dpavlin 30 };
205    
206 dpavlin 50 =head2 dispatch
207    
208     $xml = $self->dispatch('Inform', $response_arguments );
209    
210     =cut
211    
212     sub dispatch {
213     my $self = shift;
214    
215     my $dispatch = shift || die "no dispatch?";
216    
217     my $response = CWMP::Response->new({ debug => $self->debug });
218    
219     if ( $response->can( $dispatch ) ) {
220     warn ">>> dispatching to $dispatch\n";
221 dpavlin 72 my $xml = $response->$dispatch( $self->state, @_ );
222 dpavlin 53 warn "## response payload: ",length($xml)," bytes\n$xml\n" if $self->debug;
223 dpavlin 50 return $xml;
224     } else {
225     confess "can't dispatch to $dispatch";
226     }
227     };
228    
229 dpavlin 48 =head2 read_headers
230 dpavlin 40
231 dpavlin 48 parse headers from request
232    
233     =cut
234    
235 dpavlin 40 sub read_headers {
236     my $self = shift;
237    
238     $self->{headers} = {};
239    
240 dpavlin 42 while (defined($_ = $self->sock->getline)) {
241 dpavlin 40 s/[\r\n]+$//;
242     last unless length $_;
243 dpavlin 53 warn "-- $_\n" if $self->debug;
244 dpavlin 40 return 0 if ! /^ ([\w\-]+) :[\ \t]* (.*) $/x;
245     $self->{headers}->{$1} = $2;
246     }
247    
248     return 1;
249     }
250    
251 dpavlin 48 =head2 header
252    
253     Getter for specific header
254    
255     $self->header('Cookies');
256    
257     =cut
258    
259 dpavlin 40 sub header {
260     my $self = shift;
261     my $header = shift || die "no header?";
262     if ( defined( $self->{headers}->{$header} )) {
263     return $self->{headers}->{$header};
264     } else {
265     return;
266     }
267     }
268    
269 dpavlin 48 =head2 error
270    
271     return $self->error( 501, 'System error' );
272    
273     =cut
274    
275 dpavlin 44 sub error {
276 dpavlin 40 my ($self, $number, $msg) = @_;
277 dpavlin 44 $msg ||= 'ERROR';
278     $self->sock->send( "HTTP/1.1 $number $msg\r\n" );
279 dpavlin 40 warn "Error - $number - $msg\n";
280 dpavlin 48 return 0; # close connection
281 dpavlin 40 }
282    
283 dpavlin 30 1;

  ViewVC Help
Powered by ViewVC 1.1.26