/[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

Contents of /google/trunk/lib/CWMP/Session.pm

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.26