#ifdef ENABLE_IPV6
static bool use_ipv6 = FALSE;
#endif
+static bool use_gopher = FALSE;
static const char *ipv_inuse = "IPv4";
static int serverlogslocked = 0;
DOCNUMBER_404 = -1
};
+const char *end_of_headers = END_OF_HEADERS;
/* sent as reply to a QUIT */
static const char *docquit =
int prot_major, prot_minor;
char *end;
int error;
- end = strstr(line, END_OF_HEADERS);
+ end = strstr(line, end_of_headers);
logmsg("ProcessRequest() called");
/* try to figure out the request characteristics as soon as possible, but
only once! */
- if((req->testno == DOCNUMBER_NOTHING) &&
+
+ if(use_gopher &&
+ (req->testno == DOCNUMBER_NOTHING) &&
+ !strncmp("/verifiedserver", line, 15)) {
+ logmsg("Are-we-friendly question received");
+ req->testno = DOCNUMBER_WERULEZ;
+ return 1; /* done */
+ }
+
+ else if((req->testno == DOCNUMBER_NOTHING) &&
sscanf(line,
"%" REQUEST_KEYWORD_SIZE_TXT"s %" MAXDOCNAMELEN_TXT "s HTTP/%d.%d",
request,
}
logmsg("ProcessRequest found a complete request");
+ if(use_gopher) {
+ /* when using gopher we cannot check the request until the entire
+ thing has been received */
+ char *ptr;
+
+ /* find the last slash in the line */
+ ptr = strrchr(line, '/');
+
+ if(ptr) {
+ ptr++; /* skip the slash */
+
+ /* skip all non-numericals following the slash */
+ while(*ptr && !ISDIGIT(*ptr))
+ ptr++;
+
+ req->testno = strtol(ptr, &ptr, 10);
+
+ if(req->testno > 10000) {
+ req->partno = req->testno % 10000;
+ req->testno /= 10000;
+ }
+ else
+ req->partno = 0;
+
+ sprintf(logbuf, "Requested GOPHER test number %ld part %ld",
+ req->testno, req->partno);
+ logmsg("%s", logbuf);
+ }
+ }
+
if(req->pipe)
/* we do have a full set, advance the checkindex to after the end of the
headers, for the pipelining case mostly */
- req->checkindex += (end - line) + strlen(END_OF_HEADERS);
+ req->checkindex += (end - line) + strlen(end_of_headers);
/* **** Persistence ****
*
req->open &&
req->prot_version >= 11 &&
end &&
- req->reqbuf + req->offset > end + strlen(END_OF_HEADERS) &&
+ req->reqbuf + req->offset > end + strlen(end_of_headers) &&
(!strncmp(req->reqbuf, "GET", strlen("GET")) ||
!strncmp(req->reqbuf, "HEAD", strlen("HEAD")))) {
/* If we have a persistent connection, HTTP version >= 1.1
and GET/HEAD request, enable pipelining. */
- req->checkindex = (end - req->reqbuf) + strlen(END_OF_HEADERS);
+ req->checkindex = (end - req->reqbuf) + strlen(end_of_headers);
req->pipelining = TRUE;
}
return 1; /* done */
/* scan for more header ends within this chunk */
line = &req->reqbuf[req->checkindex];
- end = strstr(line, END_OF_HEADERS);
+ end = strstr(line, end_of_headers);
if(!end)
break;
- req->checkindex += (end - line) + strlen(END_OF_HEADERS);
+ req->checkindex += (end - line) + strlen(end_of_headers);
req->pipe--;
}
return 1; /* done */
if(req->cl > 0) {
- if(req->cl <= req->offset - (end - req->reqbuf) - strlen(END_OF_HEADERS))
+ if(req->cl <= req->offset - (end - req->reqbuf) - strlen(end_of_headers))
return 1; /* done */
else
return 0; /* not complete yet */
logmsg("Identifying ourselves as friends");
sprintf(msgbuf, "WE ROOLZ: %ld\r\n", (long)getpid());
msglen = strlen(msgbuf);
- sprintf(weare, "HTTP/1.1 200 OK\r\nContent-Length: %zu\r\n\r\n%s",
- msglen, msgbuf);
+ if(use_gopher)
+ sprintf(weare, "%s", msgbuf);
+ else
+ sprintf(weare, "HTTP/1.1 200 OK\r\nContent-Length: %zu\r\n\r\n%s",
+ msglen, msgbuf);
buffer = weare;
break;
case DOCNUMBER_INTERNAL:
if(cmd)
free(cmd);
- req->open = persistant;
+ req->open = use_gopher?FALSE:persistant;
prevtestno = req->testno;
prevpartno = req->partno;
if(argc>arg)
serverlogfile = argv[arg++];
}
+ else if(!strcmp("--gopher", argv[arg])) {
+ arg++;
+ use_gopher = TRUE;
+ end_of_headers = "\r\n"; /* gopher style is much simpler */
+ }
else if(!strcmp("--ipv4", argv[arg])) {
#ifdef ENABLE_IPV6
ipv_inuse = "IPv4";
" --ipv6\n"
" --port [port]\n"
" --srcdir [path]\n"
+ " --gopher\n"
" --fork");
return 0;
}
goto sws_cleanup;
}
- logmsg("Running %s version on port %d", ipv_inuse, (int)port);
+ logmsg("Running %s %s version on port %d",
+ use_gopher?"GOPHER":"HTTP", ipv_inuse, (int)port);
/* start accepting connections */
rc = listen(sock, 5);