1 |
# Dobrica Pavlinusic, <dpavlin@rot13.org> 06/18/07 10:19:50 CEST |
# Dobrica Pavlinusic, <dpavlin@rot13.org> 06/18/07 10:19:50 CEST |
2 |
package CWMP::Server; |
package CWMP::Session; |
3 |
|
|
4 |
use strict; |
use strict; |
5 |
use warnings; |
use warnings; |
7 |
use base qw/Class::Accessor/; |
use base qw/Class::Accessor/; |
8 |
__PACKAGE__->mk_accessors( qw/ |
__PACKAGE__->mk_accessors( qw/ |
9 |
debug |
debug |
10 |
port |
store |
11 |
|
|
12 |
sock |
sock |
13 |
state |
state |
14 |
queue |
store |
15 |
/ ); |
/ ); |
16 |
|
|
17 |
use IO::Socket::INET; |
use HTTP::Daemon; |
18 |
use Data::Dump qw/dump/; |
use Data::Dump qw/dump/; |
19 |
|
use Carp qw/confess cluck croak/; |
20 |
|
use File::Slurp; |
21 |
|
|
22 |
use CWMP::Request; |
use CWMP::Request; |
23 |
use CWMP::Response; |
use CWMP::Methods; |
24 |
use Carp qw/confess cluck/; |
use CWMP::Store; |
25 |
|
|
26 |
=head1 NAME |
=head1 NAME |
27 |
|
|
28 |
CWMP::Server - implement logic of CWMP protocol |
CWMP::Session - implement logic of CWMP protocol |
29 |
|
|
30 |
=head1 METHODS |
=head1 METHODS |
31 |
|
|
32 |
=head2 new |
=head2 new |
33 |
|
|
34 |
my $server = CWMP::Server->new({ port => 3333 }); |
my $server = CWMP::Session->new({ |
35 |
|
sock => $io_socket_object, |
36 |
|
store => 'state.db', |
37 |
|
debug => 1, |
38 |
|
}); |
39 |
|
|
40 |
=head2 run |
=cut |
41 |
|
|
42 |
$server->run(); |
sub new { |
43 |
|
my $class = shift; |
44 |
|
my $self = $class->SUPER::new( @_ ); |
45 |
|
|
46 |
=cut |
confess "need sock" unless $self->sock; |
47 |
|
|
48 |
sub run { |
$self->debug( 0 ) unless $self->debug; |
|
my $self = shift; |
|
49 |
|
|
50 |
my $listen = IO::Socket::INET->new( |
warn "created ", __PACKAGE__, "(", dump( @_ ), ") for ", $self->sock->peerhost, "\n" if $self->debug; |
|
Listen => 5, |
|
|
# LocalAddr => 'localhost', |
|
|
LocalPort => $self->port, |
|
|
Proto => 'tcp', |
|
|
Blocking => 1, |
|
|
ReuseAddr => 1, |
|
|
); |
|
|
|
|
|
warn "waiting for request on port ", $self->port, $/; |
|
|
|
|
|
while ( my $sock = $listen->accept ) { |
|
|
$sock->autoflush(1); |
|
|
|
|
|
warn "connection from ", $sock->peerhost, "\n"; |
|
|
|
|
|
$self->sock( $sock ); # FIXME this will not work for multiple clients |
|
|
while ( $self->process_request ) { |
|
|
warn "...another one bites a dust...\n"; |
|
|
} |
|
51 |
|
|
52 |
warn "...returning to accepting new connections\n"; |
my $store_obj = CWMP::Store->new({ |
53 |
} |
debug => $self->debug, |
54 |
|
%{ $self->store }, |
55 |
|
}); |
56 |
|
|
57 |
|
croak "can't open ", dump( $self->store ), ": $!" unless $store_obj; |
58 |
|
|
59 |
|
# FIXME looks ugly. Should we have separate accessor for this? |
60 |
|
$self->store( $store_obj ); |
61 |
|
|
62 |
|
return $self; |
63 |
} |
} |
64 |
|
|
65 |
=head2 process_request |
=head2 process_request |
68 |
facilitate brain-dead concept of adding state to stateless protocol like |
facilitate brain-dead concept of adding state to stateless protocol like |
69 |
HTTP. |
HTTP. |
70 |
|
|
71 |
|
If used with debugging level of 3 or more, it will also create dumps of |
72 |
|
requests named C<< dump/nr.request >> where C<nr> is number from 0 to total number |
73 |
|
of requests in single session. |
74 |
|
|
75 |
=cut |
=cut |
76 |
|
|
77 |
|
my $dump_nr = 0; |
78 |
|
|
79 |
sub process_request { |
sub process_request { |
80 |
my $self = shift; |
my $self = shift; |
81 |
|
|
82 |
my $sock = $self->sock || die "no sock?"; |
my $sock = $self->sock || die "no sock?"; |
83 |
|
|
84 |
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' ); |
85 |
|
|
86 |
if ( ! $sock->connected ) { |
if ( ! $sock->connected ) { |
87 |
warn "SOCKET NOT CONNECTED"; |
warn "SOCKET NOT CONNECTED\n"; |
88 |
return 0; |
return 0; |
89 |
} |
} |
90 |
|
|
91 |
$sock->autoflush( 1 ); |
bless $sock, 'HTTP::Daemon::ClientConn'; |
|
$sock->blocking( 1 ); |
|
92 |
|
|
93 |
### read the first line of response |
# why do I have to do this? |
94 |
my $line = $sock->getline; |
# solution from http://use.perl.org/~Matts/journal/12896 |
95 |
return $self->error(400, "No Data") unless ( defined $line ); |
${*$sock}{'httpd_daemon'} = HTTP::Daemon->new; |
|
|
|
|
$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"; |
|
96 |
|
|
97 |
### read in other headers |
my $r = $sock->get_request || confess "can't get_request"; |
|
$self->read_headers || return $self->error(400, "Strange headers"); |
|
98 |
|
|
99 |
### do we support the type |
my $xml = $r->content; |
|
# if ($method !~ /GET|POST|HEAD/) { |
|
|
if ($method !~ /POST/) { |
|
|
return $self->error(400, "Unsupported Method"); |
|
|
} |
|
100 |
|
|
101 |
my $chunk; |
my $size = length( $xml ); |
|
my $transfer_encoding = $self->header('Transfer-Encoding'); |
|
102 |
|
|
103 |
if ( $transfer_encoding && $transfer_encoding =~ qr/^chunked/i ) { |
warn "<<<< ", $sock->peerhost, " [" . localtime() . "] ", $r->method, " ", $r->uri, " $size bytes\n"; |
104 |
|
|
105 |
my $len = 0; |
$dump_nr++; |
106 |
|
my $file = sprintf("dump/%04d-%s.request", $dump_nr, $sock->peerhost); |
107 |
|
|
108 |
do { |
if ( $self->debug > 2 ) { |
109 |
|
write_file( $file, $r->as_string ); |
110 |
warn "get chunk len\n" if $self->debug; |
warn "### request dumped to file: $file\n"; |
|
|
|
|
my $hex; |
|
|
do { |
|
|
$hex = $sock->getline; |
|
|
$hex =~ s/[\n\r]+$//; |
|
|
} until ( $hex ne '' ); |
|
|
|
|
|
die "chunk size not valid hex: $hex" unless ( $hex =~ m/^[0-9a-f]+$/i); |
|
|
$len = hex( $hex ); |
|
|
|
|
|
warn "getting chunk of $len bytes\n" if $self->debug; |
|
|
|
|
|
$sock->read( my $buff, $len ); |
|
|
$chunk .= $buff; |
|
|
|
|
|
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"; |
|
111 |
} |
} |
112 |
|
|
|
my $size = length( $chunk ); |
|
|
|
|
|
warn "<<< " . $sock->peerhost . " [" . localtime() . "] request $size bytes\n"; |
|
|
|
|
113 |
my $state; |
my $state; |
114 |
|
|
115 |
if ( $size > 0 ) { |
if ( $size > 0 ) { |
116 |
|
|
117 |
die "no SOAPAction header in ",dump($chunk) unless defined ( $self->header('SOAPAction') ); |
die "no SOAPAction header in ",dump($xml) unless defined ( $r->header('SOAPAction') ); |
118 |
|
|
119 |
|
warn "## request payload: ",length($xml)," bytes\n$xml\n" if $self->debug; |
120 |
|
|
121 |
if ( $chunk ) { |
$state = CWMP::Request->parse( $xml ); |
|
warn "## request chunk: ",length($chunk)," bytes\n$chunk\n" if $self->debug; |
|
122 |
|
|
123 |
$state = CWMP::Request->parse( $chunk ); |
if ( defined( $state->{_dispatch} ) && $self->debug > 2 ) { |
124 |
|
my $type = sprintf("dump/%04d-%s-%s", $dump_nr, $sock->peerhost, $state->{_dispatch}); |
125 |
warn "acquired state = ", dump( $state ), "\n"; |
symlink $file, $type || warn "can't symlink $file -> $type: $!"; |
126 |
|
} |
127 |
|
|
128 |
$self->state( $state ); |
warn "## acquired state = ", dump( $state ), "\n"; |
129 |
|
|
130 |
} else { |
$self->state( $state ); |
131 |
warn "empty request\n"; |
$self->store->update_state( ID => $state->{ID}, $state ); |
|
} |
|
132 |
|
|
133 |
} else { |
} else { |
134 |
|
|
135 |
|
warn "## empty request, using last request state\n"; |
136 |
|
|
137 |
$state = $self->state; |
$state = $self->state; |
138 |
warn "last request state = ", dump( $state ), "\n"; |
delete( $state->{_dispatch} ); |
139 |
|
#warn "last request state = ", dump( $state ), "\n" if $self->debug > 1; |
140 |
} |
} |
141 |
|
|
142 |
|
|
145 |
'Content-Type: text/xml; charset="utf-8"', |
'Content-Type: text/xml; charset="utf-8"', |
146 |
'Server: AcmeCWMP/42', |
'Server: AcmeCWMP/42', |
147 |
'SOAPServer: AcmeCWMP/42' |
'SOAPServer: AcmeCWMP/42' |
148 |
)); |
)."\r\n"); |
149 |
|
|
150 |
$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} ); |
151 |
|
|
152 |
my $xml = ''; |
my $queue = CWMP::Queue->new({ |
153 |
|
id => $self->store->ID_to_uid( $state->{ID}, $state ), |
154 |
|
debug => $self->debug, |
155 |
|
}); |
156 |
|
my $job; |
157 |
|
$xml = ''; |
158 |
|
|
159 |
if ( my $dispatch = $state->{_dispatch} ) { |
if ( my $dispatch = $state->{_dispatch} ) { |
160 |
$xml = $self->dispatch( $dispatch ); |
$xml = $self->dispatch( $dispatch ); |
161 |
} elsif ( $dispatch = shift @{ $self->queue } ) { |
} elsif ( $job = $queue->dequeue ) { |
162 |
$xml = $self->dispatch( $dispatch ); |
$xml = $self->dispatch( $job->dispatch ); |
163 |
} elsif ( $size == 0 ) { |
} elsif ( $size == 0 ) { |
164 |
warn ">>> closing connection\n"; |
warn ">>> no more queued commands, closing connection\n"; |
165 |
return 0; |
return 0; |
166 |
} else { |
} else { |
167 |
warn ">>> empty response\n"; |
warn ">>> empty response\n"; |
172 |
$sock->send( "Content-Length: " . length( $xml ) . "\r\n\r\n" ); |
$sock->send( "Content-Length: " . length( $xml ) . "\r\n\r\n" ); |
173 |
$sock->send( $xml ) or die "can't send response"; |
$sock->send( $xml ) or die "can't send response"; |
174 |
|
|
175 |
warn "### request over"; |
warn ">>>> " . $sock->peerhost . " [" . localtime() . "] sent ", length( $xml )," bytes\n"; |
176 |
|
|
177 |
|
$job->finish if $job; |
178 |
|
warn "### request over\n" if $self->debug; |
179 |
|
|
180 |
|
return 1; # next request |
181 |
}; |
}; |
182 |
|
|
183 |
=head2 dispatch |
=head2 dispatch |
184 |
|
|
185 |
$xml = $self->dispatch('Inform', $response_arguments ); |
$xml = $self->dispatch('Inform', $response_arguments ); |
186 |
|
|
187 |
|
If debugging level of 3 or more, it will create dumps of responses named C<< dump/nr.response >> |
188 |
|
|
189 |
=cut |
=cut |
190 |
|
|
191 |
sub dispatch { |
sub dispatch { |
192 |
my $self = shift; |
my $self = shift; |
193 |
|
|
194 |
my $dispatch = shift || die "no dispatch?"; |
my $dispatch = shift || die "no dispatch?"; |
195 |
|
my @args = @_; |
196 |
|
|
197 |
|
if ( ref($dispatch) eq 'ARRAY' ) { |
198 |
|
my @a = @$dispatch; |
199 |
|
$dispatch = shift @a; |
200 |
|
push @args, @a; |
201 |
|
} |
202 |
|
|
203 |
my $response = CWMP::Response->new({ debug => $self->debug }); |
my $response = CWMP::Methods->new({ debug => $self->debug }); |
204 |
|
|
205 |
if ( $response->can( $dispatch ) ) { |
if ( $response->can( $dispatch ) ) { |
206 |
warn ">>> dispatching to $dispatch\n"; |
warn ">>> dispatching to $dispatch\n"; |
207 |
my $xml = $response->$dispatch( $self->state, @_ ) . "\r\n"; |
my $xml = $response->$dispatch( $self->state, @args ); |
208 |
warn "## response payload: ",length($xml)," bytes\n$xml\n"; |
warn "## response payload: ",length($xml)," bytes\n$xml\n" if $self->debug; |
209 |
|
if ( $self->debug > 2 ) { |
210 |
|
my $file = sprintf("dump/%04d-%s.response", $dump_nr++, $self->sock->peerhost); |
211 |
|
write_file( $file, $xml ); |
212 |
|
warn "### response dump: $file\n"; |
213 |
|
} |
214 |
return $xml; |
return $xml; |
215 |
} else { |
} else { |
216 |
confess "can't dispatch to $dispatch"; |
confess "can't dispatch to $dispatch"; |
217 |
} |
} |
218 |
}; |
}; |
219 |
|
|
|
=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"; |
|
|
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; |
|
|
} |
|
|
} |
|
220 |
|
|
221 |
=head2 error |
=head2 error |
222 |
|
|