xref: /aosp_15_r20/external/cronet/url/url_canon_stdurl.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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 // 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>
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   DCHECK(!parsed.has_opaque_path);
24 
25   // Scheme: this will append the colon.
26   bool success = CanonicalizeScheme(source.scheme, parsed.scheme,
27                                     output, &new_parsed->scheme);
28 
29   bool scheme_supports_user_info =
30       (scheme_type == SCHEME_WITH_HOST_PORT_AND_USER_INFORMATION);
31   bool scheme_supports_ports =
32       (scheme_type == SCHEME_WITH_HOST_PORT_AND_USER_INFORMATION ||
33        scheme_type == SCHEME_WITH_HOST_AND_PORT);
34 
35   // Authority (username, password, host, port)
36   bool have_authority;
37   if ((scheme_supports_user_info &&
38        (parsed.username.is_valid() || parsed.password.is_valid())) ||
39       parsed.host.is_nonempty() ||
40       (scheme_supports_ports && parsed.port.is_valid())) {
41     have_authority = true;
42 
43     // Only write the authority separators when we have a scheme.
44     if (parsed.scheme.is_valid()) {
45       output->push_back('/');
46       output->push_back('/');
47     }
48 
49     // User info: the canonicalizer will handle the : and @.
50     if (scheme_supports_user_info) {
51       success &= CanonicalizeUserInfo(
52           source.username, parsed.username, source.password, parsed.password,
53           output, &new_parsed->username, &new_parsed->password);
54     } else {
55       new_parsed->username.reset();
56       new_parsed->password.reset();
57     }
58 
59     success &= CanonicalizeHost(source.host, parsed.host,
60                                 output, &new_parsed->host);
61 
62     // Host must not be empty for standard URLs.
63     if (parsed.host.is_empty())
64       success = false;
65 
66     // Port: the port canonicalizer will handle the colon.
67     if (scheme_supports_ports) {
68       int default_port = DefaultPortForScheme(
69           &output->data()[new_parsed->scheme.begin], new_parsed->scheme.len);
70       success &= CanonicalizePort(source.port, parsed.port, default_port,
71                                   output, &new_parsed->port);
72     } else {
73       new_parsed->port.reset();
74     }
75   } else {
76     // No authority, clear the components.
77     have_authority = false;
78     new_parsed->host.reset();
79     new_parsed->username.reset();
80     new_parsed->password.reset();
81     new_parsed->port.reset();
82     success = false;  // Standard URLs must have an authority.
83   }
84 
85   // Path
86   if (parsed.path.is_valid()) {
87     success &= CanonicalizePath(source.path, parsed.path,
88                                 output, &new_parsed->path);
89   } else if (have_authority ||
90              parsed.query.is_valid() || parsed.ref.is_valid()) {
91     // When we have an empty path, make up a path when we have an authority
92     // or something following the path. The only time we allow an empty
93     // output path is when there is nothing else.
94     new_parsed->path = Component(output->length(), 1);
95     output->push_back('/');
96   } else {
97     // No path at all
98     new_parsed->path.reset();
99   }
100 
101   // Query
102   CanonicalizeQuery(source.query, parsed.query, query_converter,
103                     output, &new_parsed->query);
104 
105   // Ref: ignore failure for this, since the page can probably still be loaded.
106   CanonicalizeRef(source.ref, parsed.ref, output, &new_parsed->ref);
107 
108   // Carry over the flag for potentially dangling markup:
109   if (parsed.potentially_dangling_markup)
110     new_parsed->potentially_dangling_markup = true;
111 
112   return success;
113 }
114 
115 }  // namespace
116 
117 // Returns the default port for the given canonical scheme, or PORT_UNSPECIFIED
118 // if the scheme is unknown.
119 //
120 // Please keep blink::DefaultPortForProtocol and url::DefaultPortForProtocol in
121 // sync.
DefaultPortForScheme(const char * scheme,int scheme_len)122 int DefaultPortForScheme(const char* scheme, int scheme_len) {
123   int default_port = PORT_UNSPECIFIED;
124   switch (scheme_len) {
125     case 4:
126       if (!strncmp(scheme, kHttpScheme, scheme_len))
127         default_port = 80;
128       break;
129     case 5:
130       if (!strncmp(scheme, kHttpsScheme, scheme_len))
131         default_port = 443;
132       break;
133     case 3:
134       if (!strncmp(scheme, kFtpScheme, scheme_len))
135         default_port = 21;
136       else if (!strncmp(scheme, kWssScheme, scheme_len))
137         default_port = 443;
138       break;
139     case 2:
140       if (!strncmp(scheme, kWsScheme, scheme_len))
141         default_port = 80;
142       break;
143   }
144   return default_port;
145 }
146 
CanonicalizeStandardURL(const char * spec,const Parsed & parsed,SchemeType scheme_type,CharsetConverter * query_converter,CanonOutput * output,Parsed * new_parsed)147 bool CanonicalizeStandardURL(const char* spec,
148                              const Parsed& parsed,
149                              SchemeType scheme_type,
150                              CharsetConverter* query_converter,
151                              CanonOutput* output,
152                              Parsed* new_parsed) {
153   return DoCanonicalizeStandardURL(URLComponentSource(spec), parsed,
154                                    scheme_type, query_converter, output,
155                                    new_parsed);
156 }
157 
CanonicalizeStandardURL(const char16_t * spec,const Parsed & parsed,SchemeType scheme_type,CharsetConverter * query_converter,CanonOutput * output,Parsed * new_parsed)158 bool CanonicalizeStandardURL(const char16_t* spec,
159                              const Parsed& parsed,
160                              SchemeType scheme_type,
161                              CharsetConverter* query_converter,
162                              CanonOutput* output,
163                              Parsed* new_parsed) {
164   return DoCanonicalizeStandardURL(URLComponentSource(spec), parsed,
165                                    scheme_type, query_converter, output,
166                                    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(source, parsed, scheme_type, query_converter,
189                                    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<char16_t> & 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<char16_t>& 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(source, parsed, scheme_type, query_converter,
206                                    output, new_parsed);
207 }
208 
209 }  // namespace url
210