From 1e1977bc0fbef60a3dfb70c6eb65d24435d8d035 Mon Sep 17 00:00:00 2001 From: Stephen Heumann Date: Sat, 13 Apr 2019 12:57:35 -0500 Subject: [PATCH] HTTP tweaks and test program. I switched to using HTTP/1.0 for now to prevent the server from sending chunked transfer-encoding, which this code currently doesn't support. --- Makefile | 8 +++++- http.c | 11 +++++--- http.h | 2 +- httptest.c | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 97 insertions(+), 6 deletions(-) create mode 100644 httptest.c diff --git a/Makefile b/Makefile index 3fcfd5b..781e410 100644 --- a/Makefile +++ b/Makefile @@ -4,11 +4,14 @@ CFLAGS = -w-1 -O-1 JSONTEST_OBJS = jsontest.a json.a jsonutil.a JSONTEST_PROG = jsontest +HTTPTEST_OBJS = httptest.a hostname.a http.a readtcp.a seturl.a strcasecmp.a tcpconnection.a urlparser.a +HTTPTEST_PROG = httptest + DISKBROWSER_OBJS = diskbrowser.a hostname.a http.a json.a jsonutil.a readtcp.a seturl.a strcasecmp.a tcpconnection.a urlparser.a DISKBROWSER_RSRC = diskbrowser.rez DISKBROWSER_PROG = DiskBrowser -PROGS = $(JSONTEST_PROG) $(DISKBROWSER_PROG) +PROGS = $(JSONTEST_PROG) $(HTTPTEST_PROG) $(DISKBROWSER_PROG) .PHONY: default default: $(PROGS) @@ -16,6 +19,9 @@ default: $(PROGS) $(JSONTEST_PROG): $(JSONTEST_OBJS) $(CC) $(CFLAGS) -o $@ $^ +$(HTTPTEST_PROG): $(HTTPTEST_OBJS) + $(CC) $(CFLAGS) -o $@ $^ + $(DISKBROWSER_PROG): $(DISKBROWSER_OBJS) $(DISKBROWSER_RSRC) $(CC) $(CFLAGS) -o $@ $(DISKBROWSER_OBJS) occ $(DISKBROWSER_RSRC) -o $@ diff --git a/http.c b/http.c index 33c108a..edeca3e 100644 --- a/http.c +++ b/http.c @@ -66,12 +66,13 @@ Boolean BuildHTTPRequest(Session *sess, char *resourceStr) { sizeNeeded = 0; do { sizeNeeded = snprintf(sess->httpRequest, sizeNeeded, - "GET /%s HTTP/1.1\r\n" + "GET /%s HTTP/1.0\r\n" "Host: %s\r\n" "User-Agent: GS-Disk-Browser/" USER_AGENT_VERSION "\r\n" "Accept-Encoding: identity\r\n" //"Accept: */*\r\n" /* default, but some clients send explicitly */ - //"Connection: Keep-Alive\r\n" /* same */ + //"Connection: Keep-Alive\r\n" /* same (in HTTP/1.1) */ + "Connection: close\r\n" "\r\n", resourceStr, sess->hostName+1); @@ -94,13 +95,15 @@ Boolean BuildHTTPRequest(Session *sess, char *resourceStr) { } } while (round++ == 0); + sess->httpRequestLen = sizeNeeded; + free(escapedStr); return TRUE; } enum NetDiskError -DoHTTPRequest(Session *sess, unsigned long start, unsigned long end) { +DoHTTPRequest(Session *sess) { top:; union { srBuff srBuff; @@ -184,7 +187,7 @@ netRetry: if (strncmp(response, "HTTP/1.", 7) != 0) goto errorReturn; response += 7; - if (!(*response >= '1' && *response <= '9')) + if (!(*response >= '0' && *response <= '9')) goto errorReturn; response += 1; if (*response != ' ') diff --git a/http.h b/http.h index f18d1d2..f43d48f 100644 --- a/http.h +++ b/http.h @@ -6,6 +6,6 @@ #include "netdiskerror.h" Boolean BuildHTTPRequest(Session *sess, char *resourceStr); -enum NetDiskError DoHTTPRequest(Session *sess, unsigned long start, unsigned long end); +enum NetDiskError DoHTTPRequest(Session *sess); #endif \ No newline at end of file diff --git a/httptest.c b/httptest.c new file mode 100644 index 0000000..0e8d2ec --- /dev/null +++ b/httptest.c @@ -0,0 +1,82 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include "http.h" +#include "session.h" +#include "seturl.h" +#include "hostname.h" +#include "tcpconnection.h" +#include "readtcp.h" + +int main(int argc, char **argv) { + TLStartUp(); + + LoadOneTool(54,0x200); + TCPIPStartUp(); + + if (TCPIPGetConnectStatus() == FALSE) { + TCPIPConnect(NULL); + if (toolerror()) + goto exit; + } + + Session sess = {0}; + + if (argc < 2) + goto exit; + + char *url = argv[1]; + + enum NetDiskError result = SetURL(&sess, url, TRUE, FALSE); + + if (result != OPERATION_SUCCESSFUL) { + printf("SetURL error %i\n", (int)result); + goto exit; + } + + printf("Request:\n"); + printf("=========\n"); + int i; + for (i = 0; sess.httpRequest[i] != '\0'; i++) { + char ch = sess.httpRequest[i]; + if (ch != '\r') + putchar(ch); + } + printf("=========\n"); + printf("\n"); + + enum NetDiskError requestResult; + requestResult = DoHTTPRequest(&sess); + printf("RequestResult %i\n", requestResult); + printf("Response code %lu\n", sess.responseCode); + + if (requestResult == OPERATION_SUCCESSFUL) { + printf("contentLength = %lu\n", sess.contentLength); + } + + if (sess.contentLength == 0) + sess.contentLength = 0xffff; + + char *buf = malloc(sess.contentLength); + if (buf == NULL) + goto cleanup; + + InitReadTCP(&sess, sess.contentLength, buf); + while (TryReadTCP(&sess) == rsWaiting) + /* keep reading */ ; + + printf("Read %lu bytes\n", sess.contentLength - sess.readCount); + +cleanup: + EndTCPConnection(&sess); + +exit: + TCPIPShutDown(); + UnloadOneTool(54); + TLShutDown(); +}