1 // 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.
4 
5 // Functions to canonicalize "standard" URLs, which are ones that have an
6 // authority section including a host name.
7 
8 #include "url/url_canon.h"
9 #include "url/url_canon_internal.h"
10 #include "url/url_constants.h"
11 
12 namespace url {
13 
14 namespace {
15 
16 template <typename CHAR, typename UCHAR>
DoCanonicalizeStandardURL(const URLComponentSource<CHAR> & source,const Parsed & parsed,SchemeType scheme_type,CharsetConverter * query_converter,CanonOutput * output,Parsed * new_parsed)17 bool DoCanonicalizeStandardURL(const URLComponentSource<CHAR>& source,
18                                const Parsed& parsed,
19                                SchemeType scheme_type,
20                                CharsetConverter* query_converter,
21                                CanonOutput* output,
22                                Parsed* new_parsed) {
23   // Scheme: this will append the colon.
24   bool success = CanonicalizeScheme(source.scheme, parsed.scheme,
25                                     output, &new_parsed->scheme);
26 
27   bool scheme_supports_user_info =
28       (scheme_type == SCHEME_WITH_HOST_PORT_AND_USER_INFORMATION);
29   bool scheme_supports_ports =
30       (scheme_type == SCHEME_WITH_HOST_PORT_AND_USER_INFORMATION ||
31        scheme_type == SCHEME_WITH_HOST_AND_PORT);
32 
33   // Authority (username, password, host, port)
34   bool have_authority;
35   if ((scheme_supports_user_info &&
36        (parsed.username.is_valid() || parsed.password.is_valid())) ||
37       parsed.host.is_nonempty() ||
38       (scheme_supports_ports && parsed.port.is_valid())) {
39     have_authority = true;
40 
41     // Only write the authority separators when we have a scheme.
42     if (parsed.scheme.is_valid()) {
43       output->push_back('/');
44       output->push_back('/');
45     }
46 
47     // User info: the canonicalizer will handle the : and @.
48     if (scheme_supports_user_info) {
49       success &= CanonicalizeUserInfo(
50           source.username, parsed.username, source.password, parsed.password,
51           output, &new_parsed->username, &new_parsed->password);
52     } else {
53       new_parsed->username.reset();
54       new_parsed->password.reset();
55     }
56 
57     success &= CanonicalizeHost(source.host, parsed.host,
58                                 output, &new_parsed->host);
59 
60     // Host must not be empty for standard URLs.
61     if (!parsed.host.is_nonempty())
62       success = false;
63 
64     // Port: the port canonicalizer will handle the colon.
65     if (scheme_supports_ports) {
66       int default_port = DefaultPortForScheme(
67           &output->data()[new_parsed->scheme.begin], new_parsed->scheme.len);
68       success &= CanonicalizePort(source.port, parsed.port, default_port,
69                                   output, &new_parsed->port);
70     } else {
71       new_parsed->port.reset();
72     }
73   } else {
74     // No authority, clear the components.
75     have_authority = false;
76     new_parsed->host.reset();
77     new_parsed->username.reset();
78     new_parsed->password.reset();
79     new_parsed->port.reset();
80     success = false;  // Standard URLs must have an authority.
81   }
82 
83   // Path
84   if (parsed.path.is_valid()) {
85     success &= CanonicalizePath(source.path, parsed.path,
86                                 output, &new_parsed->path);
87   } else if (have_authority ||
88              parsed.query.is_valid() || parsed.ref.is_valid()) {
89     // When we have an empty path, make up a path when we have an authority
90     // or something following the path. The only time we allow an empty
91     // output path is when there is nothing else.
92     new_parsed->path = Component(output->length(), 1);
93     output->push_back('/');
94   } else {
95     // No path at all
96     new_parsed->path.reset();
97   }
98 
99   // Query
100   CanonicalizeQuery(source.query, parsed.query, query_converter,
101                     output, &new_parsed->query);
102 
103   // Ref: ignore failure for this, since the page can probably still be loaded.
104   CanonicalizeRef(source.ref, parsed.ref, output, &new_parsed->ref);
105 
106   return success;
107 }
108 
109 }  // namespace
110 
111 // Returns the default port for the given canonical scheme, or PORT_UNSPECIFIED
112 // if the scheme is unknown.
113 //
114 // Please keep blink::DefaultPortForProtocol and url::DefaultPortForProtocol in
115 // sync.
DefaultPortForScheme(const char * scheme,int scheme_len)116 int DefaultPortForScheme(const char* scheme, int scheme_len) {
117   int default_port = PORT_UNSPECIFIED;
118   switch (scheme_len) {
119     case 4:
120       if (!strncmp(scheme, kHttpScheme, scheme_len))
121         default_port = 80;
122       break;
123     case 5:
124       if (!strncmp(scheme, kHttpsScheme, scheme_len))
125         default_port = 443;
126       break;
127     case 3:
128       if (!strncmp(scheme, kFtpScheme, scheme_len))
129         default_port = 21;
130       else if (!strncmp(scheme, kWssScheme, scheme_len))
131         default_port = 443;
132       break;
133     case 2:
134       if (!strncmp(scheme, kWsScheme, scheme_len))
135         default_port = 80;
136       break;
137   }
138   return default_port;
139 }
140 
CanonicalizeStandardURL(const char * spec,int spec_len,const Parsed & parsed,SchemeType scheme_type,CharsetConverter * query_converter,CanonOutput * output,Parsed * new_parsed)141 bool CanonicalizeStandardURL(const char* spec,
142                              int spec_len,
143                              const Parsed& parsed,
144                              SchemeType scheme_type,
145                              CharsetConverter* query_converter,
146                              CanonOutput* output,
147                              Parsed* new_parsed) {
148   return DoCanonicalizeStandardURL<char, unsigned char>(
149       URLComponentSource<char>(spec), parsed, scheme_type, query_converter,
150       output, new_parsed);
151 }
152 
CanonicalizeStandardURL(const base::char16 * spec,int spec_len,const Parsed & parsed,SchemeType scheme_type,CharsetConverter * query_converter,CanonOutput * output,Parsed * new_parsed)153 bool CanonicalizeStandardURL(const base::char16* spec,
154                              int spec_len,
155                              const Parsed& parsed,
156                              SchemeType scheme_type,
157                              CharsetConverter* query_converter,
158                              CanonOutput* output,
159                              Parsed* new_parsed) {
160   return DoCanonicalizeStandardURL<base::char16, base::char16>(
161       URLComponentSource<base::char16>(spec), parsed, scheme_type,
162       query_converter, output, new_parsed);
163 }
164 
165 // It might be nice in the future to optimize this so unchanged components don't
166 // need to be recanonicalized. This is especially true since the common case for
167 // ReplaceComponents is removing things we don't want, like reference fragments
168 // and usernames. These cases can become more efficient if we can assume the
169 // rest of the URL is OK with these removed (or only the modified parts
170 // recanonicalized). This would be much more complex to implement, however.
171 //
172 // You would also need to update DoReplaceComponents in url_util.cc which
173 // relies on this re-checking everything (see the comment there for why).
ReplaceStandardURL(const char * base,const Parsed & base_parsed,const Replacements<char> & replacements,SchemeType scheme_type,CharsetConverter * query_converter,CanonOutput * output,Parsed * new_parsed)174 bool ReplaceStandardURL(const char* base,
175                         const Parsed& base_parsed,
176                         const Replacements<char>& replacements,
177                         SchemeType scheme_type,
178                         CharsetConverter* query_converter,
179                         CanonOutput* output,
180                         Parsed* new_parsed) {
181   URLComponentSource<char> source(base);
182   Parsed parsed(base_parsed);
183   SetupOverrideComponents(base, replacements, &source, &parsed);
184   return DoCanonicalizeStandardURL<char, unsigned char>(
185       source, parsed, scheme_type, query_converter, output, new_parsed);
186 }
187 
188 // For 16-bit replacements, we turn all the replacements into UTF-8 so the
189 // regular code path can be used.
ReplaceStandardURL(const char * base,const Parsed & base_parsed,const Replacements<base::char16> & replacements,SchemeType scheme_type,CharsetConverter * query_converter,CanonOutput * output,Parsed * new_parsed)190 bool ReplaceStandardURL(const char* base,
191                         const Parsed& base_parsed,
192                         const Replacements<base::char16>& replacements,
193                         SchemeType scheme_type,
194                         CharsetConverter* query_converter,
195                         CanonOutput* output,
196                         Parsed* new_parsed) {
197   RawCanonOutput<1024> utf8;
198   URLComponentSource<char> source(base);
199   Parsed parsed(base_parsed);
200   SetupUTF16OverrideComponents(base, replacements, &utf8, &source, &parsed);
201   return DoCanonicalizeStandardURL<char, unsigned char>(
202       source, parsed, scheme_type, query_converter, output, new_parsed);
203 }
204 
205 }  // namespace url
206