From a3b0699d5c110270f09ac51b5b465ca8753b35a9 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Thu, 14 May 2020 17:45:40 +0200 Subject: [PATCH] sws: as last resort, get test number from server cmd file If it can't be found in the request. Also support --cmdfile to set it to a custom file name. runtests.pl always writes this file with the test number in it since a while back. --- tests/server/sws.c | 72 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 53 insertions(+), 19 deletions(-) diff --git a/tests/server/sws.c b/tests/server/sws.c index 48ea26d5e..49790f03a 100644 --- a/tests/server/sws.c +++ b/tests/server/sws.c @@ -154,6 +154,10 @@ const char *serverlogfile = DEFAULT_LOGFILE; #define REQUEST_PROXY_DUMP "log/proxy.input" #define RESPONSE_PROXY_DUMP "log/proxy.response" +/* file in which additional instructions may be found */ +#define DEFAULT_CMDFILE "log/ftpserver.cmd" +const char *cmdfile = DEFAULT_CMDFILE; + /* very-big-path support */ #define MAXDOCNAMELEN 140000 #define MAXDOCNAMELEN_TXT "139999" @@ -231,6 +235,24 @@ static bool socket_domain_is_ip(void) } } +/* parse the file on disk that might have a test number for us */ +static int parse_cmdfile(struct httprequest *req) +{ + int testnum = DOCNUMBER_NOTHING; + char buf[256]; + FILE *f = fopen(cmdfile, FOPEN_READTEXT); + if(f) { + while(fgets(buf, sizeof(buf), f)) { + if(1 == sscanf(buf, "Testnum %d", &testnum)) { + logmsg("[%s] cmdfile says testnum %d", cmdfile, testnum); + req->testno = testnum; + } + } + fclose(f); + } + return 0; +} + /* based on the testno, parse the correct server commands */ static int parse_servercmd(struct httprequest *req) { @@ -488,34 +510,41 @@ static int ProcessRequest(struct httprequest *req) /* get the number after it */ if(ptr) { + long num; ptr++; /* skip the dot */ - req->testno = strtol(ptr, &ptr, 10); + num = strtol(ptr, &ptr, 10); - if(req->testno > 10000) { - req->partno = req->testno % 10000; - req->testno /= 10000; + if(num) { + req->testno = num; + if(req->testno > 10000) { + req->partno = req->testno % 10000; + req->testno /= 10000; - logmsg("found test %d in requested host name", req->testno); + logmsg("found test %d in requested host name", req->testno); + } + else + req->partno = 0; } - else - req->partno = 0; - - msnprintf(logbuf, sizeof(logbuf), - "Requested test number %ld part %ld (from host name)", - req->testno, req->partno); - logmsg("%s", logbuf); + if(req->testno != DOCNUMBER_NOTHING) { + logmsg("Requested test number %ld part %ld (from host name)", + req->testno, req->partno); + } } - - if(!req->testno) { - logmsg("Did not find test number in PATH"); - req->testno = DOCNUMBER_404; - } - else - parse_servercmd(req); } + + if(req->testno == DOCNUMBER_NOTHING) + /* might get the test number */ + parse_cmdfile(req); + + if(req->testno == DOCNUMBER_NOTHING) { + logmsg("Did not find test number in PATH"); + req->testno = DOCNUMBER_404; + } + else + parse_servercmd(req); } else if((req->offset >= 3) && (req->testno == DOCNUMBER_NOTHING)) { logmsg("** Unusual request. Starts with %02x %02x %02x (%c%c%c)", @@ -1886,6 +1915,11 @@ int main(int argc, char *argv[]) if(argc>arg) serverlogfile = argv[arg++]; } + else if(!strcmp("--cmdfile", argv[arg])) { + arg++; + if(argc>arg) + cmdfile = argv[arg++]; + } else if(!strcmp("--gopher", argv[arg])) { arg++; use_gopher = TRUE;