1 # Dobrica Pavlinusic, <dpavlin@rot13.org> 06/18/07 10:19:50 CEST
7 use base qw/Class::Accessor/;
8 __PACKAGE__->mk_accessors( qw/
19 use Data::Dump qw/dump/;
20 use Carp qw/confess cluck croak/;
29 CWMP::Session - implement logic of CWMP protocol
35 my $server = CWMP::Session->new({
36 sock => $io_socket_object,
40 [ 'GetParameterValyes', 'InternetGatewayDevice.DeviceInfo.SerialNumber', 0 ],
49 my $self = $class->SUPER::new( @_ );
51 confess "need sock" unless $self->sock;
53 $self->debug( 0 ) unless $self->debug;
55 warn "created ", __PACKAGE__, "(", dump( @_ ), ") for ", $self->sock->peerhost, "\n" if $self->debug;
57 my $store_obj = CWMP::Store->new({
58 debug => $self->debug,
62 croak "can't open ", dump( $self->store ), ": $!" unless $store_obj;
64 # FIXME looks ugly. Should we have separate accessor for this?
65 $self->store( $store_obj );
70 =head2 process_request
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
76 If used with debugging level of 3 or more, it will also create dumps of
77 requests named C<< dump/nr.request >> where C<nr> is number from 0 to total number
78 of requests in single session.
87 my $sock = $self->sock || die "no sock?";
89 # die "not IO::Socket::INET but ", ref( $sock ) unless ( ref($sock) eq 'Net::Server::Proto::TCP' );
91 if ( ! $sock->connected ) {
92 warn "SOCKET NOT CONNECTED\n";
96 bless $sock, 'HTTP::Daemon::ClientConn';
98 # why do I have to do this?
99 # solution from http://use.perl.org/~Matts/journal/12896
100 ${*$sock}{'httpd_daemon'} = HTTP::Daemon->new;
102 my $r = $sock->get_request || confess "can't get_request";
104 my $xml = $r->content;
106 my $size = length( $xml );
108 warn "<<<< ", $sock->peerhost, " [" . localtime() . "] ", $r->method, " ", $r->uri, " $size bytes\n";
110 if ( $self->debug > 2 ) {
111 my $file = sprintf("dump/%04d-%s.request", $dump_nr++, $sock->peerhost);
112 write_file( $file, $r->as_string );
113 warn "### request dumped to file: $file\n";
120 die "no SOAPAction header in ",dump($xml) unless defined ( $r->header('SOAPAction') );
122 warn "## request payload: ",length($xml)," bytes\n$xml\n" if $self->debug;
124 $state = CWMP::Request->parse( $xml );
126 warn "## acquired state = ", dump( $state ), "\n";
128 $self->state( $state );
129 $self->store->update_state( ID => $state->{ID}, $state );
133 warn "## empty request, using last request state\n";
135 $state = $self->state;
136 delete( $state->{_dispatch} );
137 #warn "last request state = ", dump( $state ), "\n" if $self->debug > 1;
141 $sock->send(join("\r\n",
143 'Content-Type: text/xml; charset="utf-8"',
144 'Server: AcmeCWMP/42',
145 'SOAPServer: AcmeCWMP/42'
148 $sock->send( "Set-Cookie: ID=" . $state->{ID} . "; path=/\r\n" ) if ( $state->{ID} );
152 if ( my $dispatch = $state->{_dispatch} ) {
153 $xml = $self->dispatch( $dispatch );
154 } elsif ( $dispatch = shift @{ $self->queue } ) {
155 $xml = $self->dispatch( $dispatch );
156 } elsif ( $size == 0 ) {
157 warn ">>> no more queued commands, closing connection\n";
160 warn ">>> empty response\n";
161 $state->{NoMoreRequests} = 1;
162 $xml = $self->dispatch( 'xml', sub {} );
165 $sock->send( "Content-Length: " . length( $xml ) . "\r\n\r\n" );
166 $sock->send( $xml ) or die "can't send response";
168 warn ">>>> " . $sock->peerhost . " [" . localtime() . "] sent ", length( $xml )," bytes\n";
170 warn "### request over\n" if $self->debug;
172 return 1; # next request
177 $xml = $self->dispatch('Inform', $response_arguments );
179 If debugging level of 3 or more, it will create dumps of responses named C<< dump/nr.response >>
186 my $dispatch = shift || die "no dispatch?";
189 if ( ref($dispatch) eq 'ARRAY' ) {
191 $dispatch = shift @a;
195 my $response = CWMP::Response->new({ debug => $self->debug });
197 if ( $response->can( $dispatch ) ) {
198 warn ">>> dispatching to $dispatch\n";
199 my $xml = $response->$dispatch( $self->state, @args );
200 warn "## response payload: ",length($xml)," bytes\n$xml\n" if $self->debug;
201 if ( $self->debug > 2 ) {
202 my $file = sprintf("dump/%04d-%s.response", $dump_nr++, $self->sock->peerhost);
203 write_file( $file, $xml );
204 warn "### response dump: $file\n";
208 confess "can't dispatch to $dispatch";
215 return $self->error( 501, 'System error' );
220 my ($self, $number, $msg) = @_;
222 $self->sock->send( "HTTP/1.1 $number $msg\r\n" );
223 warn "Error - $number - $msg\n";
224 return 0; # close connection