5 |
use warnings; |
use warnings; |
6 |
|
|
7 |
use base qw/HTTP::Server::Simple::CGI Class::Accessor/; |
use base qw/HTTP::Server::Simple::CGI Class::Accessor/; |
|
|
|
8 |
__PACKAGE__->mk_accessors( qw/ |
__PACKAGE__->mk_accessors( qw/ |
9 |
debug |
debug |
10 |
/ ); |
/ ); |
11 |
|
|
|
|
|
12 |
use Data::Dump qw/dump/; |
use Data::Dump qw/dump/; |
13 |
|
use CWMP::Request; |
14 |
|
use CWMP::Response; |
15 |
|
|
16 |
|
=head1 NAME |
17 |
|
|
18 |
|
CWMP::Server - implement logic of CWMP protocol |
19 |
|
|
20 |
|
=head1 METHODS |
21 |
|
|
22 |
|
=head2 handler |
23 |
|
|
24 |
|
We override L<HTTP::Server::Simple::CGI/handler> so that we can support |
25 |
|
chunked transfer encoding. |
26 |
|
|
27 |
|
=cut |
28 |
|
|
29 |
sub handler { |
sub handler { |
30 |
my $self = shift; |
my $self = shift; |
35 |
|
|
36 |
if ( $transfer_encoding && $transfer_encoding =~ qr/^chunked/i ) { |
if ( $transfer_encoding && $transfer_encoding =~ qr/^chunked/i ) { |
37 |
|
|
38 |
my $last = 0; |
my $len = 0; |
39 |
|
my $hex; |
40 |
do { |
do { |
41 |
my $len = <STDIN>; |
$hex = <STDIN>; # get chunk length |
42 |
$len =~ s/[\n\r]*$//s; |
$hex =~ s/[\n\r]*$//s; |
43 |
$len = hex( $len ); |
$len = hex( $hex ); |
44 |
|
|
45 |
$last = 1 if ( $len == 0 ); |
warn "getting chunk of 0x$hex $len bytes\n" if $self->debug; |
|
|
|
|
warn "getting chunk of $len bytes\n"; |
|
46 |
|
|
47 |
while( $len > 0 ) { |
while( $len > 0 ) { |
48 |
my $line = <STDIN>; |
my $line = <STDIN>; |
50 |
$len -= length( $line ); |
$len -= length( $line ); |
51 |
} |
} |
52 |
|
|
53 |
} while ( ! $last ); |
} while ( hex( $hex ) != 0 ); |
54 |
} |
} |
55 |
|
|
56 |
warn "chunk (", length($chunk), " bytes)\n------>>\n$chunk\n<<-----\n"; |
warn "handler got ", length($chunk), " bytes\n" if $self->debug; |
57 |
|
|
58 |
my $cgi = new CGI( $chunk ); |
my $cgi = new CGI( $chunk ); |
59 |
|
|
64 |
} |
} |
65 |
} |
} |
66 |
|
|
67 |
|
=head2 handle_request |
68 |
|
|
69 |
|
Implementation of dispatch logic |
70 |
|
|
71 |
|
=cut |
72 |
|
|
73 |
sub handle_request { |
sub handle_request { |
74 |
my ($self, $cgi) = @_; |
my ($self, $cgi) = @_; |
75 |
|
|
76 |
#... do something, print output to default |
#... do something, print output to default |
77 |
# selected filehandle... |
# selected filehandle... |
78 |
|
|
79 |
warn ">> ", $ENV{REMOTE_ADDR}, $/; |
warn ">> " . localtime() . " " . $ENV{REMOTE_ADDR} . "\n"; |
80 |
|
|
81 |
warn "not SOAP request" unless defined ( $cgi->header('SOAPAction') ); |
warn "not SOAP request" unless defined ( $cgi->header('SOAPAction') ); |
82 |
|
|
83 |
# warn $cgi->param('POSTDATA'), dump( $cgi ); |
if ( my $payload = $cgi->param('POSTDATA') ) { |
84 |
|
warn "request payload:\n$payload\n" if $self->debug; |
85 |
|
|
86 |
|
my $state = CWMP::Request->parse( $payload ); |
87 |
|
|
88 |
|
warn "acquired state = ", dump( $state ); |
89 |
|
|
90 |
|
} |
91 |
|
|
92 |
|
my $response = CWMP::Response->new({ debug => $self->debug }); |
93 |
|
|
94 |
print "Content-Type: text/xml\r\n\r\n"; |
print "Content-Type: text/xml\r\n\r\n"; |
95 |
|
|
96 |
|
print $response->Inform; |
97 |
|
|
98 |
}; |
}; |
99 |
|
|
100 |
1; |
1; |