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