• 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 #include "napi/native_api.h"
17 #include "napi/native_node_api.h"
18 #include "js_url.h"
19 #include "securec.h"
20 #include "utils/log.h"
21 
22 extern const char _binary_js_url_js_start[];
23 extern const char _binary_js_url_js_end[];
24 extern const char _binary_url_abc_start[];
25 extern const char _binary_url_abc_end[];
26 namespace OHOS::Url {
UrlStructor(napi_env & env,napi_callback_info & info,URL * & object)27     static void UrlStructor(napi_env &env, napi_callback_info &info, URL *&object)
28     {
29         napi_value thisVar = nullptr;
30         size_t argc = 2; // 2:The number of parameters is 2
31         napi_value argv[2] = { 0 }; // 2:The number of parameters is 2
32         void *data = nullptr;
33         napi_get_cb_info(env, info, &argc, nullptr, &thisVar, &data);
34         napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
35         napi_valuetype valuetype1 = napi_null;
36         napi_valuetype valuetype2 = napi_null;
37         napi_typeof(env, argv[0], &valuetype1);
38         if (valuetype1 == napi_string) {
39             std::string temp, tempType = "";
40             size_t tempSize, tempTypeSize = 0;
41             if (napi_get_value_string_utf8(env, argv[0], nullptr, 0, &tempSize) != napi_ok) {
42                 HILOG_ERROR("can not get argv[0] size");
43                 return;
44             }
45             temp.reserve(tempSize);
46             temp.resize(tempSize);
47             if (napi_get_value_string_utf8(env, argv[0], temp.data(), tempSize + 1, &tempSize) != napi_ok) {
48                 HILOG_ERROR("can not get argv[0] value");
49                 return;
50             }
51             std::string input = temp;
52             napi_typeof(env, argv[1], &valuetype2);
53             if (valuetype2 == napi_string) {
54                 if (napi_get_value_string_utf8(env, argv[1], nullptr, 0, &tempTypeSize) != napi_ok) {
55                     HILOG_ERROR("can not get argv[1] size");
56                     return;
57                 }
58                 tempType.reserve(tempTypeSize);
59                 tempType.resize(tempTypeSize);
60                 if (napi_get_value_string_utf8(env, argv[1], tempType.data(),
61                                                tempTypeSize + 1, &tempTypeSize) != napi_ok) {
62                     HILOG_ERROR("can not get argv[1] value");
63                     return;
64                 }
65                 std::string base = tempType;
66                 object = new URL(input, base);
67             } else if (valuetype2 == napi_object) {
68                 URL *tempUrl = nullptr;
69                 napi_unwrap(env, argv[1], reinterpret_cast<void**>(&tempUrl));
70                 object = new URL(input, *tempUrl);
71             } else {
72                 HILOG_INFO("secondParameter error");
73             }
74         } else {
75             HILOG_INFO("firstParameter error");
76         }
77         return;
78     }
79 
UrlConstructor(napi_env env,napi_callback_info info)80     static napi_value UrlConstructor(napi_env env, napi_callback_info info)
81     {
82         napi_value thisVar = nullptr;
83         void *data = nullptr;
84         size_t argc = 0;
85         napi_value argv[2] = { 0 }; // 2:The number of parameters is 2
86         URL *object = nullptr;
87         NAPI_CALL(env, napi_get_cb_info(env, info, &argc, nullptr, &thisVar, &data));
88         if (argc == 1) {
89             NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data));
90             napi_valuetype valuetype = napi_null;
91             NAPI_CALL(env, napi_typeof(env, argv[0], &valuetype));
92             if (valuetype == napi_string) {
93                 std::string type = "";
94                 size_t typeSize = 0;
95                 if (napi_get_value_string_utf8(env, argv[0], nullptr, 0, &typeSize) != napi_ok) {
96                     HILOG_ERROR("can not get argv[0] size");
97                     return nullptr;
98                 }
99                 type.reserve(typeSize);
100                 type.resize(typeSize);
101                 if (napi_get_value_string_utf8(env, argv[0], type.data(), typeSize + 1, &typeSize) != napi_ok) {
102                     HILOG_ERROR("can not get argv[0] value");
103                     return nullptr;
104                 }
105                 std::string input = type;
106                 object = new URL(input);
107             } else {
108                 HILOG_INFO("Parameter error");
109             }
110         } else if (argc == 2) { // 2:When the input parameter is set to 2
111             UrlStructor(env, info, object);
112         }
113         napi_wrap(
114             env, thisVar, object,
115             [](napi_env environment, void *data, void *hint) {
116                 auto obj = reinterpret_cast<URL*>(data);
117                 if (obj != nullptr) {
118                     delete obj;
119                 }
120             },
121             nullptr, nullptr);
122         return thisVar;
123     }
124 
GetHostname(napi_env env,napi_callback_info info)125     static napi_value GetHostname(napi_env env, napi_callback_info info)
126     {
127         napi_value thisVar = nullptr;
128         NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
129         URL *murl = nullptr;
130         NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&murl)));
131         napi_value retVal = murl->GetHostname(env);
132         return retVal;
133     }
134 
GetSearch(napi_env env,napi_callback_info info)135     static napi_value GetSearch(napi_env env, napi_callback_info info)
136     {
137         napi_value thisVar = nullptr;
138         NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
139         URL *murl = nullptr;
140         NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&murl)));
141         napi_value retVal = murl->GetSearch(env);
142         return retVal;
143     }
144 
GetUsername(napi_env env,napi_callback_info info)145     static napi_value GetUsername(napi_env env, napi_callback_info info)
146     {
147         napi_value thisVar = nullptr;
148         NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
149         URL *murl = nullptr;
150         NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&murl)));
151         napi_value retVal = murl->GetUsername(env);
152         return retVal;
153     }
154 
GetPassword(napi_env env,napi_callback_info info)155     static napi_value GetPassword(napi_env env, napi_callback_info info)
156     {
157         napi_value thisVar = nullptr;
158         NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
159         URL *murl = nullptr;
160         NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&murl)));
161         napi_value retVal = murl->GetPassword(env);
162         return retVal;
163     }
164 
GetUrlFragment(napi_env env,napi_callback_info info)165     static napi_value GetUrlFragment(napi_env env, napi_callback_info info)
166     {
167         napi_value thisVar = nullptr;
168         NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
169         URL *murl = nullptr;
170         NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&murl)));
171         napi_value retVal = murl->GetFragment(env);
172         return retVal;
173     }
174 
GetUrlScheme(napi_env env,napi_callback_info info)175     static napi_value GetUrlScheme(napi_env env, napi_callback_info info)
176     {
177         napi_value thisVar = nullptr;
178         NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
179         URL *murl = nullptr;
180         NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&murl)));
181         napi_value retVal = murl->GetScheme(env);
182         return retVal;
183     }
184 
GetUrlPort(napi_env env,napi_callback_info info)185     static napi_value GetUrlPort(napi_env env, napi_callback_info info)
186     {
187         napi_value thisVar = nullptr;
188         NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
189         URL *murl = nullptr;
190         NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&murl)));
191         napi_value retVal = murl->GetPort(env);
192         return retVal;
193     }
194 
GetUrlHost(napi_env env,napi_callback_info info)195     static napi_value GetUrlHost(napi_env env, napi_callback_info info)
196     {
197         napi_value thisVar = nullptr;
198         NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
199         URL *murl = nullptr;
200         NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&murl)));
201         napi_value retVal = murl->GetHost(env);
202         return retVal;
203     }
204 
GetUrlPath(napi_env env,napi_callback_info info)205     static napi_value GetUrlPath(napi_env env, napi_callback_info info)
206     {
207         napi_value thisVar = nullptr;
208         NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
209         URL *murl = nullptr;
210         NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&murl)));
211         napi_value retVal = murl->GetPath(env);
212         return retVal;
213     }
214 
GetOnOrOff(napi_env env,napi_callback_info info)215     static napi_value GetOnOrOff(napi_env env, napi_callback_info info)
216     {
217         napi_value thisVar = nullptr;
218         NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
219         URL *murl = nullptr;
220         NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&murl)));
221         napi_value retVal = murl->GetOnOrOff(env);
222         return retVal;
223     }
224 
GetIsIpv6(napi_env env,napi_callback_info info)225     static napi_value GetIsIpv6(napi_env env, napi_callback_info info)
226     {
227         napi_value thisVar = nullptr;
228         NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
229         URL *murl = nullptr;
230         NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&murl)));
231         napi_value retVal = murl->GetIsIpv6(env);
232         return retVal;
233     }
234 
SetHref(napi_env env,napi_callback_info info)235     static napi_value SetHref(napi_env env, napi_callback_info info)
236     {
237         napi_value thisVar = nullptr;
238         napi_value argv[1] = {0};
239         size_t argc = 1;
240         std::string input = "";
241         NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
242         size_t typelen = 0;
243         if (napi_get_value_string_utf8(env, argv[0], nullptr, 0, &typelen) != napi_ok) {
244             HILOG_ERROR("can not get argv[0] size");
245             return nullptr;
246         }
247         input.resize(typelen);
248         if (napi_get_value_string_utf8(env, argv[0], input.data(), typelen + 1, &typelen) != napi_ok) {
249             HILOG_ERROR("can not get argv[0] value");
250             return nullptr;
251         }
252         URL *murl = nullptr;
253         NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&murl)));
254         murl->SetHref(input);
255         napi_value result = nullptr;
256         NAPI_CALL(env, napi_get_undefined(env, &result));
257         return result;
258     }
259 
SetHostname(napi_env env,napi_callback_info info)260     static napi_value SetHostname(napi_env env, napi_callback_info info)
261     {
262         napi_value thisVar = nullptr;
263         napi_value argv[1] = {0};
264         size_t argc = 1;
265         std::string input = "";
266         NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
267         size_t typelen = 0;
268         if (napi_get_value_string_utf8(env, argv[0], nullptr, 0, &typelen) != napi_ok) {
269             HILOG_ERROR("can not get argv[0] size");
270             return nullptr;
271         }
272         input.resize(typelen);
273         if (napi_get_value_string_utf8(env, argv[0], input.data(), typelen + 1, &typelen) != napi_ok) {
274             HILOG_ERROR("can not get argv[0] value");
275             return nullptr;
276         }
277         URL *murl = nullptr;
278         NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&murl)));
279         murl->SetHostname(input);
280         napi_value result = nullptr;
281         NAPI_CALL(env, napi_get_undefined(env, &result));
282         return result;
283     }
284 
SetUrlPort(napi_env env,napi_callback_info info)285     static napi_value SetUrlPort(napi_env env, napi_callback_info info)
286     {
287         napi_value thisVar = nullptr;
288         napi_value argv[1] = {0};
289         size_t argc = 1;
290         std::string input = "";
291         NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
292         size_t typelen = 0;
293         if (napi_get_value_string_utf8(env, argv[0], nullptr, 0, &typelen) != napi_ok) {
294             HILOG_ERROR("can not get argv[0] size");
295             return nullptr;
296         }
297         input.resize(typelen);
298         if (napi_get_value_string_utf8(env, argv[0], input.data(), typelen + 1, &typelen) != napi_ok) {
299             HILOG_ERROR("can not get argv[0] value");
300             return nullptr;
301         }
302         URL *murl = nullptr;
303         NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&murl)));
304         murl->SetPort(input);
305         napi_value result = nullptr;
306         NAPI_CALL(env, napi_get_undefined(env, &result));
307         return result;
308     }
309 
SetUrlHost(napi_env env,napi_callback_info info)310     static napi_value SetUrlHost(napi_env env, napi_callback_info info)
311     {
312         napi_value thisVar = nullptr;
313         napi_value argv[1] = {0};
314         size_t argc = 1;
315         std::string input = "";
316         NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
317         size_t typelen = 0;
318         if (napi_get_value_string_utf8(env, argv[0], nullptr, 0, &typelen) != napi_ok) {
319             HILOG_ERROR("can not get argv[0] size");
320             return nullptr;
321         }
322         input.resize(typelen);
323         if (napi_get_value_string_utf8(env, argv[0], input.data(), typelen + 1, &typelen) != napi_ok) {
324             HILOG_ERROR("can not get argv[0] value");
325             return nullptr;
326         }
327         URL *murl = nullptr;
328         NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&murl)));
329         murl->SetHost(input);
330         napi_value result = nullptr;
331         NAPI_CALL(env, napi_get_undefined(env, &result));
332         return result;
333     }
334 
SetSearch(napi_env env,napi_callback_info info)335     static napi_value SetSearch(napi_env env, napi_callback_info info)
336     {
337         napi_value thisVar = nullptr;
338         napi_value argv[1] = {0};
339         size_t argc = 1;
340         std::string input = "";
341         NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
342         size_t typelen = 0;
343         if (napi_get_value_string_utf8(env, argv[0], nullptr, 0, &typelen) != napi_ok) {
344             HILOG_ERROR("can not get argv[0] size");
345             return nullptr;
346         }
347         input.resize(typelen);
348         if (napi_get_value_string_utf8(env, argv[0], input.data(), typelen + 1, &typelen) != napi_ok) {
349             HILOG_ERROR("can not get argv[0] value");
350             return nullptr;
351         }
352         URL *murl = nullptr;
353         NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&murl)));
354         murl->SetSearch(input);
355         napi_value result = nullptr;
356         NAPI_CALL(env, napi_get_undefined(env, &result));
357         return result;
358     }
359 
SetUrlScheme(napi_env env,napi_callback_info info)360     static napi_value SetUrlScheme(napi_env env, napi_callback_info info)
361     {
362         napi_value thisVar = nullptr;
363         napi_value argv[1] = {0};
364         size_t argc = 1;
365         std::string input = "";
366         NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
367         size_t typelen = 0;
368         if (napi_get_value_string_utf8(env, argv[0], nullptr, 0, &typelen) != napi_ok) {
369             HILOG_ERROR("can not get argv[0] size");
370             return nullptr;
371         }
372         input.resize(typelen);
373         if (napi_get_value_string_utf8(env, argv[0], input.data(), typelen + 1, &typelen) != napi_ok) {
374             HILOG_ERROR("can not get argv[0] value");
375             return nullptr;
376         }
377         URL *murl = nullptr;
378         NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&murl)));
379         murl->SetScheme(input);
380         napi_value result = nullptr;
381         NAPI_CALL(env, napi_get_undefined(env, &result));
382         return result;
383     }
384 
SetUrlFragment(napi_env env,napi_callback_info info)385     static napi_value SetUrlFragment(napi_env env, napi_callback_info info)
386     {
387         napi_value thisVar = nullptr;
388         napi_value argv[1] = {0};
389         size_t argc = 1;
390         std::string input = "";
391         NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
392         size_t typelen = 0;
393         if (napi_get_value_string_utf8(env, argv[0], nullptr, 0, &typelen) != napi_ok) {
394             HILOG_ERROR("can not get argv[0] size");
395             return nullptr;
396         }
397         input.resize(typelen);
398         if (napi_get_value_string_utf8(env, argv[0], input.data(), typelen + 1, &typelen) != napi_ok) {
399             HILOG_ERROR("can not get argv[0] value");
400             return nullptr;
401         }
402         URL *murl = nullptr;
403         NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&murl)));
404         murl->SetFragment(input);
405         napi_value result = nullptr;
406         NAPI_CALL(env, napi_get_undefined(env, &result));
407         return result;
408     }
409 
SetUsername(napi_env env,napi_callback_info info)410     static napi_value SetUsername(napi_env env, napi_callback_info info)
411     {
412         napi_value thisVar = nullptr;
413         napi_value argv[1] = {0};
414         size_t argc = 1;
415         std::string input = "";
416         NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
417         size_t typelen = 0;
418         if (napi_get_value_string_utf8(env, argv[0], nullptr, 0, &typelen) != napi_ok) {
419             HILOG_ERROR("can not get argv[0] size");
420             return nullptr;
421         }
422         input.resize(typelen);
423         if (napi_get_value_string_utf8(env, argv[0], input.data(), typelen + 1, &typelen) != napi_ok) {
424             HILOG_ERROR("can not get argv[0] value");
425             return nullptr;
426         }
427         URL *murl = nullptr;
428         NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&murl)));
429         murl->SetUsername(input);
430         napi_value result = nullptr;
431         NAPI_CALL(env, napi_get_undefined(env, &result));
432         return result;
433     }
434 
SetUrlPath(napi_env env,napi_callback_info info)435     static napi_value SetUrlPath(napi_env env, napi_callback_info info)
436     {
437         napi_value thisVar = nullptr;
438         napi_value argv[1] = {0};
439         size_t argc = 1;
440         std::string input = "";
441         NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
442         size_t typelen = 0;
443         if (napi_get_value_string_utf8(env, argv[0], nullptr, 0, &typelen) != napi_ok) {
444             HILOG_ERROR("can not get argv[0] size");
445             return nullptr;
446         }
447         input.resize(typelen);
448         if (napi_get_value_string_utf8(env, argv[0], input.data(), typelen + 1, &typelen) != napi_ok) {
449             HILOG_ERROR("can not get argv[0] value");
450             return nullptr;
451         }
452         URL *murl = nullptr;
453         NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&murl)));
454         murl->SetPath(input);
455         napi_value result = nullptr;
456         NAPI_CALL(env, napi_get_undefined(env, &result));
457         return result;
458     }
459 
SetPassword(napi_env env,napi_callback_info info)460     static napi_value SetPassword(napi_env env, napi_callback_info info)
461     {
462         napi_value thisVar = nullptr;
463         napi_value argv[1] = {0};
464         size_t argc = 1;
465         std::string input = "";
466         NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
467         size_t typelen = 0;
468         if (napi_get_value_string_utf8(env, argv[0], nullptr, 0, &typelen) != napi_ok) {
469             HILOG_ERROR("can not get argv[0] size");
470             return nullptr;
471         }
472         input.resize(typelen);
473         if (napi_get_value_string_utf8(env, argv[0], input.data(), typelen + 1, &typelen) != napi_ok) {
474             HILOG_ERROR("can not get argv[0] value");
475             return nullptr;
476         }
477         URL *murl = nullptr;
478         NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&murl)));
479         murl->SetPassword(input);
480         napi_value result = nullptr;
481         NAPI_CALL(env, napi_get_undefined(env, &result));
482         return result;
483     }
484 
SeachParamsConstructor(napi_env env,napi_callback_info info)485     static napi_value SeachParamsConstructor(napi_env env, napi_callback_info info)
486     {
487         napi_value thisVar = nullptr;
488         void *data = nullptr;
489         NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, &data));
490         auto object = new URLSearchParams();
491         napi_wrap(
492             env, thisVar, object,
493             [](napi_env environment, void *data, void *hint) {
494                 auto obj = reinterpret_cast<URLSearchParams*>(data);
495                 if (obj != nullptr) {
496                     delete obj;
497                 }
498             },
499             nullptr, nullptr);
500         return thisVar;
501     }
502 
SetArray(napi_env env,napi_callback_info info)503     static napi_value SetArray(napi_env env, napi_callback_info info)
504     {
505         napi_value thisVar = nullptr;
506         napi_value argv[1] = {0};
507         size_t argc = 1;
508         uint32_t length = 0;
509         napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
510         napi_get_array_length(env, argv[0], &length);
511         std::vector<std::string> vec;
512         size_t arraySize = 0;
513         napi_value napiStr = nullptr;
514         for (size_t i = 0; i < length; i++) {
515             napi_get_element(env, argv[0], i, &napiStr);
516             if (napi_get_value_string_utf8(env, napiStr, nullptr, 0, &arraySize) != napi_ok) {
517                 HILOG_ERROR("can not get napiStr size");
518                 return nullptr;
519             }
520             if (arraySize > 0) {
521                 std::string cstr = "";
522                 cstr.resize(arraySize);
523                 if (napi_get_value_string_utf8(env, napiStr, cstr.data(), arraySize + 1, &arraySize) != napi_ok) {
524                     HILOG_ERROR("can not get name value");
525                     return nullptr;
526                 }
527                 vec.push_back(cstr);
528             } else {
529                 vec.push_back("");
530             }
531         }
532         URLSearchParams *murl = nullptr;
533         NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&murl)));
534         murl->SetArray(env, vec);
535         napi_value result = nullptr;
536         NAPI_CALL(env, napi_get_undefined(env, &result));
537         return result;
538     }
539 
GetArray(napi_env env,napi_callback_info info)540     static napi_value GetArray(napi_env env, napi_callback_info info)
541     {
542         napi_value thisVar = nullptr;
543         NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
544         URLSearchParams *murl = nullptr;
545         NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&murl)));
546         napi_value retVal = murl->GetArray(env);
547         return retVal;
548     }
549 
Get(napi_env env,napi_callback_info info)550     static napi_value Get(napi_env env, napi_callback_info info)
551     {
552         napi_value thisVar = nullptr;
553         size_t argc = 1;
554         napi_value args = nullptr;
555         napi_get_cb_info(env, info, &argc, &args, &thisVar, nullptr);
556         if (argc != 1) {
557             HILOG_INFO("One arg needs to be specified");
558             return nullptr;
559         }
560         URLSearchParams *object = nullptr;
561         napi_unwrap(env, thisVar, reinterpret_cast<void**>(&object));
562         napi_value result = object->Get(env, args);
563         return result;
564     }
565 
GetAll(napi_env env,napi_callback_info info)566     static napi_value GetAll(napi_env env, napi_callback_info info)
567     {
568         napi_value thisVar = nullptr;
569         size_t argc = 1;
570         napi_value args = nullptr;
571         napi_get_cb_info(env, info, &argc, &args, &thisVar, nullptr);
572         if (argc != 1) {
573             HILOG_INFO("One arg needs to be specified");
574             return nullptr;
575         }
576         URLSearchParams *object = nullptr;
577         napi_unwrap(env, thisVar, reinterpret_cast<void**>(&object));
578         napi_value result = object->GetAll(env, args);
579         return result;
580     }
581 
Append(napi_env env,napi_callback_info info)582     static napi_value Append(napi_env env, napi_callback_info info)
583     {
584         napi_value thisVar = nullptr;
585         size_t argc = 2; // 2:The number of parameters is 2
586         napi_value args[2] = { 0 }; // 2:The number of parameters is 2
587         void *data = nullptr;
588         napi_get_cb_info(env, info, &argc, args, &thisVar, &data);
589         if (argc != 2) { // 2:If the input parameter is not set to 2,
590             HILOG_INFO("Two args needs to be specified");
591             return nullptr;
592         }
593         URLSearchParams *object = nullptr;
594         napi_unwrap(env, thisVar, reinterpret_cast<void**>(&object));
595         object->Append(env, args[0], args[1]);
596         return nullptr;
597     }
598 
Delete(napi_env env,napi_callback_info info)599     static napi_value Delete(napi_env env, napi_callback_info info)
600     {
601         napi_value thisVar = nullptr;
602         size_t argc = 1;
603         napi_value args = nullptr;
604         napi_get_cb_info(env, info, &argc, &args, &thisVar, nullptr);
605         if (argc != 1) {
606             HILOG_INFO("One arg needs to be specified");
607             return nullptr;
608         }
609         URLSearchParams *object = nullptr;
610         napi_unwrap(env, thisVar, reinterpret_cast<void**>(&object));
611         object->Delete(env, args);
612         return nullptr;
613     }
614 
Entries(napi_env env,napi_callback_info info)615     static napi_value Entries(napi_env env, napi_callback_info info)
616     {
617         napi_value thisVar = nullptr;
618         size_t argc = 0;
619         napi_value args = nullptr;
620         napi_get_cb_info(env, info, &argc, &args, &thisVar, nullptr);
621         URLSearchParams *object = nullptr;
622         napi_unwrap(env, thisVar, reinterpret_cast<void**>(&object));
623         napi_value result = object->Entries(env);
624         return result;
625     }
626 
IsHas(napi_env env,napi_callback_info info)627     static napi_value IsHas(napi_env env, napi_callback_info info)
628     {
629         napi_value thisVar = nullptr;
630         size_t argc = 1;
631         napi_value args = nullptr;
632         NAPI_CALL(env, napi_get_cb_info(env, info, &argc, &args, &thisVar, nullptr));
633         URLSearchParams *object = nullptr;
634         NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&object)));
635         napi_value result = object->IsHas(env, args);
636         return result;
637     }
638 
Set(napi_env env,napi_callback_info info)639     static napi_value Set(napi_env env, napi_callback_info info)
640     {
641         napi_value thisVar = nullptr;
642         size_t argc = 2; // 2:The number of parameters is 2
643         napi_value args[2] = { 0 }; // 2:The number of parameters is 2
644         napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr);
645         URLSearchParams *object = nullptr;
646         napi_unwrap(env, thisVar, reinterpret_cast<void**>(&object));
647         object->Set(env, args[0], args[1]);
648         return nullptr;
649     }
650 
Sort(napi_env env,napi_callback_info info)651     static napi_value Sort(napi_env env, napi_callback_info info)
652     {
653         napi_value thisVar = nullptr;
654         size_t argc = 0;
655         napi_value args = nullptr;
656         napi_get_cb_info(env, info, &argc, &args, &thisVar, nullptr);
657         URLSearchParams *object = nullptr;
658         napi_unwrap(env, thisVar, reinterpret_cast<void**>(&object));
659         object->Sort();
660         return nullptr;
661     }
662 
IterByKeys(napi_env env,napi_callback_info info)663     static napi_value IterByKeys(napi_env env, napi_callback_info info)
664     {
665         napi_value thisVar = nullptr;
666         size_t argc = 0;
667         napi_value args = nullptr;
668         napi_get_cb_info(env, info, &argc, &args, &thisVar, nullptr);
669         URLSearchParams *object = nullptr;
670         napi_unwrap(env, thisVar, reinterpret_cast<void**>(&object));
671         napi_value result = object->IterByKeys(env);
672         return result;
673     }
674 
IterByValues(napi_env env,napi_callback_info info)675     static napi_value IterByValues(napi_env env, napi_callback_info info)
676     {
677         napi_value thisVar = nullptr;
678         size_t argc = 0;
679         napi_value args = nullptr;
680         napi_get_cb_info(env, info, &argc, &args, &thisVar, nullptr);
681         URLSearchParams *object = nullptr;
682         napi_unwrap(env, thisVar, reinterpret_cast<void**>(&object));
683         napi_value result = object->IterByValues(env);
684         return result;
685     }
686 
IsPlusSign(size_t & strLastPos,const size_t & iteaor,std::string & buf,std::string & stringParm)687     static void IsPlusSign(size_t &strLastPos, const size_t &iteaor, std::string &buf, std::string &stringParm)
688     {
689         if (strLastPos < iteaor) {
690             buf += stringParm.substr(strLastPos, iteaor - strLastPos + 1);
691         } else {
692             buf += "";
693         }
694         strLastPos = iteaor + 1;
695         return;
696     }
697 
IsEqualSign(size_t & strLastPos,const size_t & iteaor,std::string & buf,std::string & stringParm,std::vector<std::string> & seachParasVec)698     static void IsEqualSign(size_t &strLastPos, const size_t &iteaor,
699         std::string &buf, std::string &stringParm, std::vector<std::string> &seachParasVec)
700     {
701         if (strLastPos < iteaor) {
702             buf += stringParm.substr(strLastPos, iteaor - strLastPos);
703         }
704         seachParasVec.push_back(buf);
705         buf = "";
706         strLastPos = iteaor + 1;
707         return;
708     }
709 
IsAddressSign(const size_t & strLastPos,const size_t & iteaor,std::string & buf,std::string & stringParm,std::vector<std::string> & seachParasVec)710     static void IsAddressSign(const size_t &strLastPos, const size_t &iteaor, std::string &buf,
711         std::string &stringParm, std::vector<std::string> &seachParasVec)
712     {
713         if (strLastPos < iteaor) {
714             buf += stringParm.substr(strLastPos, iteaor - strLastPos);
715         }
716         seachParasVec.push_back(buf);
717         return;
718     }
DealParmsString(const size_t & strLastPos,const size_t & iteaor,std::string & buf,std::string & stringParm,std::vector<std::string> & seachParasVec)719     static void DealParmsString(const size_t &strLastPos, const size_t &iteaor, std::string &buf,
720         std::string &stringParm, std::vector<std::string> &seachParasVec)
721     {
722         if (strLastPos < iteaor) {
723             buf += stringParm.substr(strLastPos, iteaor - strLastPos);
724         }
725         seachParasVec.push_back(buf);
726     }
IsEqualCode(size_t & strStartPos,const size_t & iteaor,size_t & strLastPos)727     static void IsEqualCode(size_t &strStartPos, const size_t &iteaor, size_t &strLastPos)
728     {
729         if (strStartPos == iteaor) {
730             strLastPos = iteaor + 1;
731             strStartPos = iteaor + 1;
732         }
733         return;
734     }
StringParsing(std::string stringParm)735     static std::vector<std::string> StringParsing(std::string stringParm)
736     {
737         std::vector<std::string> seachParasVec;
738         size_t strStartPos = 0;
739         size_t strLastPos = 0;
740         bool isHasSpace = false;
741         std::string buf = "";
742         size_t iteaor = 0;
743         for (iteaor = 0; iteaor < stringParm.length(); iteaor++) {
744             char code = stringParm[iteaor];
745             switch (code) {
746                 case '&':
747                     {
748                         IsEqualCode(strStartPos, iteaor, strLastPos);
749                         IsAddressSign(strLastPos, iteaor, buf, stringParm, seachParasVec);
750                         if (!isHasSpace) {
751                             seachParasVec.push_back("");
752                         }
753                         isHasSpace = false;
754                         buf = "";
755                         strLastPos = iteaor + 1;
756                         strStartPos = iteaor + 1;
757                         break;
758                     }
759                 case '=':
760                     {
761                         if (isHasSpace) {
762                             break;
763                         }
764                         IsEqualSign(strLastPos, iteaor, buf, stringParm, seachParasVec);
765                         isHasSpace = true;
766                         break;
767                     }
768                 case '+':
769                     IsPlusSign(strLastPos, iteaor, buf, stringParm);
770                     break;
771                 default:break;
772             }
773         }
774         if (strStartPos == iteaor) {
775             return seachParasVec;
776         }
777         DealParmsString(strLastPos, iteaor, buf, stringParm, seachParasVec);
778         if (!isHasSpace) {
779             seachParasVec.push_back("");
780         }
781         return seachParasVec;
782     }
783 
StringParmas(napi_env env,napi_callback_info info)784     static napi_value StringParmas(napi_env env, napi_callback_info info)
785     {
786         napi_value thisVar = nullptr;
787         napi_value argv[1] = {0};
788         size_t argc = 1;
789         std::string input = "";
790         napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
791         size_t typelen = 0;
792         if (napi_get_value_string_utf8(env, argv[0], nullptr, 0, &typelen) != napi_ok) {
793             HILOG_ERROR("can not get argv[0] size");
794             return nullptr;
795         }
796         input.resize(typelen);
797         if (napi_get_value_string_utf8(env, argv[0], input.data(), typelen + 1, &typelen) != napi_ok) {
798             HILOG_ERROR("can not get argv[0] value");
799             return nullptr;
800         }
801         std::vector<std::string> seachParasmsString;
802         seachParasmsString = StringParsing(input);
803         napi_value arr = nullptr;
804         napi_create_array(env, &arr);
805         for (size_t i = 0; i < seachParasmsString.size(); i++) {
806             napi_value result = nullptr;
807             napi_create_string_utf8(env, seachParasmsString[i].c_str(), seachParasmsString[i].size(), &result);
808             napi_set_element(env, arr, i, result);
809         }
810         return arr;
811     }
812 
SeachParamsInit(napi_env env,napi_value exports)813     static napi_value SeachParamsInit(napi_env env, napi_value exports)
814     {
815         const char *seachParamsClassName = "URLSearchParams";
816         napi_value seachParamsInitClass = nullptr;
817         napi_property_descriptor UrlDesc[] = {
818             DECLARE_NAPI_FUNCTION("has", IsHas),
819             DECLARE_NAPI_FUNCTION("set", Set),
820             DECLARE_NAPI_FUNCTION("sort", Sort),
821             DECLARE_NAPI_FUNCTION("keys", IterByKeys),
822             DECLARE_NAPI_FUNCTION("values", IterByValues),
823             DECLARE_NAPI_FUNCTION("get", Get),
824             DECLARE_NAPI_FUNCTION("getAll", GetAll),
825             DECLARE_NAPI_FUNCTION("append", Append),
826             DECLARE_NAPI_FUNCTION("delete", Delete),
827             DECLARE_NAPI_FUNCTION("entries", Entries),
828             DECLARE_NAPI_GETTER_SETTER("array", GetArray, SetArray),
829         };
830         NAPI_CALL(env, napi_define_class(env, seachParamsClassName, strlen(seachParamsClassName),
831             SeachParamsConstructor, nullptr, sizeof(UrlDesc) / sizeof(UrlDesc[0]),
832             UrlDesc, &seachParamsInitClass));
833         napi_property_descriptor desc[] = {
834             DECLARE_NAPI_PROPERTY("URLSearchParams1", seachParamsInitClass)
835         };
836         napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
837         return exports;
838     };
839 
ParamsInit(napi_env env,napi_value exports)840     static napi_value ParamsInit(napi_env env, napi_value exports)
841     {
842         const char *paramsClassName = "URLSearchParams";
843         napi_value ParamsInitClass = nullptr;
844         napi_property_descriptor UrlDesc[] = {
845             DECLARE_NAPI_FUNCTION("has", IsHas),
846             DECLARE_NAPI_FUNCTION("set", Set),
847             DECLARE_NAPI_FUNCTION("sort", Sort),
848             DECLARE_NAPI_FUNCTION("keys", IterByKeys),
849             DECLARE_NAPI_FUNCTION("values", IterByValues),
850             DECLARE_NAPI_FUNCTION("get", Get),
851             DECLARE_NAPI_FUNCTION("getAll", GetAll),
852             DECLARE_NAPI_FUNCTION("append", Append),
853             DECLARE_NAPI_FUNCTION("delete", Delete),
854             DECLARE_NAPI_FUNCTION("entries", Entries),
855             DECLARE_NAPI_GETTER_SETTER("array", GetArray, SetArray),
856         };
857         NAPI_CALL(env, napi_define_class(env, paramsClassName, strlen(paramsClassName),
858             SeachParamsConstructor, nullptr, sizeof(UrlDesc) / sizeof(UrlDesc[0]),
859             UrlDesc, &ParamsInitClass));
860         napi_property_descriptor desc[] = {
861             DECLARE_NAPI_PROPERTY("URLParams1", ParamsInitClass)
862         };
863         napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
864         return exports;
865     };
866 
UrlInit(napi_env env,napi_value exports)867     static napi_value UrlInit(napi_env env, napi_value exports)
868     {
869         const char *urlClassName = "Url";
870         napi_value urlClass = nullptr;
871         napi_property_descriptor UrlDesc[] = {
872             DECLARE_NAPI_GETTER_SETTER("hostname", GetHostname, SetHostname),
873             DECLARE_NAPI_FUNCTION("href", SetHref),
874             DECLARE_NAPI_GETTER_SETTER("search", GetSearch, SetSearch),
875             DECLARE_NAPI_GETTER_SETTER("username", GetUsername, SetUsername),
876             DECLARE_NAPI_GETTER_SETTER("password", GetPassword, SetPassword),
877             DECLARE_NAPI_GETTER_SETTER("host", GetUrlHost, SetUrlHost),
878             DECLARE_NAPI_GETTER_SETTER("hash", GetUrlFragment, SetUrlFragment),
879             DECLARE_NAPI_GETTER_SETTER("protocol", GetUrlScheme, SetUrlScheme),
880             DECLARE_NAPI_GETTER_SETTER("pathname", GetUrlPath, SetUrlPath),
881             DECLARE_NAPI_GETTER_SETTER("port", GetUrlPort, SetUrlPort),
882             DECLARE_NAPI_GETTER("onOrOff", GetOnOrOff),
883             DECLARE_NAPI_GETTER("GetIsIpv6", GetIsIpv6),
884         };
885         NAPI_CALL(env, napi_define_class(env, urlClassName, strlen(urlClassName), UrlConstructor,
886                                          nullptr, sizeof(UrlDesc) / sizeof(UrlDesc[0]), UrlDesc, &urlClass));
887         napi_property_descriptor desc[] = {
888             DECLARE_NAPI_PROPERTY("Url", urlClass)
889         };
890         napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
891         return exports;
892     }
893 
Init(napi_env env,napi_value exports)894     static napi_value Init(napi_env env, napi_value exports)
895     {
896         napi_property_descriptor desc[] = {
897             DECLARE_NAPI_FUNCTION("stringParmas", StringParmas),
898         };
899         NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
900         SeachParamsInit(env, exports);
901         ParamsInit(env, exports);
902         UrlInit(env, exports);
903         return exports;
904     }
905 
906 
907     static napi_module UrlModule = {
908         .nm_version = 1,
909         .nm_flags = 0,
910         .nm_filename = nullptr,
911         .nm_register_func = Init,
912         .nm_modname = "url",
913         .nm_priv = reinterpret_cast<void*>(0),
914         .reserved = {0},
915     };
RegisterModule()916     extern "C" __attribute__((constructor)) void RegisterModule()
917     {
918         napi_module_register(&UrlModule);
919     }
920     extern "C"
NAPI_url_GetJSCode(const char ** buf,int * bufLen)921     __attribute__((visibility("default"))) void NAPI_url_GetJSCode(const char **buf, int *bufLen)
922     {
923         if (buf != nullptr) {
924             *buf = _binary_js_url_js_start;
925         }
926         if (bufLen != nullptr) {
927             *bufLen = _binary_js_url_js_end - _binary_js_url_js_start;
928         }
929     }
930     extern "C"
NAPI_url_GetABCCode(const char ** buf,int * buflen)931     __attribute__((visibility("default"))) void NAPI_url_GetABCCode(const char** buf, int* buflen)
932     {
933         if (buf != nullptr) {
934             *buf = _binary_url_abc_start;
935         }
936         if (buflen != nullptr) {
937             *buflen = _binary_url_abc_end - _binary_url_abc_start;
938         }
939     }
940 } // namespace
941