61 |
} |
} |
62 |
} |
} |
63 |
|
|
64 |
|
=head2 process_request |
65 |
|
|
66 |
|
One request from client/response from server cycle. Call multiple times to |
67 |
|
facilitate brain-dead concept of adding state to stateless protocol like |
68 |
|
HTTP. |
69 |
|
|
70 |
|
=cut |
71 |
|
|
72 |
sub process_request { |
sub process_request { |
73 |
my $self = shift; |
my $self = shift; |
74 |
|
|
85 |
$sock->blocking( 1 ); |
$sock->blocking( 1 ); |
86 |
|
|
87 |
### read the first line of response |
### read the first line of response |
88 |
my $line = $sock->getline || $self->error(400, "No Data"); |
my $line = $sock->getline || return $self->error(400, "No Data"); |
89 |
|
|
90 |
$line =~ s/[\r\n]+$//; |
$line =~ s/[\r\n]+$//; |
91 |
if ($line !~ /^ (\w+) \ + (\S+) \ + (HTTP\/1.\d) $ /x) { |
if ($line !~ /^ (\w+) \ + (\S+) \ + (HTTP\/1.\d) $ /x) { |
158 |
|
|
159 |
my $response = CWMP::Response->new({ debug => $self->debug }); |
my $response = CWMP::Response->new({ debug => $self->debug }); |
160 |
|
|
161 |
$sock->send(join("", |
$sock->send(join("\r\n", |
162 |
$self->status(200,'OK'), |
'HTTP/1.1 200 OK', |
163 |
$self->content_type('text/xml; charset="utf-8"'), |
'Content-Type: text/xml; charset="utf-8"', |
164 |
"Server: AcmeCWMP/42\r\n", |
'Server: AcmeCWMP/42', |
165 |
"SOAPServer: AcmeCWMP/42\r\n" |
'SOAPServer: AcmeCWMP/42', |
166 |
)); |
)); |
167 |
|
|
168 |
$sock->send( "Set-Cookie: ID=" . $state->{ID} . "; path=/\r\n" ) if ( $state->{ID} ); |
$sock->send( "Set-Cookie: ID=" . $state->{ID} . "; path=/\r\n" ) if ( $state->{ID} ); |
182 |
} |
} |
183 |
|
|
184 |
$sock->send( "Content-Length: " . length( $xml ) . "\r\n\r\n" ); |
$sock->send( "Content-Length: " . length( $xml ) . "\r\n\r\n" ); |
185 |
$sock->send( "$xml\r\n\r\n" ) or die "can't send response"; |
$sock->send( $xml ) or die "can't send response"; |
186 |
|
|
187 |
warn "### request over"; |
warn "### request over"; |
188 |
|
|
189 |
}; |
}; |
190 |
|
|
191 |
|
=head2 read_headers |
192 |
|
|
193 |
|
parse headers from request |
194 |
|
|
195 |
|
=cut |
196 |
|
|
197 |
sub read_headers { |
sub read_headers { |
198 |
my $self = shift; |
my $self = shift; |
210 |
return 1; |
return 1; |
211 |
} |
} |
212 |
|
|
213 |
|
=head2 header |
214 |
|
|
215 |
|
Getter for specific header |
216 |
|
|
217 |
|
$self->header('Cookies'); |
218 |
|
|
219 |
|
=cut |
220 |
|
|
221 |
sub header { |
sub header { |
222 |
my $self = shift; |
my $self = shift; |
223 |
my $header = shift || die "no header?"; |
my $header = shift || die "no header?"; |
228 |
} |
} |
229 |
} |
} |
230 |
|
|
231 |
sub content_type { |
=head2 error |
|
my ($self, $type) = @_; |
|
|
$self->http_header; |
|
|
return "Content-type: $type\r\n"; |
|
|
} |
|
232 |
|
|
233 |
sub error{ |
return $self->error( 501, 'System error' ); |
|
my ($self, $number, $msg) = @_; |
|
|
$self->sock->send( $self->status($number, $msg) . "\r\n" ); |
|
|
warn "Error - $number - $msg\n"; |
|
|
} |
|
234 |
|
|
235 |
sub status { |
=cut |
|
my ($self, $number, $msg) = @_; |
|
|
$msg = '' if ! defined $msg; |
|
|
return if $self->http_header($number); |
|
|
return "Status $number: $msg\r\n"; |
|
|
} |
|
236 |
|
|
237 |
sub http_header { |
sub error { |
238 |
my $self = shift; |
my ($self, $number, $msg) = @_; |
239 |
my $number = shift || 200; |
$msg ||= 'ERROR'; |
240 |
return if ! delete $self->{needs_header}; |
$self->sock->send( "HTTP/1.1 $number $msg\r\n" ); |
241 |
$self->sock->send("HTTP/1.1 $number\r\n"); |
warn "Error - $number - $msg\n"; |
242 |
return 1; |
return 0; # close connection |
243 |
} |
} |
244 |
|
|
245 |
1; |
1; |