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

Side by Side Diff: chrome/browser/android/url_sanitize_utils.cc

Issue 646333002: Update EscapeExternalHandlerValue to keep '#', '[', and ']' (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Sanitize fragment and path 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chrome/browser/android/url_sanitize_utils.h"
6
7 #include "base/logging.h"
8
9 namespace chrome {
10 namespace android {
11
12 namespace {
13
14 static const char kHexString[] = "0123456789ABCDEF";
15 inline char IntToHex(int i) {
16 DCHECK_GE(i, 0) << i << " not a hex value";
17 DCHECK_LE(i, 15) << i << " not a hex value";
18 return kHexString[i];
19 }
20
21 inline bool IsHexChar(unsigned char c) {
22 if ((c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F') ||
23 (c >= '0' && c <= '9'))
24 return true;
25 return false;
26 }
27
28 // A fast bit-vector map for ascii characters.
29 //
30 // Internally stores 256 bits in an array of 8 ints.
31 // Does quick bit-flicking to lookup needed characters.
32 struct Charmap {
33 bool Contains(unsigned char c) const {
34 return ((map[c >> 5] & (1 << (c & 31))) != 0);
35 }
36
37 uint32 map[8];
38 };
39
40 // Everything except alphanumerics, and some reserved characters
41 // (;/?:@&=+$,-_.!~*'()). See RFC 2396 for the list of reserved characters.
42 static const Charmap kQueryOrRefPartCharmap = {{
43 0xffffffffL, 0x5000002dL, 0x78000000L, 0xb8000001L,
44 0xffffffffL, 0xffffffffL, 0xffffffffL, 0xffffffffL
45 }};
46
47 // Everything except alphanumerics, and some reserved characters
48 // (;/:@&=+$,-_.!~*'()). See RFC 2396 for the list of reserved characters.
49 static const Charmap kPathPartCharmap = {{
50 0xffffffffL, 0xd000002dL, 0x78000000L, 0xb8000001L,
51 0xffffffffL, 0xffffffffL, 0xffffffffL, 0xffffffffL
52 }};
asanka 2014/10/14 16:46:03 Are these tables different from net/base/escape.cc
Jaekyun Seok (inactive) 2014/10/15 08:02:25 Yes, both are different from net/base/escape.cc.
asanka 2014/10/17 00:07:28 Doesn't net::EscapeExternalHandlerValue() do what
53
54 // Given text to escape and a Charmap defining which values to escape,
55 // return an escaped string. If characters are already escaped, they aren't
56 // converted.
57 static std::string Escape(const std::string& text, const Charmap& charmap) {
58 std::string escaped;
59 escaped.reserve(text.length() * 3);
60 for (unsigned int i = 0; i < text.length(); ++i) {
61 unsigned char c = static_cast<unsigned char>(text[i]);
62 if ('%' == c && i + 2 < text.length() &&
63 IsHexChar(static_cast<unsigned char>(text[i + 1])) &&
64 IsHexChar(static_cast<unsigned char>(text[i + 2]))) {
65 escaped.push_back('%');
66 } else if (charmap.Contains(c)) {
67 escaped.push_back('%');
68 escaped.push_back(IntToHex(c >> 4));
69 escaped.push_back(IntToHex(c & 0xf));
70 } else {
71 escaped.push_back(c);
72 }
73 }
74 return escaped;
75 }
76
77 } // namespace
78
79 GURL SanitizeUrl(const GURL& url) {
80 std::string query_input = url.query();
81 std::string ref_input = url.ref();
82 std::string path_input = url.path();
83 std::string query_output = Escape(query_input, kQueryOrRefPartCharmap);
84 std::string ref_output = Escape(ref_input, kQueryOrRefPartCharmap);
85 std::string path_output = Escape(path_input, kPathPartCharmap);
86
87 GURL::Replacements replacements;
88 bool modified = false;
89 if (!query_output.empty() && query_output != query_input) {
90 replacements.SetQueryStr(query_output);
91 modified = true;
92 }
93 if (!ref_output.empty() && ref_output != ref_input) {
94 replacements.SetRefStr(ref_output);
95 modified = true;
96 }
97 if (!path_output.empty() && path_output != path_input) {
98 replacements.SetPathStr(path_output);
99 modified = true;
100 }
101
102 if (modified)
103 return url.ReplaceComponents(replacements);
104 return url;
105 }
106
107 } // namespace android
108 } // namespace chrome
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698