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_PARSE_INTERNAL_H_
6*6777b538SAndroid Build Coastguard Worker #define URL_URL_PARSE_INTERNAL_H_
7*6777b538SAndroid Build Coastguard Worker
8*6777b538SAndroid Build Coastguard Worker // Contains common inline helper functions used by the URL parsing routines.
9*6777b538SAndroid Build Coastguard Worker
10*6777b538SAndroid Build Coastguard Worker #include "url/third_party/mozilla/url_parse.h"
11*6777b538SAndroid Build Coastguard Worker
12*6777b538SAndroid Build Coastguard Worker namespace url {
13*6777b538SAndroid Build Coastguard Worker
14*6777b538SAndroid Build Coastguard Worker // A helper function to handle a URL separator, which is '/' or '\'.
15*6777b538SAndroid Build Coastguard Worker //
16*6777b538SAndroid Build Coastguard Worker // The motivation: There are many condition checks in URL Standard like the
17*6777b538SAndroid Build Coastguard Worker // following:
18*6777b538SAndroid Build Coastguard Worker //
19*6777b538SAndroid Build Coastguard Worker // > If url is special and c is U+002F (/) or U+005C (\), ...
IsSlashOrBackslash(char16_t ch)20*6777b538SAndroid Build Coastguard Worker inline bool IsSlashOrBackslash(char16_t ch) {
21*6777b538SAndroid Build Coastguard Worker return ch == '/' || ch == '\\';
22*6777b538SAndroid Build Coastguard Worker }
IsSlashOrBackslash(char ch)23*6777b538SAndroid Build Coastguard Worker inline bool IsSlashOrBackslash(char ch) {
24*6777b538SAndroid Build Coastguard Worker return IsSlashOrBackslash(static_cast<char16_t>(ch));
25*6777b538SAndroid Build Coastguard Worker }
26*6777b538SAndroid Build Coastguard Worker
27*6777b538SAndroid Build Coastguard Worker // Returns true if we should trim this character from the URL because it is a
28*6777b538SAndroid Build Coastguard Worker // space or a control character.
ShouldTrimFromURL(char16_t ch)29*6777b538SAndroid Build Coastguard Worker inline bool ShouldTrimFromURL(char16_t ch) {
30*6777b538SAndroid Build Coastguard Worker return ch <= ' ';
31*6777b538SAndroid Build Coastguard Worker }
ShouldTrimFromURL(char ch)32*6777b538SAndroid Build Coastguard Worker inline bool ShouldTrimFromURL(char ch) {
33*6777b538SAndroid Build Coastguard Worker return ShouldTrimFromURL(static_cast<char16_t>(ch));
34*6777b538SAndroid Build Coastguard Worker }
35*6777b538SAndroid Build Coastguard Worker
36*6777b538SAndroid Build Coastguard Worker // Given an already-initialized begin index and length, this shrinks the range
37*6777b538SAndroid Build Coastguard Worker // to eliminate "should-be-trimmed" characters. Note that the length does *not*
38*6777b538SAndroid Build Coastguard Worker // indicate the length of untrimmed data from |*begin|, but rather the position
39*6777b538SAndroid Build Coastguard Worker // in the input string (so the string starts at character |*begin| in the spec,
40*6777b538SAndroid Build Coastguard Worker // and goes until |*len|).
41*6777b538SAndroid Build Coastguard Worker template<typename CHAR>
42*6777b538SAndroid Build Coastguard Worker inline void TrimURL(const CHAR* spec, int* begin, int* len,
43*6777b538SAndroid Build Coastguard Worker bool trim_path_end = true) {
44*6777b538SAndroid Build Coastguard Worker // Strip leading whitespace and control characters.
45*6777b538SAndroid Build Coastguard Worker while (*begin < *len && ShouldTrimFromURL(spec[*begin]))
46*6777b538SAndroid Build Coastguard Worker (*begin)++;
47*6777b538SAndroid Build Coastguard Worker
48*6777b538SAndroid Build Coastguard Worker if (trim_path_end) {
49*6777b538SAndroid Build Coastguard Worker // Strip trailing whitespace and control characters. We need the >i test
50*6777b538SAndroid Build Coastguard Worker // for when the input string is all blanks; we don't want to back past the
51*6777b538SAndroid Build Coastguard Worker // input.
52*6777b538SAndroid Build Coastguard Worker while (*len > *begin && ShouldTrimFromURL(spec[*len - 1]))
53*6777b538SAndroid Build Coastguard Worker (*len)--;
54*6777b538SAndroid Build Coastguard Worker }
55*6777b538SAndroid Build Coastguard Worker }
56*6777b538SAndroid Build Coastguard Worker
57*6777b538SAndroid Build Coastguard Worker // Counts the number of consecutive slashes starting at the given offset
58*6777b538SAndroid Build Coastguard Worker // in the given string of the given length.
59*6777b538SAndroid Build Coastguard Worker template<typename CHAR>
CountConsecutiveSlashes(const CHAR * str,int begin_offset,int str_len)60*6777b538SAndroid Build Coastguard Worker inline int CountConsecutiveSlashes(const CHAR *str,
61*6777b538SAndroid Build Coastguard Worker int begin_offset, int str_len) {
62*6777b538SAndroid Build Coastguard Worker int count = 0;
63*6777b538SAndroid Build Coastguard Worker while (begin_offset + count < str_len &&
64*6777b538SAndroid Build Coastguard Worker IsSlashOrBackslash(str[begin_offset + count])) {
65*6777b538SAndroid Build Coastguard Worker ++count;
66*6777b538SAndroid Build Coastguard Worker }
67*6777b538SAndroid Build Coastguard Worker return count;
68*6777b538SAndroid Build Coastguard Worker }
69*6777b538SAndroid Build Coastguard Worker
70*6777b538SAndroid Build Coastguard Worker // Internal functions in url_parse.cc that parse the path, that is, everything
71*6777b538SAndroid Build Coastguard Worker // following the authority section. The input is the range of everything
72*6777b538SAndroid Build Coastguard Worker // following the authority section, and the output is the identified ranges.
73*6777b538SAndroid Build Coastguard Worker //
74*6777b538SAndroid Build Coastguard Worker // This is designed for the file URL parser or other consumers who may do
75*6777b538SAndroid Build Coastguard Worker // special stuff at the beginning, but want regular path parsing, it just
76*6777b538SAndroid Build Coastguard Worker // maps to the internal parsing function for paths.
77*6777b538SAndroid Build Coastguard Worker void ParsePathInternal(const char* spec,
78*6777b538SAndroid Build Coastguard Worker const Component& path,
79*6777b538SAndroid Build Coastguard Worker Component* filepath,
80*6777b538SAndroid Build Coastguard Worker Component* query,
81*6777b538SAndroid Build Coastguard Worker Component* ref);
82*6777b538SAndroid Build Coastguard Worker void ParsePathInternal(const char16_t* spec,
83*6777b538SAndroid Build Coastguard Worker const Component& path,
84*6777b538SAndroid Build Coastguard Worker Component* filepath,
85*6777b538SAndroid Build Coastguard Worker Component* query,
86*6777b538SAndroid Build Coastguard Worker Component* ref);
87*6777b538SAndroid Build Coastguard Worker
88*6777b538SAndroid Build Coastguard Worker // Internal functions in url_parse.cc that parse non-special URLs, which are
89*6777b538SAndroid Build Coastguard Worker // similar to `ParseNonSpecialURL` functions in url_parse.h, but with
90*6777b538SAndroid Build Coastguard Worker // `trim_path_end` parameter that controls whether to trim path end or not.
91*6777b538SAndroid Build Coastguard Worker void ParseNonSpecialURLInternal(const char* url,
92*6777b538SAndroid Build Coastguard Worker int url_len,
93*6777b538SAndroid Build Coastguard Worker bool trim_path_end,
94*6777b538SAndroid Build Coastguard Worker Parsed* parsed);
95*6777b538SAndroid Build Coastguard Worker void ParseNonSpecialURLInternal(const char16_t* url,
96*6777b538SAndroid Build Coastguard Worker int url_len,
97*6777b538SAndroid Build Coastguard Worker bool trim_path_end,
98*6777b538SAndroid Build Coastguard Worker Parsed* parsed);
99*6777b538SAndroid Build Coastguard Worker
100*6777b538SAndroid Build Coastguard Worker // Given a spec and a pointer to the character after the colon following the
101*6777b538SAndroid Build Coastguard Worker // special scheme, this parses it and fills in the structure, Every item in the
102*6777b538SAndroid Build Coastguard Worker // parsed structure is filled EXCEPT for the scheme, which is untouched.
103*6777b538SAndroid Build Coastguard Worker void ParseAfterSpecialScheme(const char* spec,
104*6777b538SAndroid Build Coastguard Worker int spec_len,
105*6777b538SAndroid Build Coastguard Worker int after_scheme,
106*6777b538SAndroid Build Coastguard Worker Parsed* parsed);
107*6777b538SAndroid Build Coastguard Worker void ParseAfterSpecialScheme(const char16_t* spec,
108*6777b538SAndroid Build Coastguard Worker int spec_len,
109*6777b538SAndroid Build Coastguard Worker int after_scheme,
110*6777b538SAndroid Build Coastguard Worker Parsed* parsed);
111*6777b538SAndroid Build Coastguard Worker
112*6777b538SAndroid Build Coastguard Worker // Given a spec and a pointer to the character after the colon following the
113*6777b538SAndroid Build Coastguard Worker // non-special scheme, this parses it and fills in the structure, Every item in
114*6777b538SAndroid Build Coastguard Worker // the parsed structure is filled EXCEPT for the scheme, which is untouched.
115*6777b538SAndroid Build Coastguard Worker void ParseAfterNonSpecialScheme(const char* spec,
116*6777b538SAndroid Build Coastguard Worker int spec_len,
117*6777b538SAndroid Build Coastguard Worker int after_scheme,
118*6777b538SAndroid Build Coastguard Worker Parsed* parsed);
119*6777b538SAndroid Build Coastguard Worker void ParseAfterNonSpecialScheme(const char16_t* spec,
120*6777b538SAndroid Build Coastguard Worker int spec_len,
121*6777b538SAndroid Build Coastguard Worker int after_scheme,
122*6777b538SAndroid Build Coastguard Worker Parsed* parsed);
123*6777b538SAndroid Build Coastguard Worker
124*6777b538SAndroid Build Coastguard Worker } // namespace url
125*6777b538SAndroid Build Coastguard Worker
126*6777b538SAndroid Build Coastguard Worker #endif // URL_URL_PARSE_INTERNAL_H_
127