1 // Copyright 2013 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "url/url_canon.h"
6 #include "url/url_canon_internal.h"
8 // Query canonicalization in IE
9 // ----------------------------
10 // IE is very permissive for query parameters specified in links on the page
11 // (in contrast to links that it constructs itself based on form data). It does
12 // not unescape any character. It does not reject any escape sequence (be they
13 // invalid like "%2y" or freaky like %00).
15 // IE only escapes spaces and nothing else. Embedded NULLs, tabs (0x09),
16 // LF (0x0a), and CR (0x0d) are removed (this probably happens at an earlier
17 // layer since they are removed from all portions of the URL). All other
18 // characters are passed unmodified. Invalid UTF-16 sequences are preserved as
19 // well, with each character in the input being converted to UTF-8. It is the
20 // server's job to make sense of this invalid query.
22 // Invalid multibyte sequences (for example, invalid UTF-8 on a UTF-8 page)
23 // are converted to the invalid character and sent as unescaped UTF-8 (0xef,
24 // 0xbf, 0xbd). This may not be canonicalization, the parser may generate these
25 // strings before the URL handler ever sees them.
27 // Our query canonicalization
28 // --------------------------
29 // We escape all non-ASCII characters and control characters, like Firefox.
30 // This is more conformant to the URL spec, and there do not seem to be many
31 // problems relating to Firefox's behavior.
33 // Like IE, we will never unescape (although the application may want to try
34 // unescaping to present the user with a more understandable URL). We will
35 // replace all invalid sequences (including invalid UTF-16 sequences, which IE
36 // doesn't) with the "invalid character," and we will escape it.
42 // Appends the given string to the output, escaping characters that do not
43 // match the given |type| in SharedCharTypes. This version will accept 8 or 16
44 // bit characters, but assumes that they have only 7-bit values. It also assumes
45 // that all UTF-8 values are correct, so doesn't bother checking
46 template<typename CHAR>
47 void AppendRaw8BitQueryString(const CHAR* source, int length,
48 CanonOutput* output) {
49 for (int i = 0; i < length; i++) {
50 if (!IsQueryChar(static_cast<unsigned char>(source[i])))
51 AppendEscapedChar(static_cast<unsigned char>(source[i]), output);
52 else // Doesn't need escaping.
53 output->push_back(static_cast<char>(source[i]));
57 // Runs the converter on the given UTF-8 input. Since the converter expects
58 // UTF-16, we have to convert first. The converter must be non-NULL.
59 void RunConverter(const char* spec,
60 const Component& query,
61 CharsetConverter* converter,
62 CanonOutput* output) {
63 DCHECK(query.is_valid());
64 // This function will replace any misencoded values with the invalid
65 // character. This is what we want so we don't have to check for error.
66 RawCanonOutputW<1024> utf16;
67 ConvertUTF8ToUTF16(&spec[query.begin], static_cast<size_t>(query.len),
69 converter->ConvertFromUTF16(utf16.data(), utf16.length(), output);
72 // Runs the converter with the given UTF-16 input. We don't have to do
73 // anything, but this overridden function allows us to use the same code
74 // for both UTF-8 and UTF-16 input.
75 void RunConverter(const char16_t* spec,
76 const Component& query,
77 CharsetConverter* converter,
78 CanonOutput* output) {
79 DCHECK(query.is_valid());
80 converter->ConvertFromUTF16(&spec[query.begin],
81 static_cast<size_t>(query.len), output);
84 template <typename CHAR, typename UCHAR>
85 void DoConvertToQueryEncoding(const CHAR* spec,
86 const Component& query,
87 CharsetConverter* converter,
88 CanonOutput* output) {
90 // Run the converter to get an 8-bit string, then append it, escaping
92 RawCanonOutput<1024> eight_bit;
93 RunConverter(spec, query, converter, &eight_bit);
94 AppendRaw8BitQueryString(eight_bit.data(), eight_bit.length(), output);
97 // No converter, do our own UTF-8 conversion.
98 AppendStringOfType(&spec[query.begin], static_cast<size_t>(query.len),
103 template<typename CHAR, typename UCHAR>
104 void DoCanonicalizeQuery(const CHAR* spec,
105 const Component& query,
106 CharsetConverter* converter,
108 Component* out_query) {
109 if (!query.is_valid()) {
110 *out_query = Component();
114 output->push_back('?');
115 out_query->begin = output->length();
117 DoConvertToQueryEncoding<CHAR, UCHAR>(spec, query, converter, output);
119 out_query->len = output->length() - out_query->begin;
124 void CanonicalizeQuery(const char* spec,
125 const Component& query,
126 CharsetConverter* converter,
128 Component* out_query) {
129 DoCanonicalizeQuery<char, unsigned char>(spec, query, converter,
133 void CanonicalizeQuery(const char16_t* spec,
134 const Component& query,
135 CharsetConverter* converter,
137 Component* out_query) {
138 DoCanonicalizeQuery<char16_t, char16_t>(spec, query, converter, output,
142 void ConvertUTF16ToQueryEncoding(const char16_t* input,
143 const Component& query,
144 CharsetConverter* converter,
145 CanonOutput* output) {
146 DoConvertToQueryEncoding<char16_t, char16_t>(input, query, converter, output);