]> git.seodisparate.com - c_simple_http/commitdiff
Use "Connection: close" in response headers
authorStephen Seo <seo.disparate@gmail.com>
Mon, 23 Sep 2024 10:44:51 +0000 (19:44 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Mon, 23 Sep 2024 10:44:51 +0000 (19:44 +0900)
The current implementation always closes the connection after sending
the response, so it should notify the client to close the connection.

src/http.c
src/main.c

index ceccbcd9ec4f148fd943c3436edfd62ec8368915..3d77df82090cd476fd3f88d91438c7c52e5ce0b8 100644 (file)
@@ -39,16 +39,21 @@ const char *c_simple_http_response_code_error_to_response(
     enum C_SIMPLE_HTTP_ResponseCode response_code) {
   switch (response_code) {
     case C_SIMPLE_HTTP_Response_400_Bad_Request:
-      return "HTTP/1.1 400 Bad Request\nAllow: GET\nContent-Type: text/html\n"
+      return "HTTP/1.1 400 Bad Request\nAllow: GET\n"
+             "Connection: close\n"
+             "Content-Type: text/html\n"
              "Content-Length: 25\n\n"
              "<h1>400 Bad Request</h1>\n";
     case C_SIMPLE_HTTP_Response_404_Not_Found:
-      return "HTTP/1.1 404 Not Found\nAllow: GET\nContent-Type: text/html\n"
+      return "HTTP/1.1 404 Not Found\nAllow: GET\n"
+             "Connection: close\n"
+             "Content-Type: text/html\n"
              "Content-Length: 23\n\n"
              "<h1>404 Not Found</h1>\n";
     case C_SIMPLE_HTTP_Response_500_Internal_Server_Error:
     default:
       return "HTTP/1.1 500 Internal Server Error\nAllow: GET\n"
+             "Connection: close\n"
              "Content-Type: text/html\n"
              "Content-Length: 35\n\n"
              "<h1>500 Internal Server Error</h1>\n";
index 035c24dc2247933409fc5a8610618bf9762be4dd..7c65510a3852c958d9ca1a9a26d02ac95a5594ae 100644 (file)
@@ -363,6 +363,7 @@ int main(int argc, char **argv) {
       if (response && response_code == C_SIMPLE_HTTP_Response_200_OK) {
         CHECK_ERROR_WRITE(write(connection_fd, "HTTP/1.1 200 OK\n", 16));
         CHECK_ERROR_WRITE(write(connection_fd, "Allow: GET\n", 11));
+        CHECK_ERROR_WRITE(write(connection_fd, "Connection: close\n", 18));
         CHECK_ERROR_WRITE(write(
           connection_fd, "Content-Type: text/html\n", 24));
         char content_length_buf[128];
@@ -400,6 +401,7 @@ int main(int argc, char **argv) {
           CHECK_ERROR_WRITE(write(
             connection_fd, "HTTP/1.1 500 Internal Server Error\n", 35));
           CHECK_ERROR_WRITE(write(connection_fd, "Allow: GET\n", 11));
+          CHECK_ERROR_WRITE(write(connection_fd, "Connection: close\n", 18));
           CHECK_ERROR_WRITE(write(
             connection_fd, "Content-Type: text/html\n", 24));
           CHECK_ERROR_WRITE(write(connection_fd, "Content-Length: 35\n", 19));