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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 43 by dpavlin, Tue Jun 19 18:50:28 2007 UTC revision 77 by dpavlin, Fri Jun 22 13:09:08 2007 UTC
# Line 8  use base qw/Class::Accessor/; Line 8  use base qw/Class::Accessor/;
8  __PACKAGE__->mk_accessors( qw/  __PACKAGE__->mk_accessors( qw/
9  debug  debug
10  port  port
11    store_path
12    
13  sock  sock
14    state
15    queue
16    store
17  / );  / );
18    
19  use IO::Socket::INET;  use IO::Socket::INET;
20  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
21    use Carp qw/confess cluck croak/;
22    
23  use CWMP::Request;  use CWMP::Request;
24  use CWMP::Response;  use CWMP::Response;
25  use Carp qw/confess cluck/;  use CWMP::Store;
26    
27  =head1 NAME  =head1 NAME
28    
# Line 25  CWMP::Server - implement logic of CWMP p Line 32  CWMP::Server - implement logic of CWMP p
32    
33  =head2 new  =head2 new
34    
35    my $server = CWMP::Server->new({ port => 3333 });    my $server = CWMP::Server->new({
36            port => 3333,
37            store_path => 'state.db',
38            debug => 1,
39      });
40    
41  =head2 run  =head2 run
42    
# Line 45  sub run { Line 56  sub run {
56                  ReuseAddr => 1,                  ReuseAddr => 1,
57          );          );
58    
59          warn "waiting for request on port ", $self->port, $/;          warn "ACS waiting for request on port ", $self->port,
60                    $self->queue ? " queue ( " . join(",",@{$self->queue}) . " )" : "",
61                    "\n";
62    
63            $self->debug( 0 ) unless $self->debug;
64            warn "## debug level: ", $self->debug, "\n" if $self->debug;
65    
66            $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          while ( my $sock = $listen->accept ) {          while ( my $sock = $listen->accept ) {
73                  $sock->autoflush(1);                  $sock->autoflush(1);
74    
75                  warn "connection from ", $sock->peerhost, "\n";                  warn "connection from ", $sock->peerhost, "\n" if $self->debug;
76    
77                  $self->sock( $sock );   # FIXME this will not work for multiple clients                  $self->sock( $sock );   # FIXME this will not work for multiple clients
78                  while ( $self->process_request ) {                  while ( $self->process_request ) {
79                          warn "...another one bites a dust...\n";                          warn "...another one bites the dust...\n";
80                  }                  }
81    
82                  warn "...returning to accepting new connections\n";                  warn "...returning to accepting new connections\n";
83          }          }
84  }  }
85    
86    =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  sub process_request {  sub process_request {
95          my $self = shift;          my $self = shift;
96    
# Line 69  sub process_request { Line 99  sub process_request {
99          die "not IO::Socket::INET but ", ref( $sock ) unless ( ref($sock) eq 'IO::Socket::INET' );          die "not IO::Socket::INET but ", ref( $sock ) unless ( ref($sock) eq 'IO::Socket::INET' );
100    
101          if ( ! $sock->connected ) {          if ( ! $sock->connected ) {
102                  warn "SOCKET NOT CONNECTED";                  warn "SOCKET NOT CONNECTED\n";
103                  return 0;                  return 0;
104          }          }
105    
# Line 77  sub process_request { Line 107  sub process_request {
107          $sock->blocking( 1 );          $sock->blocking( 1 );
108    
109          ### read the first line of response          ### read the first line of response
110          my $line = $sock->getline || $self->error(400, "No Data");          my $line = $sock->getline;
111            return $self->error(400, "No Data") unless ( defined $line );
112    
113          $line =~ s/[\r\n]+$//;          $line =~ s/[\r\n]+$//;
114          if ($line !~ /^ (\w+) \ + (\S+) \ + (HTTP\/1.\d) $ /x) {          if ($line !~ /^ (\w+) \ + (\S+) \ + (HTTP\/1.\d) $ /x) {
115                    warn "ERROR: $line\n";
116                  return $self->error(400, "Bad request");                  return $self->error(400, "Bad request");
117          }          }
118          my ($method, $req, $protocol) = ($1, $2, $3);          my ($method, $req, $protocol) = ($1, $2, $3);
119          warn "<<<< ",join(" ", time, $method, $req)."\n";          warn "<<<< ", $sock->peerhost, " - - [" . localtime() . "] \"$method $req $protocol\"\n";
120    
121          ### read in other headers          ### read in other headers
122          $self->read_headers || return $self->error(400, "Strange headers");          $self->read_headers || return $self->error(400, "Strange headers");
# Line 104  sub process_request { Line 136  sub process_request {
136    
137                  do {                  do {
138    
139                          warn "get chunk len\n" if $self->debug;                          warn "get chunk len\n" if $self->debug > 1;
140                                                    
141                          my $hex;                          my $hex;
142                          do {                          do {
# Line 115  sub process_request { Line 147  sub process_request {
147                          die "chunk size not valid hex: $hex" unless ( $hex =~ m/^[0-9a-f]+$/i);                          die "chunk size not valid hex: $hex" unless ( $hex =~ m/^[0-9a-f]+$/i);
148                          $len = hex( $hex );                          $len = hex( $hex );
149    
150                          warn "getting chunk of $len bytes\n" if $self->debug;                          warn "getting chunk of $len bytes\n" if $self->debug > 1;
151    
152                          $sock->read( my $buff, $len );                          $sock->read( my $buff, $len );
153                          $chunk .= $buff;                          $chunk .= $buff;
154    
155                          warn "--- $len bytes: --=>||$buff||<=--\n";                          warn "--- $len bytes: --=>||$buff||<=--\n" if $self->debug > 1;
156    
157                  } while ( $len > 0 );                  } while ( $len > 0 );
158                    my $sep = $sock->getline;
159                    die "expected separator, not ", dump( $sep ) if ( $sep !~ m/^[\n\r]+$/ );
160    
161          } else {          } else {
162                  die "right now, we support only Transfer-Encoding: chunked";                  die "right now, we support only Transfer-Encoding: chunked";
163          }          }
164    
165          warn "handler got ", length($chunk), " bytes\n" if $self->debug;          my $size = length( $chunk );
   
         warn "<<< " . localtime() . " " . $sock->peerhost . "\n";  
166    
167          die "not SOAP request" unless defined ( $self->header('SOAPAction') );          warn "<<<< " . $sock->peerhost . " [" . localtime() . "] request $size bytes\n";
168    
169          my $state;          my $state;
170    
171          if ( $chunk ) {          if ( $size > 0 ) {
                 warn "## request chunk: ",length($chunk)," bytes\n$chunk\n" if $self->debug;  
172    
173                  $state = CWMP::Request->parse( $chunk );                  die "no SOAPAction header in ",dump($chunk) unless defined ( $self->header('SOAPAction') );
174    
175    
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                            warn "## acquired state = ", dump( $state ), "\n";
182    
183                            $self->state( $state );
184    
185                    } else {
186                            warn "## empty request\n";
187                    }
188    
                 warn "acquired state = ", dump( $state ), "\n";  
           
189          } else {          } else {
190                  warn "empty request\n";                  $state = $self->state;
191                    warn "last request state = ", dump( $state ), "\n" if $self->debug > 1;
192          }          }
193    
194    
195          my $response = CWMP::Response->new({ debug => $self->debug });          $sock->send(join("\r\n",
196                    'HTTP/1.1 200 OK',
197          $sock->send(join("",                  'Content-Type: text/xml; charset="utf-8"',
198                  $self->status(200,'OK'),                  'Server: AcmeCWMP/42',
199                  $self->content_type('text/xml; charset="utf-8"'),                  'SOAPServer: AcmeCWMP/42'
200                  "Server: AcmeCWMP/42\r\n",          )."\r\n");
                 "SOAPServer: AcmeCWMP/42\r\n"  
         ));  
201    
202          $sock->send( "Set-Cookie: ID=" . $state->{ID} . "; path=/\r\n" ) if ( $state->{ID} );          $sock->send( "Set-Cookie: ID=" . $state->{ID} . "; path=/\r\n" ) if ( $state->{ID} );
203                    
204          my $xml = '';          my $xml = '';
205    
206          if ( my $dispatch = $state->{_dispatch} ) {          if ( my $dispatch = $state->{_dispatch} ) {
207                  if ( $response->can( $dispatch ) ) {                  $xml = $self->dispatch( $dispatch );
208                          warn ">>> dispatching to $dispatch\n";          } elsif ( $dispatch = shift @{ $self->queue } ) {
209                          $xml = $response->$dispatch( $state ) . "\r\n";                  $xml = $self->dispatch( $dispatch );
210                          warn "## response payload: ",length($xml)," bytes\n$xml\n";          } elsif ( $size == 0 ) {
211                  } else {                  warn ">>> closing connection\n";
212                          confess "can't dispatch to $dispatch";                  return 0;
                 }  
213          } else {          } else {
214                  warn ">>> empty response\n";                  warn ">>> empty response\n";
215                    $state->{NoMoreRequests} = 1;
216                    $xml = $self->dispatch( 'xml', sub {} );
217          }          }
218    
219          $sock->send( "Content-Length: " . length( $xml ) . "\r\n\r\n" );          $sock->send( "Content-Length: " . length( $xml ) . "\r\n\r\n" );
220          $sock->send( "$xml\r\n\r\n" ) or die "can't send response";          $sock->send( $xml ) or die "can't send response";
221    
222            warn ">>>> " . $sock->peerhost . " [" . localtime() . "] sent ", length( $xml )," bytes\n";
223    
224            warn "### request over\n" if $self->debug;
225    
226            return 1;       # next request
227    };
228    
229    =head2 dispatch
230    
231      $xml = $self->dispatch('Inform', $response_arguments );
232    
233          warn "### request over";  =cut
234    
235    sub dispatch {
236            my $self = shift;
237    
238            my $dispatch = shift || die "no dispatch?";
239    
240            my $response = CWMP::Response->new({ debug => $self->debug });
241    
242            if ( $response->can( $dispatch ) ) {
243                    warn ">>> dispatching to $dispatch\n";
244                    my $xml = $response->$dispatch( $self->state, @_ );
245                    warn "## response payload: ",length($xml)," bytes\n$xml\n" if $self->debug;
246                    return $xml;
247            } else {
248                    confess "can't dispatch to $dispatch";
249            }
250  };  };
251    
252    =head2 read_headers
253    
254    parse headers from request
255    
256    =cut
257    
258  sub read_headers {  sub read_headers {
259    my $self = shift;    my $self = shift;
# Line 189  sub read_headers { Line 263  sub read_headers {
263    while (defined($_ = $self->sock->getline)) {    while (defined($_ = $self->sock->getline)) {
264      s/[\r\n]+$//;      s/[\r\n]+$//;
265      last unless length $_;      last unless length $_;
266          warn "-- $_\n";          warn "-- $_\n" if $self->debug;
267      return 0 if ! /^ ([\w\-]+) :[\ \t]* (.*) $/x;      return 0 if ! /^ ([\w\-]+) :[\ \t]* (.*) $/x;
268      $self->{headers}->{$1} = $2;      $self->{headers}->{$1} = $2;
269    }    }
# Line 197  sub read_headers { Line 271  sub read_headers {
271    return 1;    return 1;
272  }  }
273    
274    =head2 header
275    
276    Getter for specific header
277    
278      $self->header('Cookies');
279    
280    =cut
281    
282  sub header {  sub header {
283          my $self = shift;          my $self = shift;
284          my $header = shift || die "no header?";          my $header = shift || die "no header?";
# Line 207  sub header { Line 289  sub header {
289          }          }
290  }  }
291    
292  sub content_type {  =head2 error
   my ($self, $type) = @_;  
   $self->http_header;  
   return "Content-type: $type\r\n";  
 }  
293    
294  sub error{    return $self->error( 501, 'System error' );
   my ($self, $number, $msg) = @_;  
   $self->sock->send( $self->status($number, $msg) . "\r\n" );  
   warn "Error - $number - $msg\n";  
 }  
295    
296  sub status {  =cut
   my ($self, $number, $msg) = @_;  
   $msg = '' if ! defined $msg;  
   return if $self->http_header($number);  
   return "Status $number: $msg\r\n";  
 }  
297    
298  sub http_header {  sub error {
299    my $self = shift;    my ($self, $number, $msg) = @_;
300    my $number = shift || 200;    $msg ||= 'ERROR';
301    return if ! delete $self->{needs_header};    $self->sock->send( "HTTP/1.1 $number $msg\r\n" );
302    $self->sock->send("HTTP/1.1 $number\r\n");    warn "Error - $number - $msg\n";
303    return 1;    return 0;     # close connection
304  }  }
305    
306  1;  1;

Legend:
Removed from v.43  
changed lines
  Added in v.77

  ViewVC Help
Powered by ViewVC 1.1.26