1 |
# Dobrica Pavlinusic, <dpavlin@rot13.org> 06/18/07 10:19:50 CEST |
# Dobrica Pavlinusic, <dpavlin@rot13.org> 06/18/07 10:19:50 CEST |
2 |
package CWMP::Server; |
package CWMP::Session; |
3 |
|
|
4 |
use strict; |
use strict; |
5 |
use warnings; |
use warnings; |
6 |
|
|
7 |
use base qw/HTTP::Server::Simple::CGI Class::Accessor/; |
use base qw/Class::Accessor/; |
8 |
__PACKAGE__->mk_accessors( qw/ |
__PACKAGE__->mk_accessors( qw/ |
9 |
debug |
debug |
10 |
|
store_path |
11 |
|
|
12 |
|
sock |
13 |
|
state |
14 |
|
queue |
15 |
|
store |
16 |
/ ); |
/ ); |
17 |
|
|
18 |
|
use HTTP::Daemon; |
19 |
use Data::Dump qw/dump/; |
use Data::Dump qw/dump/; |
20 |
|
use Carp qw/confess cluck croak/; |
21 |
|
use File::Slurp; |
22 |
|
|
23 |
|
use CWMP::Request; |
24 |
|
use CWMP::Response; |
25 |
|
use CWMP::Store; |
26 |
|
|
27 |
=head1 NAME |
=head1 NAME |
28 |
|
|
29 |
CWMP::Server - implement logic of CWMP protocol |
CWMP::Session - implement logic of CWMP protocol |
30 |
|
|
31 |
=head1 METHODS |
=head1 METHODS |
32 |
|
|
33 |
=head2 handler |
=head2 new |
34 |
|
|
35 |
We override L<HTTP::Server::Simple::CGI/handler> so that we can support |
my $server = CWMP::Session->new({ |
36 |
chunked transfer encoding. |
sock => $io_socket_object, |
37 |
|
store_path => 'state.db', |
38 |
|
queue => [ qw/GetRPCMethods GetParameterNames/ ], |
39 |
|
debug => 1, |
40 |
|
}); |
41 |
|
|
42 |
=cut |
=cut |
43 |
|
|
44 |
sub handler { |
sub new { |
45 |
my $self = shift; |
my $class = shift; |
46 |
|
my $self = $class->SUPER::new( @_ ); |
47 |
|
|
48 |
|
confess "need sock" unless $self->sock; |
49 |
|
|
50 |
|
$self->debug( 0 ) unless $self->debug; |
51 |
|
|
52 |
|
warn "created ", __PACKAGE__, "(", dump( @_ ), ") for ", $self->sock->peerhost, "\n" if $self->debug; |
53 |
|
|
54 |
|
$self->store( CWMP::Store->new({ |
55 |
|
debug => $self->debug, |
56 |
|
path => $self->store_path, |
57 |
|
}) ); |
58 |
|
|
59 |
my $chunk; |
croak "can't open ", $self->store_path, ": $!" unless $self->store; |
60 |
|
|
61 |
my $transfer_encoding = $self->header('Transfer-Encoding'); |
return $self; |
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 |
|
If used with debugging level of 3 or more, it will also create dumps of |
71 |
|
requests named C<< dump/nr.request >> where C<nr> is number from 0 to total number |
72 |
|
of requests in single session. |
73 |
|
|
74 |
if ( $transfer_encoding && $transfer_encoding =~ qr/^chunked/i ) { |
=cut |
75 |
|
|
76 |
|
my $dump_nr = 0; |
77 |
|
|
78 |
my $len = 0; |
sub process_request { |
79 |
do { |
my $self = shift; |
|
$len = <STDIN>; |
|
|
$len =~ s/[\n\r]*$//s; |
|
|
$len = hex( $len ); |
|
80 |
|
|
81 |
#warn "getting chunk of $len bytes\n"; |
my $sock = $self->sock || die "no sock?"; |
82 |
|
|
83 |
while( $len > 0 ) { |
# die "not IO::Socket::INET but ", ref( $sock ) unless ( ref($sock) eq 'Net::Server::Proto::TCP' ); |
|
my $line = <STDIN>; |
|
|
$chunk .= $line; |
|
|
$len -= length( $line ); |
|
|
} |
|
84 |
|
|
85 |
} while ( $len > 0 ); |
if ( ! $sock->connected ) { |
86 |
|
warn "SOCKET NOT CONNECTED\n"; |
87 |
|
return 0; |
88 |
} |
} |
89 |
|
|
90 |
warn "got ", length($chunk), " bytes\n" if $self->debug; |
bless $sock, 'HTTP::Daemon::ClientConn'; |
91 |
|
|
92 |
my $cgi = new CGI( $chunk ); |
# why do I have to do this? |
93 |
|
# solution from http://use.perl.org/~Matts/journal/12896 |
94 |
|
${*$sock}{'httpd_daemon'} = HTTP::Daemon->new; |
95 |
|
|
96 |
eval { $self->handle_request($cgi) }; |
my $r = $sock->get_request || confess "can't get_request"; |
|
if ($@) { |
|
|
my $error = $@; |
|
|
warn $error; |
|
|
} |
|
|
} |
|
97 |
|
|
98 |
=head2 handle_request |
my $chunk = $r->content; |
99 |
|
|
100 |
Implementation of dispatch logic |
my $size = length( $chunk ); |
101 |
|
|
102 |
=cut |
warn "<<<< ", $sock->peerhost, " [" . localtime() . "] ", $r->method, " ", $r->uri, " $size bytes\n"; |
103 |
|
|
104 |
sub handle_request { |
if ( $self->debug > 2 ) { |
105 |
my ($self, $cgi) = @_; |
my $file = sprintf("dump/%04d.request", $dump_nr); |
106 |
|
write_file( $file, $r->as_string ); |
107 |
#... do something, print output to default |
warn "### request dump: $file\n"; |
108 |
# selected filehandle... |
} |
109 |
|
|
110 |
warn ">> " . localtime() . " " . $ENV{REMOTE_ADDR} . "\n"; |
my $state; |
111 |
|
|
112 |
warn "not SOAP request" unless defined ( $cgi->header('SOAPAction') ); |
if ( $size > 0 ) { |
113 |
|
|
114 |
if ( my $payload = $cgi->param('POSTDATA') ) { |
die "no SOAPAction header in ",dump($chunk) unless defined ( $r->header('SOAPAction') ); |
|
warn "request payload:\n$payload\n" if $self->debug; |
|
115 |
|
|
|
my $state = CWMP::Request->parse( $payload ); |
|
116 |
|
|
117 |
warn "acquired state = ", dump( $state ); |
if ( $chunk ) { |
118 |
|
warn "## request chunk: ",length($chunk)," bytes\n$chunk\n" if $self->debug; |
119 |
|
|
120 |
|
$state = CWMP::Request->parse( $chunk ); |
121 |
|
|
122 |
|
warn "## acquired state = ", dump( $state ), "\n"; |
123 |
|
|
124 |
|
$self->state( $state ); |
125 |
|
$self->store->update_state( ID => $state->{ID}, $state ); |
126 |
|
|
127 |
|
} else { |
128 |
|
warn "## empty request\n"; |
129 |
|
} |
130 |
|
|
131 |
|
} else { |
132 |
|
$state = $self->state; |
133 |
|
warn "last request state = ", dump( $state ), "\n" if $self->debug > 1; |
134 |
} |
} |
135 |
|
|
|
my $response = CWMP::Server->new({ debug => $self->debug }); |
|
136 |
|
|
137 |
print "Content-Type: text/xml\r\n\r\n"; |
$sock->send(join("\r\n", |
138 |
|
'HTTP/1.1 200 OK', |
139 |
|
'Content-Type: text/xml; charset="utf-8"', |
140 |
|
'Server: AcmeCWMP/42', |
141 |
|
'SOAPServer: AcmeCWMP/42' |
142 |
|
)."\r\n"); |
143 |
|
|
144 |
print $response->Inform; |
$sock->send( "Set-Cookie: ID=" . $state->{ID} . "; path=/\r\n" ) if ( $state->{ID} ); |
145 |
|
|
146 |
|
my $xml = ''; |
147 |
|
|
148 |
|
if ( my $dispatch = $state->{_dispatch} ) { |
149 |
|
$xml = $self->dispatch( $dispatch ); |
150 |
|
} elsif ( $dispatch = shift @{ $self->queue } ) { |
151 |
|
$xml = $self->dispatch( $dispatch ); |
152 |
|
} elsif ( $size == 0 ) { |
153 |
|
warn ">>> closing connection\n"; |
154 |
|
return 0; |
155 |
|
} else { |
156 |
|
warn ">>> empty response\n"; |
157 |
|
$state->{NoMoreRequests} = 1; |
158 |
|
$xml = $self->dispatch( 'xml', sub {} ); |
159 |
|
} |
160 |
|
|
161 |
|
$sock->send( "Content-Length: " . length( $xml ) . "\r\n\r\n" ); |
162 |
|
$sock->send( $xml ) or die "can't send response"; |
163 |
|
|
164 |
|
warn ">>>> " . $sock->peerhost . " [" . localtime() . "] sent ", length( $xml )," bytes\n"; |
165 |
|
|
166 |
|
warn "### request over\n" if $self->debug; |
167 |
|
|
168 |
|
return 1; # next request |
169 |
}; |
}; |
170 |
|
|
171 |
1; |
=head2 dispatch |
172 |
|
|
173 |
|
$xml = $self->dispatch('Inform', $response_arguments ); |
174 |
|
|
175 |
|
If debugging level of 3 or more, it will create dumps of responses named C<< dump/nr.response >> |
176 |
|
|
177 |
|
=cut |
178 |
|
|
179 |
|
sub dispatch { |
180 |
|
my $self = shift; |
181 |
|
|
182 |
|
my $dispatch = shift || die "no dispatch?"; |
183 |
|
|
184 |
|
my $response = CWMP::Response->new({ debug => $self->debug }); |
185 |
|
|
186 |
|
if ( $response->can( $dispatch ) ) { |
187 |
|
warn ">>> dispatching to $dispatch\n"; |
188 |
|
my $xml = $response->$dispatch( $self->state, @_ ); |
189 |
|
warn "## response payload: ",length($xml)," bytes\n$xml\n" if $self->debug; |
190 |
|
if ( $self->debug > 2 ) { |
191 |
|
my $file = sprintf("dump/%04d.response", $dump_nr++); |
192 |
|
write_file( $file, $xml ); |
193 |
|
warn "### response dump: $file\n"; |
194 |
|
} |
195 |
|
return $xml; |
196 |
|
} else { |
197 |
|
confess "can't dispatch to $dispatch"; |
198 |
|
} |
199 |
|
}; |
200 |
|
|
201 |
|
|
202 |
|
=head2 error |
203 |
|
|
204 |
|
return $self->error( 501, 'System error' ); |
205 |
|
|
206 |
|
=cut |
207 |
|
|
208 |
|
sub error { |
209 |
|
my ($self, $number, $msg) = @_; |
210 |
|
$msg ||= 'ERROR'; |
211 |
|
$self->sock->send( "HTTP/1.1 $number $msg\r\n" ); |
212 |
|
warn "Error - $number - $msg\n"; |
213 |
|
return 0; # close connection |
214 |
|
} |
215 |
|
|
216 |
|
1; |