daemon.pm

来自「1. 记录每个帖子的访问人情况」· PM 代码 · 共 822 行 · 第 1/2 页

PM
822
字号
    $r;}sub _need_more{    my $self = shift;    #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<readbuffer>.  The next time $c->get_request is called it will consume thebytes in this buffer before reading more data from the networkconnection itself.  The read buffer is invalid after $c->get_requesthas returned an undefined value.If you handle the reading of the request content yourself you need toempty this buffer before you read more and you need to placeunconsumed bytes here.  You also need this buffer if you implementservices like I<101 Switching Protocols>.This method always return the old buffer content and can optionallyreplace the buffer content if you pass it an argument.=cutsub read_buffer{    my $self = shift;    my $old = ${*$self}{'httpd_rbuf'};    if (@_) {	${*$self}{'httpd_rbuf'} = shift;    }    $old;}=item $c->reasonWhen $c->get_request returns C<undef> you can obtain a short stringdescribing why it happened by calling $c->reason.=cutsub reason{    my $self = shift;    my $old = ${*$self}{'httpd_reason'};    if (@_) {        ${*$self}{'httpd_reason'} = shift;    }    $old;}=item $c->proto_ge($proto)Return TRUE if the client announced a protocol with version numbergreater or equal to the given argument.  The $proto argument can be astring like "HTTP/1.1" or just "1.1".=cutsub 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_clientReturn TRUE if the client speaks the HTTP/0.9 protocol.  No statuscode and no headers should be returned to such a client.  This shouldbe the same as !$c->proto_ge("HTTP/1.0").=cutsub antique_client{    my $self = shift;    ${*$self}{'httpd_client_proto'} < $HTTP_1_0;}=item $c->force_last_requestMake sure that $c->get_request will not try to read more requests offthis connection.  If you generate a response that is not selfdelimiting, then you should signal this fact by calling this method.This attribute is turned on automatically if the client announcesprotocol HTTP/1.0 or worse and does not include a "Connection:Keep-Alive" header.  It is also turned on automatically when HTTP/1.1or better clients send the "Connection: close" request header.=cutsub force_last_request{    my $self = shift;    ${*$self}{'httpd_nomore'}++;}=item $c->send_status_line( [$code, [$mess, [$proto]]] )Send the status line back to the client.  If $code is omitted 200 isassumed.  If $mess is omitted, then a message corresponding to $codeis inserted.  If $proto is missing the content of the$HTTP::Daemon::PROTO variable is used.=cutsub 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_crlfSend the CRLF sequence to the client.=cutsub send_crlf{    my $self = shift;    print $self $CRLF;}=item $c->send_basic_header( [$code, [$mess, [$proto]]] )Send the status line and the "Date:" and "Server:" headers back tothe client.  This header is assumed to be continued and does not endwith an empty CRLF line.=cutsub 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] )Write a I<HTTP::Response> object to theclient as a response.  We try hard to make sure that the response isself delimiting so that the connection can stay persistent for furtherrequest/response exchanges.The content attribute of the I<HTTP::Response> object can be a normalstring or a subroutine reference.  If it is a subroutine, thenwhatever this callback routine returns is written back to theclient as the response content.  The routine will be called until itreturn an undefined or empty value.  If the client is HTTP/1.1 awarethen we will use chunked transfer encoding for the response.=cutsub 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]] )Send a redirect response back to the client.  The location ($loc) canbe an absolute or relative URL. The $code must be one the redirectstatus codes, and defaults to "301 Moved Permanently"=cutsub 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);    my $base = $self->daemon->url;    $loc = $HTTP::URI_CLASS->new($loc, $base) unless ref($loc);    $loc = $loc->abs($base);    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 thatis incorporated in the body of the HTML entity body.=cutsub 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>$errorEOT    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 thefile is a directory we try to generate an HTML index of it.=cutsub 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);	binmode(F);	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);Copy the file to the client.  The file can be a string (whichwill be interpreted as a filename) or a reference to an I<IO::Handle>or glob.=cutsub 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->daemonReturn a reference to the corresponding I<HTTP::Daemon> object.=cutsub daemon{    my $self = shift;    ${*$self}{'httpd_daemon'};}=back=head1 SEE ALSORFC 2068L<IO::Socket::INET>, L<Apache>=head1 COPYRIGHTCopyright 1996-2001, Gisle AasThis library is free software; you can redistribute it and/ormodify it under the same terms as Perl itself.=cut1;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?