下面的程序是個很有用戶的診斷工具,可以用來調試與客戶端和代理的交互情況.
該程序 首先會等待HTTP連接,只有收到請求報文,就會將報文打印在屏幕上,然后等待用戶輸入一條響應報文,并將其回送給客戶端.
#! /usr/bin/perl
use Socket;
use Carp;
use FileHandle;# (1) use prot 8080 by default, unless overridden on command line
$proto = (@ARGV ? $ARGV[0] : 8080 );
socket(S, PF_INET, SOCK_STREAM, $proto) || die;
setsockopt(S, SOL_SOCKET, SO_REUSEADDR, pack("1", 1)) || die;
bind(S, sockaddr_in($prot, INADDR_ANY)) || die;
listen(S, SOMAXCONN) || die;# (3) print a startup message
printf(" <<<Type-O-Serve Accepting on Port %d>>> \n\n", $port);while(1)
{# (4) wait for a connection C$cport_caddr = accept(C, S);($cport, $caddr) = sockaddr_in($cport_caddr);C->autoflush(1);# (5) print who the connection is form$cname = gethostbyaddr($caddr, AF_INET);printf(" <<<Request From 's%'>>>\n", $cname);# (6) read request msg until blank line, and print on screenwhile ($line = <C>){print $line;if ($line =~ /^\r/) { last; }}# (7) prompt for response message, and input response lines,# setting response lines to client, until solitary "."printf(" <<<Type Response Followed by '.'>>>\n");while ($line = <STDIN>){$line =~ s/\r//;$line =~ s/\n//;if ($line =~ /^\./) { last; }print C $line . "\r\n";}close(C);
}
參考《HTTP權威指南》P119