Stop immediately after responding with status code 304 "Not Modified". This 
also solves missing log output for status 304.

If there is an error while sending a file, try to clean up and close the file.
---
 http.c | 1 +
 resp.c | 6 ++++--
 2 files changed, 5 insertions(+), 2 deletions(-)

diff --git a/http.c b/http.c
index efc4136..249c168 100644
--- a/http.c
+++ b/http.c
@@ -541,6 +541,7 @@ http_send_response(int fd, struct request *r)
                                    timestamp(time(NULL), t)) < 0) {
                                return S_REQUEST_TIMEOUT;
                        }
+                       return S_NOT_MODIFIED;
                }
        }

diff --git a/resp.c b/resp.c
index fbd8d5b..1716605 100644
--- a/resp.c
+++ b/resp.c
@@ -216,14 +216,16 @@ resp_file(int fd, char *name, struct request *r, struct 
stat *st, char *mime,
                while ((bread = fread(buf, 1, MIN(sizeof(buf),
                                      (size_t)remaining), fp))) {
                        if (bread < 0) {
-                               return S_INTERNAL_SERVER_ERROR;
+                               s = S_INTERNAL_SERVER_ERROR;
+                               goto cleanup;
                        }
                        remaining -= bread;
                        p = buf;
                        while (bread > 0) {
                                bwritten = write(fd, p, bread);
                                if (bwritten <= 0) {
-                                       return S_REQUEST_TIMEOUT;
+                                       s = S_REQUEST_TIMEOUT;
+                                       goto cleanup;
                                }
                                bread -= bwritten;
                                p += bwritten;
--
2.20.1


Reply via email to