backport: "Connection: close" in response headers
All checks were successful
Run Unit Tests / build-and-run-unit-tests (push) Successful in 9s

The current implementation always closes the connection after sending
the response, so it should notify the client to close the connection.
This commit is contained in:
Stephen Seo 2024-09-23 19:44:51 +09:00
parent f22a523b45
commit 8eb0578454
2 changed files with 9 additions and 2 deletions

View file

@ -36,16 +36,21 @@ const char *c_simple_http_response_code_error_to_response(
enum C_SIMPLE_HTTP_ResponseCode response_code) { enum C_SIMPLE_HTTP_ResponseCode response_code) {
switch (response_code) { switch (response_code) {
case C_SIMPLE_HTTP_Response_400_Bad_Request: 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" "Content-Length: 25\n\n"
"<h1>400 Bad Request</h1>\n"; "<h1>400 Bad Request</h1>\n";
case C_SIMPLE_HTTP_Response_404_Not_Found: 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" "Content-Length: 23\n\n"
"<h1>404 Not Found</h1>\n"; "<h1>404 Not Found</h1>\n";
case C_SIMPLE_HTTP_Response_500_Internal_Server_Error: case C_SIMPLE_HTTP_Response_500_Internal_Server_Error:
default: default:
return "HTTP/1.1 500 Internal Server Error\nAllow: GET\n" return "HTTP/1.1 500 Internal Server Error\nAllow: GET\n"
"Connection: close\n"
"Content-Type: text/html\n" "Content-Type: text/html\n"
"Content-Length: 35\n\n" "Content-Length: 35\n\n"
"<h1>500 Internal Server Error</h1>\n"; "<h1>500 Internal Server Error</h1>\n";

View file

@ -363,6 +363,7 @@ int main(int argc, char **argv) {
if (response && response_code == C_SIMPLE_HTTP_Response_200_OK) { 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, "HTTP/1.1 200 OK\n", 16));
CHECK_ERROR_WRITE(write(connection_fd, "Allow: GET\n", 11)); CHECK_ERROR_WRITE(write(connection_fd, "Allow: GET\n", 11));
CHECK_ERROR_WRITE(write(connection_fd, "Connection: close\n", 18));
CHECK_ERROR_WRITE(write( CHECK_ERROR_WRITE(write(
connection_fd, "Content-Type: text/html\n", 24)); connection_fd, "Content-Type: text/html\n", 24));
char content_length_buf[128]; char content_length_buf[128];
@ -400,6 +401,7 @@ int main(int argc, char **argv) {
CHECK_ERROR_WRITE(write( CHECK_ERROR_WRITE(write(
connection_fd, "HTTP/1.1 500 Internal Server Error\n", 35)); 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, "Allow: GET\n", 11));
CHECK_ERROR_WRITE(write(connection_fd, "Connection: close\n", 18));
CHECK_ERROR_WRITE(write( CHECK_ERROR_WRITE(write(
connection_fd, "Content-Type: text/html\n", 24)); connection_fd, "Content-Type: text/html\n", 24));
CHECK_ERROR_WRITE(write(connection_fd, "Content-Length: 35\n", 19)); CHECK_ERROR_WRITE(write(connection_fd, "Content-Length: 35\n", 19));