Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

808 lines
21 KiB

  1. # $Id: Daemon.pm,v 1.24 2001/03/14 20:59:32 gisle Exp $
  2. #
  3. use strict;
  4. package HTTP::Daemon;
  5. =head1 NAME
  6. HTTP::Daemon - a simple http server class
  7. =head1 SYNOPSIS
  8. use HTTP::Daemon;
  9. use HTTP::Status;
  10. my $d = HTTP::Daemon->new || die;
  11. print "Please contact me at: <URL:", $d->url, ">\n";
  12. while (my $c = $d->accept) {
  13. while (my $r = $c->get_request) {
  14. if ($r->method eq 'GET' and $r->url->path eq "/xyzzy") {
  15. # remember, this is *not* recommened practice :-)
  16. $c->send_file_response("/etc/passwd");
  17. } else {
  18. $c->send_error(RC_FORBIDDEN)
  19. }
  20. }
  21. $c->close;
  22. undef($c);
  23. }
  24. =head1 DESCRIPTION
  25. Instances of the I<HTTP::Daemon> class are HTTP/1.1 servers that
  26. listen on a socket for incoming requests. The I<HTTP::Daemon> is a
  27. sub-class of I<IO::Socket::INET>, so you can perform socket operations
  28. directly on it too.
  29. The accept() method will return when a connection from a client is
  30. available. The returned value will be a reference to a object of the
  31. I<HTTP::Daemon::ClientConn> class which is another I<IO::Socket::INET>
  32. subclass. Calling the get_request() method on this object will read
  33. data from the client and return an I<HTTP::Request> object reference.
  34. This HTTP daemon does not fork(2) for you. Your application, i.e. the
  35. user of the I<HTTP::Daemon> is reponsible for forking if that is
  36. desirable. Also note that the user is responsible for generating
  37. responses that conform to the HTTP/1.1 protocol. The
  38. I<HTTP::Daemon::ClientConn> class provides some methods that make this easier.
  39. =head1 METHODS
  40. The following is a list of methods that are new (or enhanced) relative
  41. to the I<IO::Socket::INET> base class.
  42. =over 4
  43. =cut
  44. use vars qw($VERSION @ISA $PROTO $DEBUG);
  45. $VERSION = sprintf("%d.%02d", q$Revision: 1.24 $ =~ /(\d+)\.(\d+)/);
  46. use IO::Socket qw(AF_INET INADDR_ANY inet_ntoa);
  47. @ISA=qw(IO::Socket::INET);
  48. $PROTO = "HTTP/1.1";
  49. =item $d = new HTTP::Daemon
  50. The constructor takes the same parameters as the
  51. I<IO::Socket::INET> constructor. It can also be called without specifying
  52. any parameters. The daemon will then set up a listen queue of 5
  53. connections and allocate some random port number. A server that wants
  54. to bind to some specific address on the standard HTTP port will be
  55. constructed like this:
  56. $d = new HTTP::Daemon
  57. LocalAddr => 'www.someplace.com',
  58. LocalPort => 80;
  59. =cut
  60. sub new
  61. {
  62. my($class, %args) = @_;
  63. $args{Listen} ||= 5;
  64. $args{Proto} ||= 'tcp';
  65. return $class->SUPER::new(%args);
  66. }
  67. =item $c = $d->accept([$pkg])
  68. This method is the same as I<IO::Socket::accept> but returns an
  69. I<HTTP::Daemon::ClientConn> reference by default. It returns
  70. undef if you specify a timeout and no connection is made within
  71. that time.
  72. =cut
  73. sub accept
  74. {
  75. my $self = shift;
  76. my $pkg = shift || "HTTP::Daemon::ClientConn";
  77. my $sock = $self->SUPER::accept($pkg);
  78. ${*$sock}{'httpd_daemon'} = $self if $sock;
  79. $sock;
  80. }
  81. =item $d->url
  82. Returns a URL string that can be used to access the server root.
  83. =cut
  84. sub url
  85. {
  86. my $self = shift;
  87. my $url = "http://";
  88. my $addr = $self->sockaddr;
  89. if ($addr eq INADDR_ANY) {
  90. require Sys::Hostname;
  91. $url .= lc Sys::Hostname::hostname();
  92. }
  93. else {
  94. $url .= gethostbyaddr($addr, AF_INET) || inet_ntoa($addr);
  95. }
  96. my $port = $self->sockport;
  97. $url .= ":$port" if $port != 80;
  98. $url .= "/";
  99. $url;
  100. }
  101. =item $d->product_tokens
  102. Returns the name that this server will use to identify itself. This
  103. is the string that is sent with the I<Server> response header. The
  104. main reason to have this method is that subclasses can override it if
  105. they want to use another product name.
  106. =cut
  107. sub product_tokens
  108. {
  109. "libwww-perl-daemon/$HTTP::Daemon::VERSION";
  110. }
  111. package HTTP::Daemon::ClientConn;
  112. use vars qw(@ISA $DEBUG);
  113. use IO::Socket ();
  114. @ISA=qw(IO::Socket::INET);
  115. *DEBUG = \$HTTP::Daemon::DEBUG;
  116. use HTTP::Request ();
  117. use HTTP::Response ();
  118. use HTTP::Status;
  119. use HTTP::Date qw(time2str);
  120. use LWP::MediaTypes qw(guess_media_type);
  121. use Carp ();
  122. my $CRLF = "\015\012"; # "\r\n" is not portable
  123. my $HTTP_1_0 = _http_version("HTTP/1.0");
  124. my $HTTP_1_1 = _http_version("HTTP/1.1");
  125. =back
  126. The I<HTTP::Daemon::ClientConn> is also a I<IO::Socket::INET>
  127. subclass. Instances of this class are returned by the accept() method
  128. of I<HTTP::Daemon>. The following additional methods are
  129. provided:
  130. =over 4
  131. =item $c->get_request([$headers_only])
  132. Read data from the client and turn it into an
  133. I<HTTP::Request> object which is then returned. It returns C<undef>
  134. if reading of the request fails. If it fails, then the
  135. I<HTTP::Daemon::ClientConn> object ($c) should be discarded, and you
  136. should not call this method again. The $c->reason method might give
  137. you some information about why $c->get_request returned C<undef>.
  138. The $c->get_request method supports HTTP/1.1 request content bodies,
  139. including I<chunked> transfer encoding with footer and self delimiting
  140. I<multipart/*> content types.
  141. The $c->get_request method will normally not return until the whole
  142. request has been received from the client. This might not be what you
  143. want if the request is an upload of a multi-mega-byte file (and with
  144. chunked transfer encoding HTTP can even support infinite request
  145. messages - uploading live audio for instance). If you pass a TRUE
  146. value as the $headers_only argument, then $c->get_request will return
  147. immediately after parsing the request headers and you are responsible
  148. for reading the rest of the request content. If you are going to
  149. call $c->get_request again on the same connection you better read the
  150. correct number of bytes.
  151. =cut
  152. sub get_request
  153. {
  154. my($self, $only_headers) = @_;
  155. if (${*$self}{'httpd_nomore'}) {
  156. $self->reason("No more requests from this connection");
  157. return;
  158. }
  159. $self->reason("");
  160. my $buf = ${*$self}{'httpd_rbuf'};
  161. $buf = "" unless defined $buf;
  162. my $timeout = $ {*$self}{'io_socket_timeout'};
  163. my $fdset = "";
  164. vec($fdset, $self->fileno, 1) = 1;
  165. local($_);
  166. READ_HEADER:
  167. while (1) {
  168. # loop until we have the whole header in $buf
  169. $buf =~ s/^(?:\015?\012)+//; # ignore leading blank lines
  170. if ($buf =~ /\012/) { # potential, has at least one line
  171. if ($buf =~ /^\w+[^\012]+HTTP\/\d+\.\d+\015?\012/) {
  172. if ($buf =~ /\015?\012\015?\012/) {
  173. last READ_HEADER; # we have it
  174. } elsif (length($buf) > 16*1024) {
  175. $self->send_error(413); # REQUEST_ENTITY_TOO_LARGE
  176. $self->reason("Very long header");
  177. return;
  178. }
  179. } else {
  180. last READ_HEADER; # HTTP/0.9 client
  181. }
  182. } elsif (length($buf) > 16*1024) {
  183. $self->send_error(414); # REQUEST_URI_TOO_LARGE
  184. $self->reason("Very long first line");
  185. return;
  186. }
  187. print STDERR "Need more data for complete header\n" if $DEBUG;
  188. return unless $self->_need_more($buf, $timeout, $fdset);
  189. }
  190. if ($buf !~ s/^(\S+)[ \t]+(\S+)(?:[ \t]+(HTTP\/\d+\.\d+))?[^\012]*\012//) {
  191. ${*$self}{'httpd_client_proto'} = _http_version("HTTP/1.0");
  192. $self->send_error(400); # BAD_REQUEST
  193. $self->reason("Bad request line: $buf");
  194. return;
  195. }
  196. my $method = $1;
  197. my $uri = $2;
  198. my $proto = $3 || "HTTP/0.9";
  199. $uri = "http://$uri" if $method eq "CONNECT";
  200. $uri = $HTTP::URI_CLASS->new($uri, $self->daemon->url);
  201. my $r = HTTP::Request->new($method, $uri);
  202. $r->protocol($proto);
  203. ${*$self}{'httpd_client_proto'} = $proto = _http_version($proto);
  204. if ($proto >= $HTTP_1_0) {
  205. # we expect to find some headers
  206. my($key, $val);
  207. HEADER:
  208. while ($buf =~ s/^([^\012]*)\012//) {
  209. $_ = $1;
  210. s/\015$//;
  211. if (/^([\w\-]+)\s*:\s*(.*)/) {
  212. $r->push_header($key, $val) if $key;
  213. ($key, $val) = ($1, $2);
  214. } elsif (/^\s+(.*)/) {
  215. $val .= " $1";
  216. } else {
  217. last HEADER;
  218. }
  219. }
  220. $r->push_header($key, $val) if $key;
  221. }
  222. my $conn = $r->header('Connection');
  223. if ($proto >= $HTTP_1_1) {
  224. ${*$self}{'httpd_nomore'}++ if $conn && lc($conn) =~ /\bclose\b/;
  225. } else {
  226. ${*$self}{'httpd_nomore'}++ unless $conn &&
  227. lc($conn) =~ /\bkeep-alive\b/;
  228. }
  229. if ($only_headers) {
  230. ${*$self}{'httpd_rbuf'} = $buf;
  231. return $r;
  232. }
  233. # Find out how much content to read
  234. my $te = $r->header('Transfer-Encoding');
  235. my $ct = $r->header('Content-Type');
  236. my $len = $r->header('Content-Length');
  237. if ($te && lc($te) eq 'chunked') {
  238. # Handle chunked transfer encoding
  239. my $body = "";
  240. CHUNK:
  241. while (1) {
  242. print STDERR "Chunked\n" if $DEBUG;
  243. if ($buf =~ s/^([^\012]*)\012//) {
  244. my $chunk_head = $1;
  245. unless ($chunk_head =~ /^([0-9A-Fa-f]+)/) {
  246. $self->send_error(400);
  247. $self->reason("Bad chunk header $chunk_head");
  248. return;
  249. }
  250. my $size = hex($1);
  251. last CHUNK if $size == 0;
  252. my $missing = $size - length($buf) + 2; # 2=CRLF at chunk end
  253. # must read until we have a complete chunk
  254. while ($missing > 0) {
  255. print STDERR "Need $missing more bytes\n" if $DEBUG;
  256. my $n = $self->_need_more($buf, $timeout, $fdset);
  257. return unless $n;
  258. $missing -= $n;
  259. }
  260. $body .= substr($buf, 0, $size);
  261. substr($buf, 0, $size+2) = '';
  262. } else {
  263. # need more data in order to have a complete chunk header
  264. return unless $self->_need_more($buf, $timeout, $fdset);
  265. }
  266. }
  267. $r->content($body);
  268. # pretend it was a normal entity body
  269. $r->remove_header('Transfer-Encoding');
  270. $r->header('Content-Length', length($body));
  271. my($key, $val);
  272. FOOTER:
  273. while (1) {
  274. if ($buf !~ /\012/) {
  275. # need at least one line to look at
  276. return unless $self->_need_more($buf, $timeout, $fdset);
  277. } else {
  278. $buf =~ s/^([^\012]*)\012//;
  279. $_ = $1;
  280. s/\015$//;
  281. if (/^([\w\-]+)\s*:\s*(.*)/) {
  282. $r->push_header($key, $val) if $key;
  283. ($key, $val) = ($1, $2);
  284. } elsif (/^\s+(.*)/) {
  285. $val .= " $1";
  286. } elsif (!length) {
  287. last FOOTER;
  288. } else {
  289. $self->reason("Bad footer syntax");
  290. return;
  291. }
  292. }
  293. }
  294. $r->push_header($key, $val) if $key;
  295. } elsif ($te) {
  296. $self->send_error(501); # Unknown transfer encoding
  297. $self->reason("Unknown transfer encoding '$te'");
  298. return;
  299. } elsif ($ct && lc($ct) =~ m/^multipart\/\w+\s*;.*boundary\s*=\s*(\w+)/) {
  300. # Handle multipart content type
  301. my $boundary = "$CRLF--$1--$CRLF";
  302. my $index;
  303. while (1) {
  304. $index = index($buf, $boundary);
  305. last if $index >= 0;
  306. # end marker not yet found
  307. return unless $self->_need_more($buf, $timeout, $fdset);
  308. }
  309. $index += length($boundary);
  310. $r->content(substr($buf, 0, $index));
  311. substr($buf, 0, $index) = '';
  312. } elsif ($len) {
  313. # Plain body specified by "Content-Length"
  314. my $missing = $len - length($buf);
  315. while ($missing > 0) {
  316. print "Need $missing more bytes of content\n" if $DEBUG;
  317. my $n = $self->_need_more($buf, $timeout, $fdset);
  318. return unless $n;
  319. $missing -= $n;
  320. }
  321. if (length($buf) > $len) {
  322. $r->content(substr($buf,0,$len));
  323. substr($buf, 0, $len) = '';
  324. } else {
  325. $r->content($buf);
  326. $buf='';
  327. }
  328. }
  329. ${*$self}{'httpd_rbuf'} = $buf;
  330. $r;
  331. }
  332. sub _need_more
  333. {
  334. my $self = shift;
  335. #my($buf,$timeout,$fdset) = @_;
  336. if ($_[1]) {
  337. my($timeout, $fdset) = @_[1,2];
  338. print STDERR "select(,,,$timeout)\n" if $DEBUG;
  339. my $n = select($fdset,undef,undef,$timeout);
  340. unless ($n) {
  341. $self->reason(defined($n) ? "Timeout" : "select: $!");
  342. return;
  343. }
  344. }
  345. print STDERR "sysread()\n" if $DEBUG;
  346. my $n = sysread($self, $_[0], 2048, length($_[0]));
  347. $self->reason(defined($n) ? "Client closed" : "sysread: $!") unless $n;
  348. $n;
  349. }
  350. =item $c->read_buffer([$new_value])
  351. Bytes read by $c->get_request, but not used are placed in the I<read
  352. buffer>. The next time $c->get_request is called it will consume the
  353. bytes in this buffer before reading more data from the network
  354. connection itself. The read buffer is invalid after $c->get_request
  355. has returned an undefined value.
  356. If you handle the reading of the request content yourself you need to
  357. empty this buffer before you read more and you need to place
  358. unconsumed bytes here. You also need this buffer if you implement
  359. services like I<101 Switching Protocols>.
  360. This method always return the old buffer content and can optionally
  361. replace the buffer content if you pass it an argument.
  362. =cut
  363. sub read_buffer
  364. {
  365. my $self = shift;
  366. my $old = ${*$self}{'httpd_rbuf'};
  367. if (@_) {
  368. ${*$self}{'httpd_rbuf'} = shift;
  369. }
  370. $old;
  371. }
  372. =item $c->reason
  373. When $c->get_request returns C<undef> you can obtain a short string
  374. describing why it happened by calling $c->reason.
  375. =cut
  376. sub reason
  377. {
  378. my $self = shift;
  379. my $old = ${*$self}{'httpd_reason'};
  380. if (@_) {
  381. ${*$self}{'httpd_reason'} = shift;
  382. }
  383. $old;
  384. }
  385. =item $c->proto_ge($proto)
  386. Return TRUE if the client announced a protocol with version number
  387. greater or equal to the given argument. The $proto argument can be a
  388. string like "HTTP/1.1" or just "1.1".
  389. =cut
  390. sub proto_ge
  391. {
  392. my $self = shift;
  393. ${*$self}{'httpd_client_proto'} >= _http_version(shift);
  394. }
  395. sub _http_version
  396. {
  397. local($_) = shift;
  398. return 0 unless m,^(?:HTTP/)?(\d+)\.(\d+)$,i;
  399. $1 * 1000 + $2;
  400. }
  401. =item $c->antique_client
  402. Return TRUE if the client speaks the HTTP/0.9 protocol. No status
  403. code and no headers should be returned to such a client. This should
  404. be the same as !$c->proto_ge("HTTP/1.0").
  405. =cut
  406. sub antique_client
  407. {
  408. my $self = shift;
  409. ${*$self}{'httpd_client_proto'} < $HTTP_1_0;
  410. }
  411. =item $c->force_last_request
  412. Make sure that $c->get_request will not try to read more requests off
  413. this connection. If you generate a response that is not self
  414. delimiting, then you should signal this fact by calling this method.
  415. This attribute is turned on automatically if the client announces
  416. protocol HTTP/1.0 or worse and does not include a "Connection:
  417. Keep-Alive" header. It is also turned on automatically when HTTP/1.1
  418. or better clients send the "Connection: close" request header.
  419. =cut
  420. sub force_last_request
  421. {
  422. my $self = shift;
  423. ${*$self}{'httpd_nomore'}++;
  424. }
  425. =item $c->send_status_line( [$code, [$mess, [$proto]]] )
  426. Send the status line back to the client. If $code is omitted 200 is
  427. assumed. If $mess is omitted, then a message corresponding to $code
  428. is inserted. If $proto is missing the content of the
  429. $HTTP::Daemon::PROTO variable is used.
  430. =cut
  431. sub send_status_line
  432. {
  433. my($self, $status, $message, $proto) = @_;
  434. return if $self->antique_client;
  435. $status ||= RC_OK;
  436. $message ||= status_message($status) || "";
  437. $proto ||= $HTTP::Daemon::PROTO || "HTTP/1.1";
  438. print $self "$proto $status $message$CRLF";
  439. }
  440. =item $c->send_crlf
  441. Send the CRLF sequence to the client.
  442. =cut
  443. sub send_crlf
  444. {
  445. my $self = shift;
  446. print $self $CRLF;
  447. }
  448. =item $c->send_basic_header( [$code, [$mess, [$proto]]] )
  449. Send the status line and the "Date:" and "Server:" headers back to
  450. the client. This header is assumed to be continued and does not end
  451. with an empty CRLF line.
  452. =cut
  453. sub send_basic_header
  454. {
  455. my $self = shift;
  456. return if $self->antique_client;
  457. $self->send_status_line(@_);
  458. print $self "Date: ", time2str(time), $CRLF;
  459. my $product = $self->daemon->product_tokens;
  460. print $self "Server: $product$CRLF" if $product;
  461. }
  462. =item $c->send_response( [$res] )
  463. Write a I<HTTP::Response> object to the
  464. client as a response. We try hard to make sure that the response is
  465. self delimiting so that the connection can stay persistent for further
  466. request/response exchanges.
  467. The content attribute of the I<HTTP::Response> object can be a normal
  468. string or a subroutine reference. If it is a subroutine, then
  469. whatever this callback routine returns is written back to the
  470. client as the response content. The routine will be called until it
  471. return an undefined or empty value. If the client is HTTP/1.1 aware
  472. then we will use chunked transfer encoding for the response.
  473. =cut
  474. sub send_response
  475. {
  476. my $self = shift;
  477. my $res = shift;
  478. if (!ref $res) {
  479. $res ||= RC_OK;
  480. $res = HTTP::Response->new($res, @_);
  481. }
  482. my $content = $res->content;
  483. my $chunked;
  484. unless ($self->antique_client) {
  485. my $code = $res->code;
  486. $self->send_basic_header($code, $res->message, $res->protocol);
  487. if ($code =~ /^(1\d\d|[23]04)$/) {
  488. # make sure content is empty
  489. $res->remove_header("Content-Length");
  490. $content = "";
  491. } elsif ($res->request && $res->request->method eq "HEAD") {
  492. # probably OK
  493. } elsif (ref($content) eq "CODE") {
  494. if ($self->proto_ge("HTTP/1.1")) {
  495. $res->push_header("Transfer-Encoding" => "chunked");
  496. $chunked++;
  497. } else {
  498. $self->force_last_request;
  499. }
  500. } elsif (length($content)) {
  501. $res->header("Content-Length" => length($content));
  502. } else {
  503. $self->force_last_request;
  504. }
  505. print $self $res->headers_as_string($CRLF);
  506. print $self $CRLF; # separates headers and content
  507. }
  508. if (ref($content) eq "CODE") {
  509. while (1) {
  510. my $chunk = &$content();
  511. last unless defined($chunk) && length($chunk);
  512. if ($chunked) {
  513. printf $self "%x%s%s%s", length($chunk), $CRLF, $chunk, $CRLF;
  514. } else {
  515. print $self $chunk;
  516. }
  517. }
  518. print $self "0$CRLF$CRLF" if $chunked; # no trailers either
  519. } elsif (length $content) {
  520. print $self $content;
  521. }
  522. }
  523. =item $c->send_redirect( $loc, [$code, [$entity_body]] )
  524. Send a redirect response back to the client. The location ($loc) can
  525. be an absolute or relative URL. The $code must be one the redirect
  526. status codes, and defaults to "301 Moved Permanently"
  527. =cut
  528. sub send_redirect
  529. {
  530. my($self, $loc, $status, $content) = @_;
  531. $status ||= RC_MOVED_PERMANENTLY;
  532. Carp::croak("Status '$status' is not redirect") unless is_redirect($status);
  533. $self->send_basic_header($status);
  534. my $base = $self->daemon->url;
  535. $loc = $HTTP::URI_CLASS->new($loc, $base) unless ref($loc);
  536. $loc = $loc->abs($base);
  537. print $self "Location: $loc$CRLF";
  538. if ($content) {
  539. my $ct = $content =~ /^\s*</ ? "text/html" : "text/plain";
  540. print $self "Content-Type: $ct$CRLF";
  541. }
  542. print $self $CRLF;
  543. print $self $content if $content;
  544. $self->force_last_request; # no use keeping the connection open
  545. }
  546. =item $c->send_error( [$code, [$error_message]] )
  547. Send an error response back to the client. If the $code is missing a
  548. "Bad Request" error is reported. The $error_message is a string that
  549. is incorporated in the body of the HTML entity body.
  550. =cut
  551. sub send_error
  552. {
  553. my($self, $status, $error) = @_;
  554. $status ||= RC_BAD_REQUEST;
  555. Carp::croak("Status '$status' is not an error") unless is_error($status);
  556. my $mess = status_message($status);
  557. $error ||= "";
  558. $mess = <<EOT;
  559. <title>$status $mess</title>
  560. <h1>$status $mess</h1>
  561. $error
  562. EOT
  563. unless ($self->antique_client) {
  564. $self->send_basic_header($status);
  565. print $self "Content-Type: text/html$CRLF";
  566. print $self "Content-Length: " . length($mess) . $CRLF;
  567. print $self $CRLF;
  568. }
  569. print $self $mess;
  570. $status;
  571. }
  572. =item $c->send_file_response($filename)
  573. Send back a response with the specified $filename as content. If the
  574. file is a directory we try to generate an HTML index of it.
  575. =cut
  576. sub send_file_response
  577. {
  578. my($self, $file) = @_;
  579. if (-d $file) {
  580. $self->send_dir($file);
  581. } elsif (-f _) {
  582. # plain file
  583. local(*F);
  584. sysopen(F, $file, 0) or
  585. return $self->send_error(RC_FORBIDDEN);
  586. binmode(F);
  587. my($ct,$ce) = guess_media_type($file);
  588. my($size,$mtime) = (stat _)[7,9];
  589. unless ($self->antique_client) {
  590. $self->send_basic_header;
  591. print $self "Content-Type: $ct$CRLF";
  592. print $self "Content-Encoding: $ce$CRLF" if $ce;
  593. print $self "Content-Length: $size$CRLF" if $size;
  594. print $self "Last-Modified: ", time2str($mtime), "$CRLF" if $mtime;
  595. print $self $CRLF;
  596. }
  597. $self->send_file(\*F);
  598. return RC_OK;
  599. } else {
  600. $self->send_error(RC_NOT_FOUND);
  601. }
  602. }
  603. sub send_dir
  604. {
  605. my($self, $dir) = @_;
  606. $self->send_error(RC_NOT_FOUND) unless -d $dir;
  607. $self->send_error(RC_NOT_IMPLEMENTED);
  608. }
  609. =item $c->send_file($fd);
  610. Copy the file to the client. The file can be a string (which
  611. will be interpreted as a filename) or a reference to an I<IO::Handle>
  612. or glob.
  613. =cut
  614. sub send_file
  615. {
  616. my($self, $file) = @_;
  617. my $opened = 0;
  618. if (!ref($file)) {
  619. local(*F);
  620. open(F, $file) || return undef;
  621. binmode(F);
  622. $file = \*F;
  623. $opened++;
  624. }
  625. my $cnt = 0;
  626. my $buf = "";
  627. my $n;
  628. while ($n = sysread($file, $buf, 8*1024)) {
  629. last if !$n;
  630. $cnt += $n;
  631. print $self $buf;
  632. }
  633. close($file) if $opened;
  634. $cnt;
  635. }
  636. =item $c->daemon
  637. Return a reference to the corresponding I<HTTP::Daemon> object.
  638. =cut
  639. sub daemon
  640. {
  641. my $self = shift;
  642. ${*$self}{'httpd_daemon'};
  643. }
  644. =back
  645. =head1 SEE ALSO
  646. RFC 2068
  647. L<IO::Socket::INET>, L<Apache>
  648. =head1 COPYRIGHT
  649. Copyright 1996-2001, Gisle Aas
  650. This library is free software; you can redistribute it and/or
  651. modify it under the same terms as Perl itself.
  652. =cut
  653. 1;