From: Alex Rousskov Date: Wed, 28 Aug 2013 23:21:45 +0000 (-0600) Subject: Added httpHeaderQuoteString() to quote strings using HTTP rules. X-Git-Tag: SQUID_3_5_0_1~117^2~44^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=22381a6b6385efca510527833ca869c8e631655e;p=thirdparty%2Fsquid.git Added httpHeaderQuoteString() to quote strings using HTTP rules. --- diff --git a/src/HttpHeaderTools.cc b/src/HttpHeaderTools.cc index 588f1c6902..45ca24d8d2 100644 --- a/src/HttpHeaderTools.cc +++ b/src/HttpHeaderTools.cc @@ -299,6 +299,36 @@ httpHeaderParseQuotedString(const char *start, const int len, String *val) return 1; } +// TODO: Optimize using SBuf +String +httpHeaderQuoteString(const char *raw) +{ + assert(raw); + + // HTTPbis says Senders SHOULD NOT escape octets in quoted-strings that + // do not require escaping (i.e., except DQUOTE and the backslash octet). + bool needInnerQuote = false; + for (const char *s = raw; !needInnerQuote && *s; ++s) + needInnerQuote = *s == '"' || *s == '\\'; + + static String quotedStr; + quotedStr.clean(); + quotedStr.append('"'); + + if (needInnerQuote) { + for (const char *s = raw; *s; ++s) { + if (*s == '"' || *s == '\\') + quotedStr.append('\\'); + quotedStr.append(*s); + } + } else { + quotedStr.append(raw); + } + + quotedStr.append('"'); + return quotedStr; +} + /** * Checks the anonymizer (header_access) configuration. *