--- google/lib/CWMP/Session.pm 2007/06/22 15:54:43 83 +++ google/trunk/lib/CWMP/Session.pm 2007/11/14 23:07:42 206 @@ -7,20 +7,21 @@ use base qw/Class::Accessor/; __PACKAGE__->mk_accessors( qw/ debug -store_path +create_dump +session sock state -queue store / ); -use IO::Socket::INET; +use HTTP::Daemon; use Data::Dump qw/dump/; use Carp qw/confess cluck croak/; +use File::Slurp; use CWMP::Request; -use CWMP::Response; +use CWMP::Methods; use CWMP::Store; =head1 NAME @@ -32,13 +33,12 @@ =head2 new my $server = CWMP::Session->new({ - sock => $io_socket_object, - store_path => 'state.db', - queue => [ qw/GetRPCMethods GetParameterNames/ ], + sock => $io_socket_object, + store => { ... }, debug => 1, + create_dump => 1, }); - =cut sub new { @@ -46,15 +46,24 @@ my $self = $class->SUPER::new( @_ ); confess "need sock" unless $self->sock; + confess "need store" unless $self->store; + my $peerhost = $self->sock->peerhost || confess "can't get sock->peerhost"; + + $self->debug( 0 ) unless $self->debug; - warn "created ", __PACKAGE__, "(", dump( @_ ), ") for ", $self->sock->peerhost, "\n" if $self->debug; + warn "created ", __PACKAGE__, "(", dump( @_ ), ") for $peerhost\n" if $self->debug; - $self->store( CWMP::Store->new({ + my $store_obj = CWMP::Store->new({ debug => $self->debug, - path => $self->store_path, - }) ); + %{ $self->store }, + }); + + croak "can't open ", dump( $self->store ), ": $!" unless $store_obj; - croak "can't open ", $self->store_path, ": $!" unless $self->store; + # FIXME looks ugly. Should we have separate accessor for this? + $self->store( $store_obj ); + + $self->create_dump( 1 ) if $self->debug > 2; return $self; } @@ -65,107 +74,80 @@ facilitate brain-dead concept of adding state to stateless protocol like HTTP. +If used with debugging level of 3 or more, it will also create dumps of +requests named C<< dump/nr.request >> where C is number from 0 to total number +of requests in single session. + =cut +my $dump_nr = 0; + sub process_request { my $self = shift; my $sock = $self->sock || die "no sock?"; - 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' ); if ( ! $sock->connected ) { warn "SOCKET NOT CONNECTED\n"; return 0; } - $sock->autoflush( 1 ); - $sock->blocking( 1 ); - - ### read the first line of response - 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 "<<<< ", $sock->peerhost, " - - [" . localtime() . "] \"$method $req $protocol\"\n"; - - ### read in other headers - $self->read_headers || return $self->error(400, "Strange headers"); - - ### do we support the type -# if ($method !~ /GET|POST|HEAD/) { - if ($method !~ /POST/) { - return $self->error(400, "Unsupported Method"); - } - - my $chunk; - my $transfer_encoding = $self->header('Transfer-Encoding'); + bless $sock, 'HTTP::Daemon::ClientConn'; - if ( $transfer_encoding && $transfer_encoding =~ qr/^chunked/i ) { + # why do I have to do this? + # solution from http://use.perl.org/~Matts/journal/12896 + ${*$sock}{'httpd_daemon'} = HTTP::Daemon->new; - my $len = 0; + my $r = $sock->get_request || confess "can't get_request"; - do { + my $xml = $r->content; - warn "get chunk len\n" if $self->debug > 1; - - my $hex; - do { - $hex = $sock->getline; - $hex =~ s/[\n\r]+$//; - } until ( $hex ne '' ); + my $size = length( $xml ); - die "chunk size not valid hex: $hex" unless ( $hex =~ m/^[0-9a-f]+$/i); - $len = hex( $hex ); + warn "<<<< ", $sock->peerhost, " [" . localtime() . "] ", $r->method, " ", $r->uri, " $size bytes\n"; - warn "getting chunk of $len bytes\n" if $self->debug > 1; + $dump_nr++; + my $file = sprintf("dump/%04d-%s.request", $dump_nr, $sock->peerhost); - $sock->read( my $buff, $len ); - $chunk .= $buff; - - warn "--- $len bytes: --=>||$buff||<=--\n" if $self->debug > 1; - - } 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"; + if ( $self->create_dump ) { + write_file( $file, $r->as_string ); + warn "### request dumped to file: $file\n"; } - my $size = length( $chunk ); - - warn "<<<< " . $sock->peerhost . " [" . localtime() . "] request $size bytes\n"; - my $state; if ( $size > 0 ) { - die "no SOAPAction header in ",dump($chunk) unless defined ( $self->header('SOAPAction') ); + die "no SOAPAction header in ",dump($xml) unless defined ( $r->header('SOAPAction') ); + warn "## request payload: ",length($xml)," bytes\n$xml\n" if $self->debug; - if ( $chunk ) { - warn "## request chunk: ",length($chunk)," bytes\n$chunk\n" if $self->debug; + $state = CWMP::Request->parse( $xml ); - $state = CWMP::Request->parse( $chunk ); - - warn "## acquired state = ", dump( $state ), "\n"; + if ( defined( $state->{_dispatch} ) && $self->create_dump ) { + my $type = sprintf("dump/%04d-%s-%s", $dump_nr, $sock->peerhost, $state->{_dispatch}); + symlink $file, $type || warn "can't symlink $file -> $type: $!"; + } - $self->state( $state ); - $self->store->update_state( $state->{ID} => $state ); + warn "## acquired state = ", dump( $state ), "\n"; - } else { - warn "## empty request\n"; + if ( ! defined( $state->{DeviceID} ) ) { + warn "## state with DeviceID, using old one...\n"; + $state->{DeviceID} = $self->state->{DeviceID}; } + $self->state( $state ); + $self->store->update_state( ID => $state->{ID}, $state ); + } else { + + warn "## empty request, using last request state\n"; + $state = $self->state; - warn "last request state = ", dump( $state ), "\n" if $self->debug > 1; + delete( $state->{_dispatch} ); + #warn "last request state = ", dump( $state ), "\n" if $self->debug > 1; } @@ -177,18 +159,25 @@ )."\r\n"); $sock->send( "Set-Cookie: ID=" . $state->{ID} . "; path=/\r\n" ) if ( $state->{ID} ); - - my $xml = ''; + + my $uid = $self->store->ID_to_uid( $state->{ID}, $state ); + + my $queue = CWMP::Queue->new({ + id => $uid, + debug => $self->debug, + }); + my $job; + $xml = ''; if ( my $dispatch = $state->{_dispatch} ) { $xml = $self->dispatch( $dispatch ); - } elsif ( $dispatch = shift @{ $self->queue } ) { - $xml = $self->dispatch( $dispatch ); + } elsif ( $job = $queue->dequeue ) { + $xml = $self->dispatch( $job->dispatch ); } elsif ( $size == 0 ) { - warn ">>> closing connection\n"; + warn ">>> no more queued commands, closing connection to $uid\n"; return 0; } else { - warn ">>> empty response\n"; + warn ">>> empty response to $uid\n"; $state->{NoMoreRequests} = 1; $xml = $self->dispatch( 'xml', sub {} ); } @@ -196,9 +185,10 @@ $sock->send( "Content-Length: " . length( $xml ) . "\r\n\r\n" ); $sock->send( $xml ) or die "can't send response"; - warn ">>>> " . $sock->peerhost . " [" . localtime() . "] sent ", length( $xml )," bytes\n"; + warn ">>>> " . $sock->peerhost . " [" . localtime() . "] sent ", length( $xml )," bytes to $uid\n"; - warn "### request over\n" if $self->debug; + $job->finish if $job; + warn "### request over for $uid\n" if $self->debug; return 1; # next request }; @@ -207,64 +197,33 @@ $xml = $self->dispatch('Inform', $response_arguments ); +If debugging level of 3 or more, it will create dumps of responses named C<< dump/nr.response >> + =cut sub dispatch { my $self = shift; my $dispatch = shift || die "no dispatch?"; + my $args = shift; - my $response = CWMP::Response->new({ debug => $self->debug }); + my $response = CWMP::Methods->new({ debug => $self->debug }); if ( $response->can( $dispatch ) ) { - warn ">>> dispatching to $dispatch\n"; - my $xml = $response->$dispatch( $self->state, @_ ); + warn ">>> dispatching to $dispatch with args ",dump( $args ),"\n"; + my $xml = $response->$dispatch( $self->state, $args ); warn "## response payload: ",length($xml)," bytes\n$xml\n" if $self->debug; + if ( $self->create_dump ) { + my $file = sprintf("dump/%04d-%s.response", $dump_nr++, $self->sock->peerhost); + write_file( $file, $xml ); + warn "### response dump: $file\n"; + } return $xml; } else { confess "can't dispatch to $dispatch"; } }; -=head2 read_headers - -parse headers from request - -=cut - -sub read_headers { - my $self = shift; - - $self->{headers} = {}; - - while (defined($_ = $self->sock->getline)) { - s/[\r\n]+$//; - last unless length $_; - warn "-- $_\n" if $self->debug; - return 0 if ! /^ ([\w\-]+) :[\ \t]* (.*) $/x; - $self->{headers}->{$1} = $2; - } - - return 1; -} - -=head2 header - -Getter for specific header - - $self->header('Cookies'); - -=cut - -sub header { - my $self = shift; - my $header = shift || die "no header?"; - if ( defined( $self->{headers}->{$header} )) { - return $self->{headers}->{$header}; - } else { - return; - } -} =head2 error