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