From eb41be3884ea25aad05c99da562e014dbff0e1e3 Mon Sep 17 00:00:00 2001 From: hhm Date: Sun, 14 Sep 2014 06:28:03 -0400 Subject: [PATCH 1/6] B"H net_io: http: check if file can be sent --- net_io.c | 22 +++++++++++++++++----- winstubs.h | 1 + 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/net_io.c b/net_io.c index 25abb88..67dfa2c 100644 --- a/net_io.c +++ b/net_io.c @@ -741,14 +741,26 @@ int handleHTTPRequest(struct client *c, char *p) { } else { struct stat sbuf; int fd = -1; + char *rp, *hrp; - if (stat(getFile, &sbuf) != -1 && (fd = open(getFile, O_RDONLY)) != -1) { - content = (char *) malloc(sbuf.st_size); - if (read(fd, content, sbuf.st_size) == -1) { - snprintf(content, sbuf.st_size, "Error reading from file: %s", strerror(errno)); + rp = realpath(getFile, NULL); + hrp = realpath(HTMLPATH, NULL); + hrp = (hrp ? hrp : HTMLPATH); + clen = -1; + content = "Server error"; + if (rp && (!strncmp(hrp, rp, strlen(hrp)))) { + if (stat(getFile, &sbuf) != -1 && (fd = open(getFile, O_RDONLY)) != -1) { + content = (char *) malloc(sbuf.st_size); + if (read(fd, content, sbuf.st_size) != -1) { + clen = sbuf.st_size; + free(content); + } } - clen = sbuf.st_size; } else { + errno = ENOENT; + } + + if (clen < 0) { char buf[128]; clen = snprintf(buf,sizeof(buf),"Error opening HTML file: %s", strerror(errno)); content = strdup(buf); diff --git a/winstubs.h b/winstubs.h index f416668..051e2f0 100644 --- a/winstubs.h +++ b/winstubs.h @@ -75,6 +75,7 @@ _inline uint64_t strtoll(const char *p, void *e, UINT32 base) {return _atoi64(p) _inline int inet_aton(const char * cp, DWORD * ulAddr) { *ulAddr = inet_addr(cp); return 0;} #define snprintf _snprintf #define vsnprintf _vsnprintf +#define realpath(A, B) _fullpath(B, A, _MAX_PATH) _inline void cls() { HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE); From 04f4abda709f13545a813c5090a49a54540da5d3 Mon Sep 17 00:00:00 2001 From: hhm Date: Mon, 15 Sep 2014 09:08:49 -0400 Subject: [PATCH 2/6] B"H net_io.c: add missing else statement --- net_io.c | 1 + 1 file changed, 1 insertion(+) diff --git a/net_io.c b/net_io.c index 67dfa2c..1679ef1 100644 --- a/net_io.c +++ b/net_io.c @@ -753,6 +753,7 @@ int handleHTTPRequest(struct client *c, char *p) { content = (char *) malloc(sbuf.st_size); if (read(fd, content, sbuf.st_size) != -1) { clen = sbuf.st_size; + } else { free(content); } } From 8d307cd0ebe8ac2b446d717d048dc11951c3eb29 Mon Sep 17 00:00:00 2001 From: hhm Date: Wed, 17 Sep 2014 06:32:03 -0400 Subject: [PATCH 3/6] B"H net_io.c: http serve: return HTTP response codes --- net_io.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/net_io.c b/net_io.c index 1679ef1..91cf47d 100644 --- a/net_io.c +++ b/net_io.c @@ -695,6 +695,7 @@ int handleHTTPRequest(struct client *c, char *p) { char hdr[512]; int clen, hdrlen; int httpver, keepalive; + int statuscode = 500; char *url, *content; char ctype[48]; char getFile[1024]; @@ -736,6 +737,7 @@ int handleHTTPRequest(struct client *c, char *p) { // "/" -> Our google map application. // "/data.json" -> Our ajax request to update planes. if (strstr(url, "/data.json")) { + statuscode = 200; content = aircraftsToJson(&clen); //snprintf(ctype, sizeof ctype, MODES_CONTENT_TYPE_JSON); } else { @@ -753,6 +755,7 @@ int handleHTTPRequest(struct client *c, char *p) { content = (char *) malloc(sbuf.st_size); if (read(fd, content, sbuf.st_size) != -1) { clen = sbuf.st_size; + statuscode = 200; } else { free(content); } @@ -764,6 +767,7 @@ int handleHTTPRequest(struct client *c, char *p) { if (clen < 0) { char buf[128]; clen = snprintf(buf,sizeof(buf),"Error opening HTML file: %s", strerror(errno)); + statuscode = 404; content = strdup(buf); } @@ -788,7 +792,7 @@ int handleHTTPRequest(struct client *c, char *p) { // Create the header and send the reply hdrlen = snprintf(hdr, sizeof(hdr), - "HTTP/1.1 200 OK\r\n" + "HTTP/1.1 %i \r\n" "Server: Dump1090\r\n" "Content-Type: %s\r\n" "Connection: %s\r\n" @@ -796,6 +800,7 @@ int handleHTTPRequest(struct client *c, char *p) { "Cache-Control: no-cache, must-revalidate\r\n" "Expires: Sat, 26 Jul 1997 05:00:00 GMT\r\n" "\r\n", + statuscode, ctype, keepalive ? "keep-alive" : "close", clen); From df19d51bc633e80ca714ce4c52438c3631024f7e Mon Sep 17 00:00:00 2001 From: hhm Date: Wed, 17 Sep 2014 06:58:53 -0400 Subject: [PATCH 4/6] B"H net_io.c: use %d like local code --- net_io.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/net_io.c b/net_io.c index 91cf47d..10e09d0 100644 --- a/net_io.c +++ b/net_io.c @@ -792,7 +792,7 @@ int handleHTTPRequest(struct client *c, char *p) { // Create the header and send the reply hdrlen = snprintf(hdr, sizeof(hdr), - "HTTP/1.1 %i \r\n" + "HTTP/1.1 %d \r\n" "Server: Dump1090\r\n" "Content-Type: %s\r\n" "Connection: %s\r\n" From 32fc5fddd0900b446600dbf05e88d44187d4b5f5 Mon Sep 17 00:00:00 2001 From: hhm Date: Wed, 17 Sep 2014 07:32:34 -0400 Subject: [PATCH 5/6] B"H net_io.c: http server: content should be dynamically allocated --- net_io.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/net_io.c b/net_io.c index 10e09d0..4a19429 100644 --- a/net_io.c +++ b/net_io.c @@ -749,7 +749,7 @@ int handleHTTPRequest(struct client *c, char *p) { hrp = realpath(HTMLPATH, NULL); hrp = (hrp ? hrp : HTMLPATH); clen = -1; - content = "Server error"; + content = calloc(1, 1); if (rp && (!strncmp(hrp, rp, strlen(hrp)))) { if (stat(getFile, &sbuf) != -1 && (fd = open(getFile, O_RDONLY)) != -1) { content = (char *) malloc(sbuf.st_size); From c265ea1d553c0fb09e22b72ed51058dfea9df35c Mon Sep 17 00:00:00 2001 From: hhm Date: Wed, 17 Sep 2014 07:58:19 -0400 Subject: [PATCH 6/6] B"H net_io.c http server: hopefully mem code is OK now Still learning the ropes memory-wise :-) --- net_io.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/net_io.c b/net_io.c index 4a19429..d1bbd39 100644 --- a/net_io.c +++ b/net_io.c @@ -749,15 +749,13 @@ int handleHTTPRequest(struct client *c, char *p) { hrp = realpath(HTMLPATH, NULL); hrp = (hrp ? hrp : HTMLPATH); clen = -1; - content = calloc(1, 1); + content = strdup("Server error occured"); if (rp && (!strncmp(hrp, rp, strlen(hrp)))) { if (stat(getFile, &sbuf) != -1 && (fd = open(getFile, O_RDONLY)) != -1) { - content = (char *) malloc(sbuf.st_size); + content = (char *) realloc(content, sbuf.st_size); if (read(fd, content, sbuf.st_size) != -1) { clen = sbuf.st_size; statuscode = 200; - } else { - free(content); } } } else { @@ -766,9 +764,9 @@ int handleHTTPRequest(struct client *c, char *p) { if (clen < 0) { char buf[128]; - clen = snprintf(buf,sizeof(buf),"Error opening HTML file: %s", strerror(errno)); + content = realloc(content, sizeof(buf)); + clen = snprintf(content,sizeof(buf),"Error opening HTML file: %s", strerror(errno)); statuscode = 404; - content = strdup(buf); } if (fd != -1) { @@ -800,7 +798,7 @@ int handleHTTPRequest(struct client *c, char *p) { "Cache-Control: no-cache, must-revalidate\r\n" "Expires: Sat, 26 Jul 1997 05:00:00 GMT\r\n" "\r\n", - statuscode, + statuscode, ctype, keepalive ? "keep-alive" : "close", clen);