blob: b3a1118cdf24f077797dc18e48d26157dacae18e [file] [log] [blame]
[email protected]51bcc5d2013-04-24 01:41:371// Copyright 2013 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.
[email protected]e7bba5f82013-04-10 20:10:524
[email protected]318076b2013-04-18 21:19:455#include "url/url_canon.h"
6#include "url/url_canon_internal.h"
[email protected]e7bba5f82013-04-10 20:10:527
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).
14//
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.
21//
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.
26//
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.
32//
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.
37
[email protected]0318f922014-04-22 00:09:2338namespace url {
[email protected]e7bba5f82013-04-10 20:10:5239
40namespace {
41
42// Returns true if the characters starting at |begin| and going until |end|
43// (non-inclusive) are all representable in 7-bits.
44template<typename CHAR, typename UCHAR>
[email protected]0318f922014-04-22 00:09:2345bool IsAllASCII(const CHAR* spec, const Component& query) {
[email protected]e7bba5f82013-04-10 20:10:5246 int end = query.end();
47 for (int i = query.begin; i < end; i++) {
48 if (static_cast<UCHAR>(spec[i]) >= 0x80)
49 return false;
50 }
51 return true;
52}
53
54// Appends the given string to the output, escaping characters that do not
55// match the given |type| in SharedCharTypes. This version will accept 8 or 16
56// bit characters, but assumes that they have only 7-bit values. It also assumes
57// that all UTF-8 values are correct, so doesn't bother checking
58template<typename CHAR>
59void AppendRaw8BitQueryString(const CHAR* source, int length,
60 CanonOutput* output) {
61 for (int i = 0; i < length; i++) {
62 if (!IsQueryChar(static_cast<unsigned char>(source[i])))
63 AppendEscapedChar(static_cast<unsigned char>(source[i]), output);
64 else // Doesn't need escaping.
65 output->push_back(static_cast<char>(source[i]));
66 }
67}
68
69// Runs the converter on the given UTF-8 input. Since the converter expects
70// UTF-16, we have to convert first. The converter must be non-NULL.
71void RunConverter(const char* spec,
[email protected]0318f922014-04-22 00:09:2372 const Component& query,
[email protected]e7bba5f82013-04-10 20:10:5273 CharsetConverter* converter,
74 CanonOutput* output) {
75 // This function will replace any misencoded values with the invalid
76 // character. This is what we want so we don't have to check for error.
77 RawCanonOutputW<1024> utf16;
78 ConvertUTF8ToUTF16(&spec[query.begin], query.len, &utf16);
79 converter->ConvertFromUTF16(utf16.data(), utf16.length(), output);
80}
81
82// Runs the converter with the given UTF-16 input. We don't have to do
qyearsley2bc727d2015-08-14 20:17:1583// anything, but this overridden function allows us to use the same code
[email protected]e7bba5f82013-04-10 20:10:5284// for both UTF-8 and UTF-16 input.
Jan Wilken Dörrie5aad5c22021-03-08 21:44:1285void RunConverter(const char16_t* spec,
[email protected]0318f922014-04-22 00:09:2386 const Component& query,
[email protected]e7bba5f82013-04-10 20:10:5287 CharsetConverter* converter,
88 CanonOutput* output) {
89 converter->ConvertFromUTF16(&spec[query.begin], query.len, output);
90}
91
92template<typename CHAR, typename UCHAR>
93void DoConvertToQueryEncoding(const CHAR* spec,
[email protected]0318f922014-04-22 00:09:2394 const Component& query,
[email protected]e7bba5f82013-04-10 20:10:5295 CharsetConverter* converter,
96 CanonOutput* output) {
97 if (IsAllASCII<CHAR, UCHAR>(spec, query)) {
98 // Easy: the input can just appended with no character set conversions.
99 AppendRaw8BitQueryString(&spec[query.begin], query.len, output);
100
101 } else {
102 // Harder: convert to the proper encoding first.
103 if (converter) {
104 // Run the converter to get an 8-bit string, then append it, escaping
105 // necessary values.
106 RawCanonOutput<1024> eight_bit;
107 RunConverter(spec, query, converter, &eight_bit);
108 AppendRaw8BitQueryString(eight_bit.data(), eight_bit.length(), output);
109
110 } else {
111 // No converter, do our own UTF-8 conversion.
112 AppendStringOfType(&spec[query.begin], query.len, CHAR_QUERY, output);
113 }
114 }
115}
116
117template<typename CHAR, typename UCHAR>
118void DoCanonicalizeQuery(const CHAR* spec,
[email protected]0318f922014-04-22 00:09:23119 const Component& query,
[email protected]e7bba5f82013-04-10 20:10:52120 CharsetConverter* converter,
121 CanonOutput* output,
[email protected]0318f922014-04-22 00:09:23122 Component* out_query) {
[email protected]e7bba5f82013-04-10 20:10:52123 if (query.len < 0) {
[email protected]0318f922014-04-22 00:09:23124 *out_query = Component();
[email protected]e7bba5f82013-04-10 20:10:52125 return;
126 }
127
128 output->push_back('?');
129 out_query->begin = output->length();
130
131 DoConvertToQueryEncoding<CHAR, UCHAR>(spec, query, converter, output);
132
133 out_query->len = output->length() - out_query->begin;
134}
135
136} // namespace
137
138void CanonicalizeQuery(const char* spec,
[email protected]0318f922014-04-22 00:09:23139 const Component& query,
[email protected]e7bba5f82013-04-10 20:10:52140 CharsetConverter* converter,
141 CanonOutput* output,
[email protected]0318f922014-04-22 00:09:23142 Component* out_query) {
[email protected]e7bba5f82013-04-10 20:10:52143 DoCanonicalizeQuery<char, unsigned char>(spec, query, converter,
144 output, out_query);
145}
146
Jan Wilken Dörrie5aad5c22021-03-08 21:44:12147void CanonicalizeQuery(const char16_t* spec,
[email protected]0318f922014-04-22 00:09:23148 const Component& query,
[email protected]e7bba5f82013-04-10 20:10:52149 CharsetConverter* converter,
150 CanonOutput* output,
[email protected]0318f922014-04-22 00:09:23151 Component* out_query) {
Jan Wilken Dörrie5aad5c22021-03-08 21:44:12152 DoCanonicalizeQuery<char16_t, char16_t>(spec, query, converter, output,
153 out_query);
[email protected]e7bba5f82013-04-10 20:10:52154}
155
Jan Wilken Dörrie5aad5c22021-03-08 21:44:12156void ConvertUTF16ToQueryEncoding(const char16_t* input,
[email protected]0318f922014-04-22 00:09:23157 const Component& query,
[email protected]e7bba5f82013-04-10 20:10:52158 CharsetConverter* converter,
159 CanonOutput* output) {
Jan Wilken Dörrie5aad5c22021-03-08 21:44:12160 DoConvertToQueryEncoding<char16_t, char16_t>(input, query, converter, output);
[email protected]e7bba5f82013-04-10 20:10:52161}
162
[email protected]0318f922014-04-22 00:09:23163} // namespace url