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