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