From db0157451f7ddec48ef3b7946d535bfa595c187f Mon Sep 17 00:00:00 2001 From: DL6ER Date: Tue, 11 Apr 2023 12:23:52 +0200 Subject: [PATCH] Apply Pi-hole CivetWeb patches Signed-off-by: DL6ER --- patch/civetweb.sh | 2 + ...EN-option-to-civetweb-s-LUA-routines.patch | 8 +-- patch/civetweb/0001-add-pihole-mods.patch | 27 +++++----- patch/lua.sh | 2 + patch/sqlite3.sh | 2 + src/webserver/civetweb/civetweb.c | 54 +++++++++++++++++++ src/webserver/civetweb/civetweb.h | 12 +++++ src/webserver/civetweb/mod_lua.inl | 4 +- 8 files changed, 92 insertions(+), 19 deletions(-) diff --git a/patch/civetweb.sh b/patch/civetweb.sh index 362b562a..9e2e5b17 100644 --- a/patch/civetweb.sh +++ b/patch/civetweb.sh @@ -3,3 +3,5 @@ set -e patch -p1 < patch/civetweb/0001-add-pihole-mods.patch patch -p1 < patch/civetweb/0001-Add-NO_DLOPEN-option-to-civetweb-s-LUA-routines.patch + +echo "ALL PATCHES APPLIED OKAY" diff --git a/patch/civetweb/0001-Add-NO_DLOPEN-option-to-civetweb-s-LUA-routines.patch b/patch/civetweb/0001-Add-NO_DLOPEN-option-to-civetweb-s-LUA-routines.patch index c4db5b0b..ee48ec54 100644 --- a/patch/civetweb/0001-Add-NO_DLOPEN-option-to-civetweb-s-LUA-routines.patch +++ b/patch/civetweb/0001-Add-NO_DLOPEN-option-to-civetweb-s-LUA-routines.patch @@ -5,13 +5,13 @@ Subject: [PATCH] Add NO_DLOPEN option to civetweb's LUA routines Signed-off-by: DL6ER --- - src/civetweb/mod_lua.inl | 4 ++-- + src/webserver/civetweb/mod_lua.inl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) -diff --git a/src/civetweb/mod_lua.inl b/src/civetweb/mod_lua.inl +diff --git a/src/webserver/civetweb/mod_lua.inl b/src/webserver/civetweb/mod_lua.inl index 5cc94318..59c4f2b3 100644 ---- a/src/civetweb/mod_lua.inl -+++ b/src/civetweb/mod_lua.inl +--- a/src/webserver/civetweb/mod_lua.inl ++++ b/src/webserver/civetweb/mod_lua.inl @@ -3634,7 +3634,7 @@ lua_init_optional_libraries(void) lua_shared_init(); diff --git a/patch/civetweb/0001-add-pihole-mods.patch b/patch/civetweb/0001-add-pihole-mods.patch index f62ffcde..38483f57 100644 --- a/patch/civetweb/0001-add-pihole-mods.patch +++ b/patch/civetweb/0001-add-pihole-mods.patch @@ -5,14 +5,14 @@ Subject: [PATCH] Add Pi-hole specific method to send HTTP headers Signed-off-by: DL6ER --- - src/civetweb/civetweb.c | 53 +++++++++++++++++++++++++++++++++++++++++ - src/civetweb/civetweb.h | 11 +++++++++ + src/webserver/civetweb/civetweb.c | 53 +++++++++++++++++++++++++++++++++++++++++ + src/webserver/civetweb/civetweb.h | 11 +++++++++ 2 files changed, 64 insertions(+) -diff --git a/src/civetweb/civetweb.c b/src/civetweb/civetweb.c +diff --git a/src/webserver/civetweb/civetweb.c b/src/webserver/civetweb/civetweb.c index 81f642be..ed360a76 100644 ---- a/src/civetweb/civetweb.c -+++ b/src/civetweb/civetweb.c +--- a/src/webserver/civetweb/civetweb.c ++++ b/src/webserver/civetweb/civetweb.c @@ -4135,6 +4135,14 @@ send_additional_header(struct mg_connection *conn) if (header && header[0]) { mg_response_header_add_lines(conn, header); @@ -28,14 +28,15 @@ index 81f642be..ed360a76 100644 } -@@ -4530,6 +4538,47 @@ mg_send_http_error_impl(struct mg_connection *conn, +@@ -4530,6 +4538,48 @@ mg_send_http_error_impl(struct mg_connection *conn, } +/************************************** Pi-hole method **************************************/ -+int my_send_http_error_headers(struct mg_connection *conn, -+ int status, const char* mime_type, -+ long long content_length) ++CIVETWEB_API int ++my_send_http_error_headers(struct mg_connection *conn, ++ int status, const char* mime_type, ++ long long content_length) +{ + if ((mime_type == NULL) || (*mime_type == 0)) { + /* No content type defined: default to text/html */ @@ -73,7 +74,7 @@ index 81f642be..ed360a76 100644 +} +/********************************************************************************************/ + - int + CIVETWEB_API int mg_send_http_error(struct mg_connection *conn, int status, const char *fmt, ...) { @@ -10649,6 +10649,10 @@ parse_http_request(char *buf, int len, struct mg_request_info *ri) @@ -87,10 +88,10 @@ index 81f642be..ed360a76 100644 /* RFC says that all initial whitespaces should be ignored */ /* This included all leading \r and \n (isspace) */ /* See table: http://www.cplusplus.com/reference/cctype/ */ -diff --git a/src/civetweb/civetweb.h b/src/civetweb/civetweb.h +diff --git a/src/webserver/civetweb/civetweb.h b/src/webserver/civetweb/civetweb.h index 7ea45fb2..f879ff3e 100644 ---- a/src/civetweb/civetweb.h -+++ b/src/civetweb/civetweb.h +--- a/src/webserver/civetweb/civetweb.h ++++ b/src/webserver/civetweb/civetweb.h @@ -186,6 +186,8 @@ struct mg_request_info { const char *acceptedWebSocketSubprotocol; /* websocket subprotocol, diff --git a/patch/lua.sh b/patch/lua.sh index 478d4581..9987b222 100644 --- a/patch/lua.sh +++ b/patch/lua.sh @@ -2,3 +2,5 @@ set -e patch -p1 < patch/lua/0001-add-pihole-library.patch + +echo "ALL PATCHES APPLIED OKAY" diff --git a/patch/sqlite3.sh b/patch/sqlite3.sh index 9b447217..5b984a3e 100644 --- a/patch/sqlite3.sh +++ b/patch/sqlite3.sh @@ -3,3 +3,5 @@ set -e patch -p1 < patch/sqlite3/0001-print-FTL-version-in-interactive-shell.patch patch -p1 < patch/sqlite3/0002-make-sqlite3ErrName-public.patch + +echo "ALL PATCHES APPLIED OKAY" diff --git a/src/webserver/civetweb/civetweb.c b/src/webserver/civetweb/civetweb.c index 9e321edf..0d293f1f 100644 --- a/src/webserver/civetweb/civetweb.c +++ b/src/webserver/civetweb/civetweb.c @@ -4142,6 +4142,14 @@ send_additional_header(struct mg_connection *conn) if (header && header[0]) { mg_response_header_add_lines(conn, header); } + + /*************** Pi-hole modification ****************/ + if (pi_hole_extra_headers[0] != '\0') { + mg_response_header_add_lines(conn, pi_hole_extra_headers); + // Invalidate extra headers after having sent them to avoid repetitions + pi_hole_extra_headers[0] = '\0'; + } + /*****************************************************/ } @@ -4558,6 +4566,48 @@ mg_send_http_error_impl(struct mg_connection *conn, } +/************************************** Pi-hole method **************************************/ +CIVETWEB_API int +my_send_http_error_headers(struct mg_connection *conn, + int status, const char* mime_type, + long long content_length) +{ + if ((mime_type == NULL) || (*mime_type == 0)) { + /* No content type defined: default to text/html */ + mime_type = "text/html"; + } + + mg_response_header_start(conn, status); + send_no_cache_header(conn); + send_additional_header(conn); + mg_response_header_add(conn, "Content-Type", mime_type, -1); + if (content_length < 0) { + /* Size not known. Use chunked encoding (HTTP/1.x) */ + if (conn->protocol_type == PROTOCOL_TYPE_HTTP1) { + /* Only HTTP/1.x defines "chunked" encoding, HTTP/2 does not*/ + mg_response_header_add(conn, "Transfer-Encoding", "chunked", -1); + } + } else { + char len[32]; + int trunc = 0; + mg_snprintf(conn, + &trunc, + len, + sizeof(len), + "%" UINT64_FMT, + (uint64_t)content_length); + if (!trunc) { + /* Since 32 bytes is enough to hold any 64 bit decimal number, + * !trunc is always true */ + mg_response_header_add(conn, "Content-Length", len, -1); + } + } + mg_response_header_send(conn); + + return 0; +} +/********************************************************************************************/ + CIVETWEB_API int mg_send_http_error(struct mg_connection *conn, int status, const char *fmt, ...) { @@ -10923,6 +10973,10 @@ parse_http_request(char *buf, int len, struct mg_request_info *ri) NULL; ri->num_headers = 0; + /******************** Pi-hole modification ********************/ + strncpy(ri->raw_http_head, buf, sizeof(ri->raw_http_head)); + /**************************************************************/ + /* RFC says that all initial whitespaces should be ignored */ /* This included all leading \r and \n (isspace) */ /* See table: http://www.cplusplus.com/reference/cctype/ */ diff --git a/src/webserver/civetweb/civetweb.h b/src/webserver/civetweb/civetweb.h index 167c6a76..e71dfedc 100644 --- a/src/webserver/civetweb/civetweb.h +++ b/src/webserver/civetweb/civetweb.h @@ -183,6 +183,8 @@ struct mg_request_info { const char *acceptedWebSocketSubprotocol; /* websocket subprotocol, * accepted during handshake */ + // Pi-hole modification + char raw_http_head[16384]; }; @@ -928,6 +930,16 @@ CIVETWEB_API int mg_send_http_error(struct mg_connection *conn, PRINTF_FORMAT_STRING(const char *fmt), ...) PRINTF_ARGS(3, 4); +/************************************** Pi-hole method **************************************/ +int my_send_http_error_headers(struct mg_connection *conn, + int status, const char* mime_type, + long long content_length); + +// Buffer used for additional "Set-Cookie" headers +#define PIHOLE_HEADERS_MAXLEN 1024 +extern char pi_hole_extra_headers[PIHOLE_HEADERS_MAXLEN]; +/********************************************************************************************/ + /* Send "HTTP 200 OK" response header. * After calling this function, use mg_write or mg_send_chunk to send the diff --git a/src/webserver/civetweb/mod_lua.inl b/src/webserver/civetweb/mod_lua.inl index f1073e55..cecb6615 100644 --- a/src/webserver/civetweb/mod_lua.inl +++ b/src/webserver/civetweb/mod_lua.inl @@ -3664,7 +3664,7 @@ lua_init_optional_libraries(void) lua_shared_init(); /* UUID library */ -#if !defined(_WIN32) +#if !defined(_WIN32) && !defined(NO_DLOPEN) lib_handle_uuid = dlopen("libuuid.so", RTLD_LAZY); pf_uuid_generate.p = (lib_handle_uuid ? dlsym(lib_handle_uuid, "uuid_generate") : 0); @@ -3678,7 +3678,7 @@ static void lua_exit_optional_libraries(void) { /* UUID library */ -#if !defined(_WIN32) +#if !defined(_WIN32) && !defined(NO_DLOPEN) if (lib_handle_uuid) { dlclose(lib_handle_uuid); }