--- google/lib/CWMP/Server.pm 2007/06/19 20:02:36 48 +++ google/lib/CWMP/Server.pm 2007/06/19 20:46:51 49 @@ -9,6 +9,7 @@ debug port sock +state / ); use IO::Socket::INET; @@ -85,14 +86,16 @@ $sock->blocking( 1 ); ### read the first line of response - my $line = $sock->getline || return $self->error(400, "No Data"); + 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 "<<<< ",join(" ", time, $method, $req)."\n"; + warn "<<<< ", $sock->peerhost, " - - [" . localtime() . "] \"$method $req $protocol\"\n"; ### read in other headers $self->read_headers || return $self->error(400, "Strange headers"); @@ -131,38 +134,49 @@ warn "--- $len bytes: --=>||$buff||<=--\n"; } while ( $len > 0 ); + my $sep = $sock->getline; + die "expected separator, not ", dump( $sep ) if ( $sep !~ m/^[\n\r]+$/ ); } else { die "right now, we support only Transfer-Encoding: chunked"; } - warn "handler got ", length($chunk), " bytes\n" if $self->debug; + my $size = length( $chunk ); - warn "<<< " . localtime() . " " . $sock->peerhost . "\n"; - - die "not SOAP request" unless defined ( $self->header('SOAPAction') ); + warn "<<< " . $sock->peerhost . " [" . localtime() . "] request $size bytes\n"; my $state; - if ( $chunk ) { - warn "## request chunk: ",length($chunk)," bytes\n$chunk\n" if $self->debug; + if ( $size > 0 ) { + + die "no SOAPAction header in ",dump($chunk) unless defined ( $self->header('SOAPAction') ); + + + if ( $chunk ) { + warn "## request chunk: ",length($chunk)," bytes\n$chunk\n" if $self->debug; + + $state = CWMP::Request->parse( $chunk ); - $state = CWMP::Request->parse( $chunk ); + warn "acquired state = ", dump( $state ), "\n"; + + $self->state( $state ); + + } else { + warn "empty request\n"; + } - warn "acquired state = ", dump( $state ), "\n"; - } else { - warn "empty request\n"; + $state = $self->state; + warn "last request state = ", dump( $state ), "\n"; } - my $response = CWMP::Response->new({ debug => $self->debug }); $sock->send(join("\r\n", 'HTTP/1.1 200 OK', 'Content-Type: text/xml; charset="utf-8"', 'Server: AcmeCWMP/42', - 'SOAPServer: AcmeCWMP/42', + 'SOAPServer: AcmeCWMP/42' )); $sock->send( "Set-Cookie: ID=" . $state->{ID} . "; path=/\r\n" ) if ( $state->{ID} );