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
.end(),
376 relative_component
.begin
, &relative_parsed
);
378 // Now we can just use the replacement function to replace all the necessary
379 // parts of the old URL with the new one.
380 Replacements
<CHAR
> replacements
;
381 replacements
.SetUsername(relative_url
, relative_parsed
.username
);
382 replacements
.SetPassword(relative_url
, relative_parsed
.password
);
383 replacements
.SetHost(relative_url
, relative_parsed
.host
);
384 replacements
.SetPort(relative_url
, relative_parsed
.port
);
385 replacements
.SetPath(relative_url
, relative_parsed
.path
);
386 replacements
.SetQuery(relative_url
, relative_parsed
.query
);
387 replacements
.SetRef(relative_url
, relative_parsed
.ref
);
389 return ReplaceStandardURL(base_url
, base_parsed
, replacements
,
390 query_converter
, output
, out_parsed
);
393 // Resolves a relative URL that happens to be an absolute file path. Examples
394 // include: "//hostname/path", "/c:/foo", and "//hostname/c:/foo".
395 template<typename CHAR
>
396 bool DoResolveAbsoluteFile(const CHAR
* relative_url
,
397 const url_parse::Component
& relative_component
,
398 CharsetConverter
* query_converter
,
400 url_parse::Parsed
* out_parsed
) {
401 // Parse the file URL. The file URl parsing function uses the same logic
402 // as we do for determining if the file is absolute, in which case it will
403 // not bother to look for a scheme.
404 url_parse::Parsed relative_parsed
;
405 url_parse::ParseFileURL(&relative_url
[relative_component
.begin
],
406 relative_component
.len
, &relative_parsed
);
408 return CanonicalizeFileURL(&relative_url
[relative_component
.begin
],
409 relative_component
.len
, relative_parsed
,
410 query_converter
, output
, out_parsed
);
413 // TODO(brettw) treat two slashes as root like Mozilla for FTP?
414 template<typename CHAR
>
415 bool DoResolveRelativeURL(const char* base_url
,
416 const url_parse::Parsed
& base_parsed
,
418 const CHAR
* relative_url
,
419 const url_parse::Component
& relative_component
,
420 CharsetConverter
* query_converter
,
422 url_parse::Parsed
* out_parsed
) {
423 // Starting point for our output parsed. We'll fix what we change.
424 *out_parsed
= base_parsed
;
426 // Sanity check: the input should have a host or we'll break badly below.
427 // We can only resolve relative URLs with base URLs that have hosts and
428 // paths (even the default path of "/" is OK).
430 // We allow hosts with no length so we can handle file URLs, for example.
431 if (base_parsed
.path
.len
<= 0) {
432 // On error, return the input (resolving a relative URL on a non-relative
434 int base_len
= base_parsed
.Length();
435 for (int i
= 0; i
< base_len
; i
++)
436 output
->push_back(base_url
[i
]);
440 if (relative_component
.len
<= 0) {
441 // Empty relative URL, leave unchanged, only removing the ref component.
442 int base_len
= base_parsed
.Length();
443 base_len
-= base_parsed
.ref
.len
+ 1;
444 out_parsed
->ref
.reset();
445 output
->Append(base_url
, base_len
);
449 int num_slashes
= url_parse::CountConsecutiveSlashes(
450 relative_url
, relative_component
.begin
, relative_component
.end());
453 // On Windows, two slashes for a file path (regardless of which direction
454 // they are) means that it's UNC. Two backslashes on any base scheme mean
455 // that it's an absolute UNC path (we use the base_is_file flag to control
456 // how strict the UNC finder is).
458 // We also allow Windows absolute drive specs on any scheme (for example
459 // "c:\foo") like IE does. There must be no preceeding slashes in this
460 // case (we reject anything like "/c:/foo") because that should be treated
461 // as a path. For file URLs, we allow any number of slashes since that would
462 // be setting the path.
464 // This assumes the absolute path resolver handles absolute URLs like this
465 // properly. url_util::DoCanonicalize does this.
466 int after_slashes
= relative_component
.begin
+ num_slashes
;
467 if (url_parse::DoesBeginUNCPath(relative_url
, relative_component
.begin
,
468 relative_component
.end(), !base_is_file
) ||
469 ((num_slashes
== 0 || base_is_file
) &&
470 url_parse::DoesBeginWindowsDriveSpec(relative_url
, after_slashes
,
471 relative_component
.end()))) {
472 return DoResolveAbsoluteFile(relative_url
, relative_component
,
473 query_converter
, output
, out_parsed
);
476 // Other platforms need explicit handling for file: URLs with multiple
477 // slashes because the generic scheme parsing always extracts a host, but a
478 // file: URL only has a host if it has exactly 2 slashes. Even if it does
479 // have a host, we want to use the special host detection logic for file
480 // URLs provided by DoResolveAbsoluteFile(), as opposed to the generic host
481 // detection logic, for consistency with parsing file URLs from scratch.
482 // This also handles the special case where the URL is only slashes,
483 // since that doesn't have a host part either.
485 (num_slashes
>= 2 || num_slashes
== relative_component
.len
)) {
486 return DoResolveAbsoluteFile(relative_url
, relative_component
,
487 query_converter
, output
, out_parsed
);
491 // Any other double-slashes mean that this is relative to the scheme.
492 if (num_slashes
>= 2) {
493 return DoResolveRelativeHost(base_url
, base_parsed
,
494 relative_url
, relative_component
,
495 query_converter
, output
, out_parsed
);
498 // When we get here, we know that the relative URL is on the same host.
499 return DoResolveRelativePath(base_url
, base_parsed
, base_is_file
,
500 relative_url
, relative_component
,
501 query_converter
, output
, out_parsed
);
506 bool IsRelativeURL(const char* base
,
507 const url_parse::Parsed
& base_parsed
,
508 const char* fragment
,
510 bool is_base_hierarchical
,
512 url_parse::Component
* relative_component
) {
513 return DoIsRelativeURL
<char>(
514 base
, base_parsed
, fragment
, fragment_len
, is_base_hierarchical
,
515 is_relative
, relative_component
);
518 bool IsRelativeURL(const char* base
,
519 const url_parse::Parsed
& base_parsed
,
520 const base::char16
* fragment
,
522 bool is_base_hierarchical
,
524 url_parse::Component
* relative_component
) {
525 return DoIsRelativeURL
<base::char16
>(
526 base
, base_parsed
, fragment
, fragment_len
, is_base_hierarchical
,
527 is_relative
, relative_component
);
530 bool ResolveRelativeURL(const char* base_url
,
531 const url_parse::Parsed
& base_parsed
,
533 const char* relative_url
,
534 const url_parse::Component
& relative_component
,
535 CharsetConverter
* query_converter
,
537 url_parse::Parsed
* out_parsed
) {
538 return DoResolveRelativeURL
<char>(
539 base_url
, base_parsed
, base_is_file
, relative_url
,
540 relative_component
, query_converter
, output
, out_parsed
);
543 bool ResolveRelativeURL(const char* base_url
,
544 const url_parse::Parsed
& base_parsed
,
546 const base::char16
* relative_url
,
547 const url_parse::Component
& relative_component
,
548 CharsetConverter
* query_converter
,
550 url_parse::Parsed
* out_parsed
) {
551 return DoResolveRelativeURL
<base::char16
>(
552 base_url
, base_parsed
, base_is_file
, relative_url
,
553 relative_component
, query_converter
, output
, out_parsed
);
556 } // namespace url_canon