1 |
# Dobrica Pavlinusic, <dpavlin@rot13.org> 06/18/07 10:19:50 CEST |
2 |
package CWMP::Server; |
3 |
|
4 |
use strict; |
5 |
use warnings; |
6 |
|
7 |
use base qw/Class::Accessor/; |
8 |
__PACKAGE__->mk_accessors( qw/ |
9 |
debug |
10 |
port |
11 |
sock |
12 |
/ ); |
13 |
|
14 |
use IO::Socket::INET; |
15 |
use Data::Dump qw/dump/; |
16 |
use CWMP::Request; |
17 |
use CWMP::Response; |
18 |
use Carp qw/confess cluck/; |
19 |
|
20 |
=head1 NAME |
21 |
|
22 |
CWMP::Server - implement logic of CWMP protocol |
23 |
|
24 |
=head1 METHODS |
25 |
|
26 |
=head2 new |
27 |
|
28 |
my $server = CWMP::Server->new({ port => 3333 }); |
29 |
|
30 |
=head2 run |
31 |
|
32 |
$server->run(); |
33 |
|
34 |
=cut |
35 |
|
36 |
sub run { |
37 |
my $self = shift; |
38 |
|
39 |
my $listen = IO::Socket::INET->new( |
40 |
Listen => 5, |
41 |
# LocalAddr => 'localhost', |
42 |
LocalPort => $self->port, |
43 |
Proto => 'tcp', |
44 |
Blocking => 1, |
45 |
ReuseAddr => 1, |
46 |
); |
47 |
|
48 |
warn "waiting for request on port ", $self->port, $/; |
49 |
|
50 |
while ( my $sock = $listen->accept ) { |
51 |
$sock->autoflush(1); |
52 |
|
53 |
warn "connection from ", $sock->peerhost, "\n"; |
54 |
|
55 |
$self->sock( $sock ); # FIXME this will not work for multiple clients |
56 |
while ( $self->process_request ) { |
57 |
warn "...another one bites a dust...\n"; |
58 |
} |
59 |
|
60 |
warn "...returning to accepting new connections\n"; |
61 |
} |
62 |
} |
63 |
|
64 |
sub process_request { |
65 |
my $self = shift; |
66 |
|
67 |
my $sock = $self->sock || die "no sock?"; |
68 |
|
69 |
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 ); |
77 |
$sock->blocking( 1 ); |
78 |
|
79 |
### read the first line of response |
80 |
my $line = $sock->getline || $self->error(400, "No Data"); |
81 |
|
82 |
$line =~ s/[\r\n]+$//; |
83 |
if ($line !~ /^ (\w+) \ + (\S+) \ + (HTTP\/1.\d) $ /x) { |
84 |
return $self->error(400, "Bad request"); |
85 |
} |
86 |
my ($method, $req, $protocol) = ($1, $2, $3); |
87 |
warn "<<<< ",join(" ", time, $method, $req)."\n"; |
88 |
|
89 |
### read in other headers |
90 |
$self->read_headers || return $self->error(400, "Strange headers"); |
91 |
|
92 |
### do we support the type |
93 |
# if ($method !~ /GET|POST|HEAD/) { |
94 |
if ($method !~ /POST/) { |
95 |
return $self->error(400, "Unsupported Method"); |
96 |
} |
97 |
|
98 |
my $chunk; |
99 |
my $transfer_encoding = $self->header('Transfer-Encoding'); |
100 |
|
101 |
if ( $transfer_encoding && $transfer_encoding =~ qr/^chunked/i ) { |
102 |
|
103 |
my $len = 0; |
104 |
|
105 |
do { |
106 |
|
107 |
warn "get chunk len\n" if $self->debug; |
108 |
|
109 |
my $hex; |
110 |
do { |
111 |
$hex = $sock->getline; |
112 |
$hex =~ s/[\n\r]+$//; |
113 |
} until ( $hex ne '' ); |
114 |
|
115 |
die "chunk size not valid hex: $hex" unless ( $hex =~ m/^[0-9a-f]+$/i); |
116 |
$len = hex( $hex ); |
117 |
|
118 |
warn "getting chunk of $len bytes\n" if $self->debug; |
119 |
|
120 |
$sock->read( my $buff, $len ); |
121 |
$chunk .= $buff; |
122 |
|
123 |
warn "--- $len bytes: --=>||$buff||<=--\n"; |
124 |
|
125 |
} while ( $len > 0 ); |
126 |
|
127 |
} else { |
128 |
die "right now, we support only Transfer-Encoding: chunked"; |
129 |
} |
130 |
|
131 |
warn "handler got ", length($chunk), " bytes\n" if $self->debug; |
132 |
|
133 |
warn "<<< " . localtime() . " " . $sock->peerhost . "\n"; |
134 |
|
135 |
die "not SOAP request" unless defined ( $self->header('SOAPAction') ); |
136 |
|
137 |
my $state; |
138 |
|
139 |
if ( $chunk ) { |
140 |
warn "## request chunk: ",length($chunk)," bytes\n$chunk\n" if $self->debug; |
141 |
|
142 |
$state = CWMP::Request->parse( $chunk ); |
143 |
|
144 |
warn "acquired state = ", dump( $state ), "\n"; |
145 |
|
146 |
} else { |
147 |
warn "empty request\n"; |
148 |
} |
149 |
|
150 |
|
151 |
my $response = CWMP::Response->new({ debug => $self->debug }); |
152 |
|
153 |
$sock->send( |
154 |
$self->status(200), |
155 |
$self->content_type('text/xml; charset="utf-8"'), |
156 |
"Server: AcmeCWMP/42\r\n", |
157 |
"SOAPServer: AcmeCWMP/42\r\n" |
158 |
); |
159 |
|
160 |
$sock->send( "Set-Cookie: ID=" , $state->{ID}, "; path=/\r\n" ) if ( $state->{ID} ); |
161 |
|
162 |
my $xml = ''; |
163 |
|
164 |
if ( my $dispatch = $state->{_dispatch} ) { |
165 |
if ( $response->can( $dispatch ) ) { |
166 |
warn ">>> dispatching to $dispatch\n"; |
167 |
$xml = $response->$dispatch( $state ) . "\r\n"; |
168 |
warn "## response payload: ",length($xml)," bytes\n$xml\n"; |
169 |
} else { |
170 |
confess "can't dispatch to $dispatch"; |
171 |
} |
172 |
} else { |
173 |
warn ">>> empty response\n"; |
174 |
} |
175 |
|
176 |
$sock->send( "Content-length: ", length( $xml ), "\r\n\r\n" ); |
177 |
$sock->send( "$xml\r\n" ) or die "can't send response"; |
178 |
|
179 |
warn "### request over"; |
180 |
|
181 |
}; |
182 |
|
183 |
|
184 |
sub read_headers { |
185 |
my $self = shift; |
186 |
|
187 |
$self->{headers} = {}; |
188 |
|
189 |
while (defined($_ = $self->sock->getline)) { |
190 |
s/[\r\n]+$//; |
191 |
last unless length $_; |
192 |
warn "-- $_\n"; |
193 |
return 0 if ! /^ ([\w\-]+) :[\ \t]* (.*) $/x; |
194 |
$self->{headers}->{$1} = $2; |
195 |
} |
196 |
|
197 |
return 1; |
198 |
} |
199 |
|
200 |
sub header { |
201 |
my $self = shift; |
202 |
my $header = shift || die "no header?"; |
203 |
if ( defined( $self->{headers}->{$header} )) { |
204 |
return $self->{headers}->{$header}; |
205 |
} else { |
206 |
return; |
207 |
} |
208 |
} |
209 |
|
210 |
sub content_type { |
211 |
my ($self, $type) = @_; |
212 |
$self->http_header; |
213 |
return "Content-type: $type\r\n"; |
214 |
} |
215 |
|
216 |
sub error{ |
217 |
my ($self, $number, $msg) = @_; |
218 |
$self->sock->send( $self->status($number, $msg), "\r\n" ); |
219 |
warn "Error - $number - $msg\n"; |
220 |
} |
221 |
|
222 |
sub status { |
223 |
my ($self, $number, $msg) = @_; |
224 |
$msg = '' if ! defined $msg; |
225 |
return if $self->http_header($number); |
226 |
return "Status $number: $msg\r\n"; |
227 |
} |
228 |
|
229 |
sub http_header { |
230 |
my $self = shift; |
231 |
my $number = shift || 200; |
232 |
return if ! delete $self->{needs_header}; |
233 |
$self->sock->send("HTTP/1.0 $number\r\n"); |
234 |
return 1; |
235 |
} |
236 |
|
237 |
1; |