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