| 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 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 318 0xffffffffL, 0xffffffffL, 0xffffffffL, 0xffffffffL | 324 0xffffffffL, 0xffffffffL, 0xffffffffL, 0xffffffffL |
| 319 }}; | 325 }}; |
| 320 | 326 |
| 321 // non-7bit | 327 // non-7bit |
| 322 static const Charmap kNonASCIICharmap = {{ | 328 static const Charmap kNonASCIICharmap = {{ |
| 323 0x00000000L, 0x00000000L, 0x00000000L, 0x00000000L, | 329 0x00000000L, 0x00000000L, 0x00000000L, 0x00000000L, |
| 324 0xffffffffL, 0xffffffffL, 0xffffffffL, 0xffffffffL | 330 0xffffffffL, 0xffffffffL, 0xffffffffL, 0xffffffffL |
| 325 }}; | 331 }}; |
| 326 | 332 |
| 327 // Everything except alphanumerics, the reserved characters(;/?:@&=+$,) and | 333 // Everything except alphanumerics, the reserved characters(;/?:@&=+$,) and |
| 328 // !'()*-._~% | 334 // !'()*-._~#[] |
| 329 static const Charmap kExternalHandlerCharmap = {{ | 335 static const Charmap kExternalHandlerCharmap = {{ |
| 330 0xffffffffL, 0x5000080dL, 0x68000000L, 0xb8000001L, | 336 0xffffffffL, 0x50000025L, 0x50000000L, 0xb8000001L, |
| 331 0xffffffffL, 0xffffffffL, 0xffffffffL, 0xffffffffL | 337 0xffffffffL, 0xffffffffL, 0xffffffffL, 0xffffffffL |
| 332 }}; | 338 }}; |
| 333 | 339 |
| 334 } // namespace | 340 } // namespace |
| 335 | 341 |
| 336 std::string EscapeQueryParamValue(const std::string& text, bool use_plus) { | 342 std::string EscapeQueryParamValue(const std::string& text, bool use_plus) { |
| 337 return Escape(text, kQueryCharmap, use_plus); | 343 return Escape(text, kQueryCharmap, use_plus); |
| 338 } | 344 } |
| 339 | 345 |
| 340 std::string EscapePath(const std::string& path) { | 346 std::string EscapePath(const std::string& path) { |
| 341 return Escape(path, kPathCharmap, false); | 347 return Escape(path, kPathCharmap, false); |
| 342 } | 348 } |
| 343 | 349 |
| 344 std::string EscapeUrlEncodedData(const std::string& path, bool use_plus) { | 350 std::string EscapeUrlEncodedData(const std::string& path, bool use_plus) { |
| 345 return Escape(path, kUrlEscape, use_plus); | 351 return Escape(path, kUrlEscape, use_plus); |
| 346 } | 352 } |
| 347 | 353 |
| 348 std::string EscapeNonASCII(const std::string& input) { | 354 std::string EscapeNonASCII(const std::string& input) { |
| 349 return Escape(input, kNonASCIICharmap, false); | 355 return Escape(input, kNonASCIICharmap, false); |
| 350 } | 356 } |
| 351 | 357 |
| 352 std::string EscapeExternalHandlerValue(const std::string& text) { | 358 std::string EscapeExternalHandlerValue(const std::string& text) { |
| 353 return Escape(text, kExternalHandlerCharmap, false); | 359 return Escape(text, kExternalHandlerCharmap, false, true); |
| 354 } | 360 } |
| 355 | 361 |
| 356 void AppendEscapedCharForHTML(char c, std::string* output) { | 362 void AppendEscapedCharForHTML(char c, std::string* output) { |
| 357 AppendEscapedCharForHTMLImpl(c, output); | 363 AppendEscapedCharForHTMLImpl(c, output); |
| 358 } | 364 } |
| 359 | 365 |
| 360 std::string EscapeForHTML(const std::string& input) { | 366 std::string EscapeForHTML(const std::string& input) { |
| 361 return EscapeForHTMLImpl(input); | 367 return EscapeForHTMLImpl(input); |
| 362 } | 368 } |
| 363 | 369 |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 434 1, kEscapeToChars[i].replacement); | 440 1, kEscapeToChars[i].replacement); |
| 435 break; | 441 break; |
| 436 } | 442 } |
| 437 } | 443 } |
| 438 } | 444 } |
| 439 } | 445 } |
| 440 return text; | 446 return text; |
| 441 } | 447 } |
| 442 | 448 |
| 443 } // namespace net | 449 } // namespace net |
| OLD | NEW |