15 |
store |
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/; |
use Carp qw/confess cluck croak/; |
21 |
|
|
32 |
=head2 new |
=head2 new |
33 |
|
|
34 |
my $server = CWMP::Session->new({ |
my $server = CWMP::Session->new({ |
35 |
sock => $io_socket_object, |
sock => $io_socket_object, |
36 |
store_path => 'state.db', |
store_path => 'state.db', |
37 |
queue => [ qw/GetRPCMethods GetParameterNames/ ], |
queue => [ qw/GetRPCMethods GetParameterNames/ ], |
38 |
debug => 1, |
debug => 1, |
39 |
}); |
}); |
40 |
|
|
|
|
|
41 |
=cut |
=cut |
42 |
|
|
43 |
sub new { |
sub new { |
73 |
|
|
74 |
my $sock = $self->sock || die "no sock?"; |
my $sock = $self->sock || die "no sock?"; |
75 |
|
|
76 |
die "not IO::Socket::INET but ", ref( $sock ) unless ( ref($sock) eq 'Net::Server::Proto::TCP' ); |
# die "not IO::Socket::INET but ", ref( $sock ) unless ( ref($sock) eq 'Net::Server::Proto::TCP' ); |
77 |
|
|
78 |
if ( ! $sock->connected ) { |
if ( ! $sock->connected ) { |
79 |
warn "SOCKET NOT CONNECTED\n"; |
warn "SOCKET NOT CONNECTED\n"; |
80 |
return 0; |
return 0; |
81 |
} |
} |
82 |
|
|
83 |
$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") 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'); |
|
|
|
|
|
if ( $transfer_encoding && $transfer_encoding =~ qr/^chunked/i ) { |
|
|
|
|
|
my $len = 0; |
|
84 |
|
|
85 |
do { |
# why do I have to do this? |
86 |
|
# solution from http://use.perl.org/~Matts/journal/12896 |
87 |
|
${*$sock}{'httpd_daemon'} = HTTP::Daemon->new; |
88 |
|
|
89 |
warn "get chunk len\n" if $self->debug > 1; |
my $r = $sock->get_request || confess "can't get_request"; |
|
|
|
|
my $hex; |
|
|
do { |
|
|
$hex = $sock->getline; |
|
|
$hex =~ s/[\n\r]+$//; |
|
|
} until ( $hex ne '' ); |
|
90 |
|
|
91 |
die "chunk size not valid hex: $hex" unless ( $hex =~ m/^[0-9a-f]+$/i); |
warn "<<<< ", $sock->peerhost, " - - [" . localtime() . "] ", $r->method, " ", $r->uri, "\n"; |
|
$len = hex( $hex ); |
|
92 |
|
|
93 |
warn "getting chunk of $len bytes\n" if $self->debug > 1; |
my $chunk = $r->content; |
|
|
|
|
$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"; |
|
|
} |
|
94 |
|
|
95 |
my $size = length( $chunk ); |
my $size = length( $chunk ); |
96 |
|
|
100 |
|
|
101 |
if ( $size > 0 ) { |
if ( $size > 0 ) { |
102 |
|
|
103 |
die "no SOAPAction header in ",dump($chunk) unless defined ( $self->header('SOAPAction') ); |
die "no SOAPAction header in ",dump($chunk) unless defined ( $r->header('SOAPAction') ); |
104 |
|
|
105 |
|
|
106 |
if ( $chunk ) { |
if ( $chunk ) { |
180 |
} |
} |
181 |
}; |
}; |
182 |
|
|
|
=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; |
|
|
} |
|
|
} |
|
183 |
|
|
184 |
=head2 error |
=head2 error |
185 |
|
|