/[cwmp]/google/trunk/lib/CWMP/Session.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /google/trunk/lib/CWMP/Session.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 50 - (hide annotations)
Tue Jun 19 21:29:04 2007 UTC (16 years, 11 months ago) by dpavlin
Original Path: google/lib/CWMP/Server.pm
File size: 5767 byte(s)
* added queue to send commands to CPE
* implemented parsing of Fault messages from CPE
* correctly emit NoMoreRequests in SOAP header
* close connection (not verified against TR-069 standard yet)
1 dpavlin 30 # 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 dpavlin 40 use base qw/Class::Accessor/;
8 dpavlin 30 __PACKAGE__->mk_accessors( qw/
9     debug
10 dpavlin 40 port
11 dpavlin 41 sock
12 dpavlin 49 state
13 dpavlin 50 queue
14 dpavlin 30 / );
15    
16 dpavlin 40 use IO::Socket::INET;
17 dpavlin 30 use Data::Dump qw/dump/;
18 dpavlin 35 use CWMP::Request;
19     use CWMP::Response;
20 dpavlin 40 use Carp qw/confess cluck/;
21 dpavlin 30
22 dpavlin 34 =head1 NAME
23    
24     CWMP::Server - implement logic of CWMP protocol
25    
26     =head1 METHODS
27    
28 dpavlin 40 =head2 new
29 dpavlin 34
30 dpavlin 40 my $server = CWMP::Server->new({ port => 3333 });
31 dpavlin 34
32 dpavlin 40 =head2 run
33    
34     $server->run();
35    
36 dpavlin 34 =cut
37    
38 dpavlin 40 sub run {
39     my $self = shift;
40 dpavlin 30
41 dpavlin 40 my $listen = IO::Socket::INET->new(
42     Listen => 5,
43     # LocalAddr => 'localhost',
44     LocalPort => $self->port,
45     Proto => 'tcp',
46     Blocking => 1,
47     ReuseAddr => 1,
48     );
49    
50     warn "waiting for request on port ", $self->port, $/;
51    
52     while ( my $sock = $listen->accept ) {
53     $sock->autoflush(1);
54    
55     warn "connection from ", $sock->peerhost, "\n";
56    
57 dpavlin 41 $self->sock( $sock ); # FIXME this will not work for multiple clients
58 dpavlin 42 while ( $self->process_request ) {
59     warn "...another one bites a dust...\n";
60     }
61 dpavlin 40
62 dpavlin 42 warn "...returning to accepting new connections\n";
63 dpavlin 40 }
64     }
65    
66 dpavlin 48 =head2 process_request
67    
68     One request from client/response from server cycle. Call multiple times to
69     facilitate brain-dead concept of adding state to stateless protocol like
70     HTTP.
71    
72     =cut
73    
74 dpavlin 40 sub process_request {
75     my $self = shift;
76    
77 dpavlin 42 my $sock = $self->sock || die "no sock?";
78 dpavlin 40
79     die "not IO::Socket::INET but ", ref( $sock ) unless ( ref($sock) eq 'IO::Socket::INET' );
80    
81 dpavlin 42 if ( ! $sock->connected ) {
82     warn "SOCKET NOT CONNECTED";
83     return 0;
84     }
85    
86 dpavlin 40 $sock->autoflush( 1 );
87     $sock->blocking( 1 );
88    
89     ### read the first line of response
90 dpavlin 49 my $line = $sock->getline;
91     return $self->error(400, "No Data") unless ( defined $line );
92 dpavlin 40
93     $line =~ s/[\r\n]+$//;
94     if ($line !~ /^ (\w+) \ + (\S+) \ + (HTTP\/1.\d) $ /x) {
95 dpavlin 49 warn "ERROR: $line\n";
96 dpavlin 40 return $self->error(400, "Bad request");
97     }
98     my ($method, $req, $protocol) = ($1, $2, $3);
99 dpavlin 49 warn "<<<< ", $sock->peerhost, " - - [" . localtime() . "] \"$method $req $protocol\"\n";
100 dpavlin 40
101     ### read in other headers
102 dpavlin 42 $self->read_headers || return $self->error(400, "Strange headers");
103 dpavlin 40
104     ### do we support the type
105     # if ($method !~ /GET|POST|HEAD/) {
106     if ($method !~ /POST/) {
107     return $self->error(400, "Unsupported Method");
108     }
109    
110 dpavlin 30 my $chunk;
111     my $transfer_encoding = $self->header('Transfer-Encoding');
112    
113     if ( $transfer_encoding && $transfer_encoding =~ qr/^chunked/i ) {
114    
115 dpavlin 31 my $len = 0;
116 dpavlin 40
117 dpavlin 30 do {
118    
119 dpavlin 40 warn "get chunk len\n" if $self->debug;
120    
121     my $hex;
122     do {
123     $hex = $sock->getline;
124     $hex =~ s/[\n\r]+$//;
125     } until ( $hex ne '' );
126 dpavlin 30
127 dpavlin 40 die "chunk size not valid hex: $hex" unless ( $hex =~ m/^[0-9a-f]+$/i);
128     $len = hex( $hex );
129 dpavlin 30
130 dpavlin 40 warn "getting chunk of $len bytes\n" if $self->debug;
131 dpavlin 30
132 dpavlin 40 $sock->read( my $buff, $len );
133     $chunk .= $buff;
134 dpavlin 30
135 dpavlin 40 warn "--- $len bytes: --=>||$buff||<=--\n";
136 dpavlin 30
137 dpavlin 40 } while ( $len > 0 );
138 dpavlin 49 my $sep = $sock->getline;
139     die "expected separator, not ", dump( $sep ) if ( $sep !~ m/^[\n\r]+$/ );
140 dpavlin 30
141 dpavlin 40 } else {
142     die "right now, we support only Transfer-Encoding: chunked";
143     }
144 dpavlin 34
145 dpavlin 49 my $size = length( $chunk );
146 dpavlin 34
147 dpavlin 49 warn "<<< " . $sock->peerhost . " [" . localtime() . "] request $size bytes\n";
148 dpavlin 34
149 dpavlin 36 my $state;
150    
151 dpavlin 49 if ( $size > 0 ) {
152 dpavlin 30
153 dpavlin 49 die "no SOAPAction header in ",dump($chunk) unless defined ( $self->header('SOAPAction') );
154 dpavlin 34
155 dpavlin 49
156     if ( $chunk ) {
157     warn "## request chunk: ",length($chunk)," bytes\n$chunk\n" if $self->debug;
158    
159     $state = CWMP::Request->parse( $chunk );
160    
161     warn "acquired state = ", dump( $state ), "\n";
162    
163     $self->state( $state );
164    
165     } else {
166     warn "empty request\n";
167     }
168    
169 dpavlin 36 } else {
170 dpavlin 49 $state = $self->state;
171     warn "last request state = ", dump( $state ), "\n";
172 dpavlin 34 }
173    
174    
175 dpavlin 44 $sock->send(join("\r\n",
176     'HTTP/1.1 200 OK',
177     'Content-Type: text/xml; charset="utf-8"',
178     'Server: AcmeCWMP/42',
179 dpavlin 49 'SOAPServer: AcmeCWMP/42'
180 dpavlin 43 ));
181 dpavlin 30
182 dpavlin 43 $sock->send( "Set-Cookie: ID=" . $state->{ID} . "; path=/\r\n" ) if ( $state->{ID} );
183 dpavlin 40
184     my $xml = '';
185    
186 dpavlin 36 if ( my $dispatch = $state->{_dispatch} ) {
187 dpavlin 50 $xml = $self->dispatch( $dispatch );
188     } elsif ( $dispatch = shift @{ $self->queue } ) {
189     $xml = $self->dispatch( $dispatch );
190     } elsif ( $size == 0 ) {
191     warn ">>> closing connection\n";
192     return 0;
193 dpavlin 36 } else {
194     warn ">>> empty response\n";
195 dpavlin 50 $state->{NoMoreRequests} = 1;
196     $xml = $self->dispatch( 'xml', sub {} );
197 dpavlin 36 }
198 dpavlin 40
199 dpavlin 43 $sock->send( "Content-Length: " . length( $xml ) . "\r\n\r\n" );
200 dpavlin 44 $sock->send( $xml ) or die "can't send response";
201 dpavlin 40
202     warn "### request over";
203    
204 dpavlin 30 };
205    
206 dpavlin 50 =head2 dispatch
207    
208     $xml = $self->dispatch('Inform', $response_arguments );
209    
210     =cut
211    
212     sub dispatch {
213     my $self = shift;
214    
215     my $dispatch = shift || die "no dispatch?";
216    
217     my $response = CWMP::Response->new({ debug => $self->debug });
218    
219     if ( $response->can( $dispatch ) ) {
220     warn ">>> dispatching to $dispatch\n";
221     my $xml = $response->$dispatch( $self->state, @_ ) . "\r\n";
222     warn "## response payload: ",length($xml)," bytes\n$xml\n";
223     return $xml;
224     } else {
225     confess "can't dispatch to $dispatch";
226     }
227     };
228    
229 dpavlin 48 =head2 read_headers
230 dpavlin 40
231 dpavlin 48 parse headers from request
232    
233     =cut
234    
235 dpavlin 40 sub read_headers {
236     my $self = shift;
237    
238     $self->{headers} = {};
239    
240 dpavlin 42 while (defined($_ = $self->sock->getline)) {
241 dpavlin 40 s/[\r\n]+$//;
242     last unless length $_;
243     warn "-- $_\n";
244     return 0 if ! /^ ([\w\-]+) :[\ \t]* (.*) $/x;
245     $self->{headers}->{$1} = $2;
246     }
247    
248     return 1;
249     }
250    
251 dpavlin 48 =head2 header
252    
253     Getter for specific header
254    
255     $self->header('Cookies');
256    
257     =cut
258    
259 dpavlin 40 sub header {
260     my $self = shift;
261     my $header = shift || die "no header?";
262     if ( defined( $self->{headers}->{$header} )) {
263     return $self->{headers}->{$header};
264     } else {
265     return;
266     }
267     }
268    
269 dpavlin 48 =head2 error
270    
271     return $self->error( 501, 'System error' );
272    
273     =cut
274    
275 dpavlin 44 sub error {
276 dpavlin 40 my ($self, $number, $msg) = @_;
277 dpavlin 44 $msg ||= 'ERROR';
278     $self->sock->send( "HTTP/1.1 $number $msg\r\n" );
279 dpavlin 40 warn "Error - $number - $msg\n";
280 dpavlin 48 return 0; # close connection
281 dpavlin 40 }
282    
283 dpavlin 30 1;

  ViewVC Help
Powered by ViewVC 1.1.26