/[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/Session.pm revision 83 by dpavlin, Fri Jun 22 15:54:43 2007 UTC google/trunk/lib/CWMP/Session.pm revision 119 by dpavlin, Fri Oct 26 15:51:27 2007 UTC
# Line 15  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    use File::Slurp;
22    
23  use CWMP::Request;  use CWMP::Request;
24  use CWMP::Response;  use CWMP::Response;
# Line 32  CWMP::Session - implement logic of CWMP Line 33  CWMP::Session - implement logic of CWMP
33  =head2 new  =head2 new
34    
35    my $server = CWMP::Session->new({    my $server = CWMP::Session->new({
36          sock => $io_socket_object,          sock => $io_socket_object,
37          store_path => 'state.db',          store_path => 'state.db',
38          queue => [ qw/GetRPCMethods GetParameterNames/ ],          queue => [ qw/GetRPCMethods GetParameterNames/ ],
39          debug => 1,          debug => 1,
40    });    });
41    
   
42  =cut  =cut
43    
44  sub new {  sub new {
# Line 47  sub new { Line 47  sub new {
47    
48          confess "need sock" unless $self->sock;          confess "need sock" unless $self->sock;
49    
50            $self->debug( 0 ) unless $self->debug;
51    
52          warn "created ", __PACKAGE__, "(", dump( @_ ), ") for ", $self->sock->peerhost, "\n" if $self->debug;          warn "created ", __PACKAGE__, "(", dump( @_ ), ") for ", $self->sock->peerhost, "\n" if $self->debug;
53    
54          $self->store( CWMP::Store->new({          $self->store( CWMP::Store->new({
# Line 65  One request from client/response from se Line 67  One request from client/response from se
67  facilitate brain-dead concept of adding state to stateless protocol like  facilitate brain-dead concept of adding state to stateless protocol like
68  HTTP.  HTTP.
69    
70    If used with debugging level of 3 or more, it will also create dumps of
71    requests named C<< dump/nr.request >> where C<nr> is number from 0 to total number
72    of requests in single session.
73    
74  =cut  =cut
75    
76    my $dump_nr = 0;
77    
78  sub process_request {  sub process_request {
79          my $self = shift;          my $self = shift;
80    
81          my $sock = $self->sock || die "no sock?";          my $sock = $self->sock || die "no sock?";
82    
83          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' );
84    
85          if ( ! $sock->connected ) {          if ( ! $sock->connected ) {
86                  warn "SOCKET NOT CONNECTED\n";                  warn "SOCKET NOT CONNECTED\n";
87                  return 0;                  return 0;
88          }          }
89    
90          $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 ) {  
91    
92                  my $len = 0;          # why do I have to do this?
93            # solution from http://use.perl.org/~Matts/journal/12896
94            ${*$sock}{'httpd_daemon'} = HTTP::Daemon->new;
95    
96                  do {          my $r = $sock->get_request || confess "can't get_request";
97    
98                          warn "get chunk len\n" if $self->debug > 1;          my $chunk = $r->content;
                           
                         my $hex;  
                         do {  
                                 $hex = $sock->getline;  
                                 $hex =~ s/[\n\r]+$//;  
                         } until ( $hex ne '' );  
99    
100                          die "chunk size not valid hex: $hex" unless ( $hex =~ m/^[0-9a-f]+$/i);          my $size = length( $chunk );
                         $len = hex( $hex );  
   
                         warn "getting chunk of $len bytes\n" if $self->debug > 1;  
   
                         $sock->read( my $buff, $len );  
                         $chunk .= $buff;  
   
                         warn "--- $len bytes: --=>||$buff||<=--\n" if $self->debug > 1;  
101    
102                  } 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]+$/ );  
103    
104          } else {          if ( $self->debug > 2 ) {
105                  die "right now, we support only Transfer-Encoding: chunked";                  my $file = sprintf("dump/%04d.request", $dump_nr++);
106                    write_file( $file, $r->as_string );
107                    warn "### request dump: $file\n";
108          }          }
109    
         my $size = length( $chunk );  
   
         warn "<<<< " . $sock->peerhost . " [" . localtime() . "] request $size bytes\n";  
   
110          my $state;          my $state;
111    
112          if ( $size > 0 ) {          if ( $size > 0 ) {
113    
114                  die "no SOAPAction header in ",dump($chunk) unless defined ( $self->header('SOAPAction') );                  die "no SOAPAction header in ",dump($chunk) unless defined ( $r->header('SOAPAction') );
   
115    
116                  if ( $chunk ) {                  $state = CWMP::Request->parse( $chunk );
                         warn "## request chunk: ",length($chunk)," bytes\n$chunk\n" if $self->debug;  
117    
118                          $state = CWMP::Request->parse( $chunk );                  warn "## acquired state = ", dump( $state ), "\n";
119    
120                          warn "## acquired state = ", dump( $state ), "\n";                  $self->state( $state );
121                    $self->store->update_state( ID => $state->{ID}, $state );
122    
123                          $self->state( $state );          } else {
                         $self->store->update_state( $state->{ID} => $state );  
124    
125                  } else {                  warn "## empty request\n";
                         warn "## empty request\n";  
                 }  
126    
         } else {  
127                  $state = $self->state;                  $state = $self->state;
128                    delete( $state->{_dispatch} );
129                  warn "last request state = ", dump( $state ), "\n" if $self->debug > 1;                  warn "last request state = ", dump( $state ), "\n" if $self->debug > 1;
130          }          }
131    
# Line 207  sub process_request { Line 168  sub process_request {
168    
169    $xml = $self->dispatch('Inform', $response_arguments );    $xml = $self->dispatch('Inform', $response_arguments );
170    
171    If debugging level of 3 or more, it will create dumps of responses named C<< dump/nr.response >>
172    
173  =cut  =cut
174    
175  sub dispatch {  sub dispatch {
# Line 220  sub dispatch { Line 183  sub dispatch {
183                  warn ">>> dispatching to $dispatch\n";                  warn ">>> dispatching to $dispatch\n";
184                  my $xml = $response->$dispatch( $self->state, @_ );                  my $xml = $response->$dispatch( $self->state, @_ );
185                  warn "## response payload: ",length($xml)," bytes\n$xml\n" if $self->debug;                  warn "## response payload: ",length($xml)," bytes\n$xml\n" if $self->debug;
186                    if ( $self->debug > 2 ) {
187                            my $file = sprintf("dump/%04d.response", $dump_nr++);
188                            write_file( $file, $xml );
189                            warn "### response dump: $file\n";
190                    }
191                  return $xml;                  return $xml;
192          } else {          } else {
193                  confess "can't dispatch to $dispatch";                  confess "can't dispatch to $dispatch";
194          }          }
195  };  };
196    
 =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;  
         }  
 }  
197    
198  =head2 error  =head2 error
199    

Legend:
Removed from v.83  
changed lines
  Added in v.119

  ViewVC Help
Powered by ViewVC 1.1.26