1 |
# Dobrica Pavlinusic, <dpavlin@rot13.org> 06/18/07 10:19:50 CEST |
2 |
package CWMP::Session; |
3 |
|
4 |
use strict; |
5 |
use warnings; |
6 |
|
7 |
use base qw/Class::Accessor/; |
8 |
__PACKAGE__->mk_accessors( qw/ |
9 |
debug |
10 |
store |
11 |
|
12 |
sock |
13 |
state |
14 |
queue |
15 |
store |
16 |
/ ); |
17 |
|
18 |
use HTTP::Daemon; |
19 |
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 |
28 |
|
29 |
CWMP::Session - implement logic of CWMP protocol |
30 |
|
31 |
=head1 METHODS |
32 |
|
33 |
=head2 new |
34 |
|
35 |
my $server = CWMP::Session->new({ |
36 |
sock => $io_socket_object, |
37 |
store => 'state.db', |
38 |
queue => [ qw/GetRPCMethods GetParameterNames/ ], |
39 |
debug => 1, |
40 |
}); |
41 |
|
42 |
=cut |
43 |
|
44 |
sub new { |
45 |
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 |
my $store_obj = CWMP::Store->new({ |
55 |
debug => $self->debug, |
56 |
%{ $self->store }, |
57 |
}); |
58 |
|
59 |
croak "can't open ", dump( $self->store ), ": $!" unless $store_obj; |
60 |
|
61 |
# FIXME looks ugly. Should we have separate accessor for this? |
62 |
$self->store( $store_obj ); |
63 |
|
64 |
return $self; |
65 |
} |
66 |
|
67 |
=head2 process_request |
68 |
|
69 |
One request from client/response from server cycle. Call multiple times to |
70 |
facilitate brain-dead concept of adding state to stateless protocol like |
71 |
HTTP. |
72 |
|
73 |
If used with debugging level of 3 or more, it will also create dumps of |
74 |
requests named C<< dump/nr.request >> where C<nr> is number from 0 to total number |
75 |
of requests in single session. |
76 |
|
77 |
=cut |
78 |
|
79 |
my $dump_nr = 0; |
80 |
|
81 |
sub process_request { |
82 |
my $self = shift; |
83 |
|
84 |
my $sock = $self->sock || die "no sock?"; |
85 |
|
86 |
# die "not IO::Socket::INET but ", ref( $sock ) unless ( ref($sock) eq 'Net::Server::Proto::TCP' ); |
87 |
|
88 |
if ( ! $sock->connected ) { |
89 |
warn "SOCKET NOT CONNECTED\n"; |
90 |
return 0; |
91 |
} |
92 |
|
93 |
bless $sock, 'HTTP::Daemon::ClientConn'; |
94 |
|
95 |
# why do I have to do this? |
96 |
# solution from http://use.perl.org/~Matts/journal/12896 |
97 |
${*$sock}{'httpd_daemon'} = HTTP::Daemon->new; |
98 |
|
99 |
my $r = $sock->get_request || confess "can't get_request"; |
100 |
|
101 |
my $chunk = $r->content; |
102 |
|
103 |
my $size = length( $chunk ); |
104 |
|
105 |
warn "<<<< ", $sock->peerhost, " [" . localtime() . "] ", $r->method, " ", $r->uri, " $size bytes\n"; |
106 |
|
107 |
if ( $self->debug > 2 ) { |
108 |
my $file = sprintf("dump/%04d-%s.request", $dump_nr++, $sock->peerhost); |
109 |
write_file( $file, $r->as_string ); |
110 |
warn "### request dump: $file\n"; |
111 |
} |
112 |
|
113 |
my $state; |
114 |
|
115 |
if ( $size > 0 ) { |
116 |
|
117 |
die "no SOAPAction header in ",dump($chunk) unless defined ( $r->header('SOAPAction') ); |
118 |
|
119 |
$state = CWMP::Request->parse( $chunk ); |
120 |
|
121 |
warn "## acquired state = ", dump( $state ), "\n"; |
122 |
|
123 |
$self->state( $state ); |
124 |
$self->store->update_state( ID => $state->{ID}, $state ); |
125 |
|
126 |
} else { |
127 |
|
128 |
warn "## empty request, using last request state\n"; |
129 |
|
130 |
$state = $self->state; |
131 |
delete( $state->{_dispatch} ); |
132 |
#warn "last request state = ", dump( $state ), "\n" if $self->debug > 1; |
133 |
} |
134 |
|
135 |
|
136 |
$sock->send(join("\r\n", |
137 |
'HTTP/1.1 200 OK', |
138 |
'Content-Type: text/xml; charset="utf-8"', |
139 |
'Server: AcmeCWMP/42', |
140 |
'SOAPServer: AcmeCWMP/42' |
141 |
)."\r\n"); |
142 |
|
143 |
$sock->send( "Set-Cookie: ID=" . $state->{ID} . "; path=/\r\n" ) if ( $state->{ID} ); |
144 |
|
145 |
my $xml = ''; |
146 |
|
147 |
if ( my $dispatch = $state->{_dispatch} ) { |
148 |
$xml = $self->dispatch( $dispatch ); |
149 |
} elsif ( $dispatch = shift @{ $self->queue } ) { |
150 |
$xml = $self->dispatch( $dispatch ); |
151 |
} elsif ( $size == 0 ) { |
152 |
warn ">>> no more queued commands, closing connection\n"; |
153 |
return 0; |
154 |
} else { |
155 |
warn ">>> empty response\n"; |
156 |
$state->{NoMoreRequests} = 1; |
157 |
$xml = $self->dispatch( 'xml', sub {} ); |
158 |
} |
159 |
|
160 |
$sock->send( "Content-Length: " . length( $xml ) . "\r\n\r\n" ); |
161 |
$sock->send( $xml ) or die "can't send response"; |
162 |
|
163 |
warn ">>>> " . $sock->peerhost . " [" . localtime() . "] sent ", length( $xml )," bytes\n"; |
164 |
|
165 |
warn "### request over\n" if $self->debug; |
166 |
|
167 |
return 1; # next request |
168 |
}; |
169 |
|
170 |
=head2 dispatch |
171 |
|
172 |
$xml = $self->dispatch('Inform', $response_arguments ); |
173 |
|
174 |
If debugging level of 3 or more, it will create dumps of responses named C<< dump/nr.response >> |
175 |
|
176 |
=cut |
177 |
|
178 |
sub dispatch { |
179 |
my $self = shift; |
180 |
|
181 |
my $dispatch = shift || die "no dispatch?"; |
182 |
|
183 |
my $response = CWMP::Response->new({ debug => $self->debug }); |
184 |
|
185 |
if ( $response->can( $dispatch ) ) { |
186 |
warn ">>> dispatching to $dispatch\n"; |
187 |
my $xml = $response->$dispatch( $self->state, @_ ); |
188 |
warn "## response payload: ",length($xml)," bytes\n$xml\n" if $self->debug; |
189 |
if ( $self->debug > 2 ) { |
190 |
my $file = sprintf("dump/%04d-%s.response", $dump_nr++, $self->sock->peerhost); |
191 |
write_file( $file, $xml ); |
192 |
warn "### response dump: $file\n"; |
193 |
} |
194 |
return $xml; |
195 |
} else { |
196 |
confess "can't dispatch to $dispatch"; |
197 |
} |
198 |
}; |
199 |
|
200 |
|
201 |
=head2 error |
202 |
|
203 |
return $self->error( 501, 'System error' ); |
204 |
|
205 |
=cut |
206 |
|
207 |
sub error { |
208 |
my ($self, $number, $msg) = @_; |
209 |
$msg ||= 'ERROR'; |
210 |
$self->sock->send( "HTTP/1.1 $number $msg\r\n" ); |
211 |
warn "Error - $number - $msg\n"; |
212 |
return 0; # close connection |
213 |
} |
214 |
|
215 |
1; |