📄 daemon.pm
字号:
#my($buf,$timeout,$fdset) = @_;
if ($_[1]) {
my($timeout, $fdset) = @_[1,2];
print STDERR "select(,,,$timeout)\n" if $DEBUG;
my $n = select($fdset,undef,undef,$timeout);
unless ($n) {
$self->reason(defined($n) ? "Timeout" : "select: $!");
return;
}
}
print STDERR "sysread()\n" if $DEBUG;
my $n = sysread($self, $_[0], 2048, length($_[0]));
$self->reason(defined($n) ? "Client closed" : "sysread: $!") unless $n;
$n;
}
=item $c->read_buffer([$new_value])
Bytes read by $c->get_request, but not used are placed in the I<read
buffer>. The next time $c->get_request is called it will consume the
bytes in this buffer before reading more data from the network
connection itself. The read buffer is invalid after $c->get_request
has returned an undefined value.
If you handle the reading of the request content yourself you need to
empty this buffer before you read more and you need to place
unconsumed bytes here. You also need this buffer if you implement
services like I<101 Switching Protocols>.
This method always return the old buffer content and can optionally
update the buffer content if you pass it an argument.
=cut
sub read_buffer
{
my $self = shift;
my $old = ${*$self}{'httpd_rbuf'};
if (@_) {
${*$self}{'httpd_rbuf'} = shift;
}
$old;
}
=item $c->reason
When $c->get_request returns C<undef> you can obtain a short string
describing why it happened by calling $c->reason.
=cut
sub reason
{
my $self = shift;
my $old = ${*$self}{'httpd_reason'};
if (@_) {
${*$self}{'httpd_reason'} = shift;
}
$old;
}
=item $c->proto_ge($proto)
Returns TRUE if the client announced a protocol with version number
greater or equal to the given argument. The $proto argument can be a
string like "HTTP/1.1" or just "1.1".
=cut
sub proto_ge
{
my $self = shift;
${*$self}{'httpd_client_proto'} >= _http_version(shift);
}
sub _http_version
{
local($_) = shift;
return 0 unless m,^(?:HTTP/)?(\d+)\.(\d+)$,i;
$1 * 1000 + $2;
}
=item $c->antique_client
Returns TRUE if the client speaks the HTTP/0.9 protocol. No status
code and no headers should be returned to such a client. This should
be the same as !$c->proto_ge("HTTP/1.0").
=cut
sub antique_client
{
my $self = shift;
${*$self}{'httpd_client_proto'} < $HTTP_1_0;
}
=item $c->force_last_request
Make sure that $c->get_request will not try to read more requests off
this connection. If you generate a response that is not self
delimiting, then you should signal this fact by calling this method.
This attribute is turned on automatically if the client announce
protocol HTTP/1.0 or worse and does not include a "Connection:
Keep-Alive" header. It is also turned on automatically when HTTP/1.1
or better clients send the "Connection: close" request header.
=cut
sub force_last_request
{
my $self = shift;
${*$self}{'httpd_nomore'}++;
}
=item $c->send_status_line( [$code, [$mess, [$proto]]] )
Sends the status line back to the client. If $code is omitted 200 is
assumed. If $mess is omitted, then a message corresponding to $code
is inserted. If $proto is missing the content of the
$HTTP::Daemon::PROTO variable is used.
=cut
sub send_status_line
{
my($self, $status, $message, $proto) = @_;
return if $self->antique_client;
$status ||= RC_OK;
$message ||= status_message($status) || "";
$proto ||= $HTTP::Daemon::PROTO || "HTTP/1.1";
print $self "$proto $status $message$CRLF";
}
=item $c->send_crlf
Send the CRLF sequence to the client.
=cut
sub send_crlf
{
my $self = shift;
print $self $CRLF;
}
=item $c->send_basic_header( [$code, [$mess, [$proto]]] )
Sends the status line and the "Date:" and "Server:" headers back to
the client. This header is assumed to be continued and does not end
with an empty CRLF line.
=cut
sub send_basic_header
{
my $self = shift;
return if $self->antique_client;
$self->send_status_line(@_);
print $self "Date: ", time2str(time), $CRLF;
my $product = $self->daemon->product_tokens;
print $self "Server: $product$CRLF" if $product;
}
=item $c->send_response( [$res] )
Takes a I<HTTP::Response> object as parameter and write it back to the
client as the response. We try hard to make sure that the response is
self delimiting so that the connection can stay persistent for further
request/response exchanges.
The content attribute of the I<HTTP::Response> object can be a normal
string or a subroutine reference. If it is a subroutine, then
whatever this callback routine returns will be written back to the
client as the response content. The routine will be called until it
return an undefined or empty value. If the client is HTTP/1.1 aware
then we will use the chunked transfer encoding for the response.
=cut
sub send_response
{
my $self = shift;
my $res = shift;
if (!ref $res) {
$res ||= RC_OK;
$res = HTTP::Response->new($res, @_);
}
my $content = $res->content;
my $chunked;
unless ($self->antique_client) {
my $code = $res->code;
$self->send_basic_header($code, $res->message, $res->protocol);
if ($code =~ /^(1\d\d|[23]04)$/) {
# make sure content is empty
$res->remove_header("Content-Length");
$content = "";
} elsif ($res->request && $res->request->method eq "HEAD") {
# probably OK
} elsif (ref($content) eq "CODE") {
if ($self->proto_ge("HTTP/1.1")) {
$res->push_header("Transfer-Encoding" => "chunked");
$chunked++;
} else {
$self->force_last_request;
}
} elsif (length($content)) {
$res->header("Content-Length" => length($content));
} else {
$self->force_last_request;
}
print $self $res->headers_as_string($CRLF);
print $self $CRLF; # separates headers and content
}
if (ref($content) eq "CODE") {
while (1) {
my $chunk = &$content();
last unless defined($chunk) && length($chunk);
if ($chunked) {
printf $self "%x%s%s%s", length($chunk), $CRLF, $chunk, $CRLF;
} else {
print $self $chunk;
}
}
print $self "0$CRLF$CRLF" if $chunked; # no trailers either
} elsif (length $content) {
print $self $content;
}
}
=item $c->send_redirect( $loc, [$code, [$entity_body]] )
Sends a redirect response back to the client. The location ($loc) can
be an absolute or a relative URL. The $code must be one the redirect
status codes, and it defaults to "301 Moved Permanently"
=cut
sub send_redirect
{
my($self, $loc, $status, $content) = @_;
$status ||= RC_MOVED_PERMANENTLY;
Carp::croak("Status '$status' is not redirect") unless is_redirect($status);
$self->send_basic_header($status);
$loc = url($loc, $self->daemon->url) unless ref($loc);
$loc = $loc->abs;
print $self "Location: $loc$CRLF";
if ($content) {
my $ct = $content =~ /^\s*</ ? "text/html" : "text/plain";
print $self "Content-Type: $ct$CRLF";
}
print $self $CRLF;
print $self $content if $content;
$self->force_last_request; # no use keeping the connection open
}
=item $c->send_error( [$code, [$error_message]] )
Send an error response back to the client. If the $code is missing a
"Bad Request" error is reported. The $error_message is a string that
is incorporated in the body of the HTML entity body.
=cut
sub send_error
{
my($self, $status, $error) = @_;
$status ||= RC_BAD_REQUEST;
Carp::croak("Status '$status' is not an error") unless is_error($status);
my $mess = status_message($status);
$error ||= "";
$mess = <<EOT;
<title>$status $mess</title>
<h1>$status $mess</h1>
$error
EOT
unless ($self->antique_client) {
$self->send_basic_header($status);
print $self "Content-Type: text/html$CRLF";
print $self "Content-Length: " . length($mess) . $CRLF;
print $self $CRLF;
}
print $self $mess;
$status;
}
=item $c->send_file_response($filename)
Send back a response with the specified $filename as content. If the
file happen to be a directory we will try to generate an HTML index
of it.
=cut
sub send_file_response
{
my($self, $file) = @_;
if (-d $file) {
$self->send_dir($file);
} elsif (-f _) {
# plain file
local(*F);
sysopen(F, $file, 0) or
return $self->send_error(RC_FORBIDDEN);
my($ct,$ce) = guess_media_type($file);
my($size,$mtime) = (stat _)[7,9];
unless ($self->antique_client) {
$self->send_basic_header;
print $self "Content-Type: $ct$CRLF";
print $self "Content-Encoding: $ce$CRLF" if $ce;
print $self "Content-Length: $size$CRLF" if $size;
print $self "Last-Modified: ", time2str($mtime), "$CRLF" if $mtime;
print $self $CRLF;
}
$self->send_file(\*F);
return RC_OK;
} else {
$self->send_error(RC_NOT_FOUND);
}
}
sub send_dir
{
my($self, $dir) = @_;
$self->send_error(RC_NOT_FOUND) unless -d $dir;
$self->send_error(RC_NOT_IMPLEMENTED);
}
=item $c->send_file($fd);
Copies the file back to the client. The file can be a string (which
will be interpreted as a filename) or a reference to an I<IO::Handle>
or glob.
=cut
sub send_file
{
my($self, $file) = @_;
my $opened = 0;
if (!ref($file)) {
local(*F);
open(F, $file) || return undef;
binmode(F);
$file = \*F;
$opened++;
}
my $cnt = 0;
my $buf = "";
my $n;
while ($n = sysread($file, $buf, 8*1024)) {
last if !$n;
$cnt += $n;
print $self $buf;
}
close($file) if $opened;
$cnt;
}
=item $c->daemon
Return a reference to the corresponding I<HTTP::Daemon> object.
=cut
sub daemon
{
my $self = shift;
${*$self}{'httpd_daemon'};
}
=back
=head1 SEE ALSO
RFC 2068
L<IO::Socket>, L<Apache>
=head1 COPYRIGHT
Copyright 1996-1998, Gisle Aas
This library is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.
=cut
1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -