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 |
|
=head2 process_request |
65 |
|
|
66 |
|
One request from client/response from server cycle. Call multiple times to |
67 |
|
facilitate brain-dead concept of adding state to stateless protocol like |
68 |
|
HTTP. |
69 |
|
|
70 |
|
=cut |
71 |
|
|
72 |
sub process_request { |
sub process_request { |
73 |
my $self = shift; |
my $self = shift; |
74 |
|
|
75 |
my $sock = shift || die "no sock?"; |
my $sock = $self->sock || die "no sock?"; |
76 |
|
|
77 |
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' ); |
78 |
|
|
79 |
|
if ( ! $sock->connected ) { |
80 |
|
warn "SOCKET NOT CONNECTED"; |
81 |
|
return 0; |
82 |
|
} |
83 |
|
|
84 |
$sock->autoflush( 1 ); |
$sock->autoflush( 1 ); |
85 |
$sock->blocking( 1 ); |
$sock->blocking( 1 ); |
86 |
|
|
87 |
### read the first line of response |
### read the first line of response |
88 |
my $line = $sock->getline; # || $self->error(400, "No Data"); |
my $line = $sock->getline || return $self->error(400, "No Data"); |
89 |
|
|
90 |
$line =~ s/[\r\n]+$//; |
$line =~ s/[\r\n]+$//; |
91 |
if ($line !~ /^ (\w+) \ + (\S+) \ + (HTTP\/1.\d) $ /x) { |
if ($line !~ /^ (\w+) \ + (\S+) \ + (HTTP\/1.\d) $ /x) { |
95 |
warn "<<<< ",join(" ", time, $method, $req)."\n"; |
warn "<<<< ",join(" ", time, $method, $req)."\n"; |
96 |
|
|
97 |
### read in other headers |
### read in other headers |
98 |
$self->read_headers($sock) || return $self->error(400, "Strange headers"); |
$self->read_headers || return $self->error(400, "Strange headers"); |
99 |
|
|
100 |
### do we support the type |
### do we support the type |
101 |
# if ($method !~ /GET|POST|HEAD/) { |
# if ($method !~ /GET|POST|HEAD/) { |
158 |
|
|
159 |
my $response = CWMP::Response->new({ debug => $self->debug }); |
my $response = CWMP::Response->new({ debug => $self->debug }); |
160 |
|
|
161 |
$sock->print( |
$sock->send(join("\r\n", |
162 |
$self->status(200), |
'HTTP/1.1 200 OK', |
163 |
$self->content_type('text/xml; charset="utf-8"'), |
'Content-Type: text/xml; charset="utf-8"', |
164 |
"Server: AcmeCWMP/42\r\n", |
'Server: AcmeCWMP/42', |
165 |
"SOAPServer: AcmeCWMP/42\r\n" |
'SOAPServer: AcmeCWMP/42', |
166 |
); |
)); |
167 |
|
|
168 |
$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} ); |
169 |
|
|
170 |
my $xml = ''; |
my $xml = ''; |
171 |
|
|
181 |
warn ">>> empty response\n"; |
warn ">>> empty response\n"; |
182 |
} |
} |
183 |
|
|
184 |
$sock->print( "Content-length: ", length( $xml ), "\r\n\r\n" ); |
$sock->send( "Content-Length: " . length( $xml ) . "\r\n\r\n" ); |
185 |
$sock->print( $xml ) or die "can't send response"; |
$sock->send( $xml ) or die "can't send response"; |
186 |
|
|
187 |
warn "### request over"; |
warn "### request over"; |
188 |
|
|
189 |
}; |
}; |
190 |
|
|
191 |
|
=head2 read_headers |
192 |
|
|
193 |
|
parse headers from request |
194 |
|
|
195 |
|
=cut |
196 |
|
|
197 |
sub read_headers { |
sub read_headers { |
198 |
my $self = shift; |
my $self = shift; |
199 |
|
|
|
my $sock = shift || die "no sock?"; |
|
|
|
|
200 |
$self->{headers} = {}; |
$self->{headers} = {}; |
201 |
|
|
202 |
while (defined($_ = $sock->getline)) { |
while (defined($_ = $self->sock->getline)) { |
203 |
s/[\r\n]+$//; |
s/[\r\n]+$//; |
204 |
last unless length $_; |
last unless length $_; |
205 |
warn "-- $_\n"; |
warn "-- $_\n"; |
210 |
return 1; |
return 1; |
211 |
} |
} |
212 |
|
|
213 |
|
=head2 header |
214 |
|
|
215 |
|
Getter for specific header |
216 |
|
|
217 |
|
$self->header('Cookies'); |
218 |
|
|
219 |
|
=cut |
220 |
|
|
221 |
sub header { |
sub header { |
222 |
my $self = shift; |
my $self = shift; |
223 |
my $header = shift || die "no header?"; |
my $header = shift || die "no header?"; |
228 |
} |
} |
229 |
} |
} |
230 |
|
|
231 |
sub content_type { |
=head2 error |
|
my ($self, $type) = @_; |
|
|
$self->http_header; |
|
|
return "Content-type: $type\r\n"; |
|
|
} |
|
232 |
|
|
233 |
sub error{ |
return $self->error( 501, 'System error' ); |
|
my ($self, $number, $msg) = @_; |
|
|
$self->sock->print( $self->status($number, $msg), "\r\n" ); |
|
|
warn "Error - $number - $msg\n"; |
|
|
} |
|
234 |
|
|
235 |
sub status { |
=cut |
|
my ($self, $number, $msg) = @_; |
|
|
$msg = '' if ! defined $msg; |
|
|
return if $self->http_header($number); |
|
|
return "Status $number: $msg\r\n"; |
|
|
} |
|
236 |
|
|
237 |
sub http_header { |
sub error { |
238 |
my $self = shift; |
my ($self, $number, $msg) = @_; |
239 |
my $number = shift || 200; |
$msg ||= 'ERROR'; |
240 |
return if ! delete $self->{needs_header}; |
$self->sock->send( "HTTP/1.1 $number $msg\r\n" ); |
241 |
$self->sock->Print("HTTP/1.0 $number\r\n"); |
warn "Error - $number - $msg\n"; |
242 |
return 1; |
return 0; # close connection |
243 |
} |
} |
244 |
|
|
245 |
1; |
1; |