xref: /aosp_15_r20/external/cronet/url/url_canon_filesystemurl.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2013 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 // Functions for canonicalizing "filesystem:file:" URLs.
6 
7 #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"
13 
14 namespace url {
15 
16 namespace {
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.
20 template <typename CHAR>
DoCanonicalizeFileSystemURL(const CHAR * spec,const URLComponentSource<CHAR> & source,const Parsed & parsed,CharsetConverter * charset_converter,CanonOutput * output,Parsed * new_parsed)21 bool DoCanonicalizeFileSystemURL(const CHAR* spec,
22                                  const URLComponentSource<CHAR>& source,
23                                  const Parsed& parsed,
24                                  CharsetConverter* charset_converter,
25                                  CanonOutput* output,
26                                  Parsed* new_parsed) {
27   // filesystem only uses {scheme, path, query, ref} -- clear the rest.
28   new_parsed->username.reset();
29   new_parsed->password.reset();
30   new_parsed->host.reset();
31   new_parsed->port.reset();
32 
33   const Parsed* inner_parsed = parsed.inner_parsed();
34   Parsed new_inner_parsed;
35 
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:");
40   new_parsed->scheme.len = 10;
41 
42   if (!inner_parsed || !inner_parsed->scheme.is_valid())
43     return false;
44 
45   bool success = true;
46   SchemeType inner_scheme_type = SCHEME_WITH_HOST_PORT_AND_USER_INFORMATION;
47   if (CompareSchemeComponent(spec, inner_parsed->scheme, url::kFileScheme)) {
48     new_inner_parsed.scheme.begin = output->length();
49     output->Append("file://");
50     new_inner_parsed.scheme.len = 4;
51     success &= CanonicalizePath(spec, inner_parsed->path, output,
52                                 &new_inner_parsed.path);
53   } else if (GetStandardSchemeType(spec, inner_parsed->scheme,
54                                    &inner_scheme_type)) {
55     if (inner_scheme_type == SCHEME_WITH_HOST_PORT_AND_USER_INFORMATION) {
56       // Strip out the user information from the inner URL, if any.
57       inner_scheme_type = SCHEME_WITH_HOST_AND_PORT;
58     }
59     success =
60         CanonicalizeStandardURL(spec, *inner_parsed, inner_scheme_type,
61                                 charset_converter, output, &new_inner_parsed);
62   } else {
63     // TODO(ericu): The URL is wrong, but should we try to output more of what
64     // we were given?  Echoing back filesystem:mailto etc. doesn't seem all that
65     // useful.
66     return false;
67   }
68   // The filesystem type must be more than just a leading slash for validity.
69   success &= new_inner_parsed.path.len > 1;
70 
71   success &= CanonicalizePath(source.path, parsed.path, output,
72                               &new_parsed->path);
73 
74   // Ignore failures for query/ref since the URL can probably still be loaded.
75   CanonicalizeQuery(source.query, parsed.query, charset_converter,
76                     output, &new_parsed->query);
77   CanonicalizeRef(source.ref, parsed.ref, output, &new_parsed->ref);
78   if (success)
79     new_parsed->set_inner_parsed(new_inner_parsed);
80 
81   return success;
82 }
83 
84 }  // namespace
85 
CanonicalizeFileSystemURL(const char * spec,const Parsed & parsed,CharsetConverter * charset_converter,CanonOutput * output,Parsed * new_parsed)86 bool CanonicalizeFileSystemURL(const char* spec,
87                                const Parsed& parsed,
88                                CharsetConverter* charset_converter,
89                                CanonOutput* output,
90                                Parsed* new_parsed) {
91   return DoCanonicalizeFileSystemURL(spec, URLComponentSource(spec), parsed,
92                                      charset_converter, output, new_parsed);
93 }
94 
CanonicalizeFileSystemURL(const char16_t * spec,const Parsed & parsed,CharsetConverter * charset_converter,CanonOutput * output,Parsed * new_parsed)95 bool CanonicalizeFileSystemURL(const char16_t* spec,
96                                const Parsed& parsed,
97                                CharsetConverter* charset_converter,
98                                CanonOutput* output,
99                                Parsed* new_parsed) {
100   return DoCanonicalizeFileSystemURL(spec, URLComponentSource(spec), parsed,
101                                      charset_converter, output, new_parsed);
102 }
103 
ReplaceFileSystemURL(const char * base,const Parsed & base_parsed,const Replacements<char> & replacements,CharsetConverter * charset_converter,CanonOutput * output,Parsed * new_parsed)104 bool ReplaceFileSystemURL(const char* base,
105                           const Parsed& base_parsed,
106                           const Replacements<char>& replacements,
107                           CharsetConverter* charset_converter,
108                           CanonOutput* output,
109                           Parsed* new_parsed) {
110   URLComponentSource<char> source(base);
111   Parsed parsed(base_parsed);
112   SetupOverrideComponents(base, replacements, &source, &parsed);
113   return DoCanonicalizeFileSystemURL(base, source, parsed, charset_converter,
114                                      output, new_parsed);
115 }
116 
ReplaceFileSystemURL(const char * base,const Parsed & base_parsed,const Replacements<char16_t> & replacements,CharsetConverter * charset_converter,CanonOutput * output,Parsed * new_parsed)117 bool ReplaceFileSystemURL(const char* base,
118                           const Parsed& base_parsed,
119                           const Replacements<char16_t>& replacements,
120                           CharsetConverter* charset_converter,
121                           CanonOutput* output,
122                           Parsed* new_parsed) {
123   RawCanonOutput<1024> utf8;
124   URLComponentSource<char> source(base);
125   Parsed parsed(base_parsed);
126   SetupUTF16OverrideComponents(base, replacements, &utf8, &source, &parsed);
127   return DoCanonicalizeFileSystemURL(base, source, parsed, charset_converter,
128                                      output, new_parsed);
129 }
130 
131 }  // namespace url
132