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

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

google/lib/CWMP/Server.pm revision 43 by dpavlin, Tue Jun 19 18:50:28 2007 UTC google/lib/CWMP/Session.pm revision 84 by dpavlin, Fri Jun 22 18:25:24 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_path
11    
12  sock  sock
13    state
14    queue
15    store
16  / );  / );
17    
18  use IO::Socket::INET;  use IO::Socket::INET;
19  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
20    use Carp qw/confess cluck croak/;
21    
22  use CWMP::Request;  use CWMP::Request;
23  use CWMP::Response;  use CWMP::Response;
24  use Carp qw/confess cluck/;  use CWMP::Store;
25    
26  =head1 NAME  =head1 NAME
27    
28  CWMP::Server - implement logic of CWMP protocol  CWMP::Session - implement logic of CWMP protocol
29    
30  =head1 METHODS  =head1 METHODS
31    
32  =head2 new  =head2 new
33    
34    my $server = CWMP::Server->new({ port => 3333 });    my $server = CWMP::Session->new({
35            sock => $io_socket_object,
36            store_path => 'state.db',
37            queue => [ qw/GetRPCMethods GetParameterNames/ ],
38            debug => 1,
39      });
40    
 =head2 run  
   
   $server->run();  
41    
42  =cut  =cut
43    
44  sub run {  sub new {
45          my $self = shift;          my $class = shift;
46            my $self = $class->SUPER::new( @_ );
47    
48          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 "waiting for request on port ", $self->port, $/;  
   
         while ( my $sock = $listen->accept ) {  
                 $sock->autoflush(1);  
   
                 warn "connection from ", $sock->peerhost, "\n";  
   
                 $self->sock( $sock );   # FIXME this will not work for multiple clients  
                 while ( $self->process_request ) {  
                         warn "...another one bites a dust...\n";  
                 }  
49    
50                  warn "...returning to accepting new connections\n";          $self->debug( 0 ) unless $self->debug;
51          }  
52            warn "created ", __PACKAGE__, "(", dump( @_ ), ") for ", $self->sock->peerhost, "\n" if $self->debug;
53    
54            $self->store( CWMP::Store->new({
55                    debug => $self->debug,
56                    path => $self->store_path,
57            }) );
58    
59            croak "can't open ", $self->store_path, ": $!" unless $self->store;
60    
61            return $self;
62  }  }
63    
64    =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  sub process_request {  sub process_request {
73          my $self = shift;          my $self = shift;
74    
75          my $sock = $self->sock || die "no sock?";          my $sock = $self->sock || die "no sock?";
76    
77          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' );
78    
79          if ( ! $sock->connected ) {          if ( ! $sock->connected ) {
80                  warn "SOCKET NOT CONNECTED";                  warn "SOCKET NOT CONNECTED\n";
81                  return 0;                  return 0;
82          }          }
83    
# Line 77  sub process_request { Line 85  sub process_request {
85          $sock->blocking( 1 );          $sock->blocking( 1 );
86    
87          ### read the first line of response          ### read the first line of response
88          my $line = $sock->getline || $self->error(400, "No Data");          my $line = $sock->getline;
89            return $self->error(400, "No Data") unless ( defined $line );
90    
91          $line =~ s/[\r\n]+$//;          $line =~ s/[\r\n]+$//;
92          if ($line !~ /^ (\w+) \ + (\S+) \ + (HTTP\/1.\d) $ /x) {          if ($line !~ /^ (\w+) \ + (\S+) \ + (HTTP\/1.\d) $ /x) {
93                    warn "ERROR: $line\n";
94                  return $self->error(400, "Bad request");                  return $self->error(400, "Bad request");
95          }          }
96          my ($method, $req, $protocol) = ($1, $2, $3);          my ($method, $req, $protocol) = ($1, $2, $3);
97          warn "<<<< ",join(" ", time, $method, $req)."\n";          warn "<<<< ", $sock->peerhost, " - - [" . localtime() . "] \"$method $req $protocol\"\n";
98    
99          ### read in other headers          ### read in other headers
100          $self->read_headers || return $self->error(400, "Strange headers");          $self->read_headers || return $self->error(400, "Strange headers");
# Line 104  sub process_request { Line 114  sub process_request {
114    
115                  do {                  do {
116    
117                          warn "get chunk len\n" if $self->debug;                          warn "get chunk len\n" if $self->debug > 1;
118                                                    
119                          my $hex;                          my $hex;
120                          do {                          do {
# Line 115  sub process_request { Line 125  sub process_request {
125                          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);
126                          $len = hex( $hex );                          $len = hex( $hex );
127    
128                          warn "getting chunk of $len bytes\n" if $self->debug;                          warn "getting chunk of $len bytes\n" if $self->debug > 1;
129    
130                          $sock->read( my $buff, $len );                          $sock->read( my $buff, $len );
131                          $chunk .= $buff;                          $chunk .= $buff;
132    
133                          warn "--- $len bytes: --=>||$buff||<=--\n";                          warn "--- $len bytes: --=>||$buff||<=--\n" if $self->debug > 1;
134    
135                  } while ( $len > 0 );                  } while ( $len > 0 );
136                    my $sep = $sock->getline;
137                    die "expected separator, not ", dump( $sep ) if ( $sep !~ m/^[\n\r]+$/ );
138    
139          } else {          } else {
140                  die "right now, we support only Transfer-Encoding: chunked";                  die "right now, we support only Transfer-Encoding: chunked";
141          }          }
142    
143          warn "handler got ", length($chunk), " bytes\n" if $self->debug;          my $size = length( $chunk );
144    
145          warn "<<< " . localtime() . " " . $sock->peerhost . "\n";          warn "<<<< " . $sock->peerhost . " [" . localtime() . "] request $size bytes\n";
   
         die "not SOAP request" unless defined ( $self->header('SOAPAction') );  
146    
147          my $state;          my $state;
148    
149          if ( $chunk ) {          if ( $size > 0 ) {
                 warn "## request chunk: ",length($chunk)," bytes\n$chunk\n" if $self->debug;  
150    
151                  $state = CWMP::Request->parse( $chunk );                  die "no SOAPAction header in ",dump($chunk) unless defined ( $self->header('SOAPAction') );
152    
153    
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                            warn "## acquired state = ", dump( $state ), "\n";
160    
161                            $self->state( $state );
162                            $self->store->update_state( $state->{ID} => $state );
163    
164                    } else {
165                            warn "## empty request\n";
166                    }
167    
                 warn "acquired state = ", dump( $state ), "\n";  
           
168          } else {          } else {
169                  warn "empty request\n";                  $state = $self->state;
170                    warn "last request state = ", dump( $state ), "\n" if $self->debug > 1;
171          }          }
172    
173    
174          my $response = CWMP::Response->new({ debug => $self->debug });          $sock->send(join("\r\n",
175                    'HTTP/1.1 200 OK',
176          $sock->send(join("",                  'Content-Type: text/xml; charset="utf-8"',
177                  $self->status(200,'OK'),                  'Server: AcmeCWMP/42',
178                  $self->content_type('text/xml; charset="utf-8"'),                  'SOAPServer: AcmeCWMP/42'
179                  "Server: AcmeCWMP/42\r\n",          )."\r\n");
                 "SOAPServer: AcmeCWMP/42\r\n"  
         ));  
180    
181          $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} );
182                    
183          my $xml = '';          my $xml = '';
184    
185          if ( my $dispatch = $state->{_dispatch} ) {          if ( my $dispatch = $state->{_dispatch} ) {
186                  if ( $response->can( $dispatch ) ) {                  $xml = $self->dispatch( $dispatch );
187                          warn ">>> dispatching to $dispatch\n";          } elsif ( $dispatch = shift @{ $self->queue } ) {
188                          $xml = $response->$dispatch( $state ) . "\r\n";                  $xml = $self->dispatch( $dispatch );
189                          warn "## response payload: ",length($xml)," bytes\n$xml\n";          } elsif ( $size == 0 ) {
190                  } else {                  warn ">>> closing connection\n";
191                          confess "can't dispatch to $dispatch";                  return 0;
                 }  
192          } else {          } else {
193                  warn ">>> empty response\n";                  warn ">>> empty response\n";
194                    $state->{NoMoreRequests} = 1;
195                    $xml = $self->dispatch( 'xml', sub {} );
196          }          }
197    
198          $sock->send( "Content-Length: " . length( $xml ) . "\r\n\r\n" );          $sock->send( "Content-Length: " . length( $xml ) . "\r\n\r\n" );
199          $sock->send( "$xml\r\n\r\n" ) or die "can't send response";          $sock->send( $xml ) or die "can't send response";
200    
201            warn ">>>> " . $sock->peerhost . " [" . localtime() . "] sent ", length( $xml )," bytes\n";
202    
203            warn "### request over\n" if $self->debug;
204    
205          warn "### request over";          return 1;       # next request
206    };
207    
208    =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                    my $xml = $response->$dispatch( $self->state, @_ );
224                    warn "## response payload: ",length($xml)," bytes\n$xml\n" if $self->debug;
225                    return $xml;
226            } else {
227                    confess "can't dispatch to $dispatch";
228            }
229  };  };
230    
231    =head2 read_headers
232    
233    parse headers from request
234    
235    =cut
236    
237  sub read_headers {  sub read_headers {
238    my $self = shift;    my $self = shift;
# Line 189  sub read_headers { Line 242  sub read_headers {
242    while (defined($_ = $self->sock->getline)) {    while (defined($_ = $self->sock->getline)) {
243      s/[\r\n]+$//;      s/[\r\n]+$//;
244      last unless length $_;      last unless length $_;
245          warn "-- $_\n";          warn "-- $_\n" if $self->debug;
246      return 0 if ! /^ ([\w\-]+) :[\ \t]* (.*) $/x;      return 0 if ! /^ ([\w\-]+) :[\ \t]* (.*) $/x;
247      $self->{headers}->{$1} = $2;      $self->{headers}->{$1} = $2;
248    }    }
# Line 197  sub read_headers { Line 250  sub read_headers {
250    return 1;    return 1;
251  }  }
252    
253    =head2 header
254    
255    Getter for specific header
256    
257      $self->header('Cookies');
258    
259    =cut
260    
261  sub header {  sub header {
262          my $self = shift;          my $self = shift;
263          my $header = shift || die "no header?";          my $header = shift || die "no header?";
# Line 207  sub header { Line 268  sub header {
268          }          }
269  }  }
270    
271  sub content_type {  =head2 error
   my ($self, $type) = @_;  
   $self->http_header;  
   return "Content-type: $type\r\n";  
 }  
272    
273  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";  
 }  
274    
275  sub status {  =cut
   my ($self, $number, $msg) = @_;  
   $msg = '' if ! defined $msg;  
   return if $self->http_header($number);  
   return "Status $number: $msg\r\n";  
 }  
276    
277  sub http_header {  sub error {
278    my $self = shift;    my ($self, $number, $msg) = @_;
279    my $number = shift || 200;    $msg ||= 'ERROR';
280    return if ! delete $self->{needs_header};    $self->sock->send( "HTTP/1.1 $number $msg\r\n" );
281    $self->sock->send("HTTP/1.1 $number\r\n");    warn "Error - $number - $msg\n";
282    return 1;    return 0;     # close connection
283  }  }
284    
285  1;  1;

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

  ViewVC Help
Powered by ViewVC 1.1.26