client: Always close connection with request body in case of error
[project/uhttpd.git] / client.c
index 6233d01cd43508c6a9f15026985da21abead8cce..2be8f22a8eef89f7398c785ac21e6692e8a80676 100644 (file)
--- a/client.c
+++ b/client.c
@@ -138,6 +138,7 @@ void uh_request_done(struct client *cl)
 void __printf(4, 5)
 uh_client_error(struct client *cl, int code, const char *summary, const char *fmt, ...)
 {
+       struct http_request *r = &cl->request;
        va_list arg;
 
        uh_http_header(cl, code, summary);
@@ -151,6 +152,17 @@ uh_client_error(struct client *cl, int code, const char *summary, const char *fm
                va_end(arg);
        }
 
+       /* Close the connection even when keep alive is set, when it
+        * contains a request body, as it was not read and we are
+        * currently out of sync. Without handling this the body will be
+        * interpreted as part of the next request. The alternative
+        * would be to read and discard the request body here.
+        */
+       if (r->transfer_chunked || r->content_length > 0) {
+               cl->state = CLIENT_STATE_CLOSE;
+               cl->request.connection_close = true;
+       }
+
        uh_request_done(cl);
 }