From 763862a61cf5a7ba1a10f607022aac2434c79f57 Mon Sep 17 00:00:00 2001
From: Danijel Tasov <data@consol.de>
Date: Wed, 21 Dec 2022 14:48:11 +0100
Subject: [PATCH] make check_http faster with larger files

The current implementation becomes exponentially slower with growing
response size.

See also:

https://github.com/nagios-plugins/nagios-plugins/blob/release-2.4.2/plugins/check_http.c#L1199-L1204

---
 plugins/check_http.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/plugins/check_http.c b/plugins/check_http.c
index 41d478163..1835a2d09 100644
--- a/plugins/check_http.c
+++ b/plugins/check_http.c
@@ -1095,9 +1095,14 @@ check_http (void)
       *pos = ' ';
     }
     buffer[i] = '\0';
-    xasprintf (&full_page_new, "%s%s", full_page, buffer);
-    free (full_page);
+
+    if ((full_page_new = realloc(full_page, pagesize + i + 1)) == NULL)
+        die (STATE_UNKNOWN, _("HTTP UNKNOWN - Could not allocate memory for full_page\n"));
+
+    memmove(&full_page_new[pagesize], buffer, i + 1);
+
     full_page = full_page_new;
+
     pagesize += i;
 
                 if (no_body && document_headers_done (full_page)) {
