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_path |
11 |
|
|
12 |
sock |
sock |
13 |
|
state |
14 |
|
queue |
15 |
|
store |
16 |
/ ); |
/ ); |
17 |
|
|
18 |
use IO::Socket::INET; |
use HTTP::Daemon; |
19 |
use Data::Dump qw/dump/; |
use Data::Dump qw/dump/; |
20 |
|
use Carp qw/confess cluck croak/; |
21 |
|
use File::Slurp; |
22 |
|
|
23 |
use CWMP::Request; |
use CWMP::Request; |
24 |
use CWMP::Response; |
use CWMP::Response; |
25 |
use Carp qw/confess cluck/; |
use CWMP::Store; |
26 |
|
|
27 |
=head1 NAME |
=head1 NAME |
28 |
|
|
29 |
CWMP::Server - implement logic of CWMP protocol |
CWMP::Session - implement logic of CWMP protocol |
30 |
|
|
31 |
=head1 METHODS |
=head1 METHODS |
32 |
|
|
33 |
=head2 new |
=head2 new |
34 |
|
|
35 |
my $server = CWMP::Server->new({ port => 3333 }); |
my $server = CWMP::Session->new({ |
36 |
|
sock => $io_socket_object, |
37 |
|
store_path => 'state.db', |
38 |
|
queue => [ qw/GetRPCMethods GetParameterNames/ ], |
39 |
|
debug => 1, |
40 |
|
}); |
41 |
|
|
42 |
=head2 run |
=cut |
43 |
|
|
44 |
$server->run(); |
sub new { |
45 |
|
my $class = shift; |
46 |
|
my $self = $class->SUPER::new( @_ ); |
47 |
|
|
48 |
=cut |
confess "need sock" unless $self->sock; |
49 |
|
|
50 |
sub run { |
$self->debug( 0 ) unless $self->debug; |
|
my $self = shift; |
|
51 |
|
|
52 |
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"; |
|
|
} |
|
53 |
|
|
54 |
warn "...returning to accepting new connections\n"; |
$self->store( CWMP::Store->new({ |
55 |
} |
debug => $self->debug, |
56 |
|
path => $self->store_path, |
57 |
|
}) ); |
58 |
|
|
59 |
|
croak "can't open ", $self->store_path, ": $!" unless $self->store; |
60 |
|
|
61 |
|
return $self; |
62 |
} |
} |
63 |
|
|
64 |
=head2 process_request |
=head2 process_request |
67 |
facilitate brain-dead concept of adding state to stateless protocol like |
facilitate brain-dead concept of adding state to stateless protocol like |
68 |
HTTP. |
HTTP. |
69 |
|
|
70 |
|
If used with debugging level of 3 or more, it will also create dumps of |
71 |
|
requests named C<< dump/nr.request >> where C<nr> is number from 0 to total number |
72 |
|
of requests in single session. |
73 |
|
|
74 |
=cut |
=cut |
75 |
|
|
76 |
|
my $dump_nr = 0; |
77 |
|
|
78 |
sub process_request { |
sub process_request { |
79 |
my $self = shift; |
my $self = shift; |
80 |
|
|
81 |
my $sock = $self->sock || die "no sock?"; |
my $sock = $self->sock || die "no sock?"; |
82 |
|
|
83 |
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' ); |
84 |
|
|
85 |
if ( ! $sock->connected ) { |
if ( ! $sock->connected ) { |
86 |
warn "SOCKET NOT CONNECTED"; |
warn "SOCKET NOT CONNECTED\n"; |
87 |
return 0; |
return 0; |
88 |
} |
} |
89 |
|
|
90 |
$sock->autoflush( 1 ); |
bless $sock, 'HTTP::Daemon::ClientConn'; |
|
$sock->blocking( 1 ); |
|
|
|
|
|
### read the first line of response |
|
|
my $line = $sock->getline || return $self->error(400, "No Data"); |
|
|
|
|
|
$line =~ s/[\r\n]+$//; |
|
|
if ($line !~ /^ (\w+) \ + (\S+) \ + (HTTP\/1.\d) $ /x) { |
|
|
return $self->error(400, "Bad request"); |
|
|
} |
|
|
my ($method, $req, $protocol) = ($1, $2, $3); |
|
|
warn "<<<< ",join(" ", time, $method, $req)."\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'); |
|
91 |
|
|
92 |
if ( $transfer_encoding && $transfer_encoding =~ qr/^chunked/i ) { |
# why do I have to do this? |
93 |
|
# solution from http://use.perl.org/~Matts/journal/12896 |
94 |
|
${*$sock}{'httpd_daemon'} = HTTP::Daemon->new; |
95 |
|
|
96 |
my $len = 0; |
my $r = $sock->get_request || confess "can't get_request"; |
97 |
|
|
98 |
do { |
my $chunk = $r->content; |
99 |
|
|
100 |
warn "get chunk len\n" if $self->debug; |
my $size = length( $chunk ); |
|
|
|
|
my $hex; |
|
|
do { |
|
|
$hex = $sock->getline; |
|
|
$hex =~ s/[\n\r]+$//; |
|
|
} until ( $hex ne '' ); |
|
101 |
|
|
102 |
die "chunk size not valid hex: $hex" unless ( $hex =~ m/^[0-9a-f]+$/i); |
warn "<<<< ", $sock->peerhost, " [" . localtime() . "] ", $r->method, " ", $r->uri, " $size bytes\n"; |
|
$len = hex( $hex ); |
|
103 |
|
|
104 |
warn "getting chunk of $len bytes\n" if $self->debug; |
if ( $self->debug > 2 ) { |
105 |
|
my $file = sprintf("dump/%04d.request", $dump_nr); |
106 |
$sock->read( my $buff, $len ); |
write_file( $file, $r->as_string ); |
107 |
$chunk .= $buff; |
warn "### request dump: $file\n"; |
108 |
|
} |
109 |
|
|
110 |
warn "--- $len bytes: --=>||$buff||<=--\n"; |
my $state; |
111 |
|
|
112 |
} while ( $len > 0 ); |
if ( $size > 0 ) { |
113 |
|
|
114 |
} else { |
die "no SOAPAction header in ",dump($chunk) unless defined ( $r->header('SOAPAction') ); |
|
die "right now, we support only Transfer-Encoding: chunked"; |
|
|
} |
|
115 |
|
|
|
warn "handler got ", length($chunk), " bytes\n" if $self->debug; |
|
116 |
|
|
117 |
warn "<<< " . localtime() . " " . $sock->peerhost . "\n"; |
if ( $chunk ) { |
118 |
|
warn "## request chunk: ",length($chunk)," bytes\n$chunk\n" if $self->debug; |
119 |
|
|
120 |
die "not SOAP request" unless defined ( $self->header('SOAPAction') ); |
$state = CWMP::Request->parse( $chunk ); |
121 |
|
|
122 |
my $state; |
warn "## acquired state = ", dump( $state ), "\n"; |
123 |
|
|
124 |
if ( $chunk ) { |
$self->state( $state ); |
125 |
warn "## request chunk: ",length($chunk)," bytes\n$chunk\n" if $self->debug; |
$self->store->update_state( ID => $state->{ID}, $state ); |
126 |
|
|
127 |
$state = CWMP::Request->parse( $chunk ); |
} else { |
128 |
|
warn "## empty request\n"; |
129 |
|
} |
130 |
|
|
|
warn "acquired state = ", dump( $state ), "\n"; |
|
|
|
|
131 |
} else { |
} else { |
132 |
warn "empty request\n"; |
$state = $self->state; |
133 |
|
warn "last request state = ", dump( $state ), "\n" if $self->debug > 1; |
134 |
} |
} |
135 |
|
|
136 |
|
|
|
my $response = CWMP::Response->new({ debug => $self->debug }); |
|
|
|
|
137 |
$sock->send(join("\r\n", |
$sock->send(join("\r\n", |
138 |
'HTTP/1.1 200 OK', |
'HTTP/1.1 200 OK', |
139 |
'Content-Type: text/xml; charset="utf-8"', |
'Content-Type: text/xml; charset="utf-8"', |
140 |
'Server: AcmeCWMP/42', |
'Server: AcmeCWMP/42', |
141 |
'SOAPServer: AcmeCWMP/42', |
'SOAPServer: AcmeCWMP/42' |
142 |
)); |
)."\r\n"); |
143 |
|
|
144 |
$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} ); |
145 |
|
|
146 |
my $xml = ''; |
my $xml = ''; |
147 |
|
|
148 |
if ( my $dispatch = $state->{_dispatch} ) { |
if ( my $dispatch = $state->{_dispatch} ) { |
149 |
if ( $response->can( $dispatch ) ) { |
$xml = $self->dispatch( $dispatch ); |
150 |
warn ">>> dispatching to $dispatch\n"; |
} elsif ( $dispatch = shift @{ $self->queue } ) { |
151 |
$xml = $response->$dispatch( $state ) . "\r\n"; |
$xml = $self->dispatch( $dispatch ); |
152 |
warn "## response payload: ",length($xml)," bytes\n$xml\n"; |
} elsif ( $size == 0 ) { |
153 |
} else { |
warn ">>> closing connection\n"; |
154 |
confess "can't dispatch to $dispatch"; |
return 0; |
|
} |
|
155 |
} else { |
} else { |
156 |
warn ">>> empty response\n"; |
warn ">>> empty response\n"; |
157 |
|
$state->{NoMoreRequests} = 1; |
158 |
|
$xml = $self->dispatch( 'xml', sub {} ); |
159 |
} |
} |
160 |
|
|
161 |
$sock->send( "Content-Length: " . length( $xml ) . "\r\n\r\n" ); |
$sock->send( "Content-Length: " . length( $xml ) . "\r\n\r\n" ); |
162 |
$sock->send( $xml ) or die "can't send response"; |
$sock->send( $xml ) or die "can't send response"; |
163 |
|
|
164 |
warn "### request over"; |
warn ">>>> " . $sock->peerhost . " [" . localtime() . "] sent ", length( $xml )," bytes\n"; |
|
|
|
|
}; |
|
|
|
|
|
=head2 read_headers |
|
|
|
|
|
parse headers from request |
|
165 |
|
|
166 |
=cut |
warn "### request over\n" if $self->debug; |
167 |
|
|
168 |
sub read_headers { |
return 1; # next request |
169 |
my $self = shift; |
}; |
170 |
|
|
171 |
$self->{headers} = {}; |
=head2 dispatch |
172 |
|
|
173 |
while (defined($_ = $self->sock->getline)) { |
$xml = $self->dispatch('Inform', $response_arguments ); |
|
s/[\r\n]+$//; |
|
|
last unless length $_; |
|
|
warn "-- $_\n"; |
|
|
return 0 if ! /^ ([\w\-]+) :[\ \t]* (.*) $/x; |
|
|
$self->{headers}->{$1} = $2; |
|
|
} |
|
174 |
|
|
175 |
return 1; |
If debugging level of 3 or more, it will create dumps of responses named C<< dump/nr.response >> |
|
} |
|
176 |
|
|
177 |
=head2 header |
=cut |
178 |
|
|
179 |
Getter for specific header |
sub dispatch { |
180 |
|
my $self = shift; |
181 |
|
|
182 |
$self->header('Cookies'); |
my $dispatch = shift || die "no dispatch?"; |
183 |
|
|
184 |
=cut |
my $response = CWMP::Response->new({ debug => $self->debug }); |
185 |
|
|
186 |
sub header { |
if ( $response->can( $dispatch ) ) { |
187 |
my $self = shift; |
warn ">>> dispatching to $dispatch\n"; |
188 |
my $header = shift || die "no header?"; |
my $xml = $response->$dispatch( $self->state, @_ ); |
189 |
if ( defined( $self->{headers}->{$header} )) { |
warn "## response payload: ",length($xml)," bytes\n$xml\n" if $self->debug; |
190 |
return $self->{headers}->{$header}; |
if ( $self->debug > 2 ) { |
191 |
|
my $file = sprintf("dump/%04d.response", $dump_nr++); |
192 |
|
write_file( $file, $xml ); |
193 |
|
warn "### response dump: $file\n"; |
194 |
|
} |
195 |
|
return $xml; |
196 |
} else { |
} else { |
197 |
return; |
confess "can't dispatch to $dispatch"; |
198 |
} |
} |
199 |
} |
}; |
200 |
|
|
201 |
|
|
202 |
=head2 error |
=head2 error |
203 |
|
|