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

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

revision 82 by dpavlin, Fri Jun 22 15:31:36 2007 UTC revision 110 by dpavlin, Sun Oct 21 01:33:53 2007 UTC
# 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
 port  
10  store_path  store_path
11    
12  sock  sock
# Line 16  queue Line 15  queue
15  store  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/;  use Carp qw/confess cluck croak/;
21    
# Line 33  CWMP::Session - implement logic of CWMP Line 32  CWMP::Session - implement logic of CWMP
32  =head2 new  =head2 new
33    
34    my $server = CWMP::Session->new({    my $server = CWMP::Session->new({
35          port => 3333,          sock => $io_socket_object,
36          store_path => 'state.db',          store_path => 'state.db',
37            queue => [ qw/GetRPCMethods GetParameterNames/ ],
38          debug => 1,          debug => 1,
39    });    });
40    
 =head2 run  
   
   $server->run();  
   
41  =cut  =cut
42    
43  sub run {  sub new {
44          my $self = shift;          my $class = shift;
45            my $self = $class->SUPER::new( @_ );
46    
47          my $listen = IO::Socket::INET->new(          confess "need sock" unless $self->sock;
                 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";  
48    
49          $self->debug( 0 ) unless $self->debug;          $self->debug( 0 ) unless $self->debug;
50          warn "## debug level: ", $self->debug, "\n" if $self->debug;  
51            warn "created ", __PACKAGE__, "(", dump( @_ ), ") for ", $self->sock->peerhost, "\n" if $self->debug;
52    
53          $self->store( CWMP::Store->new({          $self->store( CWMP::Store->new({
54                  debug => $self->debug,                  debug => $self->debug,
55                  path => $self->store_path,                  path => $self->store_path,
56          }) );          }) );
         croak "can't open ", $self->store_path, ": $!" unless $self->store;  
   
         while ( my $sock = $listen->accept ) {  
                 $sock->autoflush(1);  
57    
58                  warn "connection from ", $sock->peerhost, "\n" if $self->debug;          croak "can't open ", $self->store_path, ": $!" unless $self->store;
   
                 $self->sock( $sock );   # FIXME this will not work for multiple clients  
                 while ( $self->process_request ) {  
                         warn "...another one bites the dust...\n";  
                 }  
59    
60                  warn "...returning to accepting new connections\n";          return $self;
         }  
61  }  }
62    
63  =head2 process_request  =head2 process_request
# Line 96  sub process_request { Line 73  sub process_request {
73    
74          my $sock = $self->sock || die "no sock?";          my $sock = $self->sock || die "no sock?";
75    
76          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' );
77    
78          if ( ! $sock->connected ) {          if ( ! $sock->connected ) {
79                  warn "SOCKET NOT CONNECTED\n";                  warn "SOCKET NOT CONNECTED\n";
80                  return 0;                  return 0;
81          }          }
82    
83          $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;  
   
                 do {  
   
                         warn "get chunk len\n" if $self->debug > 1;  
                           
                         my $hex;  
                         do {  
                                 $hex = $sock->getline;  
                                 $hex =~ s/[\n\r]+$//;  
                         } until ( $hex ne '' );  
   
                         die "chunk size not valid hex: $hex" unless ( $hex =~ m/^[0-9a-f]+$/i);  
                         $len = hex( $hex );  
84    
85                          warn "getting chunk of $len bytes\n" if $self->debug > 1;          # why do I have to do this?
86            # solution from http://use.perl.org/~Matts/journal/12896
87            ${*$sock}{'httpd_daemon'} = HTTP::Daemon->new;
88    
89                          $sock->read( my $buff, $len );          my $r = $sock->get_request || confess "can't get_request";
                         $chunk .= $buff;  
90    
91                          warn "--- $len bytes: --=>||$buff||<=--\n" if $self->debug > 1;          warn "<<<< ", $sock->peerhost, " - - [" . localtime() . "] ", $r->method, " ", $r->uri, "\n";
92    
93                  } while ( $len > 0 );          my $chunk = $r->content;
                 my $sep = $sock->getline;  
                 die "expected separator, not ", dump( $sep ) if ( $sep !~ m/^[\n\r]+$/ );  
   
         } else {  
                 die "right now, we support only Transfer-Encoding: chunked";  
         }  
94    
95          my $size = length( $chunk );          my $size = length( $chunk );
96    
# Line 170  sub process_request { Line 100  sub process_request {
100    
101          if ( $size > 0 ) {          if ( $size > 0 ) {
102    
103                  die "no SOAPAction header in ",dump($chunk) unless defined ( $self->header('SOAPAction') );                  die "no SOAPAction header in ",dump($chunk) unless defined ( $r->header('SOAPAction') );
104    
105    
106                  if ( $chunk ) {                  if ( $chunk ) {
# Line 181  sub process_request { Line 111  sub process_request {
111                          warn "## acquired state = ", dump( $state ), "\n";                          warn "## acquired state = ", dump( $state ), "\n";
112    
113                          $self->state( $state );                          $self->state( $state );
114                          $self->store->update_state( $state->{ID} => $state );                          $self->store->update_state( ID => $state->{ID}, $state );
115    
116                  } else {                  } else {
117                          warn "## empty request\n";                          warn "## empty request\n";
# Line 250  sub dispatch { Line 180  sub dispatch {
180          }          }
181  };  };
182    
 =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;  
         }  
 }  
183    
184  =head2 error  =head2 error
185    

Legend:
Removed from v.82  
changed lines
  Added in v.110

  ViewVC Help
Powered by ViewVC 1.1.26