xref: /aosp_15_r20/external/cronet/url/url_canon_internal_file.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1*6777b538SAndroid Build Coastguard Worker // Copyright 2013 The Chromium Authors
2*6777b538SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*6777b538SAndroid Build Coastguard Worker // found in the LICENSE file.
4*6777b538SAndroid Build Coastguard Worker 
5*6777b538SAndroid Build Coastguard Worker #ifndef URL_URL_CANON_INTERNAL_FILE_H_
6*6777b538SAndroid Build Coastguard Worker #define URL_URL_CANON_INTERNAL_FILE_H_
7*6777b538SAndroid Build Coastguard Worker 
8*6777b538SAndroid Build Coastguard Worker // As with url_canon_internal.h, this file is intended to be included in
9*6777b538SAndroid Build Coastguard Worker // another C++ file where the template types are defined. This allows the
10*6777b538SAndroid Build Coastguard Worker // programmer to use this to use these functions for their own strings
11*6777b538SAndroid Build Coastguard Worker // types, without bloating the code by having inline templates used in
12*6777b538SAndroid Build Coastguard Worker // every call site.
13*6777b538SAndroid Build Coastguard Worker //
14*6777b538SAndroid Build Coastguard Worker // *** This file must be included after url_canon_internal as we depend on some
15*6777b538SAndroid Build Coastguard Worker // functions in it. ***
16*6777b538SAndroid Build Coastguard Worker 
17*6777b538SAndroid Build Coastguard Worker #include "base/strings/string_util.h"
18*6777b538SAndroid Build Coastguard Worker #include "url/url_file.h"
19*6777b538SAndroid Build Coastguard Worker #include "url/url_parse_internal.h"
20*6777b538SAndroid Build Coastguard Worker 
21*6777b538SAndroid Build Coastguard Worker namespace url {
22*6777b538SAndroid Build Coastguard Worker 
23*6777b538SAndroid Build Coastguard Worker // Given a pointer into the spec, this copies and canonicalizes the drive
24*6777b538SAndroid Build Coastguard Worker // letter and colon to the output, if one is found. If there is not a drive
25*6777b538SAndroid Build Coastguard Worker // spec, it won't do anything. The index of the next character in the input
26*6777b538SAndroid Build Coastguard Worker // spec is returned (after the colon when a drive spec is found, the begin
27*6777b538SAndroid Build Coastguard Worker // offset if one is not).
28*6777b538SAndroid Build Coastguard Worker template<typename CHAR>
FileDoDriveSpec(const CHAR * spec,int begin,int end,CanonOutput * output)29*6777b538SAndroid Build Coastguard Worker static int FileDoDriveSpec(const CHAR* spec, int begin, int end,
30*6777b538SAndroid Build Coastguard Worker                            CanonOutput* output) {
31*6777b538SAndroid Build Coastguard Worker   // The path could be one of several things: /foo/bar, c:/foo/bar, /c:/foo,
32*6777b538SAndroid Build Coastguard Worker   // (with backslashes instead of slashes as well).
33*6777b538SAndroid Build Coastguard Worker   int num_slashes = CountConsecutiveSlashes(spec, begin, end);
34*6777b538SAndroid Build Coastguard Worker   int after_slashes = begin + num_slashes;
35*6777b538SAndroid Build Coastguard Worker 
36*6777b538SAndroid Build Coastguard Worker   if (!DoesBeginWindowsDriveSpec(spec, after_slashes, end))
37*6777b538SAndroid Build Coastguard Worker     return begin;  // Haven't consumed any characters
38*6777b538SAndroid Build Coastguard Worker 
39*6777b538SAndroid Build Coastguard Worker   // DoesBeginWindowsDriveSpec will ensure that the drive letter is valid
40*6777b538SAndroid Build Coastguard Worker   // and that it is followed by a colon/pipe.
41*6777b538SAndroid Build Coastguard Worker 
42*6777b538SAndroid Build Coastguard Worker   // Normalize Windows drive letters to uppercase
43*6777b538SAndroid Build Coastguard Worker   if (base::IsAsciiLower(spec[after_slashes]))
44*6777b538SAndroid Build Coastguard Worker     output->push_back(spec[after_slashes] - 'a' + 'A');
45*6777b538SAndroid Build Coastguard Worker   else
46*6777b538SAndroid Build Coastguard Worker     output->push_back(static_cast<char>(spec[after_slashes]));
47*6777b538SAndroid Build Coastguard Worker 
48*6777b538SAndroid Build Coastguard Worker   // Normalize the character following it to a colon rather than pipe.
49*6777b538SAndroid Build Coastguard Worker   output->push_back(':');
50*6777b538SAndroid Build Coastguard Worker   output->push_back('/');
51*6777b538SAndroid Build Coastguard Worker   return after_slashes + 2;
52*6777b538SAndroid Build Coastguard Worker }
53*6777b538SAndroid Build Coastguard Worker 
54*6777b538SAndroid Build Coastguard Worker // FileDoDriveSpec will have already added the first backslash, so we need to
55*6777b538SAndroid Build Coastguard Worker // write everything following the slashes using the path canonicalizer.
56*6777b538SAndroid Build Coastguard Worker template<typename CHAR, typename UCHAR>
FileDoPath(const CHAR * spec,int begin,int end,CanonOutput * output)57*6777b538SAndroid Build Coastguard Worker static void FileDoPath(const CHAR* spec, int begin, int end,
58*6777b538SAndroid Build Coastguard Worker                        CanonOutput* output) {
59*6777b538SAndroid Build Coastguard Worker   // Normalize the number of slashes after the drive letter. The path
60*6777b538SAndroid Build Coastguard Worker   // canonicalizer expects the input to begin in a slash already so
61*6777b538SAndroid Build Coastguard Worker   // doesn't check. We want to handle no-slashes
62*6777b538SAndroid Build Coastguard Worker   int num_slashes = CountConsecutiveSlashes(spec, begin, end);
63*6777b538SAndroid Build Coastguard Worker   int after_slashes = begin + num_slashes;
64*6777b538SAndroid Build Coastguard Worker 
65*6777b538SAndroid Build Coastguard Worker   // Now use the regular path canonicalizer to canonicalize the rest of the
66*6777b538SAndroid Build Coastguard Worker   // path. We supply it with the path following the slashes. It won't prepend
67*6777b538SAndroid Build Coastguard Worker   // a slash because it assumes any nonempty path already starts with one.
68*6777b538SAndroid Build Coastguard Worker   // We explicitly filter out calls with no path here to prevent that case.
69*6777b538SAndroid Build Coastguard Worker   ParsedComponent sub_path(after_slashes, end - after_slashes);
70*6777b538SAndroid Build Coastguard Worker   if (sub_path.len > 0) {
71*6777b538SAndroid Build Coastguard Worker     // Give it a fake output component to write into. DoCanonicalizeFile will
72*6777b538SAndroid Build Coastguard Worker     // compute the full path component.
73*6777b538SAndroid Build Coastguard Worker     ParsedComponent fake_output_path;
74*6777b538SAndroid Build Coastguard Worker     URLCanonInternal<CHAR, UCHAR>::DoPath(
75*6777b538SAndroid Build Coastguard Worker         spec, sub_path, output, &fake_output_path);
76*6777b538SAndroid Build Coastguard Worker   }
77*6777b538SAndroid Build Coastguard Worker }
78*6777b538SAndroid Build Coastguard Worker 
79*6777b538SAndroid Build Coastguard Worker template<typename CHAR, typename UCHAR>
DoCanonicalizeFileURL(const URLComponentSource<CHAR> & source,const ParsedURL & parsed,CanonOutput * output,ParsedURL * new_parsed)80*6777b538SAndroid Build Coastguard Worker static bool DoCanonicalizeFileURL(const URLComponentSource<CHAR>& source,
81*6777b538SAndroid Build Coastguard Worker                                   const ParsedURL& parsed,
82*6777b538SAndroid Build Coastguard Worker                                   CanonOutput* output,
83*6777b538SAndroid Build Coastguard Worker                                   ParsedURL* new_parsed) {
84*6777b538SAndroid Build Coastguard Worker   // Things we don't set in file: URLs.
85*6777b538SAndroid Build Coastguard Worker   new_parsed->username = ParsedComponent(0, -1);
86*6777b538SAndroid Build Coastguard Worker   new_parsed->password = ParsedComponent(0, -1);
87*6777b538SAndroid Build Coastguard Worker   new_parsed->port = ParsedComponent(0, -1);
88*6777b538SAndroid Build Coastguard Worker 
89*6777b538SAndroid Build Coastguard Worker   // Scheme (known, so we don't bother running it through the more
90*6777b538SAndroid Build Coastguard Worker   // complicated scheme canonicalizer).
91*6777b538SAndroid Build Coastguard Worker   new_parsed->scheme.begin = output->length();
92*6777b538SAndroid Build Coastguard Worker   output->push_back('f');
93*6777b538SAndroid Build Coastguard Worker   output->push_back('i');
94*6777b538SAndroid Build Coastguard Worker   output->push_back('l');
95*6777b538SAndroid Build Coastguard Worker   output->push_back('e');
96*6777b538SAndroid Build Coastguard Worker   new_parsed->scheme.len = output->length() - new_parsed->scheme.begin;
97*6777b538SAndroid Build Coastguard Worker   output->push_back(':');
98*6777b538SAndroid Build Coastguard Worker 
99*6777b538SAndroid Build Coastguard Worker   // Write the separator for the host.
100*6777b538SAndroid Build Coastguard Worker   output->push_back('/');
101*6777b538SAndroid Build Coastguard Worker   output->push_back('/');
102*6777b538SAndroid Build Coastguard Worker 
103*6777b538SAndroid Build Coastguard Worker   // Append the host. For many file URLs, this will be empty. For UNC, this
104*6777b538SAndroid Build Coastguard Worker   // will be present.
105*6777b538SAndroid Build Coastguard Worker   // TODO(brettw) This doesn't do any checking for host name validity. We
106*6777b538SAndroid Build Coastguard Worker   // should probably handle validity checking of UNC hosts differently than
107*6777b538SAndroid Build Coastguard Worker   // for regular IP hosts.
108*6777b538SAndroid Build Coastguard Worker   bool success = URLCanonInternal<CHAR, UCHAR>::DoHost(
109*6777b538SAndroid Build Coastguard Worker       source.host, parsed.host, output, &new_parsed->host);
110*6777b538SAndroid Build Coastguard Worker 
111*6777b538SAndroid Build Coastguard Worker   // Write a separator for the start of the path. We'll ignore any slashes
112*6777b538SAndroid Build Coastguard Worker   // already at the beginning of the path.
113*6777b538SAndroid Build Coastguard Worker   new_parsed->path.begin = output->length();
114*6777b538SAndroid Build Coastguard Worker   output->push_back('/');
115*6777b538SAndroid Build Coastguard Worker 
116*6777b538SAndroid Build Coastguard Worker   // Copy and normalize the "c:" at the beginning, if present.
117*6777b538SAndroid Build Coastguard Worker   int after_drive = FileDoDriveSpec(source.path, parsed.path.begin,
118*6777b538SAndroid Build Coastguard Worker                                     parsed.path.end(), output);
119*6777b538SAndroid Build Coastguard Worker 
120*6777b538SAndroid Build Coastguard Worker   // Copy the rest of the path.
121*6777b538SAndroid Build Coastguard Worker   FileDoPath<CHAR, UCHAR>(source.path, after_drive, parsed.path.end(), output);
122*6777b538SAndroid Build Coastguard Worker   new_parsed->path.len = output->length() - new_parsed->path.begin;
123*6777b538SAndroid Build Coastguard Worker 
124*6777b538SAndroid Build Coastguard Worker   // For things following the path, we can use the standard canonicalizers.
125*6777b538SAndroid Build Coastguard Worker   success &= URLCanonInternal<CHAR, UCHAR>::DoQuery(
126*6777b538SAndroid Build Coastguard Worker       source.query, parsed.query, output, &new_parsed->query);
127*6777b538SAndroid Build Coastguard Worker   success &= URLCanonInternal<CHAR, UCHAR>::DoRef(
128*6777b538SAndroid Build Coastguard Worker       source.ref, parsed.ref, output, &new_parsed->ref);
129*6777b538SAndroid Build Coastguard Worker 
130*6777b538SAndroid Build Coastguard Worker   return success;
131*6777b538SAndroid Build Coastguard Worker }
132*6777b538SAndroid Build Coastguard Worker 
133*6777b538SAndroid Build Coastguard Worker }  // namespace url
134*6777b538SAndroid Build Coastguard Worker 
135*6777b538SAndroid Build Coastguard Worker #endif  // URL_URL_CANON_INTERNAL_FILE_H_
136