Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/base/escape.h" | 5 #include "net/base/escape.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 32 bool Contains(unsigned char c) const { | 32 bool Contains(unsigned char c) const { |
| 33 return ((map[c >> 5] & (1 << (c & 31))) != 0); | 33 return ((map[c >> 5] & (1 << (c & 31))) != 0); |
| 34 } | 34 } |
| 35 | 35 |
| 36 uint32 map[8]; | 36 uint32 map[8]; |
| 37 }; | 37 }; |
| 38 | 38 |
| 39 // Given text to escape and a Charmap defining which values to escape, | 39 // Given text to escape and a Charmap defining which values to escape, |
| 40 // return an escaped string. If use_plus is true, spaces are converted | 40 // return an escaped string. If use_plus is true, spaces are converted |
| 41 // to +, otherwise, if spaces are in the charmap, they are converted to | 41 // to +, otherwise, if spaces are in the charmap, they are converted to |
| 42 // %20. | 42 // %20. And if keep_escaped is true, %XX will be kept as it is, otherwise, if |
| 43 std::string Escape(const std::string& text, const Charmap& charmap, | 43 // '%' is in the charmap, it is converted to %25. |
| 44 bool use_plus) { | 44 std::string Escape(const std::string& text, |
| 45 const Charmap& charmap, | |
| 46 bool use_plus, | |
| 47 bool keep_escaped = false) { | |
| 45 std::string escaped; | 48 std::string escaped; |
| 46 escaped.reserve(text.length() * 3); | 49 escaped.reserve(text.length() * 3); |
| 47 for (unsigned int i = 0; i < text.length(); ++i) { | 50 for (unsigned int i = 0; i < text.length(); ++i) { |
| 48 unsigned char c = static_cast<unsigned char>(text[i]); | 51 unsigned char c = static_cast<unsigned char>(text[i]); |
| 49 if (use_plus && ' ' == c) { | 52 if (use_plus && ' ' == c) { |
| 50 escaped.push_back('+'); | 53 escaped.push_back('+'); |
| 54 } else if (keep_escaped && '%' == c && i + 2 < text.length() && | |
| 55 IsHexDigit(text[i + 1]) && IsHexDigit(text[i + 2])) { | |
| 56 escaped.push_back('%'); | |
| 51 } else if (charmap.Contains(c)) { | 57 } else if (charmap.Contains(c)) { |
| 52 escaped.push_back('%'); | 58 escaped.push_back('%'); |
| 53 escaped.push_back(IntToHex(c >> 4)); | 59 escaped.push_back(IntToHex(c >> 4)); |
| 54 escaped.push_back(IntToHex(c & 0xf)); | 60 escaped.push_back(IntToHex(c & 0xf)); |
| 55 } else { | 61 } else { |
| 56 escaped.push_back(c); | 62 escaped.push_back(c); |
| 57 } | 63 } |
| 58 } | 64 } |
| 59 return escaped; | 65 return escaped; |
| 60 } | 66 } |
| (...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 286 0xffffffffL, 0xffffffffL, 0xffffffffL, 0xffffffffL | 292 0xffffffffL, 0xffffffffL, 0xffffffffL, 0xffffffffL |
| 287 }}; | 293 }}; |
| 288 | 294 |
| 289 // non-7bit | 295 // non-7bit |
| 290 static const Charmap kNonASCIICharmap = {{ | 296 static const Charmap kNonASCIICharmap = {{ |
| 291 0x00000000L, 0x00000000L, 0x00000000L, 0x00000000L, | 297 0x00000000L, 0x00000000L, 0x00000000L, 0x00000000L, |
| 292 0xffffffffL, 0xffffffffL, 0xffffffffL, 0xffffffffL | 298 0xffffffffL, 0xffffffffL, 0xffffffffL, 0xffffffffL |
| 293 }}; | 299 }}; |
| 294 | 300 |
| 295 // Everything except alphanumerics, the reserved characters(;/?:@&=+$,) and | 301 // Everything except alphanumerics, the reserved characters(;/?:@&=+$,) and |
| 296 // !'()*-._~% | 302 // !'()*-._~#[] |
|
asanka
2014/10/23 07:20:04
Why remove '%'? Shouldn't we escape a % if it isn'
Jaekyun Seok (inactive)
2014/10/23 12:43:19
Yes. And so '%' should be removed because kExterna
| |
| 297 static const Charmap kExternalHandlerCharmap = {{ | 303 static const Charmap kExternalHandlerCharmap = {{ |
| 298 0xffffffffL, 0x5000080dL, 0x68000000L, 0xb8000001L, | 304 0xffffffffL, 0x50000025L, 0x50000000L, 0xb8000001L, |
| 299 0xffffffffL, 0xffffffffL, 0xffffffffL, 0xffffffffL | 305 0xffffffffL, 0xffffffffL, 0xffffffffL, 0xffffffffL |
| 300 }}; | 306 }}; |
| 301 | 307 |
| 302 } // namespace | 308 } // namespace |
| 303 | 309 |
| 304 std::string EscapeQueryParamValue(const std::string& text, bool use_plus) { | 310 std::string EscapeQueryParamValue(const std::string& text, bool use_plus) { |
| 305 return Escape(text, kQueryCharmap, use_plus); | 311 return Escape(text, kQueryCharmap, use_plus); |
| 306 } | 312 } |
| 307 | 313 |
| 308 std::string EscapePath(const std::string& path) { | 314 std::string EscapePath(const std::string& path) { |
| 309 return Escape(path, kPathCharmap, false); | 315 return Escape(path, kPathCharmap, false); |
| 310 } | 316 } |
| 311 | 317 |
| 312 std::string EscapeUrlEncodedData(const std::string& path, bool use_plus) { | 318 std::string EscapeUrlEncodedData(const std::string& path, bool use_plus) { |
| 313 return Escape(path, kUrlEscape, use_plus); | 319 return Escape(path, kUrlEscape, use_plus); |
| 314 } | 320 } |
| 315 | 321 |
| 316 std::string EscapeNonASCII(const std::string& input) { | 322 std::string EscapeNonASCII(const std::string& input) { |
| 317 return Escape(input, kNonASCIICharmap, false); | 323 return Escape(input, kNonASCIICharmap, false); |
| 318 } | 324 } |
| 319 | 325 |
| 320 std::string EscapeExternalHandlerValue(const std::string& text) { | 326 std::string EscapeExternalHandlerValue(const std::string& text) { |
| 321 return Escape(text, kExternalHandlerCharmap, false); | 327 return Escape(text, kExternalHandlerCharmap, false, true); |
| 322 } | 328 } |
| 323 | 329 |
| 324 void AppendEscapedCharForHTML(char c, std::string* output) { | 330 void AppendEscapedCharForHTML(char c, std::string* output) { |
| 325 AppendEscapedCharForHTMLImpl(c, output); | 331 AppendEscapedCharForHTMLImpl(c, output); |
| 326 } | 332 } |
| 327 | 333 |
| 328 std::string EscapeForHTML(const std::string& input) { | 334 std::string EscapeForHTML(const std::string& input) { |
| 329 return EscapeForHTMLImpl(input); | 335 return EscapeForHTMLImpl(input); |
| 330 } | 336 } |
| 331 | 337 |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 402 1, kEscapeToChars[i].replacement); | 408 1, kEscapeToChars[i].replacement); |
| 403 break; | 409 break; |
| 404 } | 410 } |
| 405 } | 411 } |
| 406 } | 412 } |
| 407 } | 413 } |
| 408 return text; | 414 return text; |
| 409 } | 415 } |
| 410 | 416 |
| 411 } // namespace net | 417 } // namespace net |
| OLD | NEW |