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_path |
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 |
|
22 |
use CWMP::Request; |
23 |
use CWMP::Response; |
24 |
use CWMP::Store; |
25 |
|
26 |
=head1 NAME |
27 |
|
28 |
CWMP::Session - implement logic of CWMP protocol |
29 |
|
30 |
=head1 METHODS |
31 |
|
32 |
=head2 new |
33 |
|
34 |
my $server = CWMP::Session->new({ |
35 |
sock => $io_socket_object, |
36 |
store_path => 'state.db', |
37 |
queue => [ qw/GetRPCMethods GetParameterNames/ ], |
38 |
debug => 1, |
39 |
}); |
40 |
|
41 |
=cut |
42 |
|
43 |
sub new { |
44 |
my $class = shift; |
45 |
my $self = $class->SUPER::new( @_ ); |
46 |
|
47 |
confess "need sock" unless $self->sock; |
48 |
|
49 |
$self->debug( 0 ) unless $self->debug; |
50 |
|
51 |
warn "created ", __PACKAGE__, "(", dump( @_ ), ") for ", $self->sock->peerhost, "\n" if $self->debug; |
52 |
|
53 |
$self->store( CWMP::Store->new({ |
54 |
debug => $self->debug, |
55 |
path => $self->store_path, |
56 |
}) ); |
57 |
|
58 |
croak "can't open ", $self->store_path, ": $!" unless $self->store; |
59 |
|
60 |
return $self; |
61 |
} |
62 |
|
63 |
=head2 process_request |
64 |
|
65 |
One request from client/response from server cycle. Call multiple times to |
66 |
facilitate brain-dead concept of adding state to stateless protocol like |
67 |
HTTP. |
68 |
|
69 |
=cut |
70 |
|
71 |
sub process_request { |
72 |
my $self = shift; |
73 |
|
74 |
my $sock = $self->sock || die "no sock?"; |
75 |
|
76 |
# die "not IO::Socket::INET but ", ref( $sock ) unless ( ref($sock) eq 'Net::Server::Proto::TCP' ); |
77 |
|
78 |
if ( ! $sock->connected ) { |
79 |
warn "SOCKET NOT CONNECTED\n"; |
80 |
return 0; |
81 |
} |
82 |
|
83 |
bless $sock, 'HTTP::Daemon::ClientConn'; |
84 |
|
85 |
# why do I have to do this? |
86 |
# solution from http://use.perl.org/~Matts/journal/12896 |
87 |
${*$sock}{'httpd_daemon'} = HTTP::Daemon->new; |
88 |
|
89 |
my $r = $sock->get_request || confess "can't get_request"; |
90 |
|
91 |
warn "<<<< ", $sock->peerhost, " - - [" . localtime() . "] ", $r->method, " ", $r->uri, "\n"; |
92 |
|
93 |
my $chunk = $r->content; |
94 |
|
95 |
my $size = length( $chunk ); |
96 |
|
97 |
warn "<<<< " . $sock->peerhost . " [" . localtime() . "] request $size bytes\n"; |
98 |
|
99 |
my $state; |
100 |
|
101 |
if ( $size > 0 ) { |
102 |
|
103 |
die "no SOAPAction header in ",dump($chunk) unless defined ( $r->header('SOAPAction') ); |
104 |
|
105 |
|
106 |
if ( $chunk ) { |
107 |
warn "## request chunk: ",length($chunk)," bytes\n$chunk\n" if $self->debug; |
108 |
|
109 |
$state = CWMP::Request->parse( $chunk ); |
110 |
|
111 |
warn "## acquired state = ", dump( $state ), "\n"; |
112 |
|
113 |
$self->state( $state ); |
114 |
$self->store->update_state( ID => $state->{ID}, $state ); |
115 |
|
116 |
} else { |
117 |
warn "## empty request\n"; |
118 |
} |
119 |
|
120 |
} else { |
121 |
$state = $self->state; |
122 |
warn "last request state = ", dump( $state ), "\n" if $self->debug > 1; |
123 |
} |
124 |
|
125 |
|
126 |
$sock->send(join("\r\n", |
127 |
'HTTP/1.1 200 OK', |
128 |
'Content-Type: text/xml; charset="utf-8"', |
129 |
'Server: AcmeCWMP/42', |
130 |
'SOAPServer: AcmeCWMP/42' |
131 |
)."\r\n"); |
132 |
|
133 |
$sock->send( "Set-Cookie: ID=" . $state->{ID} . "; path=/\r\n" ) if ( $state->{ID} ); |
134 |
|
135 |
my $xml = ''; |
136 |
|
137 |
if ( my $dispatch = $state->{_dispatch} ) { |
138 |
$xml = $self->dispatch( $dispatch ); |
139 |
} elsif ( $dispatch = shift @{ $self->queue } ) { |
140 |
$xml = $self->dispatch( $dispatch ); |
141 |
} elsif ( $size == 0 ) { |
142 |
warn ">>> closing connection\n"; |
143 |
return 0; |
144 |
} else { |
145 |
warn ">>> empty response\n"; |
146 |
$state->{NoMoreRequests} = 1; |
147 |
$xml = $self->dispatch( 'xml', sub {} ); |
148 |
} |
149 |
|
150 |
$sock->send( "Content-Length: " . length( $xml ) . "\r\n\r\n" ); |
151 |
$sock->send( $xml ) or die "can't send response"; |
152 |
|
153 |
warn ">>>> " . $sock->peerhost . " [" . localtime() . "] sent ", length( $xml )," bytes\n"; |
154 |
|
155 |
warn "### request over\n" if $self->debug; |
156 |
|
157 |
return 1; # next request |
158 |
}; |
159 |
|
160 |
=head2 dispatch |
161 |
|
162 |
$xml = $self->dispatch('Inform', $response_arguments ); |
163 |
|
164 |
=cut |
165 |
|
166 |
sub dispatch { |
167 |
my $self = shift; |
168 |
|
169 |
my $dispatch = shift || die "no dispatch?"; |
170 |
|
171 |
my $response = CWMP::Response->new({ debug => $self->debug }); |
172 |
|
173 |
if ( $response->can( $dispatch ) ) { |
174 |
warn ">>> dispatching to $dispatch\n"; |
175 |
my $xml = $response->$dispatch( $self->state, @_ ); |
176 |
warn "## response payload: ",length($xml)," bytes\n$xml\n" if $self->debug; |
177 |
return $xml; |
178 |
} else { |
179 |
confess "can't dispatch to $dispatch"; |
180 |
} |
181 |
}; |
182 |
|
183 |
|
184 |
=head2 error |
185 |
|
186 |
return $self->error( 501, 'System error' ); |
187 |
|
188 |
=cut |
189 |
|
190 |
sub error { |
191 |
my ($self, $number, $msg) = @_; |
192 |
$msg ||= 'ERROR'; |
193 |
$self->sock->send( "HTTP/1.1 $number $msg\r\n" ); |
194 |
warn "Error - $number - $msg\n"; |
195 |
return 0; # close connection |
196 |
} |
197 |
|
198 |
1; |