From 0311c858c3ac4f78e3afd33c1914150f28e82e35 Mon Sep 17 00:00:00 2001 From: Camden Dixie O'Brien Date: Fri, 14 Oct 2022 11:38:47 +0100 Subject: [PATCH] Send incomplete response instead of dropping request for most errors --- main.c | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/main.c b/main.c index d657d07..d6e2e1d 100644 --- a/main.c +++ b/main.c @@ -223,14 +223,14 @@ int main(int argc, char *argv[]) } n = append_selector_part(sp, splen, &pbuf[ppos], PBUF_SIZE - ppos); if (n == -1) - goto close_client_socket; + goto send_response; ppos += n; sp = &sbuf[i + 1]; splen = 0; } n = append_selector_part(sp, splen, &pbuf[ppos], PBUF_SIZE - ppos); if (n == -1) - goto close_client_socket; + goto send_response; ppos += n; /* @@ -242,13 +242,13 @@ int main(int argc, char *argv[]) */ if (ppos + 1 > PBUF_SIZE) { fprintf(stderr, "Path buffer is too small\n"); - goto close_client_socket; + goto send_response; } pbuf[ppos] = '\0'; struct stat rstat; if (stat(pbuf, &rstat) == -1) { fprintf(stderr, "Failed to stat() path \"%s\"\n", pbuf); - goto close_client_socket; + goto send_response; } if (S_ISREG(rstat.st_mode)) { /* @@ -264,7 +264,7 @@ int main(int argc, char *argv[]) } while (errno == EINTR); if (rf == NULL) { fprintf(stderr, "Failed to open %s\n", pbuf); - goto close_client_socket; + goto send_response; } /* @@ -280,7 +280,7 @@ int main(int argc, char *argv[]) n = fread(fbuf, sizeof(*fbuf), FBUF_SIZE, rf); if (n != FBUF_SIZE && ferror(rf)) { fprintf(stderr, "Failed to read from file %s\n", pbuf); - goto close_client_socket; + goto send_response; } /* Copy from fbuf to rbuf, replacing LF with CRLF. */ @@ -288,14 +288,14 @@ int main(int argc, char *argv[]) if (fbuf[fpos] == '\n') { if (RBUF_SIZE - rlen < 2) { fprintf(stderr, "Response buffer is too small"); - goto close_client_socket; + goto send_response; } rbuf[rlen++] = '\r'; rbuf[rlen++] = '\n'; } else { if (RBUF_SIZE - rlen < 1) { fprintf(stderr, "Response buffer is too small"); - goto close_client_socket; + goto send_response; } rbuf[rlen++] = fbuf[fpos]; } @@ -318,7 +318,7 @@ int main(int argc, char *argv[]) } while (errno == EINTR); if (rdir == NULL) { fprintf(stderr, "Failed to open %s\n", pbuf); - goto close_client_socket; + goto send_response; } /* @@ -343,7 +343,7 @@ int main(int argc, char *argv[]) namelen = strlen(ent->d_name); if (ppos + namelen + 2 > PBUF_SIZE) { fprintf(stderr, "Path buffer is too small\n"); - goto close_client_socket; + goto send_response; } pbuf[ppos] = '/'; memcpy(&pbuf[ppos + 1], ent->d_name, namelen); @@ -374,7 +374,7 @@ int main(int argc, char *argv[]) &pbuf[srvroot_len], HOST, PORT); if (n >= RBUF_SIZE - rlen) { fprintf(stderr, "Response buffer was too small\n"); - goto close_client_socket; + goto send_response; } rlen += n; } @@ -388,12 +388,13 @@ int main(int argc, char *argv[]) "Requested resource \"%s\" was not a directory or a " "regular file\n", pbuf); - goto close_client_socket; + goto send_response; } /* * Terminate and send the response. */ + send_response: if (RBUF_SIZE - rlen < RESP_TERM_LEN) { fprintf(stderr, "Response buffer is too small"); goto close_client_socket;