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.
5 // Canonicalizer functions for working with and resolving relative URLs.
7 #include "base/logging.h"
8 #include "url/url_canon.h"
9 #include "url/url_canon_internal.h"
10 #include "url/url_file.h"
11 #include "url/url_parse_internal.h"
12 #include "url/url_util_internal.h"
18 // Firefox does a case-sensitive compare (which is probably wrong--Mozilla bug
19 // 379034), whereas IE is case-insensetive.
21 // We choose to be more permissive like IE. We don't need to worry about
22 // unescaping or anything here: neither IE or Firefox allow this. We also
23 // don't have to worry about invalid scheme characters since we are comparing
24 // against the canonical scheme of the base.
26 // The base URL should always be canonical, therefore is ASCII.
27 template<typename CHAR
>
28 bool AreSchemesEqual(const char* base
,
29 const url_parse::Component
& base_scheme
,
31 const url_parse::Component
& cmp_scheme
) {
32 if (base_scheme
.len
!= cmp_scheme
.len
)
34 for (int i
= 0; i
< base_scheme
.len
; i
++) {
35 // We assume the base is already canonical, so we don't have to
37 if (CanonicalSchemeChar(cmp
[cmp_scheme
.begin
+ i
]) !=
38 base
[base_scheme
.begin
+ i
])
46 // Here, we also allow Windows paths to be represented as "/C:/" so we can be
47 // consistent about URL paths beginning with slashes. This function is like
48 // DoesBeginWindowsDrivePath except that it also requires a slash at the
50 template<typename CHAR
>
51 bool DoesBeginSlashWindowsDriveSpec(const CHAR
* spec
, int start_offset
,
53 if (start_offset
>= spec_len
)
55 return url_parse::IsURLSlash(spec
[start_offset
]) &&
56 url_parse::DoesBeginWindowsDriveSpec(spec
, start_offset
+ 1, spec_len
);
61 // See IsRelativeURL in the header file for usage.
62 template<typename CHAR
>
63 bool DoIsRelativeURL(const char* base
,
64 const url_parse::Parsed
& base_parsed
,
67 bool is_base_hierarchical
,
69 url_parse::Component
* relative_component
) {
70 *is_relative
= false; // So we can default later to not relative.
72 // Trim whitespace and construct a new range for the substring.
74 url_parse::TrimURL(url
, &begin
, &url_len
);
75 if (begin
>= url_len
) {
76 // Empty URLs are relative, but do nothing.
77 *relative_component
= url_parse::Component(begin
, 0);
83 // We special case paths like "C:\foo" so they can link directly to the
84 // file on Windows (IE compatability). The security domain stuff should
85 // prevent a link like this from actually being followed if its on a
88 // We treat "C:/foo" as an absolute URL. We can go ahead and treat "/c:/"
89 // as relative, as this will just replace the path when the base scheme
90 // is a file and the answer will still be correct.
92 // We require strict backslashes when detecting UNC since two forward
93 // shashes should be treated a a relative URL with a hostname.
94 if (url_parse::DoesBeginWindowsDriveSpec(url
, begin
, url_len
) ||
95 url_parse::DoesBeginUNCPath(url
, begin
, url_len
, true))
99 // See if we've got a scheme, if not, we know this is a relative URL.
100 // BUT: Just because we have a scheme, doesn't make it absolute.
101 // "http:foo.html" is a relative URL with path "foo.html". If the scheme is
102 // empty, we treat it as relative (":foo") like IE does.
103 url_parse::Component scheme
;
104 if (!url_parse::ExtractScheme(url
, url_len
, &scheme
) || scheme
.len
== 0) {
105 // Don't allow relative URLs if the base scheme doesn't support it.
106 if (!is_base_hierarchical
)
109 *relative_component
= url_parse::MakeRange(begin
, url_len
);
114 // If the scheme isn't valid, then it's relative.
115 int scheme_end
= scheme
.end();
116 for (int i
= scheme
.begin
; i
< scheme_end
; i
++) {
117 if (!CanonicalSchemeChar(url
[i
])) {
118 *relative_component
= url_parse::MakeRange(begin
, url_len
);
124 // If the scheme is not the same, then we can't count it as relative.
125 if (!AreSchemesEqual(base
, base_parsed
.scheme
, url
, scheme
))
128 // When the scheme that they both share is not hierarchical, treat the
129 // incoming scheme as absolute (this way with the base of "data:foo",
130 // "data:bar" will be reported as absolute.
131 if (!is_base_hierarchical
)
134 int colon_offset
= scheme
.end();
136 // If it's a filesystem URL, the only valid way to make it relative is not to
137 // supply a scheme. There's no equivalent to e.g. http:index.html.
138 if (url_util::CompareSchemeComponent(url
, scheme
, "filesystem"))
141 // ExtractScheme guarantees that the colon immediately follows what it
142 // considers to be the scheme. CountConsecutiveSlashes will handle the
143 // case where the begin offset is the end of the input.
144 int num_slashes
= url_parse::CountConsecutiveSlashes(url
, colon_offset
+ 1,
147 if (num_slashes
== 0 || num_slashes
== 1) {
148 // No slashes means it's a relative path like "http:foo.html". One slash
149 // is an absolute path. "http:/home/foo.html"
151 *relative_component
= url_parse::MakeRange(colon_offset
+ 1, url_len
);
155 // Two or more slashes after the scheme we treat as absolute.
159 // Copies all characters in the range [begin, end) of |spec| to the output,
160 // up until and including the last slash. There should be a slash in the
161 // range, if not, nothing will be copied.
163 // The input is assumed to be canonical, so we search only for exact slashes
164 // and not backslashes as well. We also know that it's ASCII.
165 void CopyToLastSlash(const char* spec
,
168 CanonOutput
* output
) {
169 // Find the last slash.
171 for (int i
= end
- 1; i
>= begin
; i
--) {
172 if (spec
[i
] == '/') {
181 for (int i
= begin
; i
<= last_slash
; i
++)
182 output
->push_back(spec
[i
]);
185 // Copies a single component from the source to the output. This is used
186 // when resolving relative URLs and a given component is unchanged. Since the
187 // source should already be canonical, we don't have to do anything special,
188 // and the input is ASCII.
189 void CopyOneComponent(const char* source
,
190 const url_parse::Component
& source_component
,
192 url_parse::Component
* output_component
) {
193 if (source_component
.len
< 0) {
194 // This component is not present.
195 *output_component
= url_parse::Component();
199 output_component
->begin
= output
->length();
200 int source_end
= source_component
.end();
201 for (int i
= source_component
.begin
; i
< source_end
; i
++)
202 output
->push_back(source
[i
]);
203 output_component
->len
= output
->length() - output_component
->begin
;
208 // Called on Windows when the base URL is a file URL, this will copy the "C:"
209 // to the output, if there is a drive letter and if that drive letter is not
210 // being overridden by the relative URL. Otherwise, do nothing.
212 // It will return the index of the beginning of the next character in the
213 // base to be processed: if there is a "C:", the slash after it, or if
214 // there is no drive letter, the slash at the beginning of the path, or
215 // the end of the base. This can be used as the starting offset for further
217 template<typename CHAR
>
218 int CopyBaseDriveSpecIfNecessary(const char* base_url
,
221 const CHAR
* relative_url
,
223 int relative_url_len
,
224 CanonOutput
* output
) {
225 if (base_path_begin
>= base_path_end
)
226 return base_path_begin
; // No path.
228 // If the relative begins with a drive spec, don't do anything. The existing
229 // drive spec in the base will be replaced.
230 if (url_parse::DoesBeginWindowsDriveSpec(relative_url
,
231 path_start
, relative_url_len
)) {
232 return base_path_begin
; // Relative URL path is "C:/foo"
235 // The path should begin with a slash (as all canonical paths do). We check
236 // if it is followed by a drive letter and copy it.
237 if (DoesBeginSlashWindowsDriveSpec(base_url
,
240 // Copy the two-character drive spec to the output. It will now look like
241 // "file:///C:" so the rest of it can be treated like a standard path.
242 output
->push_back('/');
243 output
->push_back(base_url
[base_path_begin
+ 1]);
244 output
->push_back(base_url
[base_path_begin
+ 2]);
245 return base_path_begin
+ 3;
248 return base_path_begin
;
253 // A subroutine of DoResolveRelativeURL, this resolves the URL knowning that
254 // the input is a relative path or less (qyuery or ref).
255 template<typename CHAR
>
256 bool DoResolveRelativePath(const char* base_url
,
257 const url_parse::Parsed
& base_parsed
,
259 const CHAR
* relative_url
,
260 const url_parse::Component
& relative_component
,
261 CharsetConverter
* query_converter
,
263 url_parse::Parsed
* out_parsed
) {
266 // We know the authority section didn't change, copy it to the output. We
267 // also know we have a path so can copy up to there.
268 url_parse::Component path
, query
, ref
;
269 url_parse::ParsePathInternal(relative_url
,
274 // Canonical URLs always have a path, so we can use that offset.
275 output
->Append(base_url
, base_parsed
.path
.begin
);
278 // The path is replaced or modified.
279 int true_path_begin
= output
->length();
281 // For file: URLs on Windows, we don't want to treat the drive letter and
282 // colon as part of the path for relative file resolution when the
283 // incoming URL does not provide a drive spec. We save the true path
284 // beginning so we can fix it up after we are done.
285 int base_path_begin
= base_parsed
.path
.begin
;
288 base_path_begin
= CopyBaseDriveSpecIfNecessary(
289 base_url
, base_parsed
.path
.begin
, base_parsed
.path
.end(),
290 relative_url
, relative_component
.begin
, relative_component
.end(),
292 // Now the output looks like either "file://" or "file:///C:"
293 // and we can start appending the rest of the path. |base_path_begin|
294 // points to the character in the base that comes next.
298 if (url_parse::IsURLSlash(relative_url
[path
.begin
])) {
299 // Easy case: the path is an absolute path on the server, so we can
300 // just replace everything from the path on with the new versions.
301 // Since the input should be canonical hierarchical URL, we should
302 // always have a path.
303 success
&= CanonicalizePath(relative_url
, path
,
304 output
, &out_parsed
->path
);
306 // Relative path, replace the query, and reference. We take the
307 // original path with the file part stripped, and append the new path.
308 // The canonicalizer will take care of resolving ".." and "."
309 int path_begin
= output
->length();
310 CopyToLastSlash(base_url
, base_path_begin
, base_parsed
.path
.end(),
312 success
&= CanonicalizePartialPath(relative_url
, path
, path_begin
,
314 out_parsed
->path
= url_parse::MakeRange(path_begin
, output
->length());
316 // Copy the rest of the stuff after the path from the relative path.
319 // Finish with the query and reference part (these can't fail).
320 CanonicalizeQuery(relative_url
, query
, query_converter
,
321 output
, &out_parsed
->query
);
322 CanonicalizeRef(relative_url
, ref
, output
, &out_parsed
->ref
);
324 // Fix the path beginning to add back the "C:" we may have written above.
325 out_parsed
->path
= url_parse::MakeRange(true_path_begin
,
326 out_parsed
->path
.end());
330 // If we get here, the path is unchanged: copy to output.
331 CopyOneComponent(base_url
, base_parsed
.path
, output
, &out_parsed
->path
);
333 if (query
.is_valid()) {
334 // Just the query specified, replace the query and reference (ignore
335 // failures for refs)
336 CanonicalizeQuery(relative_url
, query
, query_converter
,
337 output
, &out_parsed
->query
);
338 CanonicalizeRef(relative_url
, ref
, output
, &out_parsed
->ref
);
342 // If we get here, the query is unchanged: copy to output. Note that the
343 // range of the query parameter doesn't include the question mark, so we
344 // have to add it manually if there is a component.
345 if (base_parsed
.query
.is_valid())
346 output
->push_back('?');
347 CopyOneComponent(base_url
, base_parsed
.query
, output
, &out_parsed
->query
);
349 if (ref
.is_valid()) {
350 // Just the reference specified: replace it (ignoring failures).
351 CanonicalizeRef(relative_url
, ref
, output
, &out_parsed
->ref
);
355 // We should always have something to do in this function, the caller checks
356 // that some component is being replaced.
357 DCHECK(false) << "Not reached";
361 // Resolves a relative URL that contains a host. Typically, these will
362 // be of the form "//www.google.com/foo/bar?baz#ref" and the only thing which
363 // should be kept from the original URL is the scheme.
364 template<typename CHAR
>
365 bool DoResolveRelativeHost(const char* base_url
,
366 const url_parse::Parsed
& base_parsed
,
367 const CHAR
* relative_url
,
368 const url_parse::Component
& relative_component
,
369 CharsetConverter
* query_converter
,
371 url_parse::Parsed
* out_parsed
) {
372 // Parse the relative URL, just like we would for anything following a
374 url_parse::Parsed relative_parsed
; // Everything but the scheme is valid.
375 url_parse::ParseAfterScheme(&relative_url
[relative_component
.begin
],
376 relative_component
.len
, relative_component
.begin
,
379 // Now we can just use the replacement function to replace all the necessary
380 // parts of the old URL with the new one.
381 Replacements
<CHAR
> replacements
;
382 replacements
.SetUsername(relative_url
, relative_parsed
.username
);
383 replacements
.SetPassword(relative_url
, relative_parsed
.password
);
384 replacements
.SetHost(relative_url
, relative_parsed
.host
);
385 replacements
.SetPort(relative_url
, relative_parsed
.port
);
386 replacements
.SetPath(relative_url
, relative_parsed
.path
);
387 replacements
.SetQuery(relative_url
, relative_parsed
.query
);
388 replacements
.SetRef(relative_url
, relative_parsed
.ref
);
390 return ReplaceStandardURL(base_url
, base_parsed
, replacements
,
391 query_converter
, output
, out_parsed
);
394 // Resolves a relative URL that happens to be an absolute file path. Examples
395 // include: "//hostname/path", "/c:/foo", and "//hostname/c:/foo".
396 template<typename CHAR
>
397 bool DoResolveAbsoluteFile(const CHAR
* relative_url
,
398 const url_parse::Component
& relative_component
,
399 CharsetConverter
* query_converter
,
401 url_parse::Parsed
* out_parsed
) {
402 // Parse the file URL. The file URl parsing function uses the same logic
403 // as we do for determining if the file is absolute, in which case it will
404 // not bother to look for a scheme.
405 url_parse::Parsed relative_parsed
;
406 url_parse::ParseFileURL(&relative_url
[relative_component
.begin
],
407 relative_component
.len
, &relative_parsed
);
409 return CanonicalizeFileURL(&relative_url
[relative_component
.begin
],
410 relative_component
.len
, relative_parsed
,
411 query_converter
, output
, out_parsed
);
414 // TODO(brettw) treat two slashes as root like Mozilla for FTP?
415 template<typename CHAR
>
416 bool DoResolveRelativeURL(const char* base_url
,
417 const url_parse::Parsed
& base_parsed
,
419 const CHAR
* relative_url
,
420 const url_parse::Component
& relative_component
,
421 CharsetConverter
* query_converter
,
423 url_parse::Parsed
* out_parsed
) {
424 // Starting point for our output parsed. We'll fix what we change.
425 *out_parsed
= base_parsed
;
427 // Sanity check: the input should have a host or we'll break badly below.
428 // We can only resolve relative URLs with base URLs that have hosts and
429 // paths (even the default path of "/" is OK).
431 // We allow hosts with no length so we can handle file URLs, for example.
432 if (base_parsed
.path
.len
<= 0) {
433 // On error, return the input (resolving a relative URL on a non-relative
435 int base_len
= base_parsed
.Length();
436 for (int i
= 0; i
< base_len
; i
++)
437 output
->push_back(base_url
[i
]);
441 if (relative_component
.len
<= 0) {
442 // Empty relative URL, leave unchanged, only removing the ref component.
443 int base_len
= base_parsed
.Length();
444 base_len
-= base_parsed
.ref
.len
+ 1;
445 out_parsed
->ref
.reset();
446 output
->Append(base_url
, base_len
);
450 int num_slashes
= url_parse::CountConsecutiveSlashes(
451 relative_url
, relative_component
.begin
, relative_component
.end());
454 // On Windows, two slashes for a file path (regardless of which direction
455 // they are) means that it's UNC. Two backslashes on any base scheme mean
456 // that it's an absolute UNC path (we use the base_is_file flag to control
457 // how strict the UNC finder is).
459 // We also allow Windows absolute drive specs on any scheme (for example
460 // "c:\foo") like IE does. There must be no preceeding slashes in this
461 // case (we reject anything like "/c:/foo") because that should be treated
462 // as a path. For file URLs, we allow any number of slashes since that would
463 // be setting the path.
465 // This assumes the absolute path resolver handles absolute URLs like this
466 // properly. url_util::DoCanonicalize does this.
467 int after_slashes
= relative_component
.begin
+ num_slashes
;
468 if (url_parse::DoesBeginUNCPath(relative_url
, relative_component
.begin
,
469 relative_component
.end(), !base_is_file
) ||
470 ((num_slashes
== 0 || base_is_file
) &&
471 url_parse::DoesBeginWindowsDriveSpec(relative_url
, after_slashes
,
472 relative_component
.end()))) {
473 return DoResolveAbsoluteFile(relative_url
, relative_component
,
474 query_converter
, output
, out_parsed
);
477 // Other platforms need explicit handling for file: URLs with multiple
478 // slashes because the generic scheme parsing always extracts a host, but a
479 // file: URL only has a host if it has exactly 2 slashes. This also
480 // handles the special case where the URL is only slashes, since that
481 // doesn't have a host part either.
483 (num_slashes
> 2 || num_slashes
== relative_component
.len
)) {
484 return DoResolveAbsoluteFile(relative_url
, relative_component
,
485 query_converter
, output
, out_parsed
);
489 // Any other double-slashes mean that this is relative to the scheme.
490 if (num_slashes
>= 2) {
491 return DoResolveRelativeHost(base_url
, base_parsed
,
492 relative_url
, relative_component
,
493 query_converter
, output
, out_parsed
);
496 // When we get here, we know that the relative URL is on the same host.
497 return DoResolveRelativePath(base_url
, base_parsed
, base_is_file
,
498 relative_url
, relative_component
,
499 query_converter
, output
, out_parsed
);
504 bool IsRelativeURL(const char* base
,
505 const url_parse::Parsed
& base_parsed
,
506 const char* fragment
,
508 bool is_base_hierarchical
,
510 url_parse::Component
* relative_component
) {
511 return DoIsRelativeURL
<char>(
512 base
, base_parsed
, fragment
, fragment_len
, is_base_hierarchical
,
513 is_relative
, relative_component
);
516 bool IsRelativeURL(const char* base
,
517 const url_parse::Parsed
& base_parsed
,
518 const char16
* fragment
,
520 bool is_base_hierarchical
,
522 url_parse::Component
* relative_component
) {
523 return DoIsRelativeURL
<char16
>(
524 base
, base_parsed
, fragment
, fragment_len
, is_base_hierarchical
,
525 is_relative
, relative_component
);
528 bool ResolveRelativeURL(const char* base_url
,
529 const url_parse::Parsed
& base_parsed
,
531 const char* relative_url
,
532 const url_parse::Component
& relative_component
,
533 CharsetConverter
* query_converter
,
535 url_parse::Parsed
* out_parsed
) {
536 return DoResolveRelativeURL
<char>(
537 base_url
, base_parsed
, base_is_file
, relative_url
,
538 relative_component
, query_converter
, output
, out_parsed
);
541 bool ResolveRelativeURL(const char* base_url
,
542 const url_parse::Parsed
& base_parsed
,
544 const char16
* relative_url
,
545 const url_parse::Component
& relative_component
,
546 CharsetConverter
* query_converter
,
548 url_parse::Parsed
* out_parsed
) {
549 return DoResolveRelativeURL
<char16
>(
550 base_url
, base_parsed
, base_is_file
, relative_url
,
551 relative_component
, query_converter
, output
, out_parsed
);
554 } // namespace url_canon