Set launch data handler id when launching platform apps with files.
[chromium-blink-merge.git] / url / url_parse.h
blob5c3643f5c143ccf57523d3adc7e0cab9c80d73c1
1 // 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.
5 #ifndef URL_URL_PARSE_H_
6 #define URL_URL_PARSE_H_
8 #include <string>
10 #include "base/basictypes.h"
11 #include "base/string16.h"
13 namespace url_parse {
15 // Deprecated, but WebKit/WebCore/platform/KURLGooglePrivate.h and
16 // KURLGoogle.cpp still rely on this type.
17 typedef char16 UTF16Char;
19 // Component ------------------------------------------------------------------
21 // Represents a substring for URL parsing.
22 struct Component {
23 Component() : begin(0), len(-1) {}
25 // Normal constructor: takes an offset and a length.
26 Component(int b, int l) : begin(b), len(l) {}
28 int end() const {
29 return begin + len;
32 // Returns true if this component is valid, meaning the length is given. Even
33 // valid components may be empty to record the fact that they exist.
34 bool is_valid() const {
35 return (len != -1);
38 // Returns true if the given component is specified on false, the component
39 // is either empty or invalid.
40 bool is_nonempty() const {
41 return (len > 0);
44 void reset() {
45 begin = 0;
46 len = -1;
49 bool operator==(const Component& other) const {
50 return begin == other.begin && len == other.len;
53 int begin; // Byte offset in the string of this component.
54 int len; // Will be -1 if the component is unspecified.
57 // Helper that returns a component created with the given begin and ending
58 // points. The ending point is non-inclusive.
59 inline Component MakeRange(int begin, int end) {
60 return Component(begin, end - begin);
63 // Parsed ---------------------------------------------------------------------
65 // A structure that holds the identified parts of an input URL. This structure
66 // does NOT store the URL itself. The caller will have to store the URL text
67 // and its corresponding Parsed structure separately.
69 // Typical usage would be:
71 // url_parse::Parsed parsed;
72 // url_parse::Component scheme;
73 // if (!url_parse::ExtractScheme(url, url_len, &scheme))
74 // return I_CAN_NOT_FIND_THE_SCHEME_DUDE;
76 // if (IsStandardScheme(url, scheme)) // Not provided by this component
77 // url_parseParseStandardURL(url, url_len, &parsed);
78 // else if (IsFileURL(url, scheme)) // Not provided by this component
79 // url_parse::ParseFileURL(url, url_len, &parsed);
80 // else
81 // url_parse::ParsePathURL(url, url_len, &parsed);
83 struct Parsed {
84 // Identifies different components.
85 enum ComponentType {
86 SCHEME,
87 USERNAME,
88 PASSWORD,
89 HOST,
90 PORT,
91 PATH,
92 QUERY,
93 REF,
96 // The default constructor is sufficient for the components, but inner_parsed_
97 // requires special handling.
98 Parsed();
99 Parsed(const Parsed&);
100 Parsed& operator=(const Parsed&);
101 ~Parsed();
103 // Returns the length of the URL (the end of the last component).
105 // Note that for some invalid, non-canonical URLs, this may not be the length
106 // of the string. For example "http://": the parsed structure will only
107 // contain an entry for the four-character scheme, and it doesn't know about
108 // the "://". For all other last-components, it will return the real length.
109 int Length() const;
111 // Returns the number of characters before the given component if it exists,
112 // or where the component would be if it did exist. This will return the
113 // string length if the component would be appended to the end.
115 // Note that this can get a little funny for the port, query, and ref
116 // components which have a delimiter that is not counted as part of the
117 // component. The |include_delimiter| flag controls if you want this counted
118 // as part of the component or not when the component exists.
120 // This example shows the difference between the two flags for two of these
121 // delimited components that is present (the port and query) and one that
122 // isn't (the reference). The components that this flag affects are marked
123 // with a *.
124 // 0 1 2
125 // 012345678901234567890
126 // Example input: http://foo:80/?query
127 // include_delim=true, ...=false ("<-" indicates different)
128 // SCHEME: 0 0
129 // USERNAME: 5 5
130 // PASSWORD: 5 5
131 // HOST: 7 7
132 // *PORT: 10 11 <-
133 // PATH: 13 13
134 // *QUERY: 14 15 <-
135 // *REF: 20 20
137 int CountCharactersBefore(ComponentType type,
138 bool include_delimiter) const;
140 // Scheme without the colon: "http://foo"/ would have a scheme of "http".
141 // The length will be -1 if no scheme is specified ("foo.com"), or 0 if there
142 // is a colon but no scheme (":foo"). Note that the scheme is not guaranteed
143 // to start at the beginning of the string if there are preceeding whitespace
144 // or control characters.
145 Component scheme;
147 // Username. Specified in URLs with an @ sign before the host. See |password|
148 Component username;
150 // Password. The length will be -1 if unspecified, 0 if specified but empty.
151 // Not all URLs with a username have a password, as in "http://me@host/".
152 // The password is separated form the username with a colon, as in
153 // "http://me:secret@host/"
154 Component password;
156 // Host name.
157 Component host;
159 // Port number.
160 Component port;
162 // Path, this is everything following the host name. Length will be -1 if
163 // unspecified. This includes the preceeding slash, so the path on
164 // http://www.google.com/asdf" is "/asdf". As a result, it is impossible to
165 // have a 0 length path, it will be -1 in cases like "http://host?foo".
166 // Note that we treat backslashes the same as slashes.
167 Component path;
169 // Stuff between the ? and the # after the path. This does not include the
170 // preceeding ? character. Length will be -1 if unspecified, 0 if there is
171 // a question mark but no query string.
172 Component query;
174 // Indicated by a #, this is everything following the hash sign (not
175 // including it). If there are multiple hash signs, we'll use the last one.
176 // Length will be -1 if there is no hash sign, or 0 if there is one but
177 // nothing follows it.
178 Component ref;
180 // This is used for nested URL types, currently only filesystem. If you
181 // parse a filesystem URL, the resulting Parsed will have a nested
182 // inner_parsed_ to hold the parsed inner URL's component information.
183 // For all other url types [including the inner URL], it will be NULL.
184 Parsed* inner_parsed() const {
185 return inner_parsed_;
188 void set_inner_parsed(const Parsed& inner_parsed) {
189 if (!inner_parsed_)
190 inner_parsed_ = new Parsed(inner_parsed);
191 else
192 *inner_parsed_ = inner_parsed;
195 void clear_inner_parsed() {
196 if (inner_parsed_) {
197 delete inner_parsed_;
198 inner_parsed_ = NULL;
202 private:
203 Parsed* inner_parsed_; // This object is owned and managed by this struct.
206 // Initialization functions ---------------------------------------------------
208 // These functions parse the given URL, filling in all of the structure's
209 // components. These functions can not fail, they will always do their best
210 // at interpreting the input given.
212 // The string length of the URL MUST be specified, we do not check for NULLs
213 // at any point in the process, and will actually handle embedded NULLs.
215 // IMPORTANT: These functions do NOT hang on to the given pointer or copy it
216 // in any way. See the comment above the struct.
218 // The 8-bit versions require UTF-8 encoding.
220 // StandardURL is for when the scheme is known to be one that has an
221 // authority (host) like "http". This function will not handle weird ones
222 // like "about:" and "javascript:", or do the right thing for "file:" URLs.
223 void ParseStandardURL(const char* url, int url_len, Parsed* parsed);
224 void ParseStandardURL(const char16* url, int url_len, Parsed* parsed);
226 // PathURL is for when the scheme is known not to have an authority (host)
227 // section but that aren't file URLs either. The scheme is parsed, and
228 // everything after the scheme is considered as the path. This is used for
229 // things like "about:" and "javascript:"
230 void ParsePathURL(const char* url, int url_len, Parsed* parsed);
231 void ParsePathURL(const char16* url, int url_len, Parsed* parsed);
233 // FileURL is for file URLs. There are some special rules for interpreting
234 // these.
235 void ParseFileURL(const char* url, int url_len, Parsed* parsed);
236 void ParseFileURL(const char16* url, int url_len, Parsed* parsed);
238 // Filesystem URLs are structured differently than other URLs.
239 void ParseFileSystemURL(const char* url,
240 int url_len,
241 Parsed* parsed);
242 void ParseFileSystemURL(const char16* url,
243 int url_len,
244 Parsed* parsed);
246 // MailtoURL is for mailto: urls. They are made up scheme,path,query
247 void ParseMailtoURL(const char* url, int url_len, Parsed* parsed);
248 void ParseMailtoURL(const char16* url, int url_len, Parsed* parsed);
250 // Helper functions -----------------------------------------------------------
252 // Locates the scheme according to the URL parser's rules. This function is
253 // designed so the caller can find the scheme and call the correct Init*
254 // function according to their known scheme types.
256 // It also does not perform any validation on the scheme.
258 // This function will return true if the scheme is found and will put the
259 // scheme's range into *scheme. False means no scheme could be found. Note
260 // that a URL beginning with a colon has a scheme, but it is empty, so this
261 // function will return true but *scheme will = (0,0).
263 // The scheme is found by skipping spaces and control characters at the
264 // beginning, and taking everything from there to the first colon to be the
265 // scheme. The character at scheme.end() will be the colon (we may enhance
266 // this to handle full width colons or something, so don't count on the
267 // actual character value). The character at scheme.end()+1 will be the
268 // beginning of the rest of the URL, be it the authority or the path (or the
269 // end of the string).
271 // The 8-bit version requires UTF-8 encoding.
272 bool ExtractScheme(const char* url, int url_len, Component* scheme);
273 bool ExtractScheme(const char16* url, int url_len, Component* scheme);
275 // Returns true if ch is a character that terminates the authority segment
276 // of a URL.
277 bool IsAuthorityTerminator(char16 ch);
279 // Does a best effort parse of input |spec|, in range |auth|. If a particular
280 // component is not found, it will be set to invalid.
281 void ParseAuthority(const char* spec,
282 const Component& auth,
283 Component* username,
284 Component* password,
285 Component* hostname,
286 Component* port_num);
287 void ParseAuthority(const char16* spec,
288 const Component& auth,
289 Component* username,
290 Component* password,
291 Component* hostname,
292 Component* port_num);
294 // Computes the integer port value from the given port component. The port
295 // component should have been identified by one of the init functions on
296 // |Parsed| for the given input url.
298 // The return value will be a positive integer between 0 and 64K, or one of
299 // the two special values below.
300 enum SpecialPort { PORT_UNSPECIFIED = -1, PORT_INVALID = -2 };
301 int ParsePort(const char* url, const Component& port);
302 int ParsePort(const char16* url, const Component& port);
304 // Extracts the range of the file name in the given url. The path must
305 // already have been computed by the parse function, and the matching URL
306 // and extracted path are provided to this function. The filename is
307 // defined as being everything from the last slash/backslash of the path
308 // to the end of the path.
310 // The file name will be empty if the path is empty or there is nothing
311 // following the last slash.
313 // The 8-bit version requires UTF-8 encoding.
314 void ExtractFileName(const char* url,
315 const Component& path,
316 Component* file_name);
317 void ExtractFileName(const char16* url,
318 const Component& path,
319 Component* file_name);
321 // Extract the first key/value from the range defined by |*query|. Updates
322 // |*query| to start at the end of the extracted key/value pair. This is
323 // designed for use in a loop: you can keep calling it with the same query
324 // object and it will iterate over all items in the query.
326 // Some key/value pairs may have the key, the value, or both be empty (for
327 // example, the query string "?&"). These will be returned. Note that an empty
328 // last parameter "foo.com?" or foo.com?a&" will not be returned, this case
329 // is the same as "done."
331 // The initial query component should not include the '?' (this is the default
332 // for parsed URLs).
334 // If no key/value are found |*key| and |*value| will be unchanged and it will
335 // return false.
336 bool ExtractQueryKeyValue(const char* url,
337 Component* query,
338 Component* key,
339 Component* value);
340 bool ExtractQueryKeyValue(const char16* url,
341 Component* query,
342 Component* key,
343 Component* value);
345 } // namespace url_parse
347 #endif // URL_URL_PARSE_H_