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

revision 43 by dpavlin, Tue Jun 19 18:50:28 2007 UTC revision 53 by dpavlin, Tue Jun 19 22:06:46 2007 UTC
# Line 9  __PACKAGE__->mk_accessors( qw/ Line 9  __PACKAGE__->mk_accessors( qw/
9  debug  debug
10  port  port
11  sock  sock
12    state
13    queue
14  / );  / );
15    
16  use IO::Socket::INET;  use IO::Socket::INET;
# Line 45  sub run { Line 47  sub run {
47                  ReuseAddr => 1,                  ReuseAddr => 1,
48          );          );
49    
50          warn "waiting for request on port ", $self->port, $/;          warn "ACS waiting for request on port ", $self->port,
51                    $self->queue ? " queue ( " . join(",",@{$self->queue}) . " )" : "",
52                    "\n";
53    
54          while ( my $sock = $listen->accept ) {          while ( my $sock = $listen->accept ) {
55                  $sock->autoflush(1);                  $sock->autoflush(1);
56    
57                  warn "connection from ", $sock->peerhost, "\n";                  warn "connection from ", $sock->peerhost, "\n" if $self->debug;
58    
59                  $self->sock( $sock );   # FIXME this will not work for multiple clients                  $self->sock( $sock );   # FIXME this will not work for multiple clients
60                  while ( $self->process_request ) {                  while ( $self->process_request ) {
# Line 61  sub run { Line 65  sub run {
65          }          }
66  }  }
67    
68    =head2 process_request
69    
70    One request from client/response from server cycle. Call multiple times to
71    facilitate brain-dead concept of adding state to stateless protocol like
72    HTTP.
73    
74    =cut
75    
76  sub process_request {  sub process_request {
77          my $self = shift;          my $self = shift;
78    
# Line 69  sub process_request { Line 81  sub process_request {
81          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' );
82    
83          if ( ! $sock->connected ) {          if ( ! $sock->connected ) {
84                  warn "SOCKET NOT CONNECTED";                  warn "SOCKET NOT CONNECTED\n";
85                  return 0;                  return 0;
86          }          }
87    
# Line 77  sub process_request { Line 89  sub process_request {
89          $sock->blocking( 1 );          $sock->blocking( 1 );
90    
91          ### read the first line of response          ### read the first line of response
92          my $line = $sock->getline || $self->error(400, "No Data");          my $line = $sock->getline;
93            return $self->error(400, "No Data") unless ( defined $line );
94    
95          $line =~ s/[\r\n]+$//;          $line =~ s/[\r\n]+$//;
96          if ($line !~ /^ (\w+) \ + (\S+) \ + (HTTP\/1.\d) $ /x) {          if ($line !~ /^ (\w+) \ + (\S+) \ + (HTTP\/1.\d) $ /x) {
97                    warn "ERROR: $line\n";
98                  return $self->error(400, "Bad request");                  return $self->error(400, "Bad request");
99          }          }
100          my ($method, $req, $protocol) = ($1, $2, $3);          my ($method, $req, $protocol) = ($1, $2, $3);
101          warn "<<<< ",join(" ", time, $method, $req)."\n";          warn "<<<< ", $sock->peerhost, " - - [" . localtime() . "] \"$method $req $protocol\"\n";
102    
103          ### read in other headers          ### read in other headers
104          $self->read_headers || return $self->error(400, "Strange headers");          $self->read_headers || return $self->error(400, "Strange headers");
# Line 120  sub process_request { Line 134  sub process_request {
134                          $sock->read( my $buff, $len );                          $sock->read( my $buff, $len );
135                          $chunk .= $buff;                          $chunk .= $buff;
136    
137                          warn "--- $len bytes: --=>||$buff||<=--\n";                          warn "--- $len bytes: --=>||$buff||<=--\n" if $self->debug;
138    
139                  } while ( $len > 0 );                  } while ( $len > 0 );
140                    my $sep = $sock->getline;
141                    die "expected separator, not ", dump( $sep ) if ( $sep !~ m/^[\n\r]+$/ );
142    
143          } else {          } else {
144                  die "right now, we support only Transfer-Encoding: chunked";                  die "right now, we support only Transfer-Encoding: chunked";
145          }          }
146    
147          warn "handler got ", length($chunk), " bytes\n" if $self->debug;          my $size = length( $chunk );
148    
149          warn "<<< " . localtime() . " " . $sock->peerhost . "\n";          warn "<<< " . $sock->peerhost . " [" . localtime() . "] request $size bytes\n";
   
         die "not SOAP request" unless defined ( $self->header('SOAPAction') );  
150    
151          my $state;          my $state;
152    
153          if ( $chunk ) {          if ( $size > 0 ) {
                 warn "## request chunk: ",length($chunk)," bytes\n$chunk\n" if $self->debug;  
154    
155                  $state = CWMP::Request->parse( $chunk );                  die "no SOAPAction header in ",dump($chunk) unless defined ( $self->header('SOAPAction') );
156    
157    
158                    if ( $chunk ) {
159                            warn "## request chunk: ",length($chunk)," bytes\n$chunk\n" if $self->debug;
160    
161                            $state = CWMP::Request->parse( $chunk );
162    
163                            warn "acquired state = ", dump( $state ), "\n";
164    
165                            $self->state( $state );
166    
167                    } else {
168                            warn "empty request\n";
169                    }
170    
                 warn "acquired state = ", dump( $state ), "\n";  
           
171          } else {          } else {
172                  warn "empty request\n";                  $state = $self->state;
173                    warn "last request state = ", dump( $state ), "\n";
174          }          }
175    
176    
177          my $response = CWMP::Response->new({ debug => $self->debug });          $sock->send(join("\r\n",
178                    'HTTP/1.1 200 OK',
179          $sock->send(join("",                  'Content-Type: text/xml; charset="utf-8"',
180                  $self->status(200,'OK'),                  'Server: AcmeCWMP/42',
181                  $self->content_type('text/xml; charset="utf-8"'),                  'SOAPServer: AcmeCWMP/42'
                 "Server: AcmeCWMP/42\r\n",  
                 "SOAPServer: AcmeCWMP/42\r\n"  
182          ));          ));
183    
184          $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} );
# Line 162  sub process_request { Line 186  sub process_request {
186          my $xml = '';          my $xml = '';
187    
188          if ( my $dispatch = $state->{_dispatch} ) {          if ( my $dispatch = $state->{_dispatch} ) {
189                  if ( $response->can( $dispatch ) ) {                  $xml = $self->dispatch( $dispatch );
190                          warn ">>> dispatching to $dispatch\n";          } elsif ( $dispatch = shift @{ $self->queue } ) {
191                          $xml = $response->$dispatch( $state ) . "\r\n";                  $xml = $self->dispatch( $dispatch );
192                          warn "## response payload: ",length($xml)," bytes\n$xml\n";          } elsif ( $size == 0 ) {
193                  } else {                  warn ">>> closing connection\n";
194                          confess "can't dispatch to $dispatch";                  return 0;
                 }  
195          } else {          } else {
196                  warn ">>> empty response\n";                  warn ">>> empty response\n";
197                    $state->{NoMoreRequests} = 1;
198                    $xml = $self->dispatch( 'xml', sub {} );
199          }          }
200    
201          $sock->send( "Content-Length: " . length( $xml ) . "\r\n\r\n" );          $sock->send( "Content-Length: " . length( $xml ) . "\r\n\r\n" );
202          $sock->send( "$xml\r\n\r\n" ) or die "can't send response";          $sock->send( $xml ) or die "can't send response";
203    
204          warn "### request over";          warn "### request over";
205    
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, @_ ) . "\r\n";
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.53

  ViewVC Help
Powered by ViewVC 1.1.26