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

  ViewVC Help
Powered by ViewVC 1.1.26