Send incomplete response instead of dropping request for most errors

This commit is contained in:
Camden Dixie O'Brien 2022-10-14 11:38:47 +01:00
parent 58c7b6feab
commit 0311c858c3

25
main.c
View File

@ -223,14 +223,14 @@ int main(int argc, char *argv[])
} }
n = append_selector_part(sp, splen, &pbuf[ppos], PBUF_SIZE - ppos); n = append_selector_part(sp, splen, &pbuf[ppos], PBUF_SIZE - ppos);
if (n == -1) if (n == -1)
goto close_client_socket; goto send_response;
ppos += n; ppos += n;
sp = &sbuf[i + 1]; sp = &sbuf[i + 1];
splen = 0; splen = 0;
} }
n = append_selector_part(sp, splen, &pbuf[ppos], PBUF_SIZE - ppos); n = append_selector_part(sp, splen, &pbuf[ppos], PBUF_SIZE - ppos);
if (n == -1) if (n == -1)
goto close_client_socket; goto send_response;
ppos += n; ppos += n;
/* /*
@ -242,13 +242,13 @@ int main(int argc, char *argv[])
*/ */
if (ppos + 1 > PBUF_SIZE) { if (ppos + 1 > PBUF_SIZE) {
fprintf(stderr, "Path buffer is too small\n"); fprintf(stderr, "Path buffer is too small\n");
goto close_client_socket; goto send_response;
} }
pbuf[ppos] = '\0'; pbuf[ppos] = '\0';
struct stat rstat; struct stat rstat;
if (stat(pbuf, &rstat) == -1) { if (stat(pbuf, &rstat) == -1) {
fprintf(stderr, "Failed to stat() path \"%s\"\n", pbuf); fprintf(stderr, "Failed to stat() path \"%s\"\n", pbuf);
goto close_client_socket; goto send_response;
} }
if (S_ISREG(rstat.st_mode)) { if (S_ISREG(rstat.st_mode)) {
/* /*
@ -264,7 +264,7 @@ int main(int argc, char *argv[])
} while (errno == EINTR); } while (errno == EINTR);
if (rf == NULL) { if (rf == NULL) {
fprintf(stderr, "Failed to open %s\n", pbuf); 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); n = fread(fbuf, sizeof(*fbuf), FBUF_SIZE, rf);
if (n != FBUF_SIZE && ferror(rf)) { if (n != FBUF_SIZE && ferror(rf)) {
fprintf(stderr, "Failed to read from file %s\n", pbuf); 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. */ /* Copy from fbuf to rbuf, replacing LF with CRLF. */
@ -288,14 +288,14 @@ int main(int argc, char *argv[])
if (fbuf[fpos] == '\n') { if (fbuf[fpos] == '\n') {
if (RBUF_SIZE - rlen < 2) { if (RBUF_SIZE - rlen < 2) {
fprintf(stderr, "Response buffer is too small"); fprintf(stderr, "Response buffer is too small");
goto close_client_socket; goto send_response;
} }
rbuf[rlen++] = '\r'; rbuf[rlen++] = '\r';
rbuf[rlen++] = '\n'; rbuf[rlen++] = '\n';
} else { } else {
if (RBUF_SIZE - rlen < 1) { if (RBUF_SIZE - rlen < 1) {
fprintf(stderr, "Response buffer is too small"); fprintf(stderr, "Response buffer is too small");
goto close_client_socket; goto send_response;
} }
rbuf[rlen++] = fbuf[fpos]; rbuf[rlen++] = fbuf[fpos];
} }
@ -318,7 +318,7 @@ int main(int argc, char *argv[])
} while (errno == EINTR); } while (errno == EINTR);
if (rdir == NULL) { if (rdir == NULL) {
fprintf(stderr, "Failed to open %s\n", pbuf); 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); namelen = strlen(ent->d_name);
if (ppos + namelen + 2 > PBUF_SIZE) { if (ppos + namelen + 2 > PBUF_SIZE) {
fprintf(stderr, "Path buffer is too small\n"); fprintf(stderr, "Path buffer is too small\n");
goto close_client_socket; goto send_response;
} }
pbuf[ppos] = '/'; pbuf[ppos] = '/';
memcpy(&pbuf[ppos + 1], ent->d_name, namelen); memcpy(&pbuf[ppos + 1], ent->d_name, namelen);
@ -374,7 +374,7 @@ int main(int argc, char *argv[])
&pbuf[srvroot_len], HOST, PORT); &pbuf[srvroot_len], HOST, PORT);
if (n >= RBUF_SIZE - rlen) { if (n >= RBUF_SIZE - rlen) {
fprintf(stderr, "Response buffer was too small\n"); fprintf(stderr, "Response buffer was too small\n");
goto close_client_socket; goto send_response;
} }
rlen += n; rlen += n;
} }
@ -388,12 +388,13 @@ int main(int argc, char *argv[])
"Requested resource \"%s\" was not a directory or a " "Requested resource \"%s\" was not a directory or a "
"regular file\n", "regular file\n",
pbuf); pbuf);
goto close_client_socket; goto send_response;
} }
/* /*
* Terminate and send the response. * Terminate and send the response.
*/ */
send_response:
if (RBUF_SIZE - rlen < RESP_TERM_LEN) { if (RBUF_SIZE - rlen < RESP_TERM_LEN) {
fprintf(stderr, "Response buffer is too small"); fprintf(stderr, "Response buffer is too small");
goto close_client_socket; goto close_client_socket;