Fix for Geolocation webTCT failures
[platform/framework/web/chromium-efl.git] / url / url_canon_query.cc
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.
4
5 #include "url/url_canon.h"
6 #include "url/url_canon_internal.h"
7
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
38 namespace url {
39
40 namespace {
41
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]));
54   }
55 }
56
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),
68                      &utf16);
69   converter->ConvertFromUTF16(utf16.data(), utf16.length(), output);
70 }
71
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);
82 }
83
84 template <typename CHAR, typename UCHAR>
85 void DoConvertToQueryEncoding(const CHAR* spec,
86                               const Component& query,
87                               CharsetConverter* converter,
88                               CanonOutput* output) {
89   if (converter) {
90     // Run the converter to get an 8-bit string, then append it, escaping
91     // necessary values.
92     RawCanonOutput<1024> eight_bit;
93     RunConverter(spec, query, converter, &eight_bit);
94     AppendRaw8BitQueryString(eight_bit.data(), eight_bit.length(), output);
95
96   } else {
97     // No converter, do our own UTF-8 conversion.
98     AppendStringOfType(&spec[query.begin], static_cast<size_t>(query.len),
99                        CHAR_QUERY, output);
100   }
101 }
102
103 template<typename CHAR, typename UCHAR>
104 void DoCanonicalizeQuery(const CHAR* spec,
105                          const Component& query,
106                          CharsetConverter* converter,
107                          CanonOutput* output,
108                          Component* out_query) {
109   if (!query.is_valid()) {
110     *out_query = Component();
111     return;
112   }
113
114   output->push_back('?');
115   out_query->begin = output->length();
116
117   DoConvertToQueryEncoding<CHAR, UCHAR>(spec, query, converter, output);
118
119   out_query->len = output->length() - out_query->begin;
120 }
121
122 }  // namespace
123
124 void CanonicalizeQuery(const char* spec,
125                        const Component& query,
126                        CharsetConverter* converter,
127                        CanonOutput* output,
128                        Component* out_query) {
129   DoCanonicalizeQuery<char, unsigned char>(spec, query, converter,
130                                            output, out_query);
131 }
132
133 void CanonicalizeQuery(const char16_t* spec,
134                        const Component& query,
135                        CharsetConverter* converter,
136                        CanonOutput* output,
137                        Component* out_query) {
138   DoCanonicalizeQuery<char16_t, char16_t>(spec, query, converter, output,
139                                           out_query);
140 }
141
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);
147 }
148
149 }  // namespace url