xref: /aosp_15_r20/external/openscreen/third_party/mozilla/url_parse.h (revision 3f982cf4871df8771c9d4abe6e9a6f8d829b2736)
1*3f982cf4SFabien Sanglard // Copyright 2013 The Chromium Authors. All rights reserved.
2*3f982cf4SFabien Sanglard // Use of this source code is governed by a BSD-style license that can be
3*3f982cf4SFabien Sanglard // found in the LICENSE file.
4*3f982cf4SFabien Sanglard 
5*3f982cf4SFabien Sanglard #ifndef THIRD_PARTY_MOZILLA_URL_PARSE_H_
6*3f982cf4SFabien Sanglard #define THIRD_PARTY_MOZILLA_URL_PARSE_H_
7*3f982cf4SFabien Sanglard 
8*3f982cf4SFabien Sanglard namespace openscreen {
9*3f982cf4SFabien Sanglard 
10*3f982cf4SFabien Sanglard // Component ------------------------------------------------------------------
11*3f982cf4SFabien Sanglard 
12*3f982cf4SFabien Sanglard // Represents a substring for URL parsing.
13*3f982cf4SFabien Sanglard struct Component {
ComponentComponent14*3f982cf4SFabien Sanglard   Component() : begin(0), len(-1) {}
15*3f982cf4SFabien Sanglard 
16*3f982cf4SFabien Sanglard   // Normal constructor: takes an offset and a length.
ComponentComponent17*3f982cf4SFabien Sanglard   Component(int b, int l) : begin(b), len(l) {}
18*3f982cf4SFabien Sanglard 
endComponent19*3f982cf4SFabien Sanglard   int end() const { return begin + len; }
20*3f982cf4SFabien Sanglard 
21*3f982cf4SFabien Sanglard   // Returns true if this component is valid, meaning the length is given. Even
22*3f982cf4SFabien Sanglard   // valid components may be empty to record the fact that they exist.
is_validComponent23*3f982cf4SFabien Sanglard   bool is_valid() const { return (len != -1); }
24*3f982cf4SFabien Sanglard 
25*3f982cf4SFabien Sanglard   // Returns true if the given component is specified on false, the component
26*3f982cf4SFabien Sanglard   // is either empty or invalid.
is_nonemptyComponent27*3f982cf4SFabien Sanglard   bool is_nonempty() const { return (len > 0); }
28*3f982cf4SFabien Sanglard 
resetComponent29*3f982cf4SFabien Sanglard   void reset() {
30*3f982cf4SFabien Sanglard     begin = 0;
31*3f982cf4SFabien Sanglard     len = -1;
32*3f982cf4SFabien Sanglard   }
33*3f982cf4SFabien Sanglard 
34*3f982cf4SFabien Sanglard   bool operator==(const Component& other) const {
35*3f982cf4SFabien Sanglard     return begin == other.begin && len == other.len;
36*3f982cf4SFabien Sanglard   }
37*3f982cf4SFabien Sanglard 
38*3f982cf4SFabien Sanglard   int begin;  // Byte offset in the string of this component.
39*3f982cf4SFabien Sanglard   int len;    // Will be -1 if the component is unspecified.
40*3f982cf4SFabien Sanglard };
41*3f982cf4SFabien Sanglard 
42*3f982cf4SFabien Sanglard // Helper that returns a component created with the given begin and ending
43*3f982cf4SFabien Sanglard // points. The ending point is non-inclusive.
MakeRange(int begin,int end)44*3f982cf4SFabien Sanglard inline Component MakeRange(int begin, int end) {
45*3f982cf4SFabien Sanglard   return Component(begin, end - begin);
46*3f982cf4SFabien Sanglard }
47*3f982cf4SFabien Sanglard 
48*3f982cf4SFabien Sanglard // Parsed ---------------------------------------------------------------------
49*3f982cf4SFabien Sanglard 
50*3f982cf4SFabien Sanglard // A structure that holds the identified parts of an input URL. This structure
51*3f982cf4SFabien Sanglard // does NOT store the URL itself. The caller will have to store the URL text
52*3f982cf4SFabien Sanglard // and its corresponding Parsed structure separately.
53*3f982cf4SFabien Sanglard //
54*3f982cf4SFabien Sanglard // Typical usage would be:
55*3f982cf4SFabien Sanglard //
56*3f982cf4SFabien Sanglard //    Parsed parsed;
57*3f982cf4SFabien Sanglard //    Component scheme;
58*3f982cf4SFabien Sanglard //    if (!ExtractScheme(url, url_len, &scheme))
59*3f982cf4SFabien Sanglard //      return I_CAN_NOT_FIND_THE_SCHEME_DUDE;
60*3f982cf4SFabien Sanglard //
61*3f982cf4SFabien Sanglard //    if (IsStandardScheme(url, scheme))  // Not provided by this component
62*3f982cf4SFabien Sanglard //      ParseStandardURL(url, url_len, &parsed);
63*3f982cf4SFabien Sanglard //    else if (IsFileURL(url, scheme))    // Not provided by this component
64*3f982cf4SFabien Sanglard //      ParseFileURL(url, url_len, &parsed);
65*3f982cf4SFabien Sanglard //    else
66*3f982cf4SFabien Sanglard //      ParsePathURL(url, url_len, &parsed);
67*3f982cf4SFabien Sanglard //
68*3f982cf4SFabien Sanglard struct Parsed {
69*3f982cf4SFabien Sanglard   // Identifies different components.
70*3f982cf4SFabien Sanglard   enum ComponentType {
71*3f982cf4SFabien Sanglard     SCHEME,
72*3f982cf4SFabien Sanglard     USERNAME,
73*3f982cf4SFabien Sanglard     PASSWORD,
74*3f982cf4SFabien Sanglard     HOST,
75*3f982cf4SFabien Sanglard     PORT,
76*3f982cf4SFabien Sanglard     PATH,
77*3f982cf4SFabien Sanglard     QUERY,
78*3f982cf4SFabien Sanglard     REF,
79*3f982cf4SFabien Sanglard   };
80*3f982cf4SFabien Sanglard 
81*3f982cf4SFabien Sanglard   // The default constructor is sufficient for the components, but inner_parsed_
82*3f982cf4SFabien Sanglard   // requires special handling.
83*3f982cf4SFabien Sanglard   Parsed();
84*3f982cf4SFabien Sanglard   Parsed(const Parsed&);
85*3f982cf4SFabien Sanglard   Parsed& operator=(const Parsed&);
86*3f982cf4SFabien Sanglard   ~Parsed();
87*3f982cf4SFabien Sanglard 
88*3f982cf4SFabien Sanglard   // Returns the length of the URL (the end of the last component).
89*3f982cf4SFabien Sanglard   //
90*3f982cf4SFabien Sanglard   // Note that for some invalid, non-canonical URLs, this may not be the length
91*3f982cf4SFabien Sanglard   // of the string. For example "http://": the parsed structure will only
92*3f982cf4SFabien Sanglard   // contain an entry for the four-character scheme, and it doesn't know about
93*3f982cf4SFabien Sanglard   // the "://". For all other last-components, it will return the real length.
94*3f982cf4SFabien Sanglard   int Length() const;
95*3f982cf4SFabien Sanglard 
96*3f982cf4SFabien Sanglard   // Returns the number of characters before the given component if it exists,
97*3f982cf4SFabien Sanglard   // or where the component would be if it did exist. This will return the
98*3f982cf4SFabien Sanglard   // string length if the component would be appended to the end.
99*3f982cf4SFabien Sanglard   //
100*3f982cf4SFabien Sanglard   // Note that this can get a little funny for the port, query, and ref
101*3f982cf4SFabien Sanglard   // components which have a delimiter that is not counted as part of the
102*3f982cf4SFabien Sanglard   // component. The |include_delimiter| flag controls if you want this counted
103*3f982cf4SFabien Sanglard   // as part of the component or not when the component exists.
104*3f982cf4SFabien Sanglard   //
105*3f982cf4SFabien Sanglard   // This example shows the difference between the two flags for two of these
106*3f982cf4SFabien Sanglard   // delimited components that is present (the port and query) and one that
107*3f982cf4SFabien Sanglard   // isn't (the reference). The components that this flag affects are marked
108*3f982cf4SFabien Sanglard   // with a *.
109*3f982cf4SFabien Sanglard   //                 0         1         2
110*3f982cf4SFabien Sanglard   //                 012345678901234567890
111*3f982cf4SFabien Sanglard   // Example input:  http://foo:80/?query
112*3f982cf4SFabien Sanglard   //              include_delim=true,  ...=false  ("<-" indicates different)
113*3f982cf4SFabien Sanglard   //      SCHEME: 0                    0
114*3f982cf4SFabien Sanglard   //    USERNAME: 5                    5
115*3f982cf4SFabien Sanglard   //    PASSWORD: 5                    5
116*3f982cf4SFabien Sanglard   //        HOST: 7                    7
117*3f982cf4SFabien Sanglard   //       *PORT: 10                   11 <-
118*3f982cf4SFabien Sanglard   //        PATH: 13                   13
119*3f982cf4SFabien Sanglard   //      *QUERY: 14                   15 <-
120*3f982cf4SFabien Sanglard   //        *REF: 20                   20
121*3f982cf4SFabien Sanglard   //
122*3f982cf4SFabien Sanglard   int CountCharactersBefore(ComponentType type, bool include_delimiter) const;
123*3f982cf4SFabien Sanglard 
124*3f982cf4SFabien Sanglard   // Scheme without the colon: "http://foo"/ would have a scheme of "http".
125*3f982cf4SFabien Sanglard   // The length will be -1 if no scheme is specified ("foo.com"), or 0 if there
126*3f982cf4SFabien Sanglard   // is a colon but no scheme (":foo"). Note that the scheme is not guaranteed
127*3f982cf4SFabien Sanglard   // to start at the beginning of the string if there are preceeding whitespace
128*3f982cf4SFabien Sanglard   // or control characters.
129*3f982cf4SFabien Sanglard   Component scheme;
130*3f982cf4SFabien Sanglard 
131*3f982cf4SFabien Sanglard   // Username. Specified in URLs with an @ sign before the host. See |password|
132*3f982cf4SFabien Sanglard   Component username;
133*3f982cf4SFabien Sanglard 
134*3f982cf4SFabien Sanglard   // Password. The length will be -1 if unspecified, 0 if specified but empty.
135*3f982cf4SFabien Sanglard   // Not all URLs with a username have a password, as in "http://me@host/".
136*3f982cf4SFabien Sanglard   // The password is separated form the username with a colon, as in
137*3f982cf4SFabien Sanglard   // "http://me:secret@host/"
138*3f982cf4SFabien Sanglard   Component password;
139*3f982cf4SFabien Sanglard 
140*3f982cf4SFabien Sanglard   // Host name.
141*3f982cf4SFabien Sanglard   Component host;
142*3f982cf4SFabien Sanglard 
143*3f982cf4SFabien Sanglard   // Port number.
144*3f982cf4SFabien Sanglard   Component port;
145*3f982cf4SFabien Sanglard 
146*3f982cf4SFabien Sanglard   // Path, this is everything following the host name, stopping at the query of
147*3f982cf4SFabien Sanglard   // ref delimiter (if any). Length will be -1 if unspecified. This includes
148*3f982cf4SFabien Sanglard   // the preceeding slash, so the path on http://www.google.com/asdf" is
149*3f982cf4SFabien Sanglard   // "/asdf". As a result, it is impossible to have a 0 length path, it will
150*3f982cf4SFabien Sanglard   // be -1 in cases like "http://host?foo".
151*3f982cf4SFabien Sanglard   // Note that we treat backslashes the same as slashes.
152*3f982cf4SFabien Sanglard   Component path;
153*3f982cf4SFabien Sanglard 
154*3f982cf4SFabien Sanglard   // Stuff between the ? and the # after the path. This does not include the
155*3f982cf4SFabien Sanglard   // preceeding ? character. Length will be -1 if unspecified, 0 if there is
156*3f982cf4SFabien Sanglard   // a question mark but no query string.
157*3f982cf4SFabien Sanglard   Component query;
158*3f982cf4SFabien Sanglard 
159*3f982cf4SFabien Sanglard   // Indicated by a #, this is everything following the hash sign (not
160*3f982cf4SFabien Sanglard   // including it). If there are multiple hash signs, we'll use the last one.
161*3f982cf4SFabien Sanglard   // Length will be -1 if there is no hash sign, or 0 if there is one but
162*3f982cf4SFabien Sanglard   // nothing follows it.
163*3f982cf4SFabien Sanglard   Component ref;
164*3f982cf4SFabien Sanglard 
165*3f982cf4SFabien Sanglard   // The URL spec from the character after the scheme: until the end of the
166*3f982cf4SFabien Sanglard   // URL, regardless of the scheme. This is mostly useful for 'opaque' non-
167*3f982cf4SFabien Sanglard   // hierarchical schemes like data: and javascript: as a convient way to get
168*3f982cf4SFabien Sanglard   // the string with the scheme stripped off.
169*3f982cf4SFabien Sanglard   Component GetContent() const;
170*3f982cf4SFabien Sanglard 
171*3f982cf4SFabien Sanglard   // True if the URL's source contained a raw `<` character, and whitespace was
172*3f982cf4SFabien Sanglard   // removed from the URL during parsing
173*3f982cf4SFabien Sanglard   //
174*3f982cf4SFabien Sanglard   // TODO(mkwst): Link this to something in a spec if
175*3f982cf4SFabien Sanglard   // https://github.com/whatwg/url/pull/284 lands.
176*3f982cf4SFabien Sanglard   bool potentially_dangling_markup;
177*3f982cf4SFabien Sanglard 
178*3f982cf4SFabien Sanglard   // This is used for nested URL types, currently only filesystem.  If you
179*3f982cf4SFabien Sanglard   // parse a filesystem URL, the resulting Parsed will have a nested
180*3f982cf4SFabien Sanglard   // inner_parsed_ to hold the parsed inner URL's component information.
181*3f982cf4SFabien Sanglard   // For all other url types [including the inner URL], it will be NULL.
inner_parsedParsed182*3f982cf4SFabien Sanglard   Parsed* inner_parsed() const { return inner_parsed_; }
183*3f982cf4SFabien Sanglard 
set_inner_parsedParsed184*3f982cf4SFabien Sanglard   void set_inner_parsed(const Parsed& inner_parsed) {
185*3f982cf4SFabien Sanglard     if (!inner_parsed_)
186*3f982cf4SFabien Sanglard       inner_parsed_ = new Parsed(inner_parsed);
187*3f982cf4SFabien Sanglard     else
188*3f982cf4SFabien Sanglard       *inner_parsed_ = inner_parsed;
189*3f982cf4SFabien Sanglard   }
190*3f982cf4SFabien Sanglard 
clear_inner_parsedParsed191*3f982cf4SFabien Sanglard   void clear_inner_parsed() {
192*3f982cf4SFabien Sanglard     if (inner_parsed_) {
193*3f982cf4SFabien Sanglard       delete inner_parsed_;
194*3f982cf4SFabien Sanglard       inner_parsed_ = nullptr;
195*3f982cf4SFabien Sanglard     }
196*3f982cf4SFabien Sanglard   }
197*3f982cf4SFabien Sanglard 
198*3f982cf4SFabien Sanglard  private:
199*3f982cf4SFabien Sanglard   Parsed* inner_parsed_;  // This object is owned and managed by this struct.
200*3f982cf4SFabien Sanglard };
201*3f982cf4SFabien Sanglard 
202*3f982cf4SFabien Sanglard // Initialization functions ---------------------------------------------------
203*3f982cf4SFabien Sanglard //
204*3f982cf4SFabien Sanglard // These functions parse the given URL, filling in all of the structure's
205*3f982cf4SFabien Sanglard // components. These functions can not fail, they will always do their best
206*3f982cf4SFabien Sanglard // at interpreting the input given.
207*3f982cf4SFabien Sanglard //
208*3f982cf4SFabien Sanglard // The string length of the URL MUST be specified, we do not check for NULLs
209*3f982cf4SFabien Sanglard // at any point in the process, and will actually handle embedded NULLs.
210*3f982cf4SFabien Sanglard //
211*3f982cf4SFabien Sanglard // IMPORTANT: These functions do NOT hang on to the given pointer or copy it
212*3f982cf4SFabien Sanglard // in any way. See the comment above the struct.
213*3f982cf4SFabien Sanglard //
214*3f982cf4SFabien Sanglard // The 8-bit versions require UTF-8 encoding.
215*3f982cf4SFabien Sanglard 
216*3f982cf4SFabien Sanglard // StandardURL is for when the scheme is known to be one that has an
217*3f982cf4SFabien Sanglard // authority (host) like "http". This function will not handle weird ones
218*3f982cf4SFabien Sanglard // like "about:" and "javascript:", or do the right thing for "file:" URLs.
219*3f982cf4SFabien Sanglard void ParseStandardURL(const char* url, int url_len, Parsed* parsed);
220*3f982cf4SFabien Sanglard 
221*3f982cf4SFabien Sanglard // PathURL is for when the scheme is known not to have an authority (host)
222*3f982cf4SFabien Sanglard // section but that aren't file URLs either. The scheme is parsed, and
223*3f982cf4SFabien Sanglard // everything after the scheme is considered as the path. This is used for
224*3f982cf4SFabien Sanglard // things like "about:" and "javascript:"
225*3f982cf4SFabien Sanglard void ParsePathURL(const char* url,
226*3f982cf4SFabien Sanglard                   int url_len,
227*3f982cf4SFabien Sanglard                   bool trim_path_end,
228*3f982cf4SFabien Sanglard                   Parsed* parsed);
229*3f982cf4SFabien Sanglard 
230*3f982cf4SFabien Sanglard // FileURL is for file URLs. There are some special rules for interpreting
231*3f982cf4SFabien Sanglard // these.
232*3f982cf4SFabien Sanglard void ParseFileURL(const char* url, int url_len, Parsed* parsed);
233*3f982cf4SFabien Sanglard 
234*3f982cf4SFabien Sanglard // Filesystem URLs are structured differently than other URLs.
235*3f982cf4SFabien Sanglard void ParseFileSystemURL(const char* url, int url_len, Parsed* parsed);
236*3f982cf4SFabien Sanglard 
237*3f982cf4SFabien Sanglard // MailtoURL is for mailto: urls. They are made up scheme,path,query
238*3f982cf4SFabien Sanglard void ParseMailtoURL(const char* url, int url_len, Parsed* parsed);
239*3f982cf4SFabien Sanglard 
240*3f982cf4SFabien Sanglard // Helper functions -----------------------------------------------------------
241*3f982cf4SFabien Sanglard 
242*3f982cf4SFabien Sanglard // Locates the scheme according to the URL  parser's rules. This function is
243*3f982cf4SFabien Sanglard // designed so the caller can find the scheme and call the correct Init*
244*3f982cf4SFabien Sanglard // function according to their known scheme types.
245*3f982cf4SFabien Sanglard //
246*3f982cf4SFabien Sanglard // It also does not perform any validation on the scheme.
247*3f982cf4SFabien Sanglard //
248*3f982cf4SFabien Sanglard // This function will return true if the scheme is found and will put the
249*3f982cf4SFabien Sanglard // scheme's range into *scheme. False means no scheme could be found. Note
250*3f982cf4SFabien Sanglard // that a URL beginning with a colon has a scheme, but it is empty, so this
251*3f982cf4SFabien Sanglard // function will return true but *scheme will = (0,0).
252*3f982cf4SFabien Sanglard //
253*3f982cf4SFabien Sanglard // The scheme is found by skipping spaces and control characters at the
254*3f982cf4SFabien Sanglard // beginning, and taking everything from there to the first colon to be the
255*3f982cf4SFabien Sanglard // scheme. The character at scheme.end() will be the colon (we may enhance
256*3f982cf4SFabien Sanglard // this to handle full width colons or something, so don't count on the
257*3f982cf4SFabien Sanglard // actual character value). The character at scheme.end()+1 will be the
258*3f982cf4SFabien Sanglard // beginning of the rest of the URL, be it the authority or the path (or the
259*3f982cf4SFabien Sanglard // end of the string).
260*3f982cf4SFabien Sanglard //
261*3f982cf4SFabien Sanglard // The 8-bit version requires UTF-8 encoding.
262*3f982cf4SFabien Sanglard bool ExtractScheme(const char* url, int url_len, Component* scheme);
263*3f982cf4SFabien Sanglard 
264*3f982cf4SFabien Sanglard // Returns true if ch is a character that terminates the authority segment
265*3f982cf4SFabien Sanglard // of a URL.
266*3f982cf4SFabien Sanglard bool IsAuthorityTerminator(char ch);
267*3f982cf4SFabien Sanglard 
268*3f982cf4SFabien Sanglard // Does a best effort parse of input |spec|, in range |auth|. If a particular
269*3f982cf4SFabien Sanglard // component is not found, it will be set to invalid.
270*3f982cf4SFabien Sanglard void ParseAuthority(const char* spec,
271*3f982cf4SFabien Sanglard                     const Component& auth,
272*3f982cf4SFabien Sanglard                     Component* username,
273*3f982cf4SFabien Sanglard                     Component* password,
274*3f982cf4SFabien Sanglard                     Component* hostname,
275*3f982cf4SFabien Sanglard                     Component* port_num);
276*3f982cf4SFabien Sanglard 
277*3f982cf4SFabien Sanglard // Computes the integer port value from the given port component. The port
278*3f982cf4SFabien Sanglard // component should have been identified by one of the init functions on
279*3f982cf4SFabien Sanglard // |Parsed| for the given input url.
280*3f982cf4SFabien Sanglard //
281*3f982cf4SFabien Sanglard // The return value will be a positive integer between 0 and 64K, or one of
282*3f982cf4SFabien Sanglard // the two special values below.
283*3f982cf4SFabien Sanglard enum SpecialPort { PORT_UNSPECIFIED = -1, PORT_INVALID = -2 };
284*3f982cf4SFabien Sanglard int ParsePort(const char* url, const Component& port);
285*3f982cf4SFabien Sanglard 
286*3f982cf4SFabien Sanglard // Extracts the range of the file name in the given url. The path must
287*3f982cf4SFabien Sanglard // already have been computed by the parse function, and the matching URL
288*3f982cf4SFabien Sanglard // and extracted path are provided to this function. The filename is
289*3f982cf4SFabien Sanglard // defined as being everything from the last slash/backslash of the path
290*3f982cf4SFabien Sanglard // to the end of the path.
291*3f982cf4SFabien Sanglard //
292*3f982cf4SFabien Sanglard // The file name will be empty if the path is empty or there is nothing
293*3f982cf4SFabien Sanglard // following the last slash.
294*3f982cf4SFabien Sanglard //
295*3f982cf4SFabien Sanglard // The 8-bit version requires UTF-8 encoding.
296*3f982cf4SFabien Sanglard void ExtractFileName(const char* url,
297*3f982cf4SFabien Sanglard                      const Component& path,
298*3f982cf4SFabien Sanglard                      Component* file_name);
299*3f982cf4SFabien Sanglard 
300*3f982cf4SFabien Sanglard // Extract the first key/value from the range defined by |*query|. Updates
301*3f982cf4SFabien Sanglard // |*query| to start at the end of the extracted key/value pair. This is
302*3f982cf4SFabien Sanglard // designed for use in a loop: you can keep calling it with the same query
303*3f982cf4SFabien Sanglard // object and it will iterate over all items in the query.
304*3f982cf4SFabien Sanglard //
305*3f982cf4SFabien Sanglard // Some key/value pairs may have the key, the value, or both be empty (for
306*3f982cf4SFabien Sanglard // example, the query string "?&"). These will be returned. Note that an empty
307*3f982cf4SFabien Sanglard // last parameter "foo.com?" or foo.com?a&" will not be returned, this case
308*3f982cf4SFabien Sanglard // is the same as "done."
309*3f982cf4SFabien Sanglard //
310*3f982cf4SFabien Sanglard // The initial query component should not include the '?' (this is the default
311*3f982cf4SFabien Sanglard // for parsed URLs).
312*3f982cf4SFabien Sanglard //
313*3f982cf4SFabien Sanglard // If no key/value are found |*key| and |*value| will be unchanged and it will
314*3f982cf4SFabien Sanglard // return false.
315*3f982cf4SFabien Sanglard bool ExtractQueryKeyValue(const char* url,
316*3f982cf4SFabien Sanglard                           Component* query,
317*3f982cf4SFabien Sanglard                           Component* key,
318*3f982cf4SFabien Sanglard                           Component* value);
319*3f982cf4SFabien Sanglard 
320*3f982cf4SFabien Sanglard }  // namespace openscreen
321*3f982cf4SFabien Sanglard 
322*3f982cf4SFabien Sanglard #endif  // THIRD_PARTY_MOZILLA_URL_PARSE_H_
323