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

  ViewVC Help
Powered by ViewVC 1.1.26