Sfoglia il codice sorgente

Return error for unknown HTTP versions and status codes

bel2125 4 anni fa
parent
commit
0e2be8b0b7
1 ha cambiato i file con 17 aggiunte e 4 eliminazioni
  1. 17 4
      src/response.inl

+ 17 - 4
src/response.inl

@@ -40,13 +40,26 @@ free_buffered_response_header_list(struct mg_connection *conn)
 static void
 send_http1_response_status_line(struct mg_connection *conn)
 {
+	const char *status_txt;
+	const char *http_version = conn->request_info.http_version;
+	int status_code = conn->status_code;
+
+	if ((status_code < 100) || (status_code > 999)) {
+		/* Set invalid status code to "500 Internal Server Error" */
+		status_code = 500;
+	}
+	if (!http_version) {
+		http_version = "1.0";
+	}
+
 	/* mg_get_response_code_text will never return NULL */
-	const char *txt = mg_get_response_code_text(conn, conn->status_code);
+	status_txt = mg_get_response_code_text(conn, conn->status_code);
+
 	mg_printf(conn,
 	          "HTTP/%s %i %s\r\n",
-	          conn->request_info.http_version,
-	          conn->status_code,
-	          txt);
+	          http_version,
+	          status_code,
+	          status_txt);
 }