/[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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

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

Legend:
Removed from v.30  
changed lines
  Added in v.49

  ViewVC Help
Powered by ViewVC 1.1.26