Skip to content

Commit

Permalink
Refactor and improve ErrorState::Dump (squid-cache#1730)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
kinkie committed Apr 14, 2024
1 parent 5516fe4 commit 4477c3b
Showing 1 changed file with 40 additions and 46 deletions.
86 changes: 40 additions & 46 deletions src/errorpage.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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':
Expand Down

0 comments on commit 4477c3b

Please sign in to comment.