Skip to content

Commit

Permalink
Stabilized the POST request transfers, still missing some safeguards
Browse files Browse the repository at this point in the history
  • Loading branch information
wberube committed Apr 9, 2024
1 parent d046688 commit 1b78d07
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 16 deletions.
4 changes: 3 additions & 1 deletion osd/config.h
Original file line number Diff line number Diff line change
@@ -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
Expand Down
5 changes: 3 additions & 2 deletions osd/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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" \
);
Expand Down Expand Up @@ -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" \
Expand Down
55 changes: 42 additions & 13 deletions osd/net.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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;
Expand All @@ -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);

Expand Down

0 comments on commit 1b78d07

Please sign in to comment.