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 ToString(napi_env env,napi_callback_info info)663 static napi_value ToString(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->ToString(env); 672 return result; 673 } 674 IterByKeys(napi_env env,napi_callback_info info)675 static napi_value IterByKeys(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->IterByKeys(env); 684 return result; 685 } 686 IterByValues(napi_env env,napi_callback_info info)687 static napi_value IterByValues(napi_env env, napi_callback_info info) 688 { 689 napi_value thisVar = nullptr; 690 size_t argc = 0; 691 napi_value args = nullptr; 692 napi_get_cb_info(env, info, &argc, &args, &thisVar, nullptr); 693 URLSearchParams *object = nullptr; 694 napi_unwrap(env, thisVar, reinterpret_cast<void**>(&object)); 695 napi_value result = object->IterByValues(env); 696 return result; 697 } 698 IsPlusSign(size_t & strLastPos,const size_t & iteaor,std::string & buf,std::string & stringParm)699 static void IsPlusSign(size_t &strLastPos, const size_t &iteaor, std::string &buf, std::string &stringParm) 700 { 701 if (strLastPos < iteaor) { 702 buf += stringParm.substr(strLastPos, iteaor - strLastPos); 703 } 704 buf += ""; 705 strLastPos = iteaor + 1; 706 return; 707 } IsEqualSign(size_t & strLastPos,const size_t & iteaor,std::string & buf,std::string & stringParm,std::vector<std::string> & seachParasVec)708 static void IsEqualSign(size_t &strLastPos, const size_t &iteaor, 709 std::string &buf, std::string &stringParm, std::vector<std::string> &seachParasVec) 710 { 711 if (strLastPos < iteaor) { 712 buf += stringParm.substr(strLastPos, iteaor - strLastPos); 713 } 714 seachParasVec.push_back(buf); 715 buf = ""; 716 strLastPos = iteaor + 1; 717 return; 718 } 719 IsAddressSign(const size_t & strLastPos,const size_t & iteaor,std::string & buf,std::string & stringParm,std::vector<std::string> & seachParasVec)720 static void IsAddressSign(const size_t &strLastPos, const size_t &iteaor, std::string &buf, 721 std::string &stringParm, std::vector<std::string> &seachParasVec) 722 { 723 if (strLastPos < iteaor) { 724 buf += stringParm.substr(strLastPos, iteaor - strLastPos); 725 } 726 seachParasVec.push_back(buf); 727 return; 728 } DealParmsString(const size_t & strLastPos,const size_t & iteaor,std::string & buf,std::string & stringParm,std::vector<std::string> & seachParasVec)729 static void DealParmsString(const size_t &strLastPos, const size_t &iteaor, std::string &buf, 730 std::string &stringParm, std::vector<std::string> &seachParasVec) 731 { 732 if (strLastPos < iteaor) { 733 buf += stringParm.substr(strLastPos, iteaor - strLastPos); 734 } 735 seachParasVec.push_back(buf); 736 } IsEqualCode(size_t & strStartPos,const size_t & iteaor,size_t & strLastPos)737 static void IsEqualCode(size_t &strStartPos, const size_t &iteaor, size_t &strLastPos) 738 { 739 if (strStartPos == iteaor) { 740 strLastPos = iteaor + 1; 741 strStartPos = iteaor + 1; 742 } 743 return; 744 } StringParsing(std::string stringParm)745 static std::vector<std::string> StringParsing(std::string stringParm) 746 { 747 std::vector<std::string> seachParasVec; 748 size_t strStartPos = 0; 749 size_t strLastPos = 0; 750 bool isHasSpace = false; 751 std::string buf = ""; 752 size_t iteaor = 0; 753 for (iteaor = 0; iteaor < stringParm.length(); iteaor++) { 754 char code = stringParm[iteaor]; 755 switch (code) { 756 case '&': 757 { 758 IsEqualCode(strStartPos, iteaor, strLastPos); 759 IsAddressSign(strLastPos, iteaor, buf, stringParm, seachParasVec); 760 if (!isHasSpace) { 761 seachParasVec.push_back(""); 762 } 763 isHasSpace = false; 764 buf = ""; 765 strLastPos = iteaor + 1; 766 strStartPos = iteaor + 1; 767 break; 768 } 769 case '=': 770 { 771 if (isHasSpace) { 772 break; 773 } 774 IsEqualSign(strLastPos, iteaor, buf, stringParm, seachParasVec); 775 isHasSpace = true; 776 break; 777 } 778 case '+': 779 IsPlusSign(strLastPos, iteaor, buf, stringParm); 780 break; 781 default:break; 782 } 783 } 784 if (strStartPos == iteaor) { 785 return seachParasVec; 786 } 787 DealParmsString(strLastPos, iteaor, buf, stringParm, seachParasVec); 788 if (!isHasSpace) { 789 seachParasVec.push_back(""); 790 } 791 return seachParasVec; 792 } 793 StringParmas(napi_env env,napi_callback_info info)794 static napi_value StringParmas(napi_env env, napi_callback_info info) 795 { 796 napi_value thisVar = nullptr; 797 napi_value argv[1] = {0}; 798 size_t argc = 1; 799 std::string input = ""; 800 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); 801 size_t typelen = 0; 802 if (napi_get_value_string_utf8(env, argv[0], nullptr, 0, &typelen) != napi_ok) { 803 HILOG_ERROR("can not get argv[0] size"); 804 return nullptr; 805 } 806 input.resize(typelen); 807 if (napi_get_value_string_utf8(env, argv[0], input.data(), typelen + 1, &typelen) != napi_ok) { 808 HILOG_ERROR("can not get argv[0] value"); 809 return nullptr; 810 } 811 std::vector<std::string> seachParasmsString; 812 seachParasmsString = StringParsing(input); 813 napi_value arr = nullptr; 814 napi_create_array(env, &arr); 815 for (size_t i = 0; i < seachParasmsString.size(); i++) { 816 napi_value result = nullptr; 817 napi_create_string_utf8(env, seachParasmsString[i].c_str(), seachParasmsString[i].size(), &result); 818 napi_set_element(env, arr, i, result); 819 } 820 return arr; 821 } 822 SeachParamsInit(napi_env env,napi_value exports)823 static napi_value SeachParamsInit(napi_env env, napi_value exports) 824 { 825 const char *seachParamsClassName = "URLSearchParams"; 826 napi_value seachParamsInitClass = nullptr; 827 napi_property_descriptor UrlDesc[] = { 828 DECLARE_NAPI_FUNCTION("has", IsHas), 829 DECLARE_NAPI_FUNCTION("set", Set), 830 DECLARE_NAPI_FUNCTION("sort", Sort), 831 DECLARE_NAPI_FUNCTION("toString", ToString), 832 DECLARE_NAPI_FUNCTION("keys", IterByKeys), 833 DECLARE_NAPI_FUNCTION("values", IterByValues), 834 DECLARE_NAPI_FUNCTION("get", Get), 835 DECLARE_NAPI_FUNCTION("getAll", GetAll), 836 DECLARE_NAPI_FUNCTION("append", Append), 837 DECLARE_NAPI_FUNCTION("delete", Delete), 838 DECLARE_NAPI_FUNCTION("entries", Entries), 839 DECLARE_NAPI_GETTER_SETTER("array", GetArray, SetArray), 840 }; 841 NAPI_CALL(env, napi_define_class(env, seachParamsClassName, strlen(seachParamsClassName), 842 SeachParamsConstructor, nullptr, sizeof(UrlDesc) / sizeof(UrlDesc[0]), 843 UrlDesc, &seachParamsInitClass)); 844 napi_property_descriptor desc[] = { 845 DECLARE_NAPI_PROPERTY("URLSearchParams1", seachParamsInitClass) 846 }; 847 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); 848 return exports; 849 }; 850 ParamsInit(napi_env env,napi_value exports)851 static napi_value ParamsInit(napi_env env, napi_value exports) 852 { 853 const char *paramsClassName = "URLSearchParams"; 854 napi_value ParamsInitClass = nullptr; 855 napi_property_descriptor UrlDesc[] = { 856 DECLARE_NAPI_FUNCTION("has", IsHas), 857 DECLARE_NAPI_FUNCTION("set", Set), 858 DECLARE_NAPI_FUNCTION("sort", Sort), 859 DECLARE_NAPI_FUNCTION("toString", ToString), 860 DECLARE_NAPI_FUNCTION("keys", IterByKeys), 861 DECLARE_NAPI_FUNCTION("values", IterByValues), 862 DECLARE_NAPI_FUNCTION("get", Get), 863 DECLARE_NAPI_FUNCTION("getAll", GetAll), 864 DECLARE_NAPI_FUNCTION("append", Append), 865 DECLARE_NAPI_FUNCTION("delete", Delete), 866 DECLARE_NAPI_FUNCTION("entries", Entries), 867 DECLARE_NAPI_GETTER_SETTER("array", GetArray, SetArray), 868 }; 869 NAPI_CALL(env, napi_define_class(env, paramsClassName, strlen(paramsClassName), 870 SeachParamsConstructor, nullptr, sizeof(UrlDesc) / sizeof(UrlDesc[0]), 871 UrlDesc, &ParamsInitClass)); 872 napi_property_descriptor desc[] = { 873 DECLARE_NAPI_PROPERTY("URLParams1", ParamsInitClass) 874 }; 875 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); 876 return exports; 877 }; 878 UrlInit(napi_env env,napi_value exports)879 static napi_value UrlInit(napi_env env, napi_value exports) 880 { 881 const char *urlClassName = "Url"; 882 napi_value urlClass = nullptr; 883 napi_property_descriptor UrlDesc[] = { 884 DECLARE_NAPI_GETTER_SETTER("hostname", GetHostname, SetHostname), 885 DECLARE_NAPI_FUNCTION("href", SetHref), 886 DECLARE_NAPI_GETTER_SETTER("search", GetSearch, SetSearch), 887 DECLARE_NAPI_GETTER_SETTER("username", GetUsername, SetUsername), 888 DECLARE_NAPI_GETTER_SETTER("password", GetPassword, SetPassword), 889 DECLARE_NAPI_GETTER_SETTER("host", GetUrlHost, SetUrlHost), 890 DECLARE_NAPI_GETTER_SETTER("hash", GetUrlFragment, SetUrlFragment), 891 DECLARE_NAPI_GETTER_SETTER("protocol", GetUrlScheme, SetUrlScheme), 892 DECLARE_NAPI_GETTER_SETTER("pathname", GetUrlPath, SetUrlPath), 893 DECLARE_NAPI_GETTER_SETTER("port", GetUrlPort, SetUrlPort), 894 DECLARE_NAPI_GETTER("onOrOff", GetOnOrOff), 895 DECLARE_NAPI_GETTER("GetIsIpv6", GetIsIpv6), 896 }; 897 NAPI_CALL(env, napi_define_class(env, urlClassName, strlen(urlClassName), UrlConstructor, 898 nullptr, sizeof(UrlDesc) / sizeof(UrlDesc[0]), UrlDesc, &urlClass)); 899 napi_property_descriptor desc[] = { 900 DECLARE_NAPI_PROPERTY("Url", urlClass) 901 }; 902 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); 903 return exports; 904 } 905 Init(napi_env env,napi_value exports)906 static napi_value Init(napi_env env, napi_value exports) 907 { 908 napi_property_descriptor desc[] = { 909 DECLARE_NAPI_FUNCTION("stringParmas", StringParmas), 910 }; 911 NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc)); 912 SeachParamsInit(env, exports); 913 ParamsInit(env, exports); 914 UrlInit(env, exports); 915 return exports; 916 } 917 918 919 static napi_module UrlModule = { 920 .nm_version = 1, 921 .nm_flags = 0, 922 .nm_filename = nullptr, 923 .nm_register_func = Init, 924 .nm_modname = "url", 925 .nm_priv = reinterpret_cast<void*>(0), 926 .reserved = {0}, 927 }; RegisterModule()928 extern "C" __attribute__((constructor)) void RegisterModule() 929 { 930 napi_module_register(&UrlModule); 931 } 932 extern "C" NAPI_url_GetJSCode(const char ** buf,int * bufLen)933 __attribute__((visibility("default"))) void NAPI_url_GetJSCode(const char **buf, int *bufLen) 934 { 935 if (buf != nullptr) { 936 *buf = _binary_js_url_js_start; 937 } 938 if (bufLen != nullptr) { 939 *bufLen = _binary_js_url_js_end - _binary_js_url_js_start; 940 } 941 } 942 extern "C" NAPI_url_GetABCCode(const char ** buf,int * buflen)943 __attribute__((visibility("default"))) void NAPI_url_GetABCCode(const char** buf, int* buflen) 944 { 945 if (buf != nullptr) { 946 *buf = _binary_url_abc_start; 947 } 948 if (buflen != nullptr) { 949 *buflen = _binary_url_abc_end - _binary_url_abc_start; 950 } 951 } 952 } // namespace 953