blob: 18e90552949426842a7c3397bb882f84d9fe1bb1 [file] [log] [blame]
[email protected]51bcc5d2013-04-24 01:41:371// 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.
[email protected]e7bba5f82013-04-10 20:10:524
5// Functions for canonicalizing "filesystem:file:" URLs.
6
[email protected]318076b2013-04-18 21:19:457#include "url/url_canon.h"
8#include "url/url_canon_internal.h"
9#include "url/url_file.h"
10#include "url/url_parse_internal.h"
11#include "url/url_util.h"
12#include "url/url_util_internal.h"
[email protected]e7bba5f82013-04-10 20:10:5213
[email protected]0318f922014-04-22 00:09:2314namespace url {
[email protected]e7bba5f82013-04-10 20:10:5215
16namespace {
17
18// We use the URLComponentSource for the outer URL, as it can have replacements,
19// whereas the inner_url can't, so it uses spec.
20template<typename CHAR, typename UCHAR>
21bool DoCanonicalizeFileSystemURL(const CHAR* spec,
22 const URLComponentSource<CHAR>& source,
[email protected]0318f922014-04-22 00:09:2323 const Parsed& parsed,
[email protected]e7bba5f82013-04-10 20:10:5224 CharsetConverter* charset_converter,
25 CanonOutput* output,
[email protected]0318f922014-04-22 00:09:2326 Parsed* new_parsed) {
[email protected]e7bba5f82013-04-10 20:10:5227 // filesystem only uses {scheme, path, query, ref} -- clear the rest.
[email protected]e7ee68f72014-04-08 23:36:0628 new_parsed->username.reset();
29 new_parsed->password.reset();
30 new_parsed->host.reset();
31 new_parsed->port.reset();
[email protected]e7bba5f82013-04-10 20:10:5232
[email protected]0318f922014-04-22 00:09:2333 const Parsed* inner_parsed = parsed.inner_parsed();
34 Parsed new_inner_parsed;
[email protected]e7bba5f82013-04-10 20:10:5235
36 // Scheme (known, so we don't bother running it through the more
37 // complicated scheme canonicalizer).
38 new_parsed->scheme.begin = output->length();
39 output->Append("filesystem:", 11);
40 new_parsed->scheme.len = 10;
41
42 if (!parsed.inner_parsed() || !parsed.inner_parsed()->scheme.is_valid())
43 return false;
44
45 bool success = true;
[email protected]cca6f392014-05-28 21:32:2646 if (CompareSchemeComponent(spec, inner_parsed->scheme, url::kFileScheme)) {
[email protected]e7bba5f82013-04-10 20:10:5247 new_inner_parsed.scheme.begin = output->length();
48 output->Append("file://", 7);
49 new_inner_parsed.scheme.len = 4;
50 success &= CanonicalizePath(spec, inner_parsed->path, output,
51 &new_inner_parsed.path);
[email protected]0318f922014-04-22 00:09:2352 } else if (IsStandard(spec, inner_parsed->scheme)) {
53 success = CanonicalizeStandardURL(spec, parsed.inner_parsed()->Length(),
54 *parsed.inner_parsed(), charset_converter,
55 output, &new_inner_parsed);
[email protected]e7bba5f82013-04-10 20:10:5256 } else {
57 // TODO(ericu): The URL is wrong, but should we try to output more of what
58 // we were given? Echoing back filesystem:mailto etc. doesn't seem all that
59 // useful.
60 return false;
61 }
62 // The filesystem type must be more than just a leading slash for validity.
63 success &= parsed.inner_parsed()->path.len > 1;
64
65 success &= CanonicalizePath(source.path, parsed.path, output,
66 &new_parsed->path);
67
68 // Ignore failures for query/ref since the URL can probably still be loaded.
69 CanonicalizeQuery(source.query, parsed.query, charset_converter,
70 output, &new_parsed->query);
71 CanonicalizeRef(source.ref, parsed.ref, output, &new_parsed->ref);
72 if (success)
73 new_parsed->set_inner_parsed(new_inner_parsed);
74
75 return success;
76}
77
78} // namespace
79
80bool CanonicalizeFileSystemURL(const char* spec,
81 int spec_len,
[email protected]0318f922014-04-22 00:09:2382 const Parsed& parsed,
[email protected]e7bba5f82013-04-10 20:10:5283 CharsetConverter* charset_converter,
84 CanonOutput* output,
[email protected]0318f922014-04-22 00:09:2385 Parsed* new_parsed) {
[email protected]e7bba5f82013-04-10 20:10:5286 return DoCanonicalizeFileSystemURL<char, unsigned char>(
87 spec, URLComponentSource<char>(spec), parsed, charset_converter, output,
88 new_parsed);
89}
90
[email protected]3774f832013-06-11 21:21:5791bool CanonicalizeFileSystemURL(const base::char16* spec,
[email protected]e7bba5f82013-04-10 20:10:5292 int spec_len,
[email protected]0318f922014-04-22 00:09:2393 const Parsed& parsed,
[email protected]e7bba5f82013-04-10 20:10:5294 CharsetConverter* charset_converter,
95 CanonOutput* output,
[email protected]0318f922014-04-22 00:09:2396 Parsed* new_parsed) {
[email protected]3774f832013-06-11 21:21:5797 return DoCanonicalizeFileSystemURL<base::char16, base::char16>(
98 spec, URLComponentSource<base::char16>(spec), parsed, charset_converter,
99 output, new_parsed);
[email protected]e7bba5f82013-04-10 20:10:52100}
101
102bool ReplaceFileSystemURL(const char* base,
[email protected]0318f922014-04-22 00:09:23103 const Parsed& base_parsed,
[email protected]e7bba5f82013-04-10 20:10:52104 const Replacements<char>& replacements,
105 CharsetConverter* charset_converter,
106 CanonOutput* output,
[email protected]0318f922014-04-22 00:09:23107 Parsed* new_parsed) {
[email protected]e7bba5f82013-04-10 20:10:52108 URLComponentSource<char> source(base);
[email protected]0318f922014-04-22 00:09:23109 Parsed parsed(base_parsed);
[email protected]e7bba5f82013-04-10 20:10:52110 SetupOverrideComponents(base, replacements, &source, &parsed);
111 return DoCanonicalizeFileSystemURL<char, unsigned char>(
112 base, source, parsed, charset_converter, output, new_parsed);
113}
114
115bool ReplaceFileSystemURL(const char* base,
[email protected]0318f922014-04-22 00:09:23116 const Parsed& base_parsed,
[email protected]3774f832013-06-11 21:21:57117 const Replacements<base::char16>& replacements,
[email protected]e7bba5f82013-04-10 20:10:52118 CharsetConverter* charset_converter,
119 CanonOutput* output,
[email protected]0318f922014-04-22 00:09:23120 Parsed* new_parsed) {
[email protected]e7bba5f82013-04-10 20:10:52121 RawCanonOutput<1024> utf8;
122 URLComponentSource<char> source(base);
[email protected]0318f922014-04-22 00:09:23123 Parsed parsed(base_parsed);
[email protected]e7bba5f82013-04-10 20:10:52124 SetupUTF16OverrideComponents(base, replacements, &utf8, &source, &parsed);
125 return DoCanonicalizeFileSystemURL<char, unsigned char>(
126 base, source, parsed, charset_converter, output, new_parsed);
127}
128
[email protected]0318f922014-04-22 00:09:23129} // namespace url