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

google/lib/CWMP/Server.pm revision 55 by dpavlin, Wed Jun 20 20:49:53 2007 UTC google/trunk/lib/CWMP/Session.pm revision 150 by dpavlin, Sat Oct 27 22:53:14 2007 UTC
# Line 1  Line 1 
1  # Dobrica Pavlinusic, <dpavlin@rot13.org> 06/18/07 10:19:50 CEST  # Dobrica Pavlinusic, <dpavlin@rot13.org> 06/18/07 10:19:50 CEST
2  package CWMP::Server;  package CWMP::Session;
3    
4  use strict;  use strict;
5  use warnings;  use warnings;
# Line 7  use warnings; Line 7  use warnings;
7  use base qw/Class::Accessor/;  use base qw/Class::Accessor/;
8  __PACKAGE__->mk_accessors( qw/  __PACKAGE__->mk_accessors( qw/
9  debug  debug
10  port  store
11    
12  sock  sock
13  state  state
14  queue  queue
15    store
16  / );  / );
17    
18  use IO::Socket::INET;  use HTTP::Daemon;
19  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
20    use Carp qw/confess cluck croak/;
21    use File::Slurp;
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    
29  CWMP::Server - implement logic of CWMP protocol  CWMP::Session - implement logic of CWMP protocol
30    
31  =head1 METHODS  =head1 METHODS
32    
33  =head2 new  =head2 new
34    
35    my $server = CWMP::Server->new({ port => 3333 });    my $server = CWMP::Session->new({
36            sock => $io_socket_object,
37            store => 'state.db',
38            queue => [ qw/GetRPCMethods GetParameterNames/ ],
39            debug => 1,
40      });
41    
42  =head2 run  =cut
43    
44    $server->run();  sub new {
45            my $class = shift;
46            my $self = $class->SUPER::new( @_ );
47    
48  =cut          confess "need sock" unless $self->sock;
49    
50  sub run {          $self->debug( 0 ) unless $self->debug;
         my $self = shift;  
51    
52          my $listen = IO::Socket::INET->new(          warn "created ", __PACKAGE__, "(", dump( @_ ), ") for ", $self->sock->peerhost, "\n" if $self->debug;
                 Listen    => 5,  
 #               LocalAddr => 'localhost',  
                 LocalPort => $self->port,  
                 Proto     => 'tcp',  
                 Blocking  => 1,  
                 ReuseAddr => 1,  
         );  
   
         warn "ACS waiting for request on port ", $self->port,  
                 $self->queue ? " queue ( " . join(",",@{$self->queue}) . " )" : "",  
                 "\n";  
   
         warn "## debug level: ", $self->debug, "\n" if $self->debug;  
   
         while ( my $sock = $listen->accept ) {  
                 $sock->autoflush(1);  
   
                 warn "connection from ", $sock->peerhost, "\n" if $self->debug;  
   
                 $self->sock( $sock );   # FIXME this will not work for multiple clients  
                 while ( $self->process_request ) {  
                         warn "...another one bites the dust...\n";  
                 }  
53    
54                  warn "...returning to accepting new connections\n";          my $store_obj = CWMP::Store->new({
55          }                  debug => $self->debug,
56                    %{ $self->store },
57            });
58    
59            croak "can't open ", dump( $self->store ), ": $!" unless $store_obj;
60    
61            # FIXME looks ugly. Should we have separate accessor for this?
62            $self->store( $store_obj );
63    
64            return $self;
65  }  }
66    
67  =head2 process_request  =head2 process_request
# Line 73  One request from client/response from se Line 70  One request from client/response from se
70  facilitate brain-dead concept of adding state to stateless protocol like  facilitate brain-dead concept of adding state to stateless protocol like
71  HTTP.  HTTP.
72    
73    If used with debugging level of 3 or more, it will also create dumps of
74    requests named C<< dump/nr.request >> where C<nr> is number from 0 to total number
75    of requests in single session.
76    
77  =cut  =cut
78    
79    my $dump_nr = 0;
80    
81  sub process_request {  sub process_request {
82          my $self = shift;          my $self = shift;
83    
84          my $sock = $self->sock || die "no sock?";          my $sock = $self->sock || die "no sock?";
85    
86          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 'Net::Server::Proto::TCP' );
87    
88          if ( ! $sock->connected ) {          if ( ! $sock->connected ) {
89                  warn "SOCKET NOT CONNECTED\n";                  warn "SOCKET NOT CONNECTED\n";
90                  return 0;                  return 0;
91          }          }
92    
93          $sock->autoflush( 1 );          bless $sock, 'HTTP::Daemon::ClientConn';
         $sock->blocking( 1 );  
   
         ### read the first line of response  
         my $line = $sock->getline;  
         return $self->error(400, "No Data") unless ( defined $line );  
   
         $line =~ s/[\r\n]+$//;  
         if ($line !~ /^ (\w+) \ + (\S+) \ + (HTTP\/1.\d) $ /x) {  
                 warn "ERROR: $line\n";  
                 return $self->error(400, "Bad request");  
         }  
         my ($method, $req, $protocol) = ($1, $2, $3);  
         warn "<<<< ", $sock->peerhost, " - - [" . localtime() . "] \"$method $req $protocol\"\n";  
   
         ### read in other headers  
         $self->read_headers || return $self->error(400, "Strange headers");  
   
         ### do we support the type  
 #       if ($method !~ /GET|POST|HEAD/) {  
         if ($method !~ /POST/) {  
                 return $self->error(400, "Unsupported Method");  
         }  
   
     my $chunk;  
         my $transfer_encoding = $self->header('Transfer-Encoding');  
   
         if ( $transfer_encoding && $transfer_encoding =~ qr/^chunked/i ) {  
   
                 my $len = 0;  
94    
95                  do {          # why do I have to do this?
96            # solution from http://use.perl.org/~Matts/journal/12896
97            ${*$sock}{'httpd_daemon'} = HTTP::Daemon->new;
98    
99                          warn "get chunk len\n" if $self->debug;          my $r = $sock->get_request || confess "can't get_request";
                           
                         my $hex;  
                         do {  
                                 $hex = $sock->getline;  
                                 $hex =~ s/[\n\r]+$//;  
                         } until ( $hex ne '' );  
100    
101                          die "chunk size not valid hex: $hex" unless ( $hex =~ m/^[0-9a-f]+$/i);          my $chunk = $r->content;
                         $len = hex( $hex );  
102    
103                          warn "getting chunk of $len bytes\n" if $self->debug;          my $size = length( $chunk );
   
                         $sock->read( my $buff, $len );  
                         $chunk .= $buff;  
   
                         warn "--- $len bytes: --=>||$buff||<=--\n" if $self->debug;  
104    
105                  } while ( $len > 0 );          warn "<<<< ", $sock->peerhost, " [" . localtime() . "] ", $r->method, " ", $r->uri, " $size bytes\n";
                 my $sep = $sock->getline;  
                 die "expected separator, not ", dump( $sep ) if ( $sep !~ m/^[\n\r]+$/ );  
106    
107          } else {          if ( $self->debug > 2 ) {
108                  die "right now, we support only Transfer-Encoding: chunked";                  my $file = sprintf("dump/%04d-%s.request", $dump_nr++, $sock->peerhost);
109                    write_file( $file, $r->as_string );
110                    warn "### request dump: $file\n";
111          }          }
112    
         my $size = length( $chunk );  
   
         warn "<<< " . $sock->peerhost . " [" . localtime() . "] request $size bytes\n";  
   
113          my $state;          my $state;
114    
115          if ( $size > 0 ) {          if ( $size > 0 ) {
116    
117                  die "no SOAPAction header in ",dump($chunk) unless defined ( $self->header('SOAPAction') );                  die "no SOAPAction header in ",dump($chunk) unless defined ( $r->header('SOAPAction') );
   
118    
119                  if ( $chunk ) {                  $state = CWMP::Request->parse( $chunk );
                         warn "## request chunk: ",length($chunk)," bytes\n$chunk\n" if $self->debug;  
120    
121                          $state = CWMP::Request->parse( $chunk );                  warn "## acquired state = ", dump( $state ), "\n";
122    
123                          warn "acquired state = ", dump( $state ), "\n";                  $self->state( $state );
124                    $self->store->update_state( ID => $state->{ID}, $state );
125    
126                          $self->state( $state );          } else {
127    
128                  } else {                  warn "## empty request, using last request state\n";
                         warn "empty request\n";  
                 }  
129    
         } else {  
130                  $state = $self->state;                  $state = $self->state;
131                  warn "last request state = ", dump( $state ), "\n";                  delete( $state->{_dispatch} );
132                    #warn "last request state = ", dump( $state ), "\n" if $self->debug > 1;
133          }          }
134    
135    
# Line 192  sub process_request { Line 149  sub process_request {
149          } elsif ( $dispatch = shift @{ $self->queue } ) {          } elsif ( $dispatch = shift @{ $self->queue } ) {
150                  $xml = $self->dispatch( $dispatch );                  $xml = $self->dispatch( $dispatch );
151          } elsif ( $size == 0 ) {          } elsif ( $size == 0 ) {
152                  warn ">>> closing connection\n";                  warn ">>> no more queued commands, closing connection\n";
153                  return 0;                  return 0;
154          } else {          } else {
155                  warn ">>> empty response\n";                  warn ">>> empty response\n";
# Line 203  sub process_request { Line 160  sub process_request {
160          $sock->send( "Content-Length: " . length( $xml ) . "\r\n\r\n" );          $sock->send( "Content-Length: " . length( $xml ) . "\r\n\r\n" );
161          $sock->send( $xml ) or die "can't send response";          $sock->send( $xml ) or die "can't send response";
162    
163          warn "### request over";          warn ">>>> " . $sock->peerhost . " [" . localtime() . "] sent ", length( $xml )," bytes\n";
164    
165            warn "### request over\n" if $self->debug;
166    
167            return 1;       # next request
168  };  };
169    
170  =head2 dispatch  =head2 dispatch
171    
172    $xml = $self->dispatch('Inform', $response_arguments );    $xml = $self->dispatch('Inform', $response_arguments );
173    
174    If debugging level of 3 or more, it will create dumps of responses named C<< dump/nr.response >>
175    
176  =cut  =cut
177    
178  sub dispatch {  sub dispatch {
# Line 222  sub dispatch { Line 184  sub dispatch {
184    
185          if ( $response->can( $dispatch ) ) {          if ( $response->can( $dispatch ) ) {
186                  warn ">>> dispatching to $dispatch\n";                  warn ">>> dispatching to $dispatch\n";
187                  my $xml = $response->$dispatch( $self->state, @_ ) . "\r\n";                  my $xml = $response->$dispatch( $self->state, @_ );
188                  warn "## response payload: ",length($xml)," bytes\n$xml\n" if $self->debug;                  warn "## response payload: ",length($xml)," bytes\n$xml\n" if $self->debug;
189                    if ( $self->debug > 2 ) {
190                            my $file = sprintf("dump/%04d-%s.response", $dump_nr++, $self->sock->peerhost);
191                            write_file( $file, $xml );
192                            warn "### response dump: $file\n";
193                    }
194                  return $xml;                  return $xml;
195          } else {          } else {
196                  confess "can't dispatch to $dispatch";                  confess "can't dispatch to $dispatch";
197          }          }
198  };  };
199    
 =head2 read_headers  
   
 parse headers from request  
   
 =cut  
   
 sub read_headers {  
   my $self = shift;  
   
   $self->{headers} = {};  
   
   while (defined($_ = $self->sock->getline)) {  
     s/[\r\n]+$//;  
     last unless length $_;  
         warn "-- $_\n" if $self->debug;  
     return 0 if ! /^ ([\w\-]+) :[\ \t]* (.*) $/x;  
     $self->{headers}->{$1} = $2;  
   }  
   
   return 1;  
 }  
   
 =head2 header  
   
 Getter for specific header  
   
   $self->header('Cookies');  
   
 =cut  
   
 sub header {  
         my $self = shift;  
         my $header = shift || die "no header?";  
         if ( defined( $self->{headers}->{$header} )) {  
                 return $self->{headers}->{$header};  
         } else {  
                 return;  
         }  
 }  
200    
201  =head2 error  =head2 error
202    

Legend:
Removed from v.55  
changed lines
  Added in v.150

  ViewVC Help
Powered by ViewVC 1.1.26