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

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.26