📄 testclient
字号:
#!/usr/local/bin/perl## Netprog spring 98## test client used for grading proxy web servers# give this perl script the address of the server as a hostname and port number## This client reads a script that contains commands used to test# project 1 submissions. Supported commands are:## CONNECT : establish a connection to the proxy server# (forces a close as well)# SEND string : send the specified string# CLOSE : close the TCP socket the proxy# READLINE : read one line from proxy and toss away# READALL : read from proxy until EOF (tossing away)# SAVEALL file : read from proxy until EOF and save in file# SAVECONTENT : skip headers and save content in file# ECHOLINE : read one line from proxy and print# ECHOALL : read from proxy until EOF and print# EXPECT file : read from proxy until EOF and compare to a file# EXPECTCONTENT file : read from proxy, until EOF and compare to a file# tosses headers before comparing# PRINT string : print the string to STDOUT# EVAL string : eval the perl commandprint the string to STDOUTrequire 5.002;use strict;#use sigtrap;use Socket;$SIG{'PIPE'} = 'IGNORE';my %commands = ( "close",1, "connect",1, "echoall",1, "echoline",1, "expect",1, "expectcontent",1, "print",1, "readall",1, "readline",1, "send",1, "saveall",1, "savecontent",1, "eval",1, "sleep",1);select STDOUT; $|=1;# collect command line parametersmy $hostname = shift || die "No host specified!";my $port = shift || die "No port specified!";my $script = shift || die "No script specified";my($command,@tmp,$perlcom);open(SCRIPT,"$script") || die $!;# process commands from the script filewhile (<SCRIPT>) { chomp; ($command,@tmp) = split; $command =~ tr/A-Z/a-z/; if ($command =~/^\s*$/) { # nothing - skip it } elsif ($command =~ /^#.*$/) { printf "$_\n"; } elsif (! $commands{$command}) { print("Error - unknown command: $command ($_)\n"); } else { $perlcom = $command . "_handler \"$_\"";# printf("Trying $perlcom\n"); eval $perlcom; }}close(SCRIPT);sub close_handler { close(PROX);}sub sleep_handler { local($_)=$_[0]; s/SLEEP//; sleep $_;}sub eval_handler { local($_)=$_[0]; s/EVAL//;# printf("EVAL $_\n"); eval $_;}sub print_handler { local($_)=$_[0]; s/PRINT\s+//; print "$_\n";# eval $_;}sub connect_handler {# printf("CONNECT\n"); np_connect($hostname,$port);}sub echoall_handler { while (<PROX>) { printf("$_"); } close(PROX);}sub readall_handler { while (<PROX>) { } close(PROX);}sub echoline_handler { $_=<PROX>; printf("$_");}sub readline_handler { $_=<PROX>;}# sub saveall_handler { local($_) = $_[0]; my($cmd,$file) = split; open(F,">$file") || die $!; my($len,$buf); while (<PROX>) { print F $_; } close(F); close(PROX);}# savecontent assumes we are seeing the entire reply# and tosses headerssub savecontent_handler { local($_) = $_[0]; my($save) = $_; $_=<PROX>; # the reply while (($_=<PROX>) && (! /^\r\n/)) {# print; } saveall_handler($save);}sub expect_handler { local($_) = $_[0]; my($cmd,$file) = split; my($line); my($same)=0; if (! -f $file) { printf("Error - can't find the file: $_[0]\n"); } else { open(F,"$file") || die $!; while (($line = <F>) && ($same<10)) { $_=<PROX>;# chomp; s/\r//g;# $line = <F>;# chomp($line); $line =~ s/\r//g; if ( $line ne $_ ) { $same++; printf("EXPECT error - lines do not match:\n"); printf("PROXY: $_\n"); printf("FILE: $line\n"); } } close(F); close(PROX); if ($same==0) { printf("EXPECT checks OK\n"); } elsif ($same>=10) { printf("EXPECT terminating early - too many differences\n"); } }}# expectcontent just reads the headers and# then calls the expect handlersub expectcontent_handler { my($save) = $_[0]; $_=<PROX>; # get The reply line while (($_=<PROX>) && (! /^\r\n/)) { } expect_handler( $save );}# sends a string to the TCP socketsub send_handler { local($_) = $_[0]; s/SEND //; printf PROX "$_";}## handles the CONNECT command sub np_connect { my($chost,$cport) = @_; # just in case close(PROX); # create a socket socket(PROX, PF_INET, SOCK_STREAM, getprotobyname('tcp')) or die $!; my $addr = gethostbyname($chost); # build address of destination # Need perl 5.002 to do this my $serv_addr = sockaddr_in($cport,$addr) || die $!; # call connect connect(PROX,$serv_addr) || die $!; select PROX; $|=1; select STDOUT;}sub np_test { print(PROX "GET http://localhost/ HTTP/1.0\r\n\r\n"); while (<PROX>) { print "$_\n"; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -