/[cwmp]/google/lib/CWMP/Server.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

Contents of /google/lib/CWMP/Server.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 41 - (show annotations)
Tue Jun 19 18:11:37 2007 UTC (16 years, 11 months ago) by dpavlin
File size: 4858 byte(s)
added server tests (and fixed server in process ;-)
1 # 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 use base qw/Class::Accessor/;
8 __PACKAGE__->mk_accessors( qw/
9 debug
10 port
11 sock
12 / );
13
14 use IO::Socket::INET;
15 use Data::Dump qw/dump/;
16 use CWMP::Request;
17 use CWMP::Response;
18 use Carp qw/confess cluck/;
19
20 =head1 NAME
21
22 CWMP::Server - implement logic of CWMP protocol
23
24 =head1 METHODS
25
26 =head2 new
27
28 my $server = CWMP::Server->new({ port => 3333 });
29
30 =head2 run
31
32 $server->run();
33
34 =cut
35
36 sub run {
37 my $self = shift;
38
39 my $listen = IO::Socket::INET->new(
40 Listen => 5,
41 # LocalAddr => 'localhost',
42 LocalPort => $self->port,
43 Proto => 'tcp',
44 Blocking => 1,
45 ReuseAddr => 1,
46 );
47
48 warn "waiting for request on port ", $self->port, $/;
49
50 while ( my $sock = $listen->accept ) {
51 $sock->autoflush(1);
52
53 warn "connection from ", $sock->peerhost, "\n";
54
55 $self->sock( $sock ); # FIXME this will not work for multiple clients
56 $self->process_request( $sock );
57
58 warn "...another one bites a dust...\n";
59 sleep 1;
60 }
61 }
62
63 sub process_request {
64 my $self = shift;
65
66 my $sock = shift || die "no sock?";
67
68 die "not IO::Socket::INET but ", ref( $sock ) unless ( ref($sock) eq 'IO::Socket::INET' );
69
70 $sock->autoflush( 1 );
71 $sock->blocking( 1 );
72
73 ### read the first line of response
74 my $line = $sock->getline; # || $self->error(400, "No Data");
75
76 $line =~ s/[\r\n]+$//;
77 if ($line !~ /^ (\w+) \ + (\S+) \ + (HTTP\/1.\d) $ /x) {
78 return $self->error(400, "Bad request");
79 }
80 my ($method, $req, $protocol) = ($1, $2, $3);
81 warn "<<<< ",join(" ", time, $method, $req)."\n";
82
83 ### read in other headers
84 $self->read_headers($sock) || return $self->error(400, "Strange headers");
85
86 ### do we support the type
87 # if ($method !~ /GET|POST|HEAD/) {
88 if ($method !~ /POST/) {
89 return $self->error(400, "Unsupported Method");
90 }
91
92 my $chunk;
93 my $transfer_encoding = $self->header('Transfer-Encoding');
94
95 if ( $transfer_encoding && $transfer_encoding =~ qr/^chunked/i ) {
96
97 my $len = 0;
98
99 do {
100
101 warn "get chunk len\n" if $self->debug;
102
103 my $hex;
104 do {
105 $hex = $sock->getline;
106 $hex =~ s/[\n\r]+$//;
107 } until ( $hex ne '' );
108
109 die "chunk size not valid hex: $hex" unless ( $hex =~ m/^[0-9a-f]+$/i);
110 $len = hex( $hex );
111
112 warn "getting chunk of $len bytes\n" if $self->debug;
113
114 $sock->read( my $buff, $len );
115 $chunk .= $buff;
116
117 warn "--- $len bytes: --=>||$buff||<=--\n";
118
119 } while ( $len > 0 );
120
121 } else {
122 die "right now, we support only Transfer-Encoding: chunked";
123 }
124
125 warn "handler got ", length($chunk), " bytes\n" if $self->debug;
126
127 warn "<<< " . localtime() . " " . $sock->peerhost . "\n";
128
129 die "not SOAP request" unless defined ( $self->header('SOAPAction') );
130
131 my $state;
132
133 if ( $chunk ) {
134 warn "## request chunk: ",length($chunk)," bytes\n$chunk\n" if $self->debug;
135
136 $state = CWMP::Request->parse( $chunk );
137
138 warn "acquired state = ", dump( $state ), "\n";
139
140 } else {
141 warn "empty request\n";
142 }
143
144
145 my $response = CWMP::Response->new({ debug => $self->debug });
146
147 $sock->print(
148 $self->status(200),
149 $self->content_type('text/xml; charset="utf-8"'),
150 "Server: AcmeCWMP/42\r\n",
151 "SOAPServer: AcmeCWMP/42\r\n"
152 );
153
154 $sock->print( "Set-Cookie: ID=" , $state->{ID}, "; path=/\r\n" ) if ( $state->{ID} );
155
156 my $xml = '';
157
158 if ( my $dispatch = $state->{_dispatch} ) {
159 if ( $response->can( $dispatch ) ) {
160 warn ">>> dispatching to $dispatch\n";
161 $xml = $response->$dispatch( $state ) . "\r\n";
162 warn "## response payload: ",length($xml)," bytes\n$xml\n";
163 } else {
164 confess "can't dispatch to $dispatch";
165 }
166 } else {
167 warn ">>> empty response\n";
168 }
169
170 $sock->print( "Content-length: ", length( $xml ), "\r\n\r\n" );
171 $sock->print( $xml ) or die "can't send response";
172
173 warn "### request over";
174
175 };
176
177
178 sub read_headers {
179 my $self = shift;
180
181 my $sock = shift || die "no sock?";
182
183 $self->{headers} = {};
184
185 while (defined($_ = $sock->getline)) {
186 s/[\r\n]+$//;
187 last unless length $_;
188 warn "-- $_\n";
189 return 0 if ! /^ ([\w\-]+) :[\ \t]* (.*) $/x;
190 $self->{headers}->{$1} = $2;
191 }
192
193 return 1;
194 }
195
196 sub header {
197 my $self = shift;
198 my $header = shift || die "no header?";
199 if ( defined( $self->{headers}->{$header} )) {
200 return $self->{headers}->{$header};
201 } else {
202 return;
203 }
204 }
205
206 sub content_type {
207 my ($self, $type) = @_;
208 $self->http_header;
209 return "Content-type: $type\r\n";
210 }
211
212 sub error{
213 my ($self, $number, $msg) = @_;
214 $self->sock->print( $self->status($number, $msg), "\r\n" );
215 warn "Error - $number - $msg\n";
216 }
217
218 sub status {
219 my ($self, $number, $msg) = @_;
220 $msg = '' if ! defined $msg;
221 return if $self->http_header($number);
222 return "Status $number: $msg\r\n";
223 }
224
225 sub http_header {
226 my $self = shift;
227 my $number = shift || 200;
228 return if ! delete $self->{needs_header};
229 $self->sock->Print("HTTP/1.0 $number\r\n");
230 return 1;
231 }
232
233 1;

  ViewVC Help
Powered by ViewVC 1.1.26