]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
Refactor and improve ErrorState::Dump (#1730)
authorFrancesco Chemolli <5175948+kinkie@users.noreply.github.com>
Mon, 8 Apr 2024 12:01:32 +0000 (12:01 +0000)
committerSquid Anubis <squid-anubis@squid-cache.org>
Mon, 8 Apr 2024 12:01:44 +0000 (12:01 +0000)
Rework the internals for generating output in ErrorState::Dump,
used for expanding the '%W' token in error page templates.

Also fix a bug with excessive html-quoting of the output.

src/errorpage.cc

index a56998dd7264724cdd93f000c24672a495f2c7f9..c01a2c4365ec6150ed43011a46429528d9195e0a 100644 (file)
@@ -10,6 +10,8 @@
 
 #include "squid.h"
 #include "AccessLogEntry.h"
+#include "base/CharacterSet.h"
+#include "base/IoManip.h"
 #include "cache_cf.h"
 #include "clients/forward.h"
 #include "comm/Connection.h"
@@ -810,67 +812,58 @@ ErrorState::~ErrorState()
 int
 ErrorState::Dump(MemBuf * mb)
 {
-    MemBuf str;
-    char ntoabuf[MAX_IPSTRLEN];
+    PackableStream out(*mb);
+    const auto &encoding = CharacterSet::RFC3986_UNRESERVED();
+
+    out << "?subject=" <<
+        AnyP::Uri::Encode(SBuf("CacheErrorInfo - "),encoding) <<
+        AnyP::Uri::Encode(SBuf(errorPageName(type)), encoding);
+
+    SBufStream body;
+    body << "CacheHost: " << getMyHostname() << "\r\n" <<
+         "ErrPage: " << errorPageName(type) << "\r\n" <<
+         "TimeStamp: " << Time::FormatRfc1123(squid_curtime) << "\r\n" <<
+         "\r\n";
+
+    body << "ClientIP: " << src_addr << "\r\n";
+
+    if (request && request->hier.host[0] != '\0')
+        body << "ServerIP: " << request->hier.host << "\r\n";
+
+    if (xerrno)
+        body << "Err: (" << xerrno << ") " << strerror(xerrno) << "\r\n";
 
-    str.reset();
-    /* email subject line */
-    str.appendf("CacheErrorInfo - %s", errorPageName(type));
-    mb->appendf("?subject=%s", rfc1738_escape_part(str.buf));
-    str.reset();
-    /* email body */
-    str.appendf("CacheHost: %s\r\n", getMyHostname());
-    /* - Err Msgs */
-    str.appendf("ErrPage: %s\r\n", errorPageName(type));
-
-    if (xerrno) {
-        str.appendf("Err: (%d) %s\r\n", xerrno, strerror(xerrno));
-    } else {
-        str.append("Err: [none]\r\n", 13);
-    }
 #if USE_AUTH
     if (auth_user_request.getRaw() && auth_user_request->denyMessage())
-        str.appendf("Auth ErrMsg: %s\r\n", auth_user_request->denyMessage());
+        body << "Auth ErrMsg: " << auth_user_request->denyMessage() << "\r\n";
 #endif
-    if (dnsError)
-        str.appendf("DNS ErrMsg: " SQUIDSBUFPH "\r\n", SQUIDSBUFPRINT(*dnsError));
-
-    /* - TimeStamp */
-    str.appendf("TimeStamp: %s\r\n\r\n", Time::FormatRfc1123(squid_curtime));
 
-    /* - IP stuff */
-    str.appendf("ClientIP: %s\r\n", src_addr.toStr(ntoabuf,MAX_IPSTRLEN));
+    if (dnsError)
+        body << "DNS ErrMsg: " << *dnsError << "\r\n";
 
-    if (request && request->hier.host[0] != '\0') {
-        str.appendf("ServerIP: %s\r\n", request->hier.host);
-    }
+    body << "\r\n";
 
-    str.append("\r\n", 2);
-    /* - HTTP stuff */
-    str.append("HTTP Request:\r\n", 15);
     if (request) {
-        str.appendf(SQUIDSBUFPH " " SQUIDSBUFPH " %s/%d.%d\n",
-                    SQUIDSBUFPRINT(request->method.image()),
-                    SQUIDSBUFPRINT(request->url.path()),
-                    AnyP::ProtocolType_str[request->http_ver.protocol],
-                    request->http_ver.major, request->http_ver.minor);
-        request->header.packInto(&str);
+        body << "HTTP Request:\r\n";
+        MemBuf r;
+        r.init();
+        request->pack(&r);
+        body << r.content();
     }
 
-    str.append("\r\n", 2);
     /* - FTP stuff */
 
     if (ftp.request) {
-        str.appendf("FTP Request: %s\r\n", ftp.request);
-        str.appendf("FTP Reply: %s\r\n", (ftp.reply? ftp.reply:"[none]"));
-        str.append("FTP Msg: ", 9);
-        wordlistCat(ftp.server_msg, &str);
-        str.append("\r\n", 2);
+        body << "FTP Request: " << ftp.request << "\r\n";
+        if (ftp.reply)
+            body << "FTP Reply: " << ftp.reply << "\r\n";
+        if (ftp.server_msg)
+            body << "FTP Msg: " << AsList(*ftp.server_msg).delimitedBy("\n") << "\r\n";
+        body << "\r\n";
     }
 
-    str.append("\r\n", 2);
-    mb->appendf("&body=%s", rfc1738_escape_part(str.buf));
-    str.clean();
+    out << "&body=" << AnyP::Uri::Encode(body.buf(), encoding);
+
     return 0;
 }
 
@@ -1203,6 +1196,7 @@ ErrorState::compileLegacyCode(Build &build)
         if (Config.adminEmail && Config.onoff.emailErrData)
             Dump(&mb);
         no_urlescape = 1;
+        do_quote = 0;
         break;
 
     case 'x':