/usr/share/perl5/Net/HTTPServer/Response.pm is in libnet-httpserver-perl 1.1.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 | ##############################################################################
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Library General Public License for more details.
#
# You should have received a copy of the GNU Library General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
#
# Copyright (C) 2003-2005 Ryan Eatmon
#
##############################################################################
package Net::HTTPServer::Response;
=head1 NAME
Net::HTTPServer::Response - HTTP response
=head1 SYNOPSIS
Net::HTTPServer::Response handles formatting the response to the client.
=head1 DESCRIPTION
Net::HTTPServer::Response provides a nice OOP interface for easy control
of headers, cookies, sessions, and/or the content that will be sent to
the requesting client.
=head1 EXAMPLES
my $response = new Net::HTTPServer::Response();
my $response = new Net::HTTPServer::Response(code=>200,
headers=>{
);
my $response = $request->Response();
=head1 METHODS
=head2 new(%cfg)
Given a config hash, return a server object that you can start, process,
and stop. The config hash takes the options:
body => string - The contents of the response.
( Default: "" )
code => int - The return code of this reponse.
( Default: 200 )
cookies => hashref - Hash reference to a set of cookies to send.
Most people should just use the Cookie method
to set these.
( Default: {} )
headers => hashref - Hash reference to the headers to send. Most
people should just use the Header method.
( Default: {} )
=head2 Body([string])
Returns the current value of the response body. Sets the content of
the response if a value is specified.
=head2 Clear()
Reset the body to "".
=head2 Code(int)
Returns the current value of the response code. Set the status code
of the response if a value is specified.
=head2 Cookie(name[,value[,%options]])
Returns the cookie value for the specified name, or undef if it is
not set. If the value is also specified, then the cookie is set
to the value. The optional hash options that you can provide to
the cookie are:
domain => string - If specified, the client will return the
cookie for any hostname that is part of
the domain.
expires => string - When should the cookie expire. Must be
formatted according to the rules:
Wednesday, 30-June-2004 18:14:24 GMT
Optionally you can specify "now" which
will resolve to the current time.
path => string - The path on the server that the client should
return the cookie for.
secure => 0|1 - The client will only return the cookie over
an HTTPS connection.
=head2 Header(name[,value])
Returns the header value for the specified name, or undef if it is not
set. If the value is specified, then the header is set to the value.
=head2 Print(arg1[,arg2,...,argN])
Appends the arguments to the end of the body.
=head2 Redirect(url)
Redirect the client to the specified URL.
=head2 Session(object)
Register the Net::HTTPServer::Session object with the response. When
the server builds the actual reponse to the client it will set the
appropriate cookie and save the session. If the response is
created from the request object, and there was a session created
from the request object then this, will be prepopulated with that
session.
=head2 CaptureSTDOUT()
If you use the CGI perl module then it wants to print everything to
STDOUT. CaptureSTDOUT() will put the Reponse object into a mode
where it will capture all the output from the module. See
ProcessSTDOUT() for more information.
=head2 ProcessSTDOUT([%args])
This will harvest all of the data printed to STDOUT and put it into
the Response object via a Print() call. This will also stop
monitoring STDOUT and release it. You can specify some options:
strip_header => 0|1 - If you use the CGI module and you
print the headers then ProcessSTDOUT()
can try to strip those out. The best
plan is not to print them.
See CaptureSTDOUT() for more information.
=head1 AUTHOR
Ryan Eatmon
=head1 COPYRIGHT
Copyright (c) 2003-2005 Ryan Eatmon <reatmon@mail.com>. All rights
reserved. This program is free software; you can redistribute it
and/or modify it under the same terms as Perl itself.
=cut
use strict;
use Carp;
use URI::Escape;
use Net::HTTPServer::CaptureSTDOUT;
use vars qw ( $VERSION );
$VERSION = "1.0.3";
sub new
{
my $proto = shift;
my $class = ref($proto) || $proto;
my $self = { };
bless($self, $proto);
my (%args) = @_;
$self->{ARGS} = \%args;
$self->{CODE} = $self->_arg("code","200");
$self->{HEADERS} = $self->_arg("headers",{});
$self->{COOKIES} = $self->_arg("cookies",{});
$self->{BODY} = $self->_arg("body","");
return $self;
}
sub Body
{
my $self = shift;
my $body = shift;
return $self->{BODY} unless defined($body);
$self->{BODY} = $body;
}
sub Clear
{
my $self = shift;
$self->{BODY} = "";
}
sub Code
{
my $self = shift;
my $code = shift;
return $self->{CODE} unless defined($code);
$self->{CODE} = $code;
}
sub Cookie
{
my $self = shift;
my $cookie = shift;
my $value = shift;
my (%args) = @_;
return unless (defined($cookie) && defined($value));
$self->{COOKIES}->{$cookie}->{value} = $value;
if (exists($args{expires}))
{
$self->{COOKIES}->{$cookie}->{expires} = $args{expires};
$self->{COOKIES}->{$cookie}->{expires} = &Net::HTTPServer::_date()
if ($args{expires} eq "now");
}
$self->{COOKIES}->{$cookie}->{domain} = $args{domain}
if exists($args{domain});
$self->{COOKIES}->{$cookie}->{path} = $args{path}
if exists($args{path});
$self->{COOKIES}->{$cookie}->{secure} = $args{secure}
if exists($args{secure});
}
sub Header
{
my $self = shift;
my $header = shift;
my $value = shift;
return unless defined($header);
$self->{HEADERS}->{$header} = $value if defined($value);
return unless exists($self->{HEADERS}->{$header});
return $self->{HEADERS}->{$header};
}
sub Print
{
my $self = shift;
$self->{BODY} .= join("",@_);
}
sub Redirect
{
my $self = shift;
my $url = shift;
$self->Code(307);
$self->Clear();
$self->Header("Location",$url);
}
sub Session
{
my $self = shift;
my $session = shift;
$self->{SESSION} = $session if defined($session);
return unless exists($self->{SESSION});
return $self->{SESSION};
}
sub CaptureSTDOUT
{
my $self = shift;
if (tied *STDOUT)
{
croak("You cannot call CaptureSTDOUT more than once without calling ProcessSTDOUT");
}
tie(*STDOUT, "Net::HTTPServer::CaptureSTDOUT");
}
sub ProcessSTDOUT
{
my $self = shift;
my (%args) = @_;
my $output = join("",<STDOUT>);
#--------------------------------------------------------------------------
# Try and strip out the headers if the user printed any...
#--------------------------------------------------------------------------
if (exists($args{strip_header}) && ($args{strip_header} == 1))
{
$output =~ s/^.+\015?\012\015?\012//;
}
$self->Print($output);
untie(*STDOUT);
}
###############################################################################
#
# _arg - if the arg exists then use it, else use the default.
#
###############################################################################
sub _arg
{
my $self = shift;
my $arg = shift;
my $default = shift;
return (exists($self->{ARGS}->{$arg}) ? $self->{ARGS}->{$arg} : $default);
}
sub _build
{
my $self = shift;
#-------------------------------------------------------------------------
# Format the return headers
#-------------------------------------------------------------------------
my $header = "HTTP/1.1 ".$self->{CODE}."\n";
foreach my $key (sort {$a cmp $b} keys(%{$self->{HEADERS}}))
{
$header .= "$key: ".$self->{HEADERS}->{$key}."\n";
}
#-------------------------------------------------------------------------
# Session in, cookie out
#-------------------------------------------------------------------------
if (exists($self->{SESSION}))
{
my $value = $self->{SESSION}->_key();
my $delta = 4*60*60; # 4 hours from now
if ($self->{SESSION}->_valid())
{
$self->{SESSION}->_save();
}
else
{
$value = "";
$delta = -(100*24*60*60); # 100 days ago
}
$self->Cookie("NETHTTPSERVERSESSION",
$value,
expires=>&Net::HTTPServer::_date(time,$delta),
);
}
#-------------------------------------------------------------------------
# Mmmm.... Cookies....
#-------------------------------------------------------------------------
foreach my $cookie (sort {$a cmp $b} keys(%{$self->{COOKIES}}))
{
my $value = uri_escape($self->{COOKIES}->{$cookie}->{value});
$header .= "Set-Cookie: $cookie=$value";
foreach my $key (sort {$a cmp $b} keys(%{$self->{COOKIES}->{$cookie}}))
{
next if ($key eq "value");
if ($key eq "secure")
{
if ($self->{COOKIES}->{$cookie}->{$key} == 1)
{
$header .= ";$key";
}
}
else
{
$header .= ";$key=".$self->{COOKIES}->{$cookie}->{$key};
}
}
$header .= "\n";
}
chomp($header);
$header .= "\r\n\r\n";
return ($header,$self->{BODY});
}
1;
|