Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(527)

Unified Diff: net/base/escape.cc

Issue 632843003: Add a function to escape a query part of url (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Follow RFC 2396 to escape a query part of url Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: net/base/escape.cc
diff --git a/net/base/escape.cc b/net/base/escape.cc
index ab70f1db30187e6a28e09757819d2f307291fd53..1844f64613ad4d715c0d3c7f97677d4c695403bd 100644
--- a/net/base/escape.cc
+++ b/net/base/escape.cc
@@ -24,6 +24,13 @@ inline char IntToHex(int i) {
return kHexString[i];
}
+inline bool IsHexChar(unsigned char c) {
+ if ((c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F') ||
+ (c >= '0' && c <= '9'))
+ return true;
+ return false;
+}
+
// A fast bit-vector map for ascii characters.
//
// Internally stores 256 bits in an array of 8 ints.
@@ -41,13 +48,17 @@ struct Charmap {
// to +, otherwise, if spaces are in the charmap, they are converted to
// %20.
std::string Escape(const std::string& text, const Charmap& charmap,
- bool use_plus) {
+ bool use_plus, bool keep_escaped = false) {
std::string escaped;
escaped.reserve(text.length() * 3);
for (unsigned int i = 0; i < text.length(); ++i) {
unsigned char c = static_cast<unsigned char>(text[i]);
if (use_plus && ' ' == c) {
escaped.push_back('+');
+ } else if (keep_escaped && '%' == c && i + 2 < text.length() &&
+ IsHexChar(static_cast<unsigned char>(text[i + 1])) &&
+ IsHexChar(static_cast<unsigned char>(text[i + 2]))) {
+ escaped.push_back('%');
} else if (charmap.Contains(c)) {
escaped.push_back('%');
escaped.push_back(IntToHex(c >> 4));
@@ -299,6 +310,13 @@ static const Charmap kExternalHandlerCharmap = {{
0xffffffffL, 0xffffffffL, 0xffffffffL, 0xffffffffL
}};
+// Everything except alphanumerics, the reserved characters(;/?:@&=+$,), the
+// mark characters(-_.!~*'()).
+static const Charmap kQueryPartCharmap = {{
+ 0xffffffffL, 0x5000002dL, 0x78000000L, 0xb8000001L,
+ 0xffffffffL, 0xffffffffL, 0xffffffffL, 0xffffffffL
+}};
+
} // namespace
std::string EscapeQueryParamValue(const std::string& text, bool use_plus) {
@@ -321,6 +339,10 @@ std::string EscapeExternalHandlerValue(const std::string& text) {
return Escape(text, kExternalHandlerCharmap, false);
}
+std::string EscapeQueryPart(const std::string& query) {
+ return Escape(query, kQueryPartCharmap, false, true);
+}
+
void AppendEscapedCharForHTML(char c, std::string* output) {
AppendEscapedCharForHTMLImpl(c, output);
}
« net/base/escape.h ('K') | « net/base/escape.h ('k') | net/base/escape_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698