53 |
warn "connection from ", $sock->peerhost, "\n"; |
warn "connection from ", $sock->peerhost, "\n"; |
54 |
|
|
55 |
$self->sock( $sock ); # FIXME this will not work for multiple clients |
$self->sock( $sock ); # FIXME this will not work for multiple clients |
56 |
$self->process_request( $sock ); |
while ( $self->process_request ) { |
57 |
|
warn "...another one bites a dust...\n"; |
58 |
|
} |
59 |
|
|
60 |
warn "...another one bites a dust...\n"; |
warn "...returning to accepting new connections\n"; |
|
sleep 1; |
|
61 |
} |
} |
62 |
} |
} |
63 |
|
|
64 |
sub process_request { |
sub process_request { |
65 |
my $self = shift; |
my $self = shift; |
66 |
|
|
67 |
my $sock = shift || die "no sock?"; |
my $sock = $self->sock || die "no sock?"; |
68 |
|
|
69 |
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 'IO::Socket::INET' ); |
70 |
|
|
71 |
|
if ( ! $sock->connected ) { |
72 |
|
warn "SOCKET NOT CONNECTED"; |
73 |
|
return 0; |
74 |
|
} |
75 |
|
|
76 |
$sock->autoflush( 1 ); |
$sock->autoflush( 1 ); |
77 |
$sock->blocking( 1 ); |
$sock->blocking( 1 ); |
78 |
|
|
79 |
### read the first line of response |
### read the first line of response |
80 |
my $line = $sock->getline; # || $self->error(400, "No Data"); |
my $line = $sock->getline || $self->error(400, "No Data"); |
81 |
|
|
82 |
$line =~ s/[\r\n]+$//; |
$line =~ s/[\r\n]+$//; |
83 |
if ($line !~ /^ (\w+) \ + (\S+) \ + (HTTP\/1.\d) $ /x) { |
if ($line !~ /^ (\w+) \ + (\S+) \ + (HTTP\/1.\d) $ /x) { |
87 |
warn "<<<< ",join(" ", time, $method, $req)."\n"; |
warn "<<<< ",join(" ", time, $method, $req)."\n"; |
88 |
|
|
89 |
### read in other headers |
### read in other headers |
90 |
$self->read_headers($sock) || return $self->error(400, "Strange headers"); |
$self->read_headers || return $self->error(400, "Strange headers"); |
91 |
|
|
92 |
### do we support the type |
### do we support the type |
93 |
# if ($method !~ /GET|POST|HEAD/) { |
# if ($method !~ /GET|POST|HEAD/) { |
150 |
|
|
151 |
my $response = CWMP::Response->new({ debug => $self->debug }); |
my $response = CWMP::Response->new({ debug => $self->debug }); |
152 |
|
|
153 |
$sock->print( |
$sock->send( |
154 |
$self->status(200), |
$self->status(200), |
155 |
$self->content_type('text/xml; charset="utf-8"'), |
$self->content_type('text/xml; charset="utf-8"'), |
156 |
"Server: AcmeCWMP/42\r\n", |
"Server: AcmeCWMP/42\r\n", |
157 |
"SOAPServer: AcmeCWMP/42\r\n" |
"SOAPServer: AcmeCWMP/42\r\n" |
158 |
); |
); |
159 |
|
|
160 |
$sock->print( "Set-Cookie: ID=" , $state->{ID}, "; path=/\r\n" ) if ( $state->{ID} ); |
$sock->send( "Set-Cookie: ID=" , $state->{ID}, "; path=/\r\n" ) if ( $state->{ID} ); |
161 |
|
|
162 |
my $xml = ''; |
my $xml = ''; |
163 |
|
|
173 |
warn ">>> empty response\n"; |
warn ">>> empty response\n"; |
174 |
} |
} |
175 |
|
|
176 |
$sock->print( "Content-length: ", length( $xml ), "\r\n\r\n" ); |
$sock->send( "Content-length: ", length( $xml ), "\r\n\r\n" ); |
177 |
$sock->print( $xml ) or die "can't send response"; |
$sock->send( "$xml\r\n" ) or die "can't send response"; |
178 |
|
|
179 |
warn "### request over"; |
warn "### request over"; |
180 |
|
|
184 |
sub read_headers { |
sub read_headers { |
185 |
my $self = shift; |
my $self = shift; |
186 |
|
|
|
my $sock = shift || die "no sock?"; |
|
|
|
|
187 |
$self->{headers} = {}; |
$self->{headers} = {}; |
188 |
|
|
189 |
while (defined($_ = $sock->getline)) { |
while (defined($_ = $self->sock->getline)) { |
190 |
s/[\r\n]+$//; |
s/[\r\n]+$//; |
191 |
last unless length $_; |
last unless length $_; |
192 |
warn "-- $_\n"; |
warn "-- $_\n"; |
215 |
|
|
216 |
sub error{ |
sub error{ |
217 |
my ($self, $number, $msg) = @_; |
my ($self, $number, $msg) = @_; |
218 |
$self->sock->print( $self->status($number, $msg), "\r\n" ); |
$self->sock->send( $self->status($number, $msg), "\r\n" ); |
219 |
warn "Error - $number - $msg\n"; |
warn "Error - $number - $msg\n"; |
220 |
} |
} |
221 |
|
|
230 |
my $self = shift; |
my $self = shift; |
231 |
my $number = shift || 200; |
my $number = shift || 200; |
232 |
return if ! delete $self->{needs_header}; |
return if ! delete $self->{needs_header}; |
233 |
$self->sock->Print("HTTP/1.0 $number\r\n"); |
$self->sock->send("HTTP/1.0 $number\r\n"); |
234 |
return 1; |
return 1; |
235 |
} |
} |
236 |
|
|