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