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