From 1b78d071a9674fcbd55c5f8e675abe6e4df2bf23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?William=20B=C3=A9rub=C3=A9?= Date: Mon, 8 Apr 2024 22:41:52 -0400 Subject: [PATCH] Stabilized the POST request transfers, still missing some safeguards --- osd/config.h | 4 +++- osd/main.c | 5 +++-- osd/net.c | 55 +++++++++++++++++++++++++++++++++++++++------------- 3 files changed, 48 insertions(+), 16 deletions(-) diff --git a/osd/config.h b/osd/config.h index a81056a..bcfa175 100644 --- a/osd/config.h +++ b/osd/config.h @@ -1,4 +1,6 @@ -#define BUF_SIZE 65535 +// Good for ARGB8888 360x360 and ARGB1555 512x512 +#define BUF_SIZE (512*1024) +// OpenIPC font must be replaced for Unicode support #define DEF_FONT "UbuntuMono-Regular" #define DEF_POSX 16 #define DEF_POSY 16 diff --git a/osd/main.c b/osd/main.c index 1e5f6b9..7a6f06e 100644 --- a/osd/main.c +++ b/osd/main.c @@ -179,6 +179,7 @@ void route() void *payloade = memmem( payloadb, payload_size - (payloadb - payload), bound, strlen(bound)); + // TODO: Manage the case where payloade returns 0 payloade -= 4; char path[32]; @@ -201,7 +202,7 @@ void route() printf(\ "HTTP/1.1 415 Unsupported Media Type\r\n" \ "Content-Type: text/plain\r\n" \ - "Connection: close\r\n\r\n" \ + "Connection: close\r\n" \ "\r\n" \ "The payload must be presented as multipart/form-data.\r\n" \ ); @@ -278,7 +279,7 @@ void route() } printf(\ - "HTTP/1.1 500 Not Handled\r\n" \ + "HTTP/1.1 400 Bad Request\r\n" \ "Content-Type: text/plain\r\n" \ "Connection: close\r\n" \ "\r\n" \ diff --git a/osd/net.c b/osd/net.c index d0f4fe6..2331c01 100644 --- a/osd/net.c +++ b/osd/net.c @@ -151,21 +151,24 @@ void start_server() fatal("listen() error"); } -void respond(int n) +void respond(int slot) { int rcvd; + int total = 0; buf = malloc(BUF_SIZE); - rcvd = recv(clients[n], buf, BUF_SIZE, 0); + + rcvd = recv(clients[slot], buf + total, BUF_SIZE - total, 0); if (rcvd < 0) - fprintf(stderr, ("recv() error\n")); + goto finish; else if (rcvd == 0) - fprintf(stderr, "Client disconnected upexpectedly.\n"); - else - { - buf[rcvd] = '\0'; + fputs("Client disconnected unexpectedly.\n", stderr); + + total += rcvd; + if (total > 0) + { method = strtok(buf, " \t\r\n"); uri = strtok(NULL, " \t"); prot = strtok(NULL, " \t\r\n"); @@ -181,7 +184,7 @@ void respond(int n) char *t, *t2; while (h < reqhdr + 16) { - char *k, *v, *t; + char *k, *v, *e; k = strtok(NULL, "\r\n: \t"); if (!k) break; @@ -192,16 +195,42 @@ void respond(int n) h->value = v; h++; fprintf(stderr, "[H] %s: %s\n", k, v); - t = v + 1 + strlen(v); - if (t[1] == '\r' && t[2] == '\n') + e = v + 1 + strlen(v); + if (e[1] == '\r' && e[2] == '\n') break; } + t = strtok(NULL, "\r\n"); - t2 = request_header("Content-Length"); payload = t; - payload_size = t2 ? atol(t2) : (rcvd - (t - buf)); + t2 = request_header("Content-Length"); + payload_size = t2 ? atol(t2) : (total - (t - buf)); + + while (t2 && total < payload_size) + { + rcvd = recv(clients[slot], buf + total, BUF_SIZE - total, 0); + + if (rcvd < 0) + { + fputs("recv() error\n", stderr); + break; + } + else if (rcvd == 0) + { + fputs("Client disconnected unexpectedly.\n", stderr); + break; + } + + total += rcvd; + } + + if (!t) + { + t = strtok(NULL, "\r\n"); + payload = t; + } - int clientfd = clients[n]; +finish: + int clientfd = clients[slot]; dup2(clientfd, STDOUT_FILENO); close(clientfd);