realtm has asked for the wisdom of the Perl Monks concerning the following question:

I have a simple http server which I use for testing client behavior when the server sends errors out. This http server is like a fake server written to test the client behavior. Now, I want to make this flexible enough so it can be used by other users to test their client apps. Right now, I have to modify the content file (CGI) each time I want the server to return different response.

Should I take the content file and make functions of each response for a request? Something like - getsuccess() and then have a stub HTML response for getsucess()?

Also, I want the user be able to write a small script which will control which CGI function to call? Or a config file? Please help me on how to do this. I am posting both the server.pl and server_content.pl so you can see what I have so far.

Many Thanks!

#!/usr/local/bin/perl # server.pl use HTTP::Daemon; use HTTP::Status; mkdir("/var/tmp/cluster") || die "/var/tmp/cluster probably already ex +ists"; my $d = HTTP::Daemon->new(LocalPort => 8888, ReuseAddr => 1); print "Please contact me at: <URL:", $d->url, ">\n"; # TODO: make an external program get executed for each request # TODO: add errors while (my $c = $d->accept) { while (my $r = $c->get_request) { if ($r->uri->path eq '/quit') { goto OUTTAHERE; } my $dir = $0; $dir =~ s%/[^/]*$%%; my $method = $r->method; my $path = $r->uri->path; my $len = 0; if (length(${$r->content_ref}) > 0) { open CONTENT, ">/var/tmp/cosmos_server.content.gz" || die +"cannot write content"; binmode(CONTENT); print CONTENT ${$r->content_ref}; close CONTENT; $len = `gzip -d </var/tmp/server.content.gz|tee /var/tmp/s +erver.content|wc -c`; } my $respstr = `$dir/server_content.pl $method $path $len`; my $headstr = $respstr; my $code = 500; my $codestr = ""; if ($headstr =~ /^([^\s]*)\s([0-9]*)/) { $codestr=$1;$code=$2 } $headstr =~ s/^.*?\n//; $headstr =~ s/\r?\n\r?\n.*//s; my $header; if ($headstr ne '') { my @fields = split(/\r?\n/, $headstr); $header = HTTP::Headers->new(map({split(/:\s/)} @fields)); $respstr =~ s/.*?\r?\n\r?\n//s; # body } my $retstat = $?; if ($code == 200 || $code == 404 || $code == 100) { $resp = HTTP::Response->new($code, $codestr, $header, $respstr +); $c->send_response($resp); } else { $c->send_error($code) } } $c->close; undef($c); } OUTTAHERE:
#!/usr/local/bin/perl #server_content.pl use Sys::Hostname qw/hostname/; use HTTP::Headers; my $respstr; if (rand(100) < 20) { print "Conflict 409\n\r\n\r\n"; exit(1); } if ($ARGV[0] eq 'HEAD') { $respstr = '<?xml version="1.0" encoding="UTF-8"?> <CsStreamInfo> <StreamName>http://co1.search.live.net:88'; $respstr .= $ARGV[1]; $respstr .= <<GETEOF2; </StreamName> <CommittedLength>0</CommittedLength> <Length>0</Length> <CreateTime>128629751982538969</CreateTime> <ExpireTime>18446744073709551615</ExpireTime> <PublishedUpdateTime>128632226810164042</PublishedUpdateTime> <NumExtents>3</NumExtents> <ProviderVC>sandbox</ProviderVC> <Permissions>ERWDX</Permissions> </CsStreamInfo> GETEOF2 #<?xml version="1.0" encoding="UTF-8"?> #<CsError> #<Description>Authentication failed </Description> #<ErrorCode>0x00000000</ErrorCode> #<ErrorCodeText>The operation succeeded</ErrorCodeText> #<ErrorNode>co1sch7080801</ErrorNode> #</CsError> } elsif ($ARGV[0] eq 'GET') { $respstr = '<?xml version="1.0" encoding="UTF-8"?> <CsStreamInfo> <StreamName>http://portal.cosmos02-prod-co1.search.live.net:88'; $respstr .= $ARGV[1]; $respstr .= <<GETEOF2; </StreamName> <CommittedLength>0</CommittedLength> <Length>0</Length> <CreateTime>128629751982538969</CreateTime> <ExpireTime>18446744073709551615</ExpireTime> <PublishedUpdateTime>128632226810164042</PublishedUpdateTime> <NumExtents>3</NumExtents> <ProviderVC>sandbox</ProviderVC> <Permissions>ERWDX</Permissions> </CsStreamInfo> GETEOF2 } elsif ($ARGV[0] eq 'POST') { $respstr = '<?xml version="1.0" encoding="UTF-8"?> <CsStreamAppend> <StreamName>http://co1.search.live.net:88'; $respstr .= $ARGV[1]; $respstr .= <<POSTEOF2; </StreamName> <BytesUploaded>$ARGV[2]</BytesUploaded> </CsStreamAppend> POSTEOF2 system("cat /var/tmp/cosmos_server.content>>/var/tmp/cluster/orgs_ +".hostname); } elsif ($ARGV[0] eq 'PUT') { $respstr = '<?xml version="1.0" encoding="UTF-8"?> <CsStreamUpload> <StreamName>http://prod-co1.search.live.net:88'; $respstr .= $ARGV[1]; $respstr .= <<PUTEOF2; </StreamName> <BytesUploaded>$ARGV[2]</BytesUploaded> </CsStreamUpload> PUTEOF2 system("cat /var/tmp/server.content>>/var/tmp/cluster/orgs_".hostn +ame); } else { exit(1); } print "OK 200\nCache-Control: private\nContent-Length: ", length($resp +str) ,"\nContent-Type: application/xml; charset=utf-8\nSet-Cookie: Se +ssion=F306972E-F9B0-4D13-958D-0A561AFD6985; Path=/\nProxy-Connection: + Keep-Alive\r\n\r\n",$respstr;