/usr/share/perl5/AnyEvent/HTTPD/Request.pm is in libanyevent-httpd-perl 0.93-4.
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 | package AnyEvent::HTTPD::Request;
use common::sense;
=head1 NAME
AnyEvent::HTTPD::Request - A web application request handle for L<AnyEvent::HTTPD>
=head1 DESCRIPTION
This is the request object as generated by L<AnyEvent::HTTPD> and given
in the request callbacks.
=head1 METHODS
=over 4
=cut
sub new {
my $this = shift;
my $class = ref($this) || $this;
my $self = { @_ };
bless $self, $class
}
=item B<url>
This method returns the URL of the current request as L<URI> object.
=cut
sub url {
my ($self) = @_;
my $url = $self->{url};
my $u = URI->new ($url);
$u
}
=item B<respond ([$res])>
C<$res> can be:
=over 4
=item * an array reference
Then the array reference has these elements:
my ($code, $message, $header_hash, $content) =
[200, 'ok', { 'Content-Type' => 'text/html' }, '<h1>Test</h1>' }]
You can remove most headers added by default (like C<Cache-Control>,
C<Expires>, and C<Content-Length>) by setting them to undef, like so:
$req->respond([
200, 'OK', {
'Content-Type' => 'text/html',
'Cache-Control' => 'max-age=3600',
'Expires' => undef,
},
'This data will be cached for one hour.'
]);
=item * a hash reference
If it was a hash reference the hash is first searched for the C<redirect>
key and if that key does not exist for the C<content> key.
The value for the C<redirect> key should contain the URL that you want to redirect
the request to.
The value for the C<content> key should contain an array reference with the first
value being the content type and the second the content.
=back
Here is an example:
$httpd->reg_cb (
'/image/elmex' => sub {
my ($httpd, $req) = @_;
open IMG, "$ENV{HOME}/media/images/elmex.png"
or $req->respond (
[404, 'not found', { 'Content-Type' => 'text/plain' }, 'not found']
);
$req->respond ({ content => ['image/png', do { local $/; <IMG> }] });
}
);
B<How to send large files:>
For longer responses you can give a callback instead of a string to
the response function for the value of the C<$content>.
$req->respond ({ content => ['video/x-ms-asf', sub {
my ($data_cb) = @_;
# start some async retrieve operation, for example use
# IO::AIO (with AnyEvent::AIO). Or retrieve chunks of data
# to send somehow else.
} });
The given callback will receive as first argument either another callback
(C<$data_cb> in the above example) or an undefined value, which means that
there is no more data required and the transfer has been completed (either by
you sending no more data, or by a disconnect of the client).
The callback given to C<respond> will be called whenever the send queue of the
HTTP connection becomes empty (meaning that the data is written out to the
kernel). If it is called you have to start delivering the next chunk of data.
That doesn't have to be immediately, before the callback returns. This means
that you can initiate for instance an L<IO::AIO> request (see also
L<AnyEvent::AIO>) and send the data later. That is what the C<$data_cb>
callback is for. You have to call it once you got the next chunk of data. Once
you sent a chunk of data via C<$data_cb> you can just wait until your callback
is called again to deliver the next chunk.
If you are done transferring all data call the C<$data_cb> with an empty string
or with no argument at all.
Please consult the example script C<large_response_example> from the
C<samples/> directory of the L<AnyEvent::HTTPD> distribution for an example of
how to use this mechanism.
B<NOTE:> You should supply a 'Content-Length' header if you are going to send a
larger file. If you don't do that the client will have no chance to know if the
transfer was complete. To supply additional header fields the hash argument
format will not work. You should use the array argument format for this case.
=cut
sub respond {
my ($self, $res) = @_;
return unless $self->{resp};
my $rescb = delete $self->{resp};
if (ref $res eq 'HASH') {
my $h = $res;
if ($h->{redirect}) {
$res = [
301, 'redirected', { Location => $h->{redirect} },
"Redirected to <a href=\"$h->{redirect}\">here</a>"
];
} elsif ($h->{content}) {
$res = [
200, 'ok', { 'Content-Type' => $h->{content}->[0] },
$h->{content}->[1]
];
}
}
$self->{responded} = 1;
my $no_body = $self->method eq 'HEAD';
if (not defined $res) {
$rescb->(404, "ok", { 'Content-Type' => 'text/html' }, "<h1>No content</h1>", $no_body);
} else {
$rescb->(@$res, $no_body);
}
}
=item B<responded>
Returns true if this request already has been responded to.
=cut
sub responded { $_[0]->{responded} }
=item B<parm ($key)>
Returns the first value of the form parameter C<$key> or undef.
=cut
sub parm {
my ($self, $key) = @_;
if (exists $self->{parm}->{$key}) {
return $self->{parm}->{$key}->[0]->[0]
}
return undef;
}
=item B<params>
Returns list of parameter names.
=cut
sub params { keys %{$_[0]->{parm} || {}} }
=item B<vars>
Returns a hash of form parameters. The value is either the
value of the parameter, and in case there are multiple values
present it will contain an array reference of values.
=cut
sub vars {
my ($self) = @_;
my $p = $self->{parm};
my %v = map {
my $k = $_;
$k =>
@{$p->{$k}} > 1
? [ map { $_->[0] } @{$p->{$k}} ]
: $p->{$k}->[0]->[0]
} keys %$p;
%v
}
=item B<method>
This method returns the method of the current request.
=cut
sub method { $_[0]{method} }
=item B<content>
Returns the request content or undef if only parameters for a form
were transmitted.
=cut
sub content { $_[0]->{content} }
=item B<headers>
This method will return a hash reference containing the HTTP headers for this
HTTP request.
=cut
sub headers { $_[0]->{hdr} }
=item B<client_host>
This method returns the host/IP of the HTTP client this request was received
from.
=cut
sub client_host { $_[0]->{host} }
=item B<client_port>
This method returns the TCP port number of the HTTP client this
request was received from.
=cut
sub client_port { $_[0]->{port} }
=back
=head1 COPYRIGHT & LICENSE
Copyright 2008-2011 Robin Redeker, all rights reserved.
This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
=cut
1;
|