10 |
port |
port |
11 |
sock |
sock |
12 |
state |
state |
13 |
|
queue |
14 |
/ ); |
/ ); |
15 |
|
|
16 |
use IO::Socket::INET; |
use IO::Socket::INET; |
47 |
ReuseAddr => 1, |
ReuseAddr => 1, |
48 |
); |
); |
49 |
|
|
50 |
warn "waiting for request on port ", $self->port, $/; |
warn "ACS waiting for request on port ", $self->port, |
51 |
|
$self->queue ? " queue ( " . join(",",@{$self->queue}) . " )" : "", |
52 |
|
"\n"; |
53 |
|
|
54 |
|
warn "## debug level: ", $self->debug, "\n" if $self->debug; |
55 |
|
|
56 |
while ( my $sock = $listen->accept ) { |
while ( my $sock = $listen->accept ) { |
57 |
$sock->autoflush(1); |
$sock->autoflush(1); |
58 |
|
|
59 |
warn "connection from ", $sock->peerhost, "\n"; |
warn "connection from ", $sock->peerhost, "\n" if $self->debug; |
60 |
|
|
61 |
$self->sock( $sock ); # FIXME this will not work for multiple clients |
$self->sock( $sock ); # FIXME this will not work for multiple clients |
62 |
while ( $self->process_request ) { |
while ( $self->process_request ) { |
63 |
warn "...another one bites a dust...\n"; |
warn "...another one bites the dust...\n"; |
64 |
} |
} |
65 |
|
|
66 |
warn "...returning to accepting new connections\n"; |
warn "...returning to accepting new connections\n"; |
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 'IO::Socket::INET' ); |
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 |
|
|
120 |
|
|
121 |
do { |
do { |
122 |
|
|
123 |
warn "get chunk len\n" if $self->debug; |
warn "get chunk len\n" if $self->debug > 1; |
124 |
|
|
125 |
my $hex; |
my $hex; |
126 |
do { |
do { |
131 |
die "chunk size not valid hex: $hex" unless ( $hex =~ m/^[0-9a-f]+$/i); |
die "chunk size not valid hex: $hex" unless ( $hex =~ m/^[0-9a-f]+$/i); |
132 |
$len = hex( $hex ); |
$len = hex( $hex ); |
133 |
|
|
134 |
warn "getting chunk of $len bytes\n" if $self->debug; |
warn "getting chunk of $len bytes\n" if $self->debug > 1; |
135 |
|
|
136 |
$sock->read( my $buff, $len ); |
$sock->read( my $buff, $len ); |
137 |
$chunk .= $buff; |
$chunk .= $buff; |
138 |
|
|
139 |
warn "--- $len bytes: --=>||$buff||<=--\n"; |
warn "--- $len bytes: --=>||$buff||<=--\n" if $self->debug > 1; |
140 |
|
|
141 |
} while ( $len > 0 ); |
} while ( $len > 0 ); |
142 |
my $sep = $sock->getline; |
my $sep = $sock->getline; |
172 |
|
|
173 |
} else { |
} else { |
174 |
$state = $self->state; |
$state = $self->state; |
175 |
warn "last request state = ", dump( $state ), "\n"; |
warn "last request state = ", dump( $state ), "\n" if $self->debug > 1; |
176 |
} |
} |
177 |
|
|
|
my $response = CWMP::Response->new({ debug => $self->debug }); |
|
178 |
|
|
179 |
$sock->send(join("\r\n", |
$sock->send(join("\r\n", |
180 |
'HTTP/1.1 200 OK', |
'HTTP/1.1 200 OK', |
181 |
'Content-Type: text/xml; charset="utf-8"', |
'Content-Type: text/xml; charset="utf-8"', |
182 |
'Server: AcmeCWMP/42', |
'Server: AcmeCWMP/42', |
183 |
'SOAPServer: AcmeCWMP/42' |
'SOAPServer: AcmeCWMP/42' |
184 |
)); |
)."\r\n"); |
185 |
|
|
186 |
$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} ); |
187 |
|
|
188 |
my $xml = ''; |
my $xml = ''; |
189 |
|
|
190 |
if ( my $dispatch = $state->{_dispatch} ) { |
if ( my $dispatch = $state->{_dispatch} ) { |
191 |
if ( $response->can( $dispatch ) ) { |
$xml = $self->dispatch( $dispatch ); |
192 |
warn ">>> dispatching to $dispatch\n"; |
} elsif ( $dispatch = shift @{ $self->queue } ) { |
193 |
$xml = $response->$dispatch( $state ) . "\r\n"; |
$xml = $self->dispatch( $dispatch ); |
194 |
warn "## response payload: ",length($xml)," bytes\n$xml\n"; |
} elsif ( $size == 0 ) { |
195 |
} else { |
warn ">>> closing connection\n"; |
196 |
confess "can't dispatch to $dispatch"; |
return 0; |
|
} |
|
197 |
} else { |
} else { |
198 |
warn ">>> empty response\n"; |
warn ">>> empty response\n"; |
199 |
|
$state->{NoMoreRequests} = 1; |
200 |
|
$xml = $self->dispatch( 'xml', sub {} ); |
201 |
} |
} |
202 |
|
|
203 |
$sock->send( "Content-Length: " . length( $xml ) . "\r\n\r\n" ); |
$sock->send( "Content-Length: " . length( $xml ) . "\r\n\r\n" ); |
207 |
|
|
208 |
}; |
}; |
209 |
|
|
210 |
|
=head2 dispatch |
211 |
|
|
212 |
|
$xml = $self->dispatch('Inform', $response_arguments ); |
213 |
|
|
214 |
|
=cut |
215 |
|
|
216 |
|
sub dispatch { |
217 |
|
my $self = shift; |
218 |
|
|
219 |
|
my $dispatch = shift || die "no dispatch?"; |
220 |
|
|
221 |
|
my $response = CWMP::Response->new({ debug => $self->debug }); |
222 |
|
|
223 |
|
if ( $response->can( $dispatch ) ) { |
224 |
|
warn ">>> dispatching to $dispatch\n"; |
225 |
|
my $xml = $response->$dispatch( $self->state, @_ ) . "\r\n"; |
226 |
|
warn "## response payload: ",length($xml)," bytes\n$xml\n" if $self->debug; |
227 |
|
return $xml; |
228 |
|
} else { |
229 |
|
confess "can't dispatch to $dispatch"; |
230 |
|
} |
231 |
|
}; |
232 |
|
|
233 |
=head2 read_headers |
=head2 read_headers |
234 |
|
|
235 |
parse headers from request |
parse headers from request |
244 |
while (defined($_ = $self->sock->getline)) { |
while (defined($_ = $self->sock->getline)) { |
245 |
s/[\r\n]+$//; |
s/[\r\n]+$//; |
246 |
last unless length $_; |
last unless length $_; |
247 |
warn "-- $_\n"; |
warn "-- $_\n" if $self->debug; |
248 |
return 0 if ! /^ ([\w\-]+) :[\ \t]* (.*) $/x; |
return 0 if ! /^ ([\w\-]+) :[\ \t]* (.*) $/x; |
249 |
$self->{headers}->{$1} = $2; |
$self->{headers}->{$1} = $2; |
250 |
} |
} |