• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1  /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef URL_JS_URL_H_
17 #define URL_JS_URL_H_
18 
19 #include <algorithm>
20 #include <bitset>
21 #include <cmath>
22 #include <cstdio>
23 #include <cstdlib>
24 #include <map>
25 #include <string>
26 #include <vector>
27 #include "napi/native_api.h"
28 #include "napi/native_node_api.h"
29 namespace OHOS::Url {
30     enum class BitsetStatusFlag {
31         BIT0 = 0, // 0:Bit 0 Set to true,The URL analysis failed
32         BIT1 = 1, // 1:Bit 1 Set to true,The protocol is the default protocol
33         BIT2 = 2, // 2:Bit 2 Set to true,The URL has username
34         BIT3 = 3, // 3:Bit 3 Set to true,The URL has password
35         BIT4 = 4, // 4:Bit 4 Set to true,The URL has hostname
36         BIT5 = 5, // 5:Bit 5 Set to true,The URL Port is the specially
37         BIT6 = 6, // 6:Bit 6 Set to true,The URL has pathname
38         BIT7 = 7, // 7:Bit 7 Set to true,The URL has query
39         BIT8 = 8, // 8:Bit 8 Set to true,The URL has fragment
40         BIT9 = 9, // 9:Bit 9 Set to true,The URL Can not be base
41         BIT10 = 10, // 10:Bit 10 Set to true,The host is IPV6
42         BIT_STATUS_11 = 11, // 11:Each bit of a BIT represents a different parsing state.
43         BIT_ASCII_32 = 32, // 32:32-bit previously invalid control characters in ascii
44         BIT_ASCII_127 = 127, // 127:127 bits in ascii are DEL characters
45         MAX_BIT_SIZE = 128 // 128:ascii max range
46     };
47 
48     struct UrlData {
49         int port = -1;
50         bool isSpecialPath = false;
51         std::vector<std::string> path;
52         std::string password = "";
53         std::string scheme = "";
54         std::string query = "";
55         std::string username = "";
56         std::string fragment = "";
57         std::string host = "";
58     };
59 
60     class URL {
61     public:
62         /**
63          * URI constructor, which is used to instantiate a URI object.
64          *
65          * @param input Constructs a URI by parsing a given string.
66          */
67         explicit URL(const std::string& input);
68 
69         /**
70          * URI constructor, which is used to instantiate a URI object.
71          *
72          * @param input Constructs a URI by parsing a given string.
73          * @param base The input parameter is a character string.
74          */
75         URL(const std::string& input, const std::string& base);
76 
77         /**
78          * URI constructor, which is used to instantiate a URI object.
79          *
80          * @param input Constructs a URI by parsing a given string.
81          * @param base The input parameter is the URL object.
82          */
83         URL(const std::string& input, const URL& base);
84 
85         /**
86          *
87          * @param env NAPI environment parameters.
88          * Gets the host name portion of the URL��not include the port.
89          */
90         napi_value GetHostname(napi_env env) const;
91 
92         /**
93          * Sets the host name portion of the URL��not include the port.
94          *
95          * @param input Constructs a URI by parsing a given string.
96          */
97         void SetHostname(const std::string& input);
98 
99         /**
100          * Sets the username name portion of the URL��not include the port.
101          *
102          * @param input Constructs a URI by parsing a given string.
103          */
104         void SetUsername(const std::string& input);
105 
106         /**
107          * Sets the password portion of the URL��not include the port.
108          *
109          * @param input Constructs a URI by parsing a given string.
110          */
111         void SetPassword(const std::string& input);
112 
113         /**
114          * Sets the scheme portion of the URL��not include the port.
115          *
116          * @param input Constructs a URI by parsing a given string.
117          */
118         void SetScheme(const std::string& input);
119 
120         /**
121          * Sets the fragment portion of the URL��not include the port.
122          *
123          * @param input Constructs a URI by parsing a given string.
124          */
125         void SetFragment(const std::string& input);
126 
127         /**
128          * Sets the search portion of the URL��not include the port.
129          *
130          * @param input Constructs a URI by parsing a given string.
131          */
132         void SetSearch(const std::string& input);
133 
134         /**
135          * Sets the host portion of the URL��not include the port.
136          *
137          * @param input Constructs a URI by parsing a given string.
138          */
139         void SetHost(const std::string& input);
140 
141         /**
142          * Sets the port portion of the URL��not include the port.
143          *
144          * @param input Constructs a URI by parsing a given string.
145          */
146         void SetPort(const std::string& input);
147 
148         /**
149          * Sets the href portion of the URL��not include the port.
150          *
151          * @param input Constructs a URI by parsing a given string.
152          */
153         void SetHref(const std::string& input);
154 
155         /**
156          * Sets the path portion of the URL��not include the port.
157          *
158          * @param input Constructs a URI by parsing a given string.
159          */
160         void SetPath(const std::string& input);
161 
162         /**
163          * Gets the search portion of the URL��not include the port.
164          *
165          * @param env NAPI environment parameters.
166          */
167         napi_value GetSearch(napi_env env) const;
168 
169         /**
170          * Gets the username portion of the URL��not include the port.
171          *
172          * @param env NAPI environment parameters.
173          */
174         napi_value GetUsername(napi_env env) const;
175 
176         /**
177          * Gets the password portion of the URL��not include the port.
178          *
179          * @param env NAPI environment parameters.
180          */
181         napi_value GetPassword(napi_env env) const;
182 
183         /**
184          * Gets the fragment portion of the URL��not include the port.
185          *
186          * @param env NAPI environment parameters.
187          */
188         napi_value GetFragment(napi_env env) const;
189 
190         /**
191          * Gets the scheme portion of the URL��not include the port.
192          *
193          * @param env NAPI environment parameters.
194          */
195         napi_value GetScheme(napi_env env) const;
196 
197         /**
198          * Gets the path portion of the URL��not include the port.
199          *
200          * @param env NAPI environment parameters.
201          */
202         napi_value GetPath(napi_env env) const;
203 
204         /**
205          * Gets the port portion of the URL��not include the port.
206          *
207          * @param env NAPI environment parameters.
208          */
209         napi_value GetPort(napi_env env) const;
210 
211         /**
212          * Judge whether it's on or off.
213          *
214          * @param env NAPI environment parameters.
215          */
216         napi_value GetOnOrOff(napi_env env) const;
217 
218         /**
219          * Judge whether it's Ipv6.
220          *
221          * @param env NAPI environment parameters.
222          */
223         napi_value GetIsIpv6(napi_env env) const;
224 
225         /**
226          * Gets the host name portion of the URL��not include the port.
227          *
228          * @param env NAPI environment parameters.
229          */
230         napi_value GetHost(napi_env env) const;
231 
232         /**
233          * The destructor of the url
234          */
~URL()235         virtual ~URL() {}
236 
237     private:
238         UrlData urlData_;
239         std::bitset<static_cast<size_t>(BitsetStatusFlag::BIT_STATUS_11)> flags_;
240         // bitset<11>:Similar to bool array, each bit status represents the real-time status of current URL parsing
241     };
242 
243     class URLSearchParams {
244     public:
245         /**
246          * A parameterized constructor used to create an URLSearchParams instance.
247          */
URLSearchParams()248         explicit URLSearchParams() {}
249 
250         /**
251          * Virtual destructor of URLSearchParams
252          */
~URLSearchParams()253         virtual ~URLSearchParams() {}
254 
255         /**
256          * Returns a Boolean that indicates whether a parameter with the specified name exists.
257          *
258          * @param env NAPI environment parameters.
259          * @param name Specifies the name of a key-value pair.
260          */
261         napi_value IsHas(napi_env env, napi_value  name) const;
262 
263         /**
264          * Returns the first value associated to the given search parameter.
265          *
266          * @param env NAPI environment parameters.
267          * @param buffer Returns the first value associated to the given search parameter.
268          */
269         napi_value Get(napi_env env, napi_value buffer);
270 
271         /**
272          * Returns all key-value pairs associated with a given search parameter as an array.
273          *
274          * @param env NAPI environment parameters.
275          * @param buffer Specifies the name of a key value.
276          */
277         napi_value GetAll(napi_env env, napi_value buffer);
278 
279         /**
280          * Appends a specified key/value pair as a new search parameter.
281          *
282          * @param env NAPI environment parameters.
283          * @param buffer Key name of the search parameter to be inserted.
284          * @param temp Values of search parameters to be inserted.
285          */
286         void Append(napi_env env, napi_value buffer, napi_value temp);
287 
288         /**
289          * Deletes the given search parameter and its associated value,from the list of all search parameters.
290          *
291          * @param env NAPI environment parameters.
292          * @param buffer Name of the key-value pair to be deleted.
293          */
294         void Delete(napi_env env, napi_value buffer);
295 
296         /**
297          * Returns an ES6 iterator. Each item of the iterator is a JavaScript Array.
298          *
299          * @param env NAPI environment parameters.
300          */
301         napi_value Entries(napi_env env) const;
302 
303         /**
304          * Sets the value associated with a given search parameter to the
305          * given value. If there were several matching values, this method
306          * deletes the others. If the search parameter doesn't exist, this
307          * method creates it.
308          *
309          * @param env NAPI environment parameters.
310          * @param name Key name of the parameter to be set.
311          * @param value Indicates the parameter value to be set.
312          */
313         void Set(napi_env env, napi_value name, napi_value value);
314 
315         /**
316          * Sort all key/value pairs contained in this object in place and return undefined.
317          */
318         void Sort();
319 
320         /**
321          * Returns an iterator allowing to go through all keys contained in this object.
322          *
323          * @param env NAPI environment parameters.
324          */
325         napi_value IterByKeys(napi_env env);
326 
327         /**
328          * Returns an iterator allowing to go through all values contained in this object.
329          *
330          * @param env NAPI environment parameters.
331          */
332         napi_value IterByValues(napi_env env);
333 
334         /**
335          * Sets the string array of searchParams.
336          *
337          * @param env NAPI environment parameters.
338          * @param input String array.
339          */
340         void SetArray(napi_env env, std::vector<std::string> input);
341 
342         /**
343          * Gets the string array of searchParams.
344          *
345          * @param env NAPI environment parameters.
346          */
347         napi_value GetArray(napi_env env) const;
348 
349         /**
350          * This function will decode the string and put the parsed key and value values into the
351          * vector container of urlsearchparams class according to the rules.
352          *
353          * @param env NAPI environment parameters.
354          * @param Stringpar The input parameter of urlsearchparams is string.
355          */
356         std::vector<std::string> StringParmas(napi_env env, std::string Stringpar);
357 
358     private:
359         std::string ToUSVString(std::string inputStr);
360         void HandleIllegalChar(std::wstring& inputStr, std::wstring::const_iterator it);
361         std::vector<std::string> searchParams;
362     };
363 } // namespace OHOS::Url
364 #endif // URL_JS_URL_H_
365