url_canon_relative.cc revision 0529e5d033099cbfc42635f6f6183833b09dff6e
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// Canonicalizer functions for working with and resolving relative URLs.
6
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"
13
14namespace url {
15
16namespace {
17
18// Firefox does a case-sensitive compare (which is probably wrong--Mozilla bug
19// 379034), whereas IE is case-insensetive.
20//
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.
25//
26// The base URL should always be canonical, therefore is ASCII.
27template<typename CHAR>
28bool AreSchemesEqual(const char* base,
29                     const Component& base_scheme,
30                     const CHAR* cmp,
31                     const Component& cmp_scheme) {
32  if (base_scheme.len != cmp_scheme.len)
33    return false;
34  for (int i = 0; i < base_scheme.len; i++) {
35    // We assume the base is already canonical, so we don't have to
36    // canonicalize it.
37    if (CanonicalSchemeChar(cmp[cmp_scheme.begin + i]) !=
38        base[base_scheme.begin + i])
39      return false;
40  }
41  return true;
42}
43
44#ifdef WIN32
45
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
49// beginning.
50template<typename CHAR>
51bool DoesBeginSlashWindowsDriveSpec(const CHAR* spec, int start_offset,
52                                    int spec_len) {
53  if (start_offset >= spec_len)
54    return false;
55  return IsURLSlash(spec[start_offset]) &&
56         DoesBeginWindowsDriveSpec(spec, start_offset + 1, spec_len);
57}
58
59#endif  // WIN32
60
61// See IsRelativeURL in the header file for usage.
62template<typename CHAR>
63bool DoIsRelativeURL(const char* base,
64                     const Parsed& base_parsed,
65                     const CHAR* url,
66                     int url_len,
67                     bool is_base_hierarchical,
68                     bool* is_relative,
69                     Component* relative_component) {
70  *is_relative = false;  // So we can default later to not relative.
71
72  // Trim whitespace and construct a new range for the substring.
73  int begin = 0;
74  TrimURL(url, &begin, &url_len);
75  if (begin >= url_len) {
76    // Empty URLs are relative, but do nothing.
77    *relative_component = Component(begin, 0);
78    *is_relative = true;
79    return true;
80  }
81
82#ifdef WIN32
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
86  // web page.
87  //
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.
91  //
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 (DoesBeginWindowsDriveSpec(url, begin, url_len) ||
95      DoesBeginUNCPath(url, begin, url_len, true))
96    return true;
97#endif  // WIN32
98
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  Component scheme;
104  const bool scheme_is_empty =
105      !ExtractScheme(url, url_len, &scheme) || scheme.len == 0;
106  if (scheme_is_empty) {
107    if (url[begin] == '#') {
108      // |url| is a bare fragement (e.g. "#foo"). This can be resolved against
109      // any base. Fall-through.
110    } else if (!is_base_hierarchical) {
111      // Don't allow relative URLs if the base scheme doesn't support it.
112      return false;
113    }
114
115    *relative_component = MakeRange(begin, url_len);
116    *is_relative = true;
117    return true;
118  }
119
120  // If the scheme isn't valid, then it's relative.
121  int scheme_end = scheme.end();
122  for (int i = scheme.begin; i < scheme_end; i++) {
123    if (!CanonicalSchemeChar(url[i])) {
124      if (!is_base_hierarchical) {
125        // Don't allow relative URLs if the base scheme doesn't support it.
126        return false;
127      }
128      *relative_component = MakeRange(begin, url_len);
129      *is_relative = true;
130      return true;
131    }
132  }
133
134  // If the scheme is not the same, then we can't count it as relative.
135  if (!AreSchemesEqual(base, base_parsed.scheme, url, scheme))
136    return true;
137
138  // When the scheme that they both share is not hierarchical, treat the
139  // incoming scheme as absolute (this way with the base of "data:foo",
140  // "data:bar" will be reported as absolute.
141  if (!is_base_hierarchical)
142    return true;
143
144  int colon_offset = scheme.end();
145
146  // If it's a filesystem URL, the only valid way to make it relative is not to
147  // supply a scheme.  There's no equivalent to e.g. http:index.html.
148  if (CompareSchemeComponent(url, scheme, "filesystem"))
149    return true;
150
151  // ExtractScheme guarantees that the colon immediately follows what it
152  // considers to be the scheme. CountConsecutiveSlashes will handle the
153  // case where the begin offset is the end of the input.
154  int num_slashes = CountConsecutiveSlashes(url, colon_offset + 1, url_len);
155
156  if (num_slashes == 0 || num_slashes == 1) {
157    // No slashes means it's a relative path like "http:foo.html". One slash
158    // is an absolute path. "http:/home/foo.html"
159    *is_relative = true;
160    *relative_component = MakeRange(colon_offset + 1, url_len);
161    return true;
162  }
163
164  // Two or more slashes after the scheme we treat as absolute.
165  return true;
166}
167
168// Copies all characters in the range [begin, end) of |spec| to the output,
169// up until and including the last slash. There should be a slash in the
170// range, if not, nothing will be copied.
171//
172// The input is assumed to be canonical, so we search only for exact slashes
173// and not backslashes as well. We also know that it's ASCII.
174void CopyToLastSlash(const char* spec,
175                     int begin,
176                     int end,
177                     CanonOutput* output) {
178  // Find the last slash.
179  int last_slash = -1;
180  for (int i = end - 1; i >= begin; i--) {
181    if (spec[i] == '/') {
182      last_slash = i;
183      break;
184    }
185  }
186  if (last_slash < 0)
187    return;  // No slash.
188
189  // Copy.
190  for (int i = begin; i <= last_slash; i++)
191    output->push_back(spec[i]);
192}
193
194// Copies a single component from the source to the output. This is used
195// when resolving relative URLs and a given component is unchanged. Since the
196// source should already be canonical, we don't have to do anything special,
197// and the input is ASCII.
198void CopyOneComponent(const char* source,
199                      const Component& source_component,
200                      CanonOutput* output,
201                      Component* output_component) {
202  if (source_component.len < 0) {
203    // This component is not present.
204    *output_component = Component();
205    return;
206  }
207
208  output_component->begin = output->length();
209  int source_end = source_component.end();
210  for (int i = source_component.begin; i < source_end; i++)
211    output->push_back(source[i]);
212  output_component->len = output->length() - output_component->begin;
213}
214
215#ifdef WIN32
216
217// Called on Windows when the base URL is a file URL, this will copy the "C:"
218// to the output, if there is a drive letter and if that drive letter is not
219// being overridden by the relative URL. Otherwise, do nothing.
220//
221// It will return the index of the beginning of the next character in the
222// base to be processed: if there is a "C:", the slash after it, or if
223// there is no drive letter, the slash at the beginning of the path, or
224// the end of the base. This can be used as the starting offset for further
225// path processing.
226template<typename CHAR>
227int CopyBaseDriveSpecIfNecessary(const char* base_url,
228                                 int base_path_begin,
229                                 int base_path_end,
230                                 const CHAR* relative_url,
231                                 int path_start,
232                                 int relative_url_len,
233                                 CanonOutput* output) {
234  if (base_path_begin >= base_path_end)
235    return base_path_begin;  // No path.
236
237  // If the relative begins with a drive spec, don't do anything. The existing
238  // drive spec in the base will be replaced.
239  if (DoesBeginWindowsDriveSpec(relative_url, path_start, relative_url_len)) {
240    return base_path_begin;  // Relative URL path is "C:/foo"
241  }
242
243  // The path should begin with a slash (as all canonical paths do). We check
244  // if it is followed by a drive letter and copy it.
245  if (DoesBeginSlashWindowsDriveSpec(base_url,
246                                     base_path_begin,
247                                     base_path_end)) {
248    // Copy the two-character drive spec to the output. It will now look like
249    // "file:///C:" so the rest of it can be treated like a standard path.
250    output->push_back('/');
251    output->push_back(base_url[base_path_begin + 1]);
252    output->push_back(base_url[base_path_begin + 2]);
253    return base_path_begin + 3;
254  }
255
256  return base_path_begin;
257}
258
259#endif  // WIN32
260
261// A subroutine of DoResolveRelativeURL, this resolves the URL knowning that
262// the input is a relative path or less (qyuery or ref).
263template<typename CHAR>
264bool DoResolveRelativePath(const char* base_url,
265                           const Parsed& base_parsed,
266                           bool base_is_file,
267                           const CHAR* relative_url,
268                           const Component& relative_component,
269                           CharsetConverter* query_converter,
270                           CanonOutput* output,
271                           Parsed* out_parsed) {
272  bool success = true;
273
274  // We know the authority section didn't change, copy it to the output. We
275  // also know we have a path so can copy up to there.
276  Component path, query, ref;
277  ParsePathInternal(relative_url, relative_component, &path, &query, &ref);
278  // Canonical URLs always have a path, so we can use that offset.
279  output->Append(base_url, base_parsed.path.begin);
280
281  if (path.len > 0) {
282    // The path is replaced or modified.
283    int true_path_begin = output->length();
284
285    // For file: URLs on Windows, we don't want to treat the drive letter and
286    // colon as part of the path for relative file resolution when the
287    // incoming URL does not provide a drive spec. We save the true path
288    // beginning so we can fix it up after we are done.
289    int base_path_begin = base_parsed.path.begin;
290#ifdef WIN32
291    if (base_is_file) {
292      base_path_begin = CopyBaseDriveSpecIfNecessary(
293          base_url, base_parsed.path.begin, base_parsed.path.end(),
294          relative_url, relative_component.begin, relative_component.end(),
295          output);
296      // Now the output looks like either "file://" or "file:///C:"
297      // and we can start appending the rest of the path. |base_path_begin|
298      // points to the character in the base that comes next.
299    }
300#endif  // WIN32
301
302    if (IsURLSlash(relative_url[path.begin])) {
303      // Easy case: the path is an absolute path on the server, so we can
304      // just replace everything from the path on with the new versions.
305      // Since the input should be canonical hierarchical URL, we should
306      // always have a path.
307      success &= CanonicalizePath(relative_url, path,
308                                  output, &out_parsed->path);
309    } else {
310      // Relative path, replace the query, and reference. We take the
311      // original path with the file part stripped, and append the new path.
312      // The canonicalizer will take care of resolving ".." and "."
313      int path_begin = output->length();
314      CopyToLastSlash(base_url, base_path_begin, base_parsed.path.end(),
315                      output);
316      success &= CanonicalizePartialPath(relative_url, path, path_begin,
317                                         output);
318      out_parsed->path = MakeRange(path_begin, output->length());
319
320      // Copy the rest of the stuff after the path from the relative path.
321    }
322
323    // Finish with the query and reference part (these can't fail).
324    CanonicalizeQuery(relative_url, query, query_converter,
325                      output, &out_parsed->query);
326    CanonicalizeRef(relative_url, ref, output, &out_parsed->ref);
327
328    // Fix the path beginning to add back the "C:" we may have written above.
329    out_parsed->path = MakeRange(true_path_begin, out_parsed->path.end());
330    return success;
331  }
332
333  // If we get here, the path is unchanged: copy to output.
334  CopyOneComponent(base_url, base_parsed.path, output, &out_parsed->path);
335
336  if (query.is_valid()) {
337    // Just the query specified, replace the query and reference (ignore
338    // failures for refs)
339    CanonicalizeQuery(relative_url, query, query_converter,
340                      output, &out_parsed->query);
341    CanonicalizeRef(relative_url, ref, output, &out_parsed->ref);
342    return success;
343  }
344
345  // If we get here, the query is unchanged: copy to output. Note that the
346  // range of the query parameter doesn't include the question mark, so we
347  // have to add it manually if there is a component.
348  if (base_parsed.query.is_valid())
349    output->push_back('?');
350  CopyOneComponent(base_url, base_parsed.query, output, &out_parsed->query);
351
352  if (ref.is_valid()) {
353    // Just the reference specified: replace it (ignoring failures).
354    CanonicalizeRef(relative_url, ref, output, &out_parsed->ref);
355    return success;
356  }
357
358  // We should always have something to do in this function, the caller checks
359  // that some component is being replaced.
360  DCHECK(false) << "Not reached";
361  return success;
362}
363
364// Resolves a relative URL that contains a host. Typically, these will
365// be of the form "//www.google.com/foo/bar?baz#ref" and the only thing which
366// should be kept from the original URL is the scheme.
367template<typename CHAR>
368bool DoResolveRelativeHost(const char* base_url,
369                           const Parsed& base_parsed,
370                           const CHAR* relative_url,
371                           const Component& relative_component,
372                           CharsetConverter* query_converter,
373                           CanonOutput* output,
374                           Parsed* out_parsed) {
375  // Parse the relative URL, just like we would for anything following a
376  // scheme.
377  Parsed relative_parsed;  // Everything but the scheme is valid.
378  ParseAfterScheme(relative_url, relative_component.end(),
379                   relative_component.begin, &relative_parsed);
380
381  // Now we can just use the replacement function to replace all the necessary
382  // parts of the old URL with the new one.
383  Replacements<CHAR> replacements;
384  replacements.SetUsername(relative_url, relative_parsed.username);
385  replacements.SetPassword(relative_url, relative_parsed.password);
386  replacements.SetHost(relative_url, relative_parsed.host);
387  replacements.SetPort(relative_url, relative_parsed.port);
388  replacements.SetPath(relative_url, relative_parsed.path);
389  replacements.SetQuery(relative_url, relative_parsed.query);
390  replacements.SetRef(relative_url, relative_parsed.ref);
391
392  return ReplaceStandardURL(base_url, base_parsed, replacements,
393                            query_converter, output, out_parsed);
394}
395
396// Resolves a relative URL that happens to be an absolute file path.  Examples
397// include: "//hostname/path", "/c:/foo", and "//hostname/c:/foo".
398template<typename CHAR>
399bool DoResolveAbsoluteFile(const CHAR* relative_url,
400                           const Component& relative_component,
401                           CharsetConverter* query_converter,
402                           CanonOutput* output,
403                           Parsed* out_parsed) {
404  // Parse the file URL. The file URl parsing function uses the same logic
405  // as we do for determining if the file is absolute, in which case it will
406  // not bother to look for a scheme.
407  Parsed relative_parsed;
408  ParseFileURL(&relative_url[relative_component.begin], relative_component.len,
409               &relative_parsed);
410
411  return CanonicalizeFileURL(&relative_url[relative_component.begin],
412                             relative_component.len, relative_parsed,
413                             query_converter, output, out_parsed);
414}
415
416// TODO(brettw) treat two slashes as root like Mozilla for FTP?
417template<typename CHAR>
418bool DoResolveRelativeURL(const char* base_url,
419                          const Parsed& base_parsed,
420                          bool base_is_file,
421                          const CHAR* relative_url,
422                          const Component& relative_component,
423                          CharsetConverter* query_converter,
424                          CanonOutput* output,
425                          Parsed* out_parsed) {
426  // Starting point for our output parsed. We'll fix what we change.
427  *out_parsed = base_parsed;
428
429  // Sanity check: the input should have a host or we'll break badly below.
430  // We can only resolve relative URLs with base URLs that have hosts and
431  // paths (even the default path of "/" is OK).
432  //
433  // We allow hosts with no length so we can handle file URLs, for example.
434  if (base_parsed.path.len <= 0) {
435    // On error, return the input (resolving a relative URL on a non-relative
436    // base = the base).
437    int base_len = base_parsed.Length();
438    for (int i = 0; i < base_len; i++)
439      output->push_back(base_url[i]);
440    return false;
441  }
442
443  if (relative_component.len <= 0) {
444    // Empty relative URL, leave unchanged, only removing the ref component.
445    int base_len = base_parsed.Length();
446    base_len -= base_parsed.ref.len + 1;
447    out_parsed->ref.reset();
448    output->Append(base_url, base_len);
449    return true;
450  }
451
452  int num_slashes = CountConsecutiveSlashes(
453      relative_url, relative_component.begin, relative_component.end());
454
455#ifdef WIN32
456  // On Windows, two slashes for a file path (regardless of which direction
457  // they are) means that it's UNC. Two backslashes on any base scheme mean
458  // that it's an absolute UNC path (we use the base_is_file flag to control
459  // how strict the UNC finder is).
460  //
461  // We also allow Windows absolute drive specs on any scheme (for example
462  // "c:\foo") like IE does. There must be no preceeding slashes in this
463  // case (we reject anything like "/c:/foo") because that should be treated
464  // as a path. For file URLs, we allow any number of slashes since that would
465  // be setting the path.
466  //
467  // This assumes the absolute path resolver handles absolute URLs like this
468  // properly. DoCanonicalize does this.
469  int after_slashes = relative_component.begin + num_slashes;
470  if (DoesBeginUNCPath(relative_url, relative_component.begin,
471                       relative_component.end(), !base_is_file) ||
472      ((num_slashes == 0 || base_is_file) &&
473       DoesBeginWindowsDriveSpec(
474           relative_url, after_slashes, relative_component.end()))) {
475    return DoResolveAbsoluteFile(relative_url, relative_component,
476                                 query_converter, output, out_parsed);
477  }
478#else
479  // Other platforms need explicit handling for file: URLs with multiple
480  // slashes because the generic scheme parsing always extracts a host, but a
481  // file: URL only has a host if it has exactly 2 slashes. Even if it does
482  // have a host, we want to use the special host detection logic for file
483  // URLs provided by DoResolveAbsoluteFile(), as opposed to the generic host
484  // detection logic, for consistency with parsing file URLs from scratch.
485  // This also handles the special case where the URL is only slashes,
486  // since that doesn't have a host part either.
487  if (base_is_file &&
488      (num_slashes >= 2 || num_slashes == relative_component.len)) {
489    return DoResolveAbsoluteFile(relative_url, relative_component,
490                                 query_converter, output, out_parsed);
491  }
492#endif
493
494  // Any other double-slashes mean that this is relative to the scheme.
495  if (num_slashes >= 2) {
496    return DoResolveRelativeHost(base_url, base_parsed,
497                                 relative_url, relative_component,
498                                 query_converter, output, out_parsed);
499  }
500
501  // When we get here, we know that the relative URL is on the same host.
502  return DoResolveRelativePath(base_url, base_parsed, base_is_file,
503                               relative_url, relative_component,
504                               query_converter, output, out_parsed);
505}
506
507}  // namespace
508
509bool IsRelativeURL(const char* base,
510                   const Parsed& base_parsed,
511                   const char* fragment,
512                   int fragment_len,
513                   bool is_base_hierarchical,
514                   bool* is_relative,
515                   Component* relative_component) {
516  return DoIsRelativeURL<char>(
517      base, base_parsed, fragment, fragment_len, is_base_hierarchical,
518      is_relative, relative_component);
519}
520
521bool IsRelativeURL(const char* base,
522                   const Parsed& base_parsed,
523                   const base::char16* fragment,
524                   int fragment_len,
525                   bool is_base_hierarchical,
526                   bool* is_relative,
527                   Component* relative_component) {
528  return DoIsRelativeURL<base::char16>(
529      base, base_parsed, fragment, fragment_len, is_base_hierarchical,
530      is_relative, relative_component);
531}
532
533bool ResolveRelativeURL(const char* base_url,
534                        const Parsed& base_parsed,
535                        bool base_is_file,
536                        const char* relative_url,
537                        const Component& relative_component,
538                        CharsetConverter* query_converter,
539                        CanonOutput* output,
540                        Parsed* out_parsed) {
541  return DoResolveRelativeURL<char>(
542      base_url, base_parsed, base_is_file, relative_url,
543      relative_component, query_converter, output, out_parsed);
544}
545
546bool ResolveRelativeURL(const char* base_url,
547                        const Parsed& base_parsed,
548                        bool base_is_file,
549                        const base::char16* relative_url,
550                        const Component& relative_component,
551                        CharsetConverter* query_converter,
552                        CanonOutput* output,
553                        Parsed* out_parsed) {
554  return DoResolveRelativeURL<base::char16>(
555      base_url, base_parsed, base_is_file, relative_url,
556      relative_component, query_converter, output, out_parsed);
557}
558
559}  // namespace url
560