/[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 42 by dpavlin, Tue Jun 19 18:37:24 2007 UTC revision 50 by dpavlin, Tue Jun 19 21:29:04 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 61  sub run { Line 63  sub run {
63          }          }
64  }  }
65    
66    =head2 process_request
67    
68    One request from client/response from server cycle. Call multiple times to
69    facilitate brain-dead concept of adding state to stateless protocol like
70    HTTP.
71    
72    =cut
73    
74  sub process_request {  sub process_request {
75          my $self = shift;          my $self = shift;
76    
# Line 77  sub process_request { Line 87  sub process_request {
87          $sock->blocking( 1 );          $sock->blocking( 1 );
88    
89          ### read the first line of response          ### read the first line of response
90          my $line = $sock->getline || $self->error(400, "No Data");          my $line = $sock->getline;
91            return $self->error(400, "No Data") unless ( defined $line );
92    
93          $line =~ s/[\r\n]+$//;          $line =~ s/[\r\n]+$//;
94          if ($line !~ /^ (\w+) \ + (\S+) \ + (HTTP\/1.\d) $ /x) {          if ($line !~ /^ (\w+) \ + (\S+) \ + (HTTP\/1.\d) $ /x) {
95                    warn "ERROR: $line\n";
96                  return $self->error(400, "Bad request");                  return $self->error(400, "Bad request");
97          }          }
98          my ($method, $req, $protocol) = ($1, $2, $3);          my ($method, $req, $protocol) = ($1, $2, $3);
99          warn "<<<< ",join(" ", time, $method, $req)."\n";          warn "<<<< ", $sock->peerhost, " - - [" . localtime() . "] \"$method $req $protocol\"\n";
100    
101          ### read in other headers          ### read in other headers
102          $self->read_headers || return $self->error(400, "Strange headers");          $self->read_headers || return $self->error(400, "Strange headers");
# Line 123  sub process_request { Line 135  sub process_request {
135                          warn "--- $len bytes: --=>||$buff||<=--\n";                          warn "--- $len bytes: --=>||$buff||<=--\n";
136    
137                  } while ( $len > 0 );                  } while ( $len > 0 );
138                    my $sep = $sock->getline;
139                    die "expected separator, not ", dump( $sep ) if ( $sep !~ m/^[\n\r]+$/ );
140    
141          } else {          } else {
142                  die "right now, we support only Transfer-Encoding: chunked";                  die "right now, we support only Transfer-Encoding: chunked";
143          }          }
144    
145          warn "handler got ", length($chunk), " bytes\n" if $self->debug;          my $size = length( $chunk );
146    
147          warn "<<< " . localtime() . " " . $sock->peerhost . "\n";          warn "<<< " . $sock->peerhost . " [" . localtime() . "] request $size bytes\n";
   
         die "not SOAP request" unless defined ( $self->header('SOAPAction') );  
148    
149          my $state;          my $state;
150    
151          if ( $chunk ) {          if ( $size > 0 ) {
                 warn "## request chunk: ",length($chunk)," bytes\n$chunk\n" if $self->debug;  
152    
153                  $state = CWMP::Request->parse( $chunk );                  die "no SOAPAction header in ",dump($chunk) unless defined ( $self->header('SOAPAction') );
154    
155    
156                    if ( $chunk ) {
157                            warn "## request chunk: ",length($chunk)," bytes\n$chunk\n" if $self->debug;
158    
159                            $state = CWMP::Request->parse( $chunk );
160    
161                            warn "acquired state = ", dump( $state ), "\n";
162    
163                            $self->state( $state );
164    
165                    } else {
166                            warn "empty request\n";
167                    }
168    
                 warn "acquired state = ", dump( $state ), "\n";  
           
169          } else {          } else {
170                  warn "empty request\n";                  $state = $self->state;
171                    warn "last request state = ", dump( $state ), "\n";
172          }          }
173    
174    
175          my $response = CWMP::Response->new({ debug => $self->debug });          $sock->send(join("\r\n",
176                    'HTTP/1.1 200 OK',
177          $sock->send(                  'Content-Type: text/xml; charset="utf-8"',
178                  $self->status(200),                  'Server: AcmeCWMP/42',
179                  $self->content_type('text/xml; charset="utf-8"'),                  'SOAPServer: AcmeCWMP/42'
180                  "Server: AcmeCWMP/42\r\n",          ));
                 "SOAPServer: AcmeCWMP/42\r\n"  
         );  
181    
182          $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} );
183                    
184          my $xml = '';          my $xml = '';
185    
186          if ( my $dispatch = $state->{_dispatch} ) {          if ( my $dispatch = $state->{_dispatch} ) {
187                  if ( $response->can( $dispatch ) ) {                  $xml = $self->dispatch( $dispatch );
188                          warn ">>> dispatching to $dispatch\n";          } elsif ( $dispatch = shift @{ $self->queue } ) {
189                          $xml = $response->$dispatch( $state ) . "\r\n";                  $xml = $self->dispatch( $dispatch );
190                          warn "## response payload: ",length($xml)," bytes\n$xml\n";          } elsif ( $size == 0 ) {
191                  } else {                  warn ">>> closing connection\n";
192                          confess "can't dispatch to $dispatch";                  return 0;
                 }  
193          } else {          } else {
194                  warn ">>> empty response\n";                  warn ">>> empty response\n";
195                    $state->{NoMoreRequests} = 1;
196                    $xml = $self->dispatch( 'xml', sub {} );
197          }          }
198    
199          $sock->send( "Content-length: ", length( $xml ), "\r\n\r\n" );          $sock->send( "Content-Length: " . length( $xml ) . "\r\n\r\n" );
200          $sock->send( "$xml\r\n" ) or die "can't send response";          $sock->send( $xml ) or die "can't send response";
201    
202          warn "### request over";          warn "### request over";
203    
204  };  };
205    
206    =head2 dispatch
207    
208      $xml = $self->dispatch('Inform', $response_arguments );
209    
210    =cut
211    
212    sub dispatch {
213            my $self = shift;
214    
215            my $dispatch = shift || die "no dispatch?";
216    
217            my $response = CWMP::Response->new({ debug => $self->debug });
218    
219            if ( $response->can( $dispatch ) ) {
220                    warn ">>> dispatching to $dispatch\n";
221                    my $xml = $response->$dispatch( $self->state, @_ ) . "\r\n";
222                    warn "## response payload: ",length($xml)," bytes\n$xml\n";
223                    return $xml;
224            } else {
225                    confess "can't dispatch to $dispatch";
226            }
227    };
228    
229    =head2 read_headers
230    
231    parse headers from request
232    
233    =cut
234    
235  sub read_headers {  sub read_headers {
236    my $self = shift;    my $self = shift;
# Line 197  sub read_headers { Line 248  sub read_headers {
248    return 1;    return 1;
249  }  }
250    
251    =head2 header
252    
253    Getter for specific header
254    
255      $self->header('Cookies');
256    
257    =cut
258    
259  sub header {  sub header {
260          my $self = shift;          my $self = shift;
261          my $header = shift || die "no header?";          my $header = shift || die "no header?";
# Line 207  sub header { Line 266  sub header {
266          }          }
267  }  }
268    
269  sub content_type {  =head2 error
   my ($self, $type) = @_;  
   $self->http_header;  
   return "Content-type: $type\r\n";  
 }  
270    
271  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";  
 }  
272    
273  sub status {  =cut
   my ($self, $number, $msg) = @_;  
   $msg = '' if ! defined $msg;  
   return if $self->http_header($number);  
   return "Status $number: $msg\r\n";  
 }  
274    
275  sub http_header {  sub error {
276    my $self = shift;    my ($self, $number, $msg) = @_;
277    my $number = shift || 200;    $msg ||= 'ERROR';
278    return if ! delete $self->{needs_header};    $self->sock->send( "HTTP/1.1 $number $msg\r\n" );
279    $self->sock->send("HTTP/1.0 $number\r\n");    warn "Error - $number - $msg\n";
280    return 1;    return 0;     # close connection
281  }  }
282    
283  1;  1;

Legend:
Removed from v.42  
changed lines
  Added in v.50

  ViewVC Help
Powered by ViewVC 1.1.26