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

revision 40 by dpavlin, Tue Jun 19 17:29:07 2007 UTC revision 72 by dpavlin, Wed Jun 20 23:48:45 2007 UTC
# Line 8  use base qw/Class::Accessor/; Line 8  use base qw/Class::Accessor/;
8  __PACKAGE__->mk_accessors( qw/  __PACKAGE__->mk_accessors( qw/
9  debug  debug
10  port  port
11    sock
12    state
13    queue
14  / );  / );
15    
16  use IO::Socket::INET;  use IO::Socket::INET;
# Line 44  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            warn "## debug level: ", $self->debug, "\n" if $self->debug;
55    
56          while ( my $sock = $listen->accept ) {          while ( my $sock = $listen->accept ) {
57                  $sock->autoflush(1);                  $sock->autoflush(1);
58    
59                  warn "connection from ", $sock->peerhost, "\n";                  warn "connection from ", $sock->peerhost, "\n" if $self->debug;
60    
61                  $self->process_request( $sock );                  $self->sock( $sock );   # FIXME this will not work for multiple clients
62                    while ( $self->process_request ) {
63                            warn "...another one bites the dust...\n";
64                    }
65    
66                  warn "...another one bites a dust...\n";                  warn "...returning to accepting new connections\n";
                 sleep 1;  
67          }          }
68  }  }
69    
70    =head2 process_request
71    
72    One request from client/response from server cycle. Call multiple times to
73    facilitate brain-dead concept of adding state to stateless protocol like
74    HTTP.
75    
76    =cut
77    
78  sub process_request {  sub process_request {
79          my $self = shift;          my $self = shift;
80    
81          my $sock = shift || 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 'IO::Socket::INET' );
84    
85            if ( ! $sock->connected ) {
86                    warn "SOCKET NOT CONNECTED\n";
87                    return 0;
88            }
89    
90          $sock->autoflush( 1 );          $sock->autoflush( 1 );
91          $sock->blocking( 1 );          $sock->blocking( 1 );
92    
93          ### read the first line of response          ### read the first line of response
94          my $line = $sock->getline; # || $self->error(400, "No Data");          my $line = $sock->getline;
95            return $self->error(400, "No Data") unless ( defined $line );
96    
97          $line =~ s/[\r\n]+$//;          $line =~ s/[\r\n]+$//;
98          if ($line !~ /^ (\w+) \ + (\S+) \ + (HTTP\/1.\d) $ /x) {          if ($line !~ /^ (\w+) \ + (\S+) \ + (HTTP\/1.\d) $ /x) {
99                    warn "ERROR: $line\n";
100                  return $self->error(400, "Bad request");                  return $self->error(400, "Bad request");
101          }          }
102          my ($method, $req, $protocol) = ($1, $2, $3);          my ($method, $req, $protocol) = ($1, $2, $3);
103          warn "<<<< ",join(" ", time, $method, $req)."\n";          warn "<<<< ", $sock->peerhost, " - - [" . localtime() . "] \"$method $req $protocol\"\n";
104    
105          ### read in other headers          ### read in other headers
106          $self->read_headers($sock) || return $self->error(400, "Strange headers");          $self->read_headers || return $self->error(400, "Strange headers");
107    
108          ### do we support the type          ### do we support the type
109  #       if ($method !~ /GET|POST|HEAD/) {  #       if ($method !~ /GET|POST|HEAD/) {
# Line 96  sub process_request { Line 120  sub process_request {
120    
121                  do {                  do {
122    
123                          warn "get chunk len\n" if $self->debug;                          warn "get chunk len\n" if $self->debug > 1;
124                                                    
125                          my $hex;                          my $hex;
126                          do {                          do {
# Line 107  sub process_request { Line 131  sub process_request {
131                          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);
132                          $len = hex( $hex );                          $len = hex( $hex );
133    
134                          warn "getting chunk of $len bytes\n" if $self->debug;                          warn "getting chunk of $len bytes\n" if $self->debug > 1;
135    
136                          $sock->read( my $buff, $len );                          $sock->read( my $buff, $len );
137                          $chunk .= $buff;                          $chunk .= $buff;
138    
139                          warn "--- $len bytes: --=>||$buff||<=--\n";                          warn "--- $len bytes: --=>||$buff||<=--\n" if $self->debug > 1;
140    
141                  } while ( $len > 0 );                  } while ( $len > 0 );
142                    my $sep = $sock->getline;
143                    die "expected separator, not ", dump( $sep ) if ( $sep !~ m/^[\n\r]+$/ );
144    
145          } else {          } else {
146                  die "right now, we support only Transfer-Encoding: chunked";                  die "right now, we support only Transfer-Encoding: chunked";
147          }          }
148    
149          warn "handler got ", length($chunk), " bytes\n" if $self->debug;          my $size = length( $chunk );
150    
151          warn "<<< " . localtime() . " " . $sock->peerhost . "\n";          warn "<<<< " . $sock->peerhost . " [" . localtime() . "] request $size bytes\n";
   
         die "not SOAP request" unless defined ( $self->header('SOAPAction') );  
152    
153          my $state;          my $state;
154    
155          if ( $chunk ) {          if ( $size > 0 ) {
                 warn "## request chunk: ",length($chunk)," bytes\n$chunk\n" if $self->debug;  
156    
157                  $state = CWMP::Request->parse( $chunk );                  die "no SOAPAction header in ",dump($chunk) unless defined ( $self->header('SOAPAction') );
158    
                 warn "acquired state = ", dump( $state ), "\n";  
           
         } else {  
                 warn "empty request\n";  
         }  
159    
160                    if ( $chunk ) {
161                            warn "## request chunk: ",length($chunk)," bytes\n$chunk\n" if $self->debug;
162    
163          my $response = CWMP::Response->new({ debug => $self->debug });                          $state = CWMP::Request->parse( $chunk );
164    
165          print $self->status(200), $self->content_type('text/xml; charset="utf-8"'), "\r\n";                          warn "## acquired state = ", dump( $state ), "\n";
166    
167          print "Server: AcmeCWMP/42\r\nSOAPServer: AcmeCWMP/42\r\n";                          $self->state( $state );
168    
169          print "Set-Cookie: ID=" , $state->{ID}, "; path=/\r\n" if ( $state->{ID} );                  } else {
170                            warn "## empty request\n";
171                    }
172    
173            } else {
174                    $state = $self->state;
175                    warn "last request state = ", dump( $state ), "\n" if $self->debug > 1;
176            }
177    
178    
179            $sock->send(join("\r\n",
180                    'HTTP/1.1 200 OK',
181                    'Content-Type: text/xml; charset="utf-8"',
182                    'Server: AcmeCWMP/42',
183                    'SOAPServer: AcmeCWMP/42'
184            )."\r\n");
185    
186            $sock->send( "Set-Cookie: ID=" . $state->{ID} . "; path=/\r\n" ) if ( $state->{ID} );
187                    
188          my $xml = '';          my $xml = '';
189    
190          if ( my $dispatch = $state->{_dispatch} ) {          if ( my $dispatch = $state->{_dispatch} ) {
191                  if ( $response->can( $dispatch ) ) {                  $xml = $self->dispatch( $dispatch );
192                          warn ">>> dispatching to $dispatch\n";          } elsif ( $dispatch = shift @{ $self->queue } ) {
193                          $xml = $response->$dispatch( $state ) . "\r\n";                  $xml = $self->dispatch( $dispatch );
194                          warn "## response payload: ",length($xml)," bytes\n$xml\n";          } elsif ( $size == 0 ) {
195                  } else {                  warn ">>> closing connection\n";
196                          confess "can't dispatch to $dispatch";                  return 0;
                 }  
197          } else {          } else {
198                  warn ">>> empty response\n";                  warn ">>> empty response\n";
199                    $state->{NoMoreRequests} = 1;
200                    $xml = $self->dispatch( 'xml', sub {} );
201          }          }
202    
203          print "Content-length: ", length( $xml ), "\r\n\r\n";          $sock->send( "Content-Length: " . length( $xml ) . "\r\n\r\n" );
204          print $xml or die "can't send response";          $sock->send( $xml ) or die "can't send response";
205    
206            warn ">>>> " . $sock->peerhost . " [" . localtime() . "] sent ", length( $xml )," bytes\n";
207    
208          warn "### request over";          warn "### request over\n" if $self->debug;
209    
210            return 1;       # next request
211  };  };
212    
213    =head2 dispatch
214    
215      $xml = $self->dispatch('Inform', $response_arguments );
216    
217    =cut
218    
219    sub dispatch {
220            my $self = shift;
221    
222            my $dispatch = shift || die "no dispatch?";
223    
224            my $response = CWMP::Response->new({ debug => $self->debug });
225    
226            if ( $response->can( $dispatch ) ) {
227                    warn ">>> dispatching to $dispatch\n";
228                    my $xml = $response->$dispatch( $self->state, @_ );
229                    warn "## response payload: ",length($xml)," bytes\n$xml\n" if $self->debug;
230                    return $xml;
231            } else {
232                    confess "can't dispatch to $dispatch";
233            }
234    };
235    
236    =head2 read_headers
237    
238    parse headers from request
239    
240    =cut
241    
242  sub read_headers {  sub read_headers {
243    my $self = shift;    my $self = shift;
244    
         my $sock = shift || die "no sock?";  
   
245    $self->{headers} = {};    $self->{headers} = {};
246    
247    while (defined($_ = $sock->getline)) {    while (defined($_ = $self->sock->getline)) {
248      s/[\r\n]+$//;      s/[\r\n]+$//;
249      last unless length $_;      last unless length $_;
250          warn "-- $_\n";          warn "-- $_\n" if $self->debug;
251      return 0 if ! /^ ([\w\-]+) :[\ \t]* (.*) $/x;      return 0 if ! /^ ([\w\-]+) :[\ \t]* (.*) $/x;
252      $self->{headers}->{$1} = $2;      $self->{headers}->{$1} = $2;
253    }    }
# Line 188  sub read_headers { Line 255  sub read_headers {
255    return 1;    return 1;
256  }  }
257    
258    =head2 header
259    
260    Getter for specific header
261    
262      $self->header('Cookies');
263    
264    =cut
265    
266  sub header {  sub header {
267          my $self = shift;          my $self = shift;
268          my $header = shift || die "no header?";          my $header = shift || die "no header?";
# Line 198  sub header { Line 273  sub header {
273          }          }
274  }  }
275    
276  sub content_type {  =head2 error
   my ($self, $type) = @_;  
   $self->http_header;  
   return "Content-type: $type\r\n";  
 }  
277    
278  sub error{    return $self->error( 501, 'System error' );
   my ($self, $number, $msg) = @_;  
   print $self->status($number, $msg), "\r\n";  
   warn "Error - $number - $msg\n";  
 }  
279    
280  sub status {  =cut
   my ($self, $number, $msg) = @_;  
   $msg = '' if ! defined $msg;  
   return if $self->http_header($number);  
   return "Status $number: $msg\r\n";  
 }  
281    
282  sub http_header {  sub error {
283    my $self = shift;    my ($self, $number, $msg) = @_;
284    my $number = shift || 200;    $msg ||= 'ERROR';
285    return if ! delete $self->{needs_header};    $self->sock->send( "HTTP/1.1 $number $msg\r\n" );
286    print "HTTP/1.0 $number\r\n";    warn "Error - $number - $msg\n";
287    return 1;    return 0;     # close connection
288  }  }
289    
290  1;  1;

Legend:
Removed from v.40  
changed lines
  Added in v.72

  ViewVC Help
Powered by ViewVC 1.1.26