From 49fcc6a3b872e4b916fc40b990257edf9f520152 Mon Sep 17 00:00:00 2001 From: l00619703 <1036802353.com> Date: Thu, 2 Sep 2021 17:51:32 +0800 Subject: [PATCH] commit url code Signed-off-by: l00619703 <1036802353.com> --- LICENSE | 177 ----- ohos.build | 2 +- README.en.md => readme.en.md | 6 +- README.md => readme.md | 4 +- url/BUILD.gn | 0 url/js_url.cpp | 1242 ++++++++++++++++++---------------- url/js_url.h | 71 +- url/js_url.js | 34 +- url/native_module_url.cpp | 281 ++++---- 9 files changed, 876 insertions(+), 941 deletions(-) delete mode 100644 LICENSE mode change 100644 => 100755 ohos.build rename README.en.md => readme.en.md (95%) mode change 100644 => 100755 rename README.md => readme.md (96%) mode change 100644 => 100755 mode change 100644 => 100755 url/BUILD.gn mode change 100644 => 100755 url/js_url.cpp mode change 100644 => 100755 url/js_url.h mode change 100644 => 100755 url/js_url.js mode change 100644 => 100755 url/native_module_url.cpp diff --git a/LICENSE b/LICENSE deleted file mode 100644 index 4a459866..00000000 --- a/LICENSE +++ /dev/null @@ -1,177 +0,0 @@ - - Apache License - Version 2.0, January 2004 - http://www.apache.org/licenses/ - - TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION - - 1. Definitions. - - "License" shall mean the terms and conditions for use, reproduction, - and distribution as defined by Sections 1 through 9 of this document. - - "Licensor" shall mean the copyright owner or entity authorized by - the copyright owner that is granting the License. - - "Legal Entity" shall mean the union of the acting entity and all - other entities that control, are controlled by, or are under common - control with that entity. For the purposes of this definition, - "control" means (i) the power, direct or indirect, to cause the - direction or management of such entity, whether by contract or - otherwise, or (ii) ownership of fifty percent (50%) or more of the - outstanding shares, or (iii) beneficial ownership of such entity. - - "You" (or "Your") shall mean an individual or Legal Entity - exercising permissions granted by this License. - - "Source" form shall mean the preferred form for making modifications, - including but not limited to software source code, documentation - source, and configuration files. - - "Object" form shall mean any form resulting from mechanical - transformation or translation of a Source form, including but - not limited to compiled object code, generated documentation, - and conversions to other media types. - - "Work" shall mean the work of authorship, whether in Source or - Object form, made available under the License, as indicated by a - copyright notice that is included in or attached to the work - (an example is provided in the Appendix below). - - "Derivative Works" shall mean any work, whether in Source or Object - form, that is based on (or derived from) the Work and for which the - editorial revisions, annotations, elaborations, or other modifications - represent, as a whole, an original work of authorship. For the purposes - of this License, Derivative Works shall not include works that remain - separable from, or merely link (or bind by name) to the interfaces of, - the Work and Derivative Works thereof. - - "Contribution" shall mean any work of authorship, including - the original version of the Work and any modifications or additions - to that Work or Derivative Works thereof, that is intentionally - submitted to Licensor for inclusion in the Work by the copyright owner - or by an individual or Legal Entity authorized to submit on behalf of - the copyright owner. For the purposes of this definition, "submitted" - means any form of electronic, verbal, or written communication sent - to the Licensor or its representatives, including but not limited to - communication on electronic mailing lists, source code control systems, - and issue tracking systems that are managed by, or on behalf of, the - Licensor for the purpose of discussing and improving the Work, but - excluding communication that is conspicuously marked or otherwise - designated in writing by the copyright owner as "Not a Contribution." - - "Contributor" shall mean Licensor and any individual or Legal Entity - on behalf of whom a Contribution has been received by Licensor and - subsequently incorporated within the Work. - - 2. Grant of Copyright License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - copyright license to reproduce, prepare Derivative Works of, - publicly display, publicly perform, sublicense, and distribute the - Work and such Derivative Works in Source or Object form. - - 3. Grant of Patent License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - (except as stated in this section) patent license to make, have made, - use, offer to sell, sell, import, and otherwise transfer the Work, - where such license applies only to those patent claims licensable - by such Contributor that are necessarily infringed by their - Contribution(s) alone or by combination of their Contribution(s) - with the Work to which such Contribution(s) was submitted. If You - institute patent litigation against any entity (including a - cross-claim or counterclaim in a lawsuit) alleging that the Work - or a Contribution incorporated within the Work constitutes direct - or contributory patent infringement, then any patent licenses - granted to You under this License for that Work shall terminate - as of the date such litigation is filed. - - 4. Redistribution. You may reproduce and distribute copies of the - Work or Derivative Works thereof in any medium, with or without - modifications, and in Source or Object form, provided that You - meet the following conditions: - - (a) You must give any other recipients of the Work or - Derivative Works a copy of this License; and - - (b) You must cause any modified files to carry prominent notices - stating that You changed the files; and - - (c) You must retain, in the Source form of any Derivative Works - that You distribute, all copyright, patent, trademark, and - attribution notices from the Source form of the Work, - excluding those notices that do not pertain to any part of - the Derivative Works; and - - (d) If the Work includes a "NOTICE" text file as part of its - distribution, then any Derivative Works that You distribute must - include a readable copy of the attribution notices contained - within such NOTICE file, excluding those notices that do not - pertain to any part of the Derivative Works, in at least one - of the following places: within a NOTICE text file distributed - as part of the Derivative Works; within the Source form or - documentation, if provided along with the Derivative Works; or, - within a display generated by the Derivative Works, if and - wherever such third-party notices normally appear. The contents - of the NOTICE file are for informational purposes only and - do not modify the License. You may add Your own attribution - notices within Derivative Works that You distribute, alongside - or as an addendum to the NOTICE text from the Work, provided - that such additional attribution notices cannot be construed - as modifying the License. - - You may add Your own copyright statement to Your modifications and - may provide additional or different license terms and conditions - for use, reproduction, or distribution of Your modifications, or - for any such Derivative Works as a whole, provided Your use, - reproduction, and distribution of the Work otherwise complies with - the conditions stated in this License. - - 5. Submission of Contributions. Unless You explicitly state otherwise, - any Contribution intentionally submitted for inclusion in the Work - by You to the Licensor shall be under the terms and conditions of - this License, without any additional terms or conditions. - Notwithstanding the above, nothing herein shall supersede or modify - the terms of any separate license agreement you may have executed - with Licensor regarding such Contributions. - - 6. Trademarks. This License does not grant permission to use the trade - names, trademarks, service marks, or product names of the Licensor, - except as required for reasonable and customary use in describing the - origin of the Work and reproducing the content of the NOTICE file. - - 7. Disclaimer of Warranty. Unless required by applicable law or - agreed to in writing, Licensor provides the Work (and each - Contributor provides its Contributions) on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or - implied, including, without limitation, any warranties or conditions - of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A - PARTICULAR PURPOSE. You are solely responsible for determining the - appropriateness of using or redistributing the Work and assume any - risks associated with Your exercise of permissions under this License. - - 8. Limitation of Liability. In no event and under no legal theory, - whether in tort (including negligence), contract, or otherwise, - unless required by applicable law (such as deliberate and grossly - negligent acts) or agreed to in writing, shall any Contributor be - liable to You for damages, including any direct, indirect, special, - incidental, or consequential damages of any character arising as a - result of this License or out of the use or inability to use the - Work (including but not limited to damages for loss of goodwill, - work stoppage, computer failure or malfunction, or any and all - other commercial damages or losses), even if such Contributor - has been advised of the possibility of such damages. - - 9. Accepting Warranty or Additional Liability. While redistributing - the Work or Derivative Works thereof, You may choose to offer, - and charge a fee for, acceptance of support, warranty, indemnity, - or other liability obligations and/or rights consistent with this - License. However, in accepting such obligations, You may act only - on Your own behalf and on Your sole responsibility, not on behalf - of any other Contributor, and only if You agree to indemnify, - defend, and hold each Contributor harmless for any liability - incurred by, or claims asserted against, such Contributor by reason - of your accepting any such warranty or additional liability. - - END OF TERMS AND CONDITIONS \ No newline at end of file diff --git a/ohos.build b/ohos.build old mode 100644 new mode 100755 index ef9210f0..d27627fd --- a/ohos.build +++ b/ohos.build @@ -1,7 +1,7 @@ { "subsystem": "ccruntime", "parts": { - "jsapi_api": { + "jsapi_url": { "variants": [ "wearable", "phone" diff --git a/README.en.md b/readme.en.md old mode 100644 new mode 100755 similarity index 95% rename from README.en.md rename to readme.en.md index 0d7b8f4b..795c75ae --- a/README.en.md +++ b/readme.en.md @@ -1,5 +1,5 @@ -# js_api_module -# Introduction to URL +#### js_api_module +#### 1st. Introduction to URL The URL interface is used to parse, construct, normalize, and encode URLs. The URL constructor creates a new URL object. In order to make changes to the resolved components of the URL or to the URL. Interface introduction @@ -36,7 +36,7 @@ const url = new URL("https://developer.mozilla.org/en-US/docs/Web/API/URL/toStri url.toJSON(); // => 'https://developer.mozilla.org/en-US/docs/Web/API/URL/toString' -####2nd. Introduction to URLSreachParams +#### 2nd. Introduction to URLSreachParams The URLSearchParams interface defines some practical methods to process URL query strings. diff --git a/README.md b/readme.md old mode 100644 new mode 100755 similarity index 96% rename from README.md rename to readme.md index 7d449a87..d64122b9 --- a/README.md +++ b/readme.md @@ -1,5 +1,5 @@ #### js_api_module -####一、URL简介 +#### 一、URL简介 URL接口用于解析,构造,规范化和编码 URLs。 URL的构造函数创建新的URL对象。 以便对URL的已解析组成部分或对URL进行更改。 接口介绍 @@ -36,7 +36,7 @@ const url = new URL("https://developer.mozilla.org/en-US/docs/Web/API/URL/toStri url.toJSON(); // => 'https://developer.mozilla.org/en-US/docs/Web/API/URL/toString' -####二、URLSreachParams简介 +#### 二、URLSreachParams简介 URLSearchParams 接口定义了一些实用的方法来处理 URL 的查询字符串。 diff --git a/url/BUILD.gn b/url/BUILD.gn old mode 100644 new mode 100755 diff --git a/url/js_url.cpp b/url/js_url.cpp old mode 100644 new mode 100755 index d92e43a1..7988910c --- a/url/js_url.cpp +++ b/url/js_url.cpp @@ -13,42 +13,50 @@ * limitations under the License. */ #include "js_url.h" -#include "utils/log.h" -#include "securec.h" +#include +#include #include +#include "securec.h" +#include "utils/log.h" -std::map g_head = { {"ftp:", 21}, {"file:", -1}, {"gopher:", 70}, {"http:", 80}, - {"https:", 443}, {"ws:", 80}, {"wss:", 443} }; +static std::map g_head = { + {"ftp:", 21}, {"file:", -1}, {"gopher:", 70}, {"http:", 80}, { + "https:", 443}, {"ws:", 80}, {"wss:", 443} +}; -std::vector g_doubleSegment = { "..", ".%2e", ".%2E", "%2e.", "%2E.", - "%2e%2e", "%2E%2E", "%2e%2E", "%2E%2e" }; +static std::vector g_doubleSegment = { "..", ".%2e", ".%2E", "%2e.", "%2E.", + "%2e%2e", "%2E%2E", "%2e%2E", "%2E%2e" +}; -std::vector g_singlesegment = { ".", "%2e", "%2E" }; +static std::vector g_singlesegment = { ".", "%2e", "%2E" }; -std::vector g_specialcharacter = { '\0', '\t', '\n', '\r', ' ', '#', '%', '/', ':', '?', '@', '[', '\\', ']' }; +static std::vector g_specialcharacter = { '\0', '\t', '\n', '\r', ' ', '#', '%', '/', ':', '?', + '@', '[', '\\', ']' +}; -#define MAXPORT 65535 +static const int MAXPORT = 65535; // 65535:Maximum port number -void ReplaceSpecialSymbols(std::string& input,std::string& oldstr,std::string& newstr) +static const size_t STEP_SZIE = 2; // 2:Searching for the number and number of keys and values +static void ReplaceSpecialSymbols(std::string& input, std::string& oldstr, std::string& newstr) { size_t oldlen = oldstr.size(); - while(true) { - size_t pos = 0; - if((pos = input.find(oldstr)) != std::string::npos) { - input.replace(pos,oldlen,newstr); + while (true) { + size_t pos = 0; + if ((pos = input.find(oldstr)) != std::string::npos) { + input.replace(pos, oldlen, newstr); } else { break; } } } -template +template bool IsASCIITabOrNewline(const T ch) { return (ch == '\t' || ch == '\n' || ch == '\r'); } -template +template bool IsHexDigit(const T ch) { if (isdigit(ch) || (ch >= 'A' && ch <= 'F') || (ch >= 'a' && ch <= 'f')) { @@ -57,22 +65,21 @@ bool IsHexDigit(const T ch) return false; } -template -unsigned Hex2bin(const T ch) +static unsigned AsciiToHex(const unsigned char pram) { - if (ch >= '0' && ch <= '9') { - return ch - '0'; + if (pram >= '0' && pram <= '9') { + return pram - '0'; } - if (ch >= 'A' && ch <= 'F') { - return 10 + (ch - 'A'); + if (pram >= 'A' && pram <= 'F') { + return pram - 'A' + 10; // 10:Convert to hexadecimal } - if (ch >= 'a' && ch <= 'f') { - return 10 + (ch - 'a'); + if (pram >= 'a' && pram <= 'f') { + return pram - 'a' + 10; // 10:Convert to hexadecimal } return static_cast(-1); } -std::string DecodePercent(const char* input, size_t len) +static std::string DecodePercent(const char* input, size_t len) { std::string temp; if (len == 0) { @@ -84,22 +91,23 @@ std::string DecodePercent(const char* input, size_t len) while (it < end) { const char ch = it[0]; size_t left = end - it - 1; - if (ch != '%' || left < 2 || (ch == '%' && (!IsHexDigit(it[1]) || !IsHexDigit(it[2])))) { + if (ch != '%' || left < 2 || (ch == '%' && (!IsHexDigit(it[1]) || // 2:The length is less than 2 + !IsHexDigit(it[2])))) { // 2:The number of characters is less than 2 temp += ch; it++; continue; } else { - unsigned a = Hex2bin(it[1]); - unsigned b = Hex2bin(it[2]); - char c = static_cast(a * 16 + b); // 16:Convert hex - temp += c; - it += 3; + unsigned first = AsciiToHex(it[1]); + unsigned second = AsciiToHex(it[2]); // 2:Subscript 2 + char pram = static_cast(first * 16 + second); // 16:Convert hex + temp += pram; + it += 3; // 3:Move the pointer to the right by 3 digits. } } return temp; } -void DeleteC0OrSpace(std::string& str) +static void DeleteC0OrSpace(std::string& str) { if (str.empty()) { return; @@ -124,9 +132,9 @@ void DeleteC0OrSpace(std::string& str) } } -void DeleteTabOrNewline(std::string& str1) +static void DeleteTabOrNewline(std::string& str1) { - for (auto item = str1.begin(); item != str1.end(); ) { + for (auto item = str1.begin(); item != str1.end();) { if (IsASCIITabOrNewline(*item)) { item = str1.erase(item); } else { @@ -135,7 +143,7 @@ void DeleteTabOrNewline(std::string& str1) } } -bool IsSpecial(std::string scheme) +static bool IsSpecial(std::string scheme) { auto temp = g_head.count(scheme); if (temp > 0) { @@ -144,10 +152,10 @@ bool IsSpecial(std::string scheme) return false; } -bool AnalysisScheme(std::string& input, std::string& scheme, std::bitset<11>& flags) +static bool AnalysisScheme(std::string& input, std::string& scheme, std::bitset& flags) { if (!isalpha(input[0])) { - flags.set(0); // 0:Bit 0 Set to true,The URL analysis failed + flags.set(BIT0); return false; } else { size_t strlen = input.size(); @@ -157,32 +165,31 @@ bool AnalysisScheme(std::string& input, std::string& scheme, std::bitset<11>& fl input[i] = tolower(input[i]); } } else { - flags.set(0); // 0:Bit 0 Set to true,The URL analysis failed + flags.set(BIT0); // 0:Bit 0 Set to true,The URL analysis failed return false; } } scheme = input; if (IsSpecial(scheme)) { - flags.set(1); // 1:Bit 1 Set to true,The protocol is the default protocol + flags.set(BIT1); } return true; } } -void AnalysisFragment(std::string& input, std::string& fragment, std::bitset<11>& flags) +static void AnalysisFragment(std::string& input, std::string& fragment, std::bitset& flags) { fragment = input; - flags.set(8); // 8:Bit 8 Set to true,The URL has fragment + flags.set(BIT8); } -void AnalysisQuery(std::string& input, std::string& query, std::bitset<11>& flags) +static void AnalysisQuery(std::string& input, std::string& query, std::bitset& flags) { query = input; - flags.set(7); // 7:Bit 7 Set to true,The URL has query + flags.set(BIT7); } - -void AnalysisUsernameAndPasswd(std::string& input, std::string& username, std::string& password, - std::bitset<11>& flags) +static void AnalysisUsernameAndPasswd(std::string& input, std::string& username, std::string& password, + std::bitset& flags) { int pos = input.size() - 1; for (; pos >= 0; pos--) { @@ -190,7 +197,7 @@ void AnalysisUsernameAndPasswd(std::string& input, std::string& username, std::s break; } } - std::string userAndPasswd = input.substr(0, pos); +std::string userAndPasswd = input.substr(0, pos); input = input.substr(pos + 1); if (userAndPasswd.empty()) { return; @@ -209,22 +216,23 @@ void AnalysisUsernameAndPasswd(std::string& input, std::string& username, std::s if (userAndPasswd.find(':') != std::string::npos) { size_t i = userAndPasswd.find(':'); std::string user = userAndPasswd.substr(0, i); - std::string passwd = userAndPasswd.substr(i + 1); + std::string keyWord = userAndPasswd.substr(i + 1); if (!user.empty()) { username = user; - flags.set(2); // 2:Bit 2 Set to true,The URL has username + flags.set(BIT2); } - if (!passwd.empty()) { - password = passwd; - flags.set(3); // 3:Bit 3 Set to true,The URL has password + if (!keyWord.empty()) { + password = keyWord; + flags.set(BIT3); } } else { username = userAndPasswd; - flags.set(2); // 2:Bit 2 Set to true,The URL has username + flags.set(BIT2); } } -void AnalysisPath(std::string& input, std::vector& path, std::bitset<11>& flags, bool isSpecial) +static void AnalysisPath(std::string& input, std::vector& path, + std::bitset& flags, bool isSpecial) { std::vector temp; size_t pos = 0; @@ -240,86 +248,110 @@ void AnalysisPath(std::string& input, std::vector& path, std::bitse if (path.empty()) { if (it == temp.size() - 1) { path.emplace_back(""); - flags.set(6); // 6:Bit 6 Set to true,The URL has pathname + flags.set(BIT6); } continue; } path.pop_back(); if (it == temp.size() - 1) { path.emplace_back(""); - flags.set(6); // 6:Bit 6 Set to true,The URL has pathname + flags.set(BIT6); } continue; } result = find(g_singlesegment.begin(), g_singlesegment.end(), temp[it]); if (result != g_singlesegment.end() && it == temp.size() - 1) { path.emplace_back(""); - flags.set(6); // 6:Bit 6 Set to true,The URL has pathname + flags.set(BIT6); continue; } if (result == g_singlesegment.end()) { path.push_back(temp[it]); - flags.set(6); // 6:Bit 6 Set to true,The URL has pathname + flags.set(BIT6); } } } -void AnalysisPort(std::string input, url_data& urlinfo, std::bitset<11>& flags) +static void AnalysisPort(std::string input, UrlData& urlinfo, std::bitset& flags) { for (auto i : input) { if (!isdigit(i)) { - flags.set(0); // 0:Bit 0 Set to true,The URL analysis failed + flags.set(BIT0); return; } } int it = stoi(input); if (it > MAXPORT) { - flags.set(0); // 0:Bit 0 Set to true,The URL analysis failed + flags.set(BIT0); return; } - flags.set(5);// 5:Bit 5 Set to true,The URL Port is the specially + flags.set(BIT5); for (auto i : g_head) { if (i.first == urlinfo.scheme && i.second == it) { urlinfo.port = -1; - flags.set(5, 0); // 5:Bit 5 Set to false,The URL Port is the default + flags.set(BIT5, 0); return; } } urlinfo.port = it; } -void AnalysisOpaqueHost(std::string input, std::string& host, std::bitset<11>& flags) +static void AnalysisOpaqueHost(std::string input, std::string& host, std::bitset& flags) { size_t strlen = input.size(); for (size_t i = 0; i < strlen; ++i) { char ch = input[i]; auto result = find(g_specialcharacter.begin(), g_specialcharacter.end(), ch); if (ch != '%' && (result != g_specialcharacter.end())) { - flags.set(0); // 0:Bit 0 Set to true,The URL analysis failed + flags.set(BIT0); return; } } host = input; - flags.set(4); // 4:Bit 4 Set to true,The URL has hostname + flags.set(BIT4); } -std::string IPv6HostCompress(std::vector& tempIPV6, int flag) +static std::string IPv6ZeroComperess(std::vector& tempIPV6, std::string& input, + int maxZeroIndex, int max) { - std::string input; - if (flag == 1) { - size_t strlen = tempIPV6.size(); + for (int i = 0; i < maxZeroIndex; ++i) { + input += tempIPV6[i]; + if (i != maxZeroIndex - 1) { + input += ":"; + } + } + input += "::"; + size_t strlen = tempIPV6.size(); + for (size_t i = maxZeroIndex + max; i < strlen; ++i) { + input += tempIPV6[i]; + if (i != strlen - 1) { + input += ":"; + } + } + return input; +} + +static std::string IPv6NoComperess(std::vector& tempIPV6, std::string& input) +{ + size_t strlen = tempIPV6.size(); for (size_t i = 0; i < strlen; ++i) { if (tempIPV6[i][0] == '?' && tempIPV6[i].size() == 1) { input += ":"; - } - else { + } else { input += tempIPV6[i]; if (i != tempIPV6.size() - 1) { input += ":"; } } } - return input; + return input; +} + +static std::string IPv6HostCompress(std::vector& tempIPV6, int flag) +{ + std::string input; + if (flag == 1) { + return IPv6NoComperess(tempIPV6, input); } else { int max = 0; int count = 0; @@ -332,14 +364,16 @@ std::string IPv6HostCompress(std::vector& tempIPV6, int flag) i++; count++; } - max < count ? max = count, maxZeroIndex = index : 0; - } - else { + if (max < count) { + max = count; + maxZeroIndex = index; + } + } else { count = 0; i++; } } - if (count == 8) { // 8:If IPv6 is all 0 + if (count == 8) { // 8:If IPv6 is all 0 return "::"; } else if (max == 0) { strlen = tempIPV6.size(); @@ -360,52 +394,36 @@ std::string IPv6HostCompress(std::vector& tempIPV6, int flag) input.pop_back(); return input; } else { - for (size_t i = 0; i < maxZeroIndex; ++i) { - input += tempIPV6[i]; - if (i != maxZeroIndex - 1) { - input += ":"; - } - } - input += "::"; - strlen = tempIPV6.size(); - for (size_t i = maxZeroIndex + max; i < strlen; ++i) { - input += tempIPV6[i]; - if (i != strlen - 1) { - input += ":"; - } - } - return input; + return IPv6ZeroComperess(tempIPV6, input, maxZeroIndex, max); } } } } -void IPv6Host(std::string& input, std::string& host, std::bitset<11>& flags) +static void IPv6Host(std::string& input, std::string& host, std::bitset& flags) { if (input.size() == 0) { - flags.set(0); // 0:Bit 0 Set to true,The URL analysis failed + flags.set(BIT0); return; } std::string strInput = input; std::stringstream ss; - std::string number_hex; + std::string numberHex; unsigned val = 0; - unsigned number_flag = 0; - std::vector temp; - std::vector temp_end; - std::vector IPV6; + unsigned numberFlag = 0; + std::vector temp; + std::vector tempEnd; + std::vector tempIpv6; size_t pos = 0; - int temp_prot[4] = { 0 }; - int n = 0; + int tempProt[4] = { 0 }; + int n = 0; int flag = 0; if ((pos = strInput.find("::", 0)) != std::string::npos) { flag = 1; - if (strInput.find("::", pos + 2) != std::string::npos) { - flags.set(0); // 0:Bit 0 Set to true,The URL analysis failed + if (strInput.find("::", pos + 2) != std::string::npos) { // 2:Subscript Move Right2 + flags.set(BIT0); return; } - } else { - flag = 0; } while (((pos = strInput.find(':')) != std::string::npos)) { temp.push_back(strInput.substr(0, pos)); @@ -413,146 +431,151 @@ void IPv6Host(std::string& input, std::string& host, std::bitset<11>& flags) } temp.push_back(strInput); if (temp.size() > 8) { // 8:The incoming value does not meet the criteria - flags.set(0); // 0:Bit 0 Set to true,The URL analysis failed + flags.set(BIT0); return; } for (size_t i = 0; i < temp.size(); ++i) { if (temp[i].empty()) { - IPV6.push_back("?"); + tempIpv6.push_back("?"); } else { size_t strlen = temp[i].size(); for (size_t j = 0; j < strlen; ++j) { if (((temp[i].find('.')) != std::string::npos)) { - number_flag = i; - if (temp.size() == i || temp.size() > 7) { - flags.set(0); // 0:Bit 0 Set to true,The URL analysis failed + numberFlag = i; + if (temp.size() == i || temp.size() > 7) { // 7:The size cannot be greater than 7 + flags.set(BIT0); return; } break; } else if (IsHexDigit(temp[i][j])) { - val = val * 0x10 + Hex2bin(temp[i][j]); + val = val * 0x10 + AsciiToHex(temp[i][j]); } } ss << std::hex << val; - ss >> number_hex; - IPV6.push_back(number_hex); + ss >> numberHex; + tempIpv6.push_back(numberHex); ss.clear(); - number_hex.clear(); + numberHex.clear(); val = 0; } } - if (number_flag != 0) { - while (((pos = temp[number_flag].find('.')) != std::string::npos)) { - temp_end.push_back(temp[number_flag].substr(0, pos)); - temp[number_flag] = temp[number_flag].substr(pos + 1); + if (numberFlag != 0) { + while (((pos = temp[numberFlag].find('.')) != std::string::npos)) { + tempEnd.push_back(temp[numberFlag].substr(0, pos)); + temp[numberFlag] = temp[numberFlag].substr(pos + 1); } - temp_end.push_back(temp[number_flag]); - if (temp_end.size() != 4) { - flags.set(0); // 0:Bit 0 Set to true,The URL analysis failed + tempEnd.push_back(temp[numberFlag]); + if (tempEnd.size() != 4) { // 4:The size is not 4 + flags.set(BIT0); return; } else { - size_t strlen = temp_end.size(); + size_t strlen = tempEnd.size(); for (size_t x = 0; x < strlen; ++x) { - val = stoi(temp_end[x]); - if (val > 255) { - flags.set(0); // 0:Bit 0 Set to true,The URL analysis failed + val = stoi(tempEnd[x]); + if (val > 255) { // 255:The maximum value is 255 + flags.set(BIT0); return; } - temp_prot[n] = val; + tempProt[n] = val; n++; val = 0; } } - ss << std::hex << temp_prot[0] * 0x100 + temp_prot[1]; - ss >> number_hex; - IPV6.push_back(number_hex); + ss << std::hex << tempProt[0] * 0x100 + tempProt[1]; + ss >> numberHex; + tempIpv6.push_back(numberHex); ss.clear(); - number_hex.clear(); - ss << std::hex << temp_prot[2] * 0x100 + temp_prot[3]; - ss >> number_hex; - IPV6.push_back(number_hex); + numberHex.clear(); + ss << std::hex << tempProt[2] * 0x100 + tempProt[3]; // 2: 3:subscript position + ss >> numberHex; + tempIpv6.push_back(numberHex); ss.clear(); - number_hex.clear(); - IPV6.erase(IPV6.end() - 3); + numberHex.clear(); + tempIpv6.erase(tempIpv6.end() - 3); // 3:Remove redundant space } - strInput = IPv6HostCompress(IPV6, flag); + strInput = IPv6HostCompress(tempIpv6, flag); host = '[' + strInput + ']'; - flags.set(4); // 4:Bit 4 Set to true,The URL has host - flags.set(10);// 10:Bit 10 Set to true,The host is IPV6 + flags.set(BIT4); + flags.set(BIT10); } -int64_t AnalyseNum(std::string parts) +static bool CheckNunType(const char ch, const unsigned num) { - enum NUMERATION - { + enum NUMERATION { OCT = 8, // 8:Octal DEC = 10, // 10:Decimal HEX = 16 // 16:Hexadecimal }; + if (NUMERATION(num) == OCT) { + if (ch < '0' || ch > '7') { // 0~7:Octal + return false; + } + } else if (NUMERATION(num) == DEC) { + if (ch < '0' || ch > '9') { // 0~9:Decimal + return false; + } + } else if (NUMERATION(num) == HEX) { + if (!((ch >= '0' && ch <= '9') || // 0~9, a~f, A~F:Hexadecimal + (ch >= 'A' && ch <= 'F') || + (ch >= 'a' && ch <= 'f'))) { + return false; + } + } + return true; +} + +static int64_t AnalyseNum(std::string parts) +{ unsigned num = 10; // 10:Decimal - std::string partsBeg = parts.substr(0, 2); // 2:Take two digits to determine whether it is hexadecimal + std::string partsBeg = parts.substr(0, 2); // 2:Take two digits to determine whether it is hexadecimal size_t partsLen = parts.length(); - if (partsLen >= 2 && (partsBeg == "0X" || partsBeg == "0x")) { - num = 16; + if (partsLen >= 2 && (partsBeg == "0X" || partsBeg == "0x")) { // 2:parts length + num = 16; // 16:Convert to hexadecimal parts = parts.substr(2); // 2:delete '0x' - } else if (num == 10 && partsLen > 1 && parts.substr(0, 1) == "0") { // 0:delete '0' - num = 8; + } else if (num == 10 && partsLen > 1 && parts.substr(0, 1) == "0") { // 10:Conversion to Decimal Coefficient + num = 8; // 8:Convert to octal parts = parts.substr(1); } for (size_t i = 0; i < parts.length(); i++) { - if (NUMERATION(num) == OCT) { - if (parts[i] < '0' || parts[i] > '7') { // 0~7:Octal - return -1; - } - } else if (NUMERATION(num) == DEC) { - if (parts[i] < '0' || parts[i] > '9') { // 0~9:Decimal - return -1; - } - } else if (NUMERATION(num) == HEX) { - if (!((parts[i] >= '0' && parts[i] <= '9') || // 0~9, a~f, A~F:Hexadecimal - (parts[i] >= 'A' && parts[i] <= 'F') || - (parts[i] >= 'a' && parts[i] <= 'f'))) { - return -1; - } + bool ret = CheckNunType(parts[i], num); + if (!ret) { + return -1; } } return strtoll(parts.c_str(), nullptr, num); } -bool OverHex(std::string input) +static bool OverHex(std::string input) { size_t size = input.size(); - for (size_t i = 0; i < size; i++) - { + for (size_t i = 0; i < size; i++) { return !IsHexDigit(input[i]); } return false; } -bool NotAllNum(std::string input) { - +static bool NotAllNum(std::string input) +{ size_t size = input.size(); - for (size_t i = 0; i < size; i++) - { - if (!isdigit(input[i])) - { + for (size_t i = 0; i < size; i++) { + if (!isdigit(input[i])) { return true; } } return false; } -bool AnalyseIPv4(const char* instr, size_t len, std::string& host, std::bitset<11>& flags) +static bool AnalyseIPv4(const char* instr, size_t len, std::string& host, std::bitset& flags) { int count = 0; for (const char* ptr = instr; *ptr != '\0'; ptr++) { if (*ptr == '.') { - if (++count > 3) { //3: The IPV4 address has only four segments + if (++count > 3) { // 3:The IPV4 address has only four segments return false; } } } - if (count != 3) { // 3:The IPV4 address has only four segments + if (count != 3) { // 3:The IPV4 address has only four segments return false; } @@ -566,57 +589,52 @@ bool AnalyseIPv4(const char* instr, size_t len, std::string& host, std::bitset<1 strVec.push_back(input); size_t size = strVec.size(); for (size_t i = 0; i < size; i++) { - if (strVec[i].empty()) - { + if (strVec[i].empty()) { return false; } - std::string begStr = strVec[i].substr(0, 2); // Intercept the first two characters - if ((begStr == "0x" || begStr == "0X") && OverHex(strVec[i].substr(2))) - { + std::string begStr = strVec[i].substr(0, 2); // 2:Intercept the first two characters + if ((begStr == "0x" || begStr == "0X") && OverHex(strVec[i].substr(2))) { // 2:Intercept return false; - } else if ((begStr == "0x" || begStr == "0X") && !(OverHex(strVec[i].substr(2)))) - { + } else if ((begStr == "0x" || begStr == "0X") && !(OverHex(strVec[i].substr(2)))) { // 2:Intercept continue; } - if (NotAllNum(strVec[i])) - { + if (NotAllNum(strVec[i])) { return false; } } for (size_t i = 0; i < size; i++) { int64_t value = AnalyseNum(strVec[i].c_str()); if ((value < 0) || (value > 255)) { // 255:Only handle numbers between 0 and 255 - flags.set(0); // 0:Bit 0 Set to true,The URL analysis failed + flags.set(BIT0); return false; } else { host += std::to_string(value); - if (i != size - 1) - { + if (i != size - 1) { host += "."; } } } - flags.set(4); // 4:Bit 4 Set to true,The URL has hostname + flags.set(BIT4); return true; } -void AnalysisHost(std::string& input, std::string& host, std::bitset<11>& flags, bool is_Specoal) +static void AnalysisHost(std::string& input, std::string& host, std::bitset& flags, bool special) { if (input.empty()) { - flags.set(0); // 0:Bit 0 Set to true,The URL analysis failed + flags.set(BIT0); return; } if (input[0] == '[') { if ((input[input.length() - 1]) == ']') { size_t b = input.length(); - input = input.substr(1, b - 2); + input = input.substr(1, b - 2); // 2:Truncating Strings IPv6Host(input, host, flags); return; } else { - flags.set(0); // 0:Bit 0 Set to true,The URL analysis failed + flags.set(BIT0); return; } } - if (!is_Specoal) { + if (!special) { AnalysisOpaqueHost(input, host, flags); return; } @@ -626,7 +644,7 @@ void AnalysisHost(std::string& input, std::string& host, std::bitset<11>& flags, char ch = decodeInput[pos]; auto result = find(g_specialcharacter.begin(), g_specialcharacter.end(), ch); if (result != g_specialcharacter.end()) { - flags.set(0); // 0:Bit 0 Set to true,The URL analysis failed + flags.set(BIT0); return; } } @@ -635,16 +653,16 @@ void AnalysisHost(std::string& input, std::string& host, std::bitset<11>& flags, return; } host = decodeInput; - flags.set(4); // 4:Bit 4 Set to true,The URL has hostname + flags.set(BIT4); } -bool ISFileNohost(const std::string& input) +static bool ISFileNohost(const std::string& input) { if ((isalpha(input[0]) && (input[1] == ':' || input[1] == '|'))) { return true; } return false; } -void AnalysisFilePath(std::string& input, url_data& urlinfo, std::bitset<11>& flags) +static void AnalysisFilePath(std::string& input, UrlData& urlinfo, std::bitset& flags) { std::vector temp; size_t pos = 0; @@ -659,7 +677,7 @@ void AnalysisFilePath(std::string& input, url_data& urlinfo, std::bitset<11>& fl if (a != g_doubleSegment.end()) { if ((urlinfo.path.size() == 1) && (isalpha(urlinfo.path[0][0]) && ((urlinfo.path[0][1] == ':') || (urlinfo.path[0][1] == '|')))) { - if (urlinfo.path[0].size() == 2) { + if (urlinfo.path[0].size() == 2) { // 2:The interception length is 2 urlinfo.path[0][1] = ':'; } } else if (!urlinfo.path.empty()) { @@ -678,23 +696,23 @@ void AnalysisFilePath(std::string& input, url_data& urlinfo, std::bitset<11>& fl continue; } urlinfo.path.push_back(temp[i]); - flags.set(6); // 6:Bit 6 Set to true,The URL has pathname + flags.set(BIT6); } std::string it = urlinfo.path[0]; if (isalpha(it[0]) && (it[1] == ':' || it[1] == '|')) { - if (it.size() == 2) { + if (it.size() == 2) { // 2:The length is 2 it[1] = ':'; - flags.set(4, 0); // 4:Bit 4 Set to false,The URL not have pathname + flags.set(BIT4, 0); urlinfo.host.clear(); } } } -void AnalysisFile(std::string& input, url_data& urlinfo, std::bitset<11>& flags) +static void AnalysisFile(std::string& input, UrlData& urlinfo, std::bitset& flags) { - bool is_Specoal = true; + bool special = true; if ((input[0] == '/' || input[0] == '\\') && (input[1] == '/' || input[1] == '\\')) { - std::string temp = input.substr(2); + std::string temp = input.substr(2); // 2:Intercept from 2 subscripts size_t pos = 0; if (((pos = temp.find('/')) != std::string::npos) || ((pos = temp.find('\\')) != std::string::npos)) { if (pos == 0) { @@ -703,10 +721,10 @@ void AnalysisFile(std::string& input, url_data& urlinfo, std::bitset<11>& flags) } else { std::string strHost = temp.substr(0, pos); std::string strPath = temp.substr(pos + 1); - + if (!ISFileNohost(strHost)) { - AnalysisHost(strHost, urlinfo.host, flags, is_Specoal); - if (flags.test(0)) { // 0:Bit 0 is true,The URL Parsing failed + AnalysisHost(strHost, urlinfo.host, flags, special); + if (flags.test(BIT0)) { return; } AnalysisFilePath(strPath, urlinfo, flags); @@ -716,8 +734,8 @@ void AnalysisFile(std::string& input, url_data& urlinfo, std::bitset<11>& flags) } } else { if (!temp.empty()) { - AnalysisHost(temp, urlinfo.host, flags, is_Specoal); - if (flags.test(0)) { // 0:Bit 0 is true,The URL Parsing failed + AnalysisHost(temp, urlinfo.host, flags, special); + if (flags.test(BIT0)) { return; } } @@ -730,156 +748,166 @@ void AnalysisFile(std::string& input, url_data& urlinfo, std::bitset<11>& flags) } } -void AnalysisFilescheme(std::string& input, url_data& urlinfo, std::bitset<11>& flags) +static void AnalysisFilescheme(std::string& input, UrlData& urlinfo, std::bitset& flags) { std::string strPath = urlinfo.scheme + input; urlinfo.scheme = "file:"; - flags.set(1); // 1:Bit 1 Set to true,The protocol is the default protocol + flags.set(BIT1); AnalysisFilePath(strPath, urlinfo, flags); } -void AnalysisHostAndPath(std::string& input, url_data& urlinfo, std::bitset<11>& flags) +static void AnalysisNoDefaultProtocol(std::string& input, UrlData& urlinfo, std::bitset& flags) { - if (flags.test(1)) { // 1:Bit 1 is true,The protocol is the default protocol - size_t pos = 0; - bool is_Special = true; - for (; pos < input.size();) { - if (input[pos] == '/' || input[pos] == '\\') { - pos++; - } else { - break; + if (urlinfo.scheme.size() == 2) { // 2:The length is 2 + AnalysisFilescheme(input, urlinfo, flags); + return; + } + if (input[0] == '/') { + if (input[1] == '/') { + std::string hostandpath = input.substr(2); // 2:Intercept from 2 subscripts + if (hostandpath.empty()) { + return; } - } - - input = input.substr(pos); - if (input.size() == 0) { - flags.set(0); // 0:Bit 0 Set to true,The URL analysis failed - return; - } else { - if (input.find('/') != std::string::npos || input.find('\\') != std::string::npos) { - for (pos = 0; pos < input.size(); pos++) { - if (input[pos] == '/' || input[pos] == '\\') { - break; - } - } - std::string strHost = input.substr(0, pos); - std::string strPath = input.substr(pos + 1); + size_t i = 0; + bool special = false; + if (hostandpath.find('/') != std::string::npos) { + i = hostandpath.find('/'); + std::string strHost = hostandpath.substr(0, i); + std::string strPath = hostandpath.substr(i + 1); if (strHost.find('@') != std::string::npos) { AnalysisUsernameAndPasswd(strHost, urlinfo.username, urlinfo.password, flags); } if (strHost.empty()) { - flags.set(0); // 0:Bit 0 Set to true,The URL analysis failed + flags.set(BIT0); return; } + size_t pos = 0; if (strHost[strHost.size() - 1] != ']') { if ((pos = strHost.find_last_of(':')) != std::string::npos) { std::string port = strHost.substr(pos + 1); strHost = strHost.substr(0, pos); AnalysisPort(port, urlinfo, flags); - if (flags.test(0)) { // 0:Bit 0 is true,The URL Parsing failed + if (flags.test(BIT0)) { return; } } } - AnalysisHost(strHost, urlinfo.host, flags, is_Special); - AnalysisPath(strPath, urlinfo.path, flags, is_Special); + AnalysisHost(strHost, urlinfo.host, flags, special); + AnalysisPath(strPath, urlinfo.path, flags, special); } else { - std::string strHost = input; + std::string strHost = hostandpath; if (strHost.find('@') != std::string::npos) { AnalysisUsernameAndPasswd(strHost, urlinfo.username, urlinfo.password, flags); } if (strHost.empty()) { - flags.set(0); // 0:Bit 0 Set to true,The URL analysis failed + flags.set(BIT0); return; } if (strHost[strHost.size() - 1] != ']') { - if ((pos = strHost.find_last_of(':')) != std::string::npos) { + if (size_t pos = (strHost.find_last_of(':')) != std::string::npos) { std::string port = strHost.substr(pos + 1); strHost = strHost.substr(0, pos); AnalysisPort(port, urlinfo, flags); - if (flags.test(0)) { // 0:Bit 0 is true,The URL Parsing failed + if (flags.test(BIT0)) { return; } } } - AnalysisHost(strHost, urlinfo.host, flags, is_Special); + AnalysisHost(strHost, urlinfo.host, flags, special); } + } else { + std::string strPath = input.substr(1); + AnalysisPath(strPath, urlinfo.path, flags, false); } } else { - if (urlinfo.scheme.size() == 2) { - AnalysisFilescheme(input, urlinfo, flags); - return; + flags.set(BIT9); + if (urlinfo.path.empty()) { + urlinfo.path.emplace_back(""); + } + urlinfo.path[0] = input; + flags.set(BIT6); + } +} + +static void AnalysisOnlyHost(std::string& input, UrlData& urlinfo, std::bitset& flags, size_t pos) +{ + std::string strHost = input; + if (strHost.find('@') != std::string::npos) { + AnalysisUsernameAndPasswd(strHost, urlinfo.username, urlinfo.password, flags); + } + if (strHost.empty()) { + flags.set(BIT0); + return; + } + if (strHost[strHost.size() - 1] != ']') { + if ((pos = strHost.find_last_of(':')) != std::string::npos) { + std::string port = strHost.substr(pos + 1); + strHost = strHost.substr(0, pos); + AnalysisPort(port, urlinfo, flags); + if (flags.test(BIT0)) { + return; + } + } + } + AnalysisHost(strHost, urlinfo.host, flags, true); +} + +static void AnalysisHostAndPath(std::string& input, UrlData& urlinfo, std::bitset& flags) +{ + if (flags.test(BIT1)) { + size_t pos = 0; + bool special = true; + size_t inputLen = input.size(); + for (; pos < inputLen;) { + if (input[pos] == '/' || input[pos] == '\\') { + pos++; + } else { + break; + } } - if (input[0] == '/') { - if (input[1] == '/') { - std::string hostandpath = input.substr(2); - if (hostandpath.empty()) { + input = input.substr(pos); + if (input.size() == 0) { + flags.set(BIT0); + return; + } else { + if (input.find('/') != std::string::npos || input.find('\\') != std::string::npos) { + for (pos = 0; pos < input.size(); pos++) { + if (input[pos] == '/' || input[pos] == '\\') { + break; + } + } + std::string strHost = input.substr(0, pos); + std::string strPath = input.substr(pos + 1); + if (strHost.find('@') != std::string::npos) { + AnalysisUsernameAndPasswd(strHost, urlinfo.username, urlinfo.password, flags); + } + if (strHost.empty()) { + flags.set(BIT0); return; } - size_t i = 0; - bool is_Special = false; - if (hostandpath.find('/') != std::string::npos) { - i = hostandpath.find('/'); - std::string strHost = hostandpath.substr(0, i); - std::string strPath = hostandpath.substr(i + 1); - if (strHost.find('@') != std::string::npos) { - AnalysisUsernameAndPasswd(strHost, urlinfo.username, urlinfo.password, flags); - } - if (strHost.empty()) { - flags.set(0); // 0:Bit 0 Set to true,The URL analysis failed - return; - } - size_t pos = 0; - if (strHost[strHost.size() - 1] != ']') { - if ((pos = strHost.find_last_of(':')) != std::string::npos) { - std::string port = strHost.substr(pos + 1); - strHost = strHost.substr(0, pos); - AnalysisPort(port, urlinfo, flags); - if (flags.test(0)) { // 0:Bit 0 is true,The URL Parsing failed - return; - } - } - } - AnalysisHost(strHost, urlinfo.host, flags, is_Special); - AnalysisPath(strPath, urlinfo.path, flags, is_Special); - } else { - std::string strHost = hostandpath; - if (strHost.find('@') != std::string::npos) { - AnalysisUsernameAndPasswd(strHost, urlinfo.username, urlinfo.password, flags); - } - if (strHost.empty()) { - flags.set(0); // 0:Bit 0 Set to true,The URL analysis failed - return; - } - if (strHost[strHost.size() - 1] != ']') { - if (size_t pos = (strHost.find_last_of(':')) != std::string::npos) { - std::string port = strHost.substr(pos + 1); - strHost = strHost.substr(0, pos); - AnalysisPort(port, urlinfo, flags); - if (flags.test(0)) { // 0:Bit 0 is true,The URL Parsing failed - return; - } + if (strHost[strHost.size() - 1] != ']') { + if ((pos = strHost.find_last_of(':')) != std::string::npos) { + std::string port = strHost.substr(pos + 1); + strHost = strHost.substr(0, pos); + AnalysisPort(port, urlinfo, flags); + if (flags.test(BIT0)) { + return; } } - AnalysisHost(strHost, urlinfo.host, flags, is_Special); } + AnalysisHost(strHost, urlinfo.host, flags, special); + AnalysisPath(strPath, urlinfo.path, flags, special); } else { - std::string strPath = input.substr(1); - bool is_Special = false; - AnalysisPath(strPath, urlinfo.path, flags, is_Special); + AnalysisOnlyHost(input, urlinfo, flags, pos); } - } else { - flags.set(9); // 6:Bit 6 Set to true,The URL Can not be base - if (urlinfo.path.empty()) { - urlinfo.path.emplace_back(""); - } - urlinfo.path[0] = input; - flags.set(6); // 6:Bit 6 Set to true,The URL has pathname } + } else { + AnalysisNoDefaultProtocol(input, urlinfo, flags); } } -void AnalysisInput(std::string& input, url_data& urlData, std::bitset<11>& flags, std::bitset<11>& baseflags) +static void AnalysisInput(std::string& input, UrlData& urlData, std::bitset& flags, + std::bitset& baseflags) { if (input.find('#') != std::string::npos) { size_t i = input.find('#'); @@ -893,33 +921,33 @@ void AnalysisInput(std::string& input, url_data& urlData, std::bitset<11>& flags AnalysisQuery(query, urlData.query, flags); input = input.substr(0, i); } - bool is_Special = (baseflags.test(1) ? true : false); // 1:Bit 1 is true,The URL Default for protocol - AnalysisPath(input, urlData.path, flags, is_Special); + bool special = (baseflags.test(BIT1) ? true : false); + AnalysisPath(input, urlData.path, flags, special); } -void BaseInfoToUrl(const url_data& baseInfo, const std::bitset<11>& baseflags, url_data& urlData, - std::bitset<11>& flags, bool inputIsEmpty) +static void BaseInfoToUrl(const UrlData& baseInfo, const std::bitset& baseflags, UrlData& urlData, + std::bitset& flags, bool inputIsEmpty) { urlData.scheme = baseInfo.scheme; - flags.set(1, baseflags.test(1)); // 1:Base flag to the current URL + flags.set(BIT1, baseflags.test(BIT1)); urlData.host = baseInfo.host; - flags.set(4); // 4:Bit 4 Set to true,The URL has hostname + flags.set(BIT4); urlData.username = baseInfo.username; - flags.set(2, baseflags.test(2)); // 2:Base flag to the current URL + flags.set(BIT2, baseflags.test(BIT2)); urlData.password = baseInfo.password; - flags.set(3, baseflags.test(3)); // 3:Base flag to the current URL + flags.set(BIT3, baseflags.test(BIT3)); urlData.port = baseInfo.port; - flags.set(5, baseflags.test(5)); // 5:Base flag to the current URL + flags.set(BIT5, baseflags.test(BIT5)); if (inputIsEmpty) { urlData.path = baseInfo.path; - flags.set(6, baseflags.test(6)); // 6:Base flag to the current URL + flags.set(BIT6, baseflags.test(BIT6)); urlData.query = baseInfo.query; - flags.set(7, baseflags.test(7)); // 7:Base flag to the current URL + flags.set(BIT7, baseflags.test(BIT7)); urlData.fragment = baseInfo.fragment; - flags.set(8, baseflags.test(8)); // 8:Base flag to the current URL + flags.set(BIT8, baseflags.test(BIT8)); } - flags.set(9,baseflags.test(9)); // 9:Base flag to the current URL - flags.set(10,baseflags.test(10)); // 10:Base flag to the current U + flags.set(BIT9, baseflags.test(BIT9)); + flags.set(BIT10, baseflags.test(BIT10)); } URL::URL(napi_env env, const std::string& input) @@ -934,43 +962,43 @@ URL::URL(napi_env env, const std::string& input) URL::URL(napi_env env, const std::string& input, const std::string& base) { env_ = env; - url_data baseInfo; - std::bitset<11> baseflags; + UrlData baseInfo; + std::bitset baseflags; std::string strBase = base; std::string strInput = input; if (strBase.empty()) { - baseflags.set(0); // 0:Bit 0 Set to true,The baseURL analysis failed + baseflags.set(BIT0); } DeleteC0OrSpace(strBase); DeleteTabOrNewline(strBase); DeleteC0OrSpace(strInput); DeleteTabOrNewline(strInput); InitOnlyInput(strBase, baseInfo, baseflags); - if (baseflags.test(0)) { - flags_.set(0); // 0:Bit 0 Set to true,The URL analysis failed + if (baseflags.test(BIT0)) { + flags_.set(BIT0); return; - } else if (!baseflags.test(0)) { // 0:Bit 0 not is true,The baseURL analysis success + } else if (!baseflags.test(BIT0)) { InitOnlyInput(strInput, urlData_, flags_); - if (!flags_.test(0)) { // 0:Bit 0 not is true,The URL analysis success + if (!flags_.test(BIT0)) { return; } if ((input[0] == '/' || input[0] == '\\') && (input[1] == '/' || input[1] == '\\')) { - std::string new_input = baseInfo.scheme + input; - flags_.set(0, 0); // 0:Bit 0 Set to false,The URL analysis success - InitOnlyInput(new_input, urlData_, flags_); + std::string newInput = baseInfo.scheme + input; + flags_.set(BIT0, 0); + InitOnlyInput(newInput, urlData_, flags_); return; } - if (baseflags.test(4)) { //4:Bit 4 is true,The URL has hostname - flags_.set(0, 0); + if (baseflags.test(BIT4)) { + flags_.set(BIT0, 0); BaseInfoToUrl(baseInfo, baseflags, urlData_, flags_, input.empty()); if (!input.empty()) { - if ((input[0] == '/') || (input[0] == '\\' && flags_.test(1))) { + if ((input[0] == '/') || (input[0] == '\\' && flags_.test(BIT1))) { strInput = input.substr(1); } AnalysisInput(strInput, urlData_, flags_, baseflags); } - } else if (!baseflags.test(4)) { //4:Bit 4 is true,The URL has hostname - flags_.set(0); // 0:Bit 0 Set to true,The URL analysis failed + } else if (!baseflags.test(BIT4)) { + flags_.set(BIT0); return; } } @@ -980,32 +1008,32 @@ URL::URL(napi_env env, const std::string& input, const URL& base) { env_ = env; std::string strInput = input; - url_data baseInfo = base.urlData_; - std::bitset<11> baseflags = base.flags_; + UrlData baseInfo = base.urlData_; + std::bitset baseflags = base.flags_; DeleteC0OrSpace(strInput); DeleteTabOrNewline(strInput); InitOnlyInput(strInput, urlData_, flags_); - if (!flags_.test(0)) { // 0:Bit 0 not is true,The URL analysis failed + if (!flags_.test(BIT0)) { return; } if ((input[0] == '/' || input[0] == '\\') && (input[1] == '/' || input[1] == '\\')) { - std::string new_input = baseInfo.scheme + input; - flags_.set(0, 0); // 0:Bit 0 Set to false - InitOnlyInput(new_input, urlData_, flags_); + std::string newInput = baseInfo.scheme + input; + flags_.set(BIT0, 0); + InitOnlyInput(newInput, urlData_, flags_); return; } - if (baseflags.test(4)) { // 4:Bit 4 is true,The baseURL has host - flags_.set(0, 0); // 0:Bit 0 set to true + if (baseflags.test(BIT4)) { + flags_.set(BIT0, 0); BaseInfoToUrl(baseInfo, baseflags, urlData_, flags_, input.empty()); if (!input.empty()) { if ((input[0] == '/') || - (input[0] == '\\' && flags_.test(1))) { // 1:Bit 1 is true,The URL Default for protocol - strInput = input.substr(1); + (input[0] == '\\' && flags_.test(BIT1))) { + strInput = input.substr(1); } AnalysisInput(strInput, urlData_, flags_, baseflags); } - } else if (!baseflags.test(4)) { // 4:Bit 4 is false,The URL analysis failed - flags_.set(0); // 0:Bit 0 Set to true,The URL analysis failed + } else if (!baseflags.test(BIT4)) { + flags_.set(BIT0); return; } } @@ -1014,7 +1042,7 @@ napi_value URL::GetHostname() const { napi_value result; const char* temp = nullptr; - if (flags_.test(4)) { // 4:Bit 4 is true,The URL has hostname + if (flags_.test(BIT4)) { temp = urlData_.host.c_str(); } else { temp = ""; @@ -1028,7 +1056,7 @@ napi_value URL::GetSearch() const { napi_value result; const char* temp = nullptr; - if (flags_.test(7)) { // 7:Bit 7 is true,The URL has Search + if (flags_.test(BIT7)) { if (urlData_.query.size() == 1) { temp = ""; } else { @@ -1046,7 +1074,7 @@ napi_value URL::GetUsername() const { napi_value result; const char* temp = nullptr; - if (flags_.test(2)) { // 2:Bit 2 is true,The URL has username + if (flags_.test(BIT2)) { temp = urlData_.username.c_str(); } else temp = ""; @@ -1059,7 +1087,7 @@ napi_value URL::GetPassword() const { napi_value result; const char* temp = nullptr; - if (flags_.test(3)) { // 3:Bit 3 is true,The URL has Password + if (flags_.test(BIT3)) { temp = urlData_.password.c_str(); } else { temp = ""; @@ -1073,7 +1101,7 @@ napi_value URL::GetFragment() const { napi_value result; const char* temp = nullptr; - if (flags_.test(8)) { // 8:Bit 8 is true,The URL has Fragment + if (flags_.test(BIT8)) { if (urlData_.fragment.size() == 1) { temp = ""; } else { @@ -1106,7 +1134,7 @@ napi_value URL::GetPath() const napi_value result; std::string temp1 = "/"; const char* temp = nullptr; - if (flags_.test(6)) { // 6:Bit 6 is true,The URL has pathname + if (flags_.test(BIT6)) { for (size_t i = 0; i < urlData_.path.size(); i++) { if (i < urlData_.path.size() - 1) { temp1 += urlData_.path[i] + "/"; @@ -1116,8 +1144,8 @@ napi_value URL::GetPath() const temp = temp1.c_str(); } } else { - bool Special = IsSpecial(urlData_.scheme); - if(Special) { + bool special = IsSpecial(urlData_.scheme); + if (special) { temp = "/"; } else { temp = ""; @@ -1132,7 +1160,7 @@ napi_value URL::GetPort() const { napi_value result; const char* temp = nullptr; - if (flags_.test(5)) { // 5:Bit 5 is true,The URL has Port + if (flags_.test(BIT5)) { temp = std::to_string(urlData_.port).c_str(); } else { temp = ""; @@ -1145,22 +1173,19 @@ napi_value URL::GetPort() const napi_value URL::GetHost() const { napi_value result; - const char* temp = nullptr; std::string temp1 = urlData_.host; - if (flags_.test(5)) { // 5:Bit 5 is true,The URL has Port + if (flags_.test(BIT5)) { temp1 += ":"; temp1 += std::to_string(urlData_.port); } - temp = temp1.c_str(); - size_t templen = strlen(temp); - NAPI_CALL(env_, napi_create_string_utf8(env_, temp, templen, &result)); + NAPI_CALL(env_, napi_create_string_utf8(env_, temp1.c_str(), temp1.size(), &result)); return result; } napi_value URL::GetOnOrOff() const { napi_value result; - if (flags_.test(0)) { // 1:Bit 1 is true,The URL Parsing succeeded + if (flags_.test(BIT0)) { bool flag = false; NAPI_CALL(env_, napi_get_boolean(env_, flag, &result)); } else { @@ -1173,7 +1198,7 @@ napi_value URL::GetOnOrOff() const napi_value URL::GetIsIpv6() const { napi_value result; - if (flags_.test(10)) { // 10:Bit 10 is true,The URL is ipv6 + if (flags_.test(BIT10)) { bool flag = true; NAPI_CALL(env_, napi_get_boolean(env_, flag, &result)); } else { @@ -1185,7 +1210,7 @@ napi_value URL::GetIsIpv6() const void URL::SetHostname(const std::string& input) { - if (flags_.test(9)) { // 9:Bit 9 is true,The URL Can not be base + if (flags_.test(BIT9)) { return; } std::string strHost = input; @@ -1199,16 +1224,16 @@ void URL::SetHostname(const std::string& input) if (strHost.size() == 0) { return; } - bool is_Special = IsSpecial(urlData_.scheme); - std::bitset<11> thisflags_; + bool special = IsSpecial(urlData_.scheme); + std::bitset thisFlags; std::string thisHostname = ""; - AnalysisHost(strHost, thisHostname, thisflags_, is_Special); - if (thisflags_.test(4)) { //4:Bit 4 is true,The URL has hostname + AnalysisHost(strHost, thisHostname, thisFlags, special); + if (thisFlags.test(BIT4)) { if ((urlData_.scheme == "file:") && (thisHostname == "localhost")) { thisHostname = ""; } urlData_.host = thisHostname; - flags_.set(4); + flags_.set(BIT4); } } @@ -1217,10 +1242,10 @@ void URL::SetHref(const std::string& input) std::string str = input; DeleteC0OrSpace(str); DeleteTabOrNewline(str); - url_data thisNewUrl; - std::bitset<11> thisNewFlags; + UrlData thisNewUrl; + std::bitset thisNewFlags; InitOnlyInput(str, thisNewUrl, thisNewFlags); - if (!thisNewFlags.test(0)) { + if (!thisNewFlags.test(BIT0)) { urlData_ = thisNewUrl; flags_ = thisNewFlags; } @@ -1229,7 +1254,7 @@ void URL::SetHref(const std::string& input) void URL::SetPath(const std::string& input) { std::string strPath = input; - if (flags_.test(9)) { + if (flags_.test(BIT9)) { return; } if (strPath.empty()) { @@ -1237,43 +1262,35 @@ void URL::SetPath(const std::string& input) } std::string oldstr = "%3A"; std::string newstr = ":"; - ReplaceSpecialSymbols(strPath,oldstr,newstr); - bool is_Special = IsSpecial(urlData_.scheme); + ReplaceSpecialSymbols(strPath, oldstr, newstr); + bool special = IsSpecial(urlData_.scheme); if (urlData_.scheme == "file:") { - url_data thisFileDate; - std::bitset<11> thisFileFlag; - if ((strPath[0] == '/') || (strPath[0] == '\\' && flags_.test(1))) { + UrlData thisFileDate; + std::bitset thisFileFlag; + if ((strPath[0] == '/') || (strPath[0] == '\\' && flags_.test(BIT1))) { strPath = strPath.substr(1); } AnalysisFilePath(strPath, thisFileDate, thisFileFlag); - if (thisFileFlag.test(6)) { + if (thisFileFlag.test(BIT6)) { urlData_.path = thisFileDate.path; - flags_.set(6); + flags_.set(BIT6); } } else { - std::vector thisPath_; - std::bitset<11> thisFlags_; - if ((strPath[0] == '/') || (strPath[0] == '\\' && flags_.test(1))) { + std::vector thisPath; + std::bitset thisFlags; + if ((strPath[0] == '/') || (strPath[0] == '\\' && flags_.test(BIT1))) { strPath = strPath.substr(1); } - AnalysisPath(strPath, thisPath_, thisFlags_, is_Special); - if (thisFlags_.test(6)) { - urlData_.path = thisPath_; - flags_.set(6); + AnalysisPath(strPath, thisPath, thisFlags, special); + if (thisFlags.test(BIT6)) { + urlData_.path = thisPath; + flags_.set(BIT6); } } } -void URL::SetHost(const std::string& input) +void SplitString(const std::string& input, std::string& strHost, std::string& port) { - if (flags_.test(9)) { - return; - } - if(input.empty()) { - return; - } - std::string strHost = input; - std::string port = ""; size_t strlen = input.size(); for (size_t pos = 0; pos < strlen; pos++) { if ((input[pos] == ':') || (input[pos] == '?') || (input[pos] == '#') || @@ -1286,24 +1303,37 @@ void URL::SetHost(const std::string& input) break; } } +} + +void URL::SetHost(const std::string& input) +{ + if (flags_.test(BIT9)) { + return; + } + if (input.empty()) { + return; + } + std::string strHost = input; + std::string port = ""; + SplitString(input, strHost, port); if (strHost.size() == 0) { return; } - bool is_Special = IsSpecial(urlData_.scheme); - std::bitset<11> hostnameflags; + bool special = IsSpecial(urlData_.scheme); + std::bitset hostnameflags; std::string thisHostname = ""; - AnalysisHost(strHost, thisHostname, hostnameflags, is_Special); - if (hostnameflags.test(4)) { //4:Bit 4 is true,The URL has hostname + AnalysisHost(strHost, thisHostname, hostnameflags, special); + if (hostnameflags.test(BIT4)) { if ((urlData_.scheme == "file:") && (thisHostname == "localhost")) { thisHostname = ""; } urlData_.host = thisHostname; - flags_.set(4); // 4:Bit 4 Set to true,The URL has hostname + flags_.set(BIT4); } else { return; } if (port.size() > 0) { - strlen = port.size(); + size_t strlen = port.size(); for (size_t pos = 0; pos < strlen; pos++) { if ((port[pos] == '?') || (port[pos] == '#') || (port[pos] == '/') || (port[pos] == '\\')) { port = port.substr(0, pos); @@ -1311,11 +1341,11 @@ void URL::SetHost(const std::string& input) } } if (port.size() > 0) { - std::bitset<11> thisflags_; - url_data thisport; - AnalysisPort(port, thisport, thisflags_); - if (thisflags_.test(5)) { // 5:Bit 5 is true,The URL has port - flags_.set(5); // 5:Bit 5 get to true,The URL has port + std::bitset thisFlags; + UrlData thisport; + AnalysisPort(port, thisport, thisFlags); + if (thisFlags.test(BIT5)) { + flags_.set(BIT5); urlData_.port = thisport.port; } } @@ -1333,11 +1363,11 @@ void URL::SetPort(const std::string& input) } } if (port.size() > 0) { - std::bitset<11> thisflags_; - url_data thisport; - AnalysisPort(port, thisport, thisflags_); - if (thisflags_.test(5)) { // 5:Bit 5 is true,The URL has port - flags_.set(5); // 5:Bit 5 get to true,The URL has port + std::bitset thisFlags; + UrlData thisport; + AnalysisPort(port, thisport, thisFlags); + if (thisFlags.test(BIT5)) { + flags_.set(BIT5); urlData_.port = thisport.port; } } @@ -1348,7 +1378,7 @@ void URL::SetSearch(const std::string& input) std::string temp; if (input.size() == 0) { urlData_.query = ""; - flags_.set(7, 0); // 7:Bit 7 set to false,The URL not have Search + flags_.set(BIT7, 0); } else { if (input[0] != '?') { temp = "?"; @@ -1358,7 +1388,7 @@ void URL::SetSearch(const std::string& input) } std::string oldstr = "#"; std::string newstr = "%23"; - ReplaceSpecialSymbols(temp,oldstr,newstr); + ReplaceSpecialSymbols(temp, oldstr, newstr); AnalysisQuery(temp, urlData_.query, flags_); } } @@ -1368,7 +1398,7 @@ void URL::SetFragment(const std::string& input) std::string temp; if (input.size() == 0) { urlData_.fragment = ""; - flags_.set(8, 0); // 8:Bit 8 set to false,The URL not have Fragment + flags_.set(BIT8, 0); } else { if (input[0] != '#') { temp = "#"; @@ -1383,17 +1413,17 @@ void URL::SetFragment(const std::string& input) void URL::SetScheme(const std::string& input) { std::string strInput = input; - bool is_Special = IsSpecial(urlData_.scheme); + bool special = IsSpecial(urlData_.scheme); bool inputIsSpecial = IsSpecial(input); - if ((is_Special != inputIsSpecial) || ((input == "file") && (flags_.test(2) || - flags_.test(3) || flags_.test(5)))) { //2 3 5:The URL has username password host + if ((special != inputIsSpecial) || ((input == "file") && (flags_.test(BIT2) || + flags_.test(BIT3) || flags_.test(BIT5)))) { return; } std::string thisScheme = ""; - std::bitset<11> thisFlags; + std::bitset thisFlags; if (AnalysisScheme(strInput, thisScheme, thisFlags)) { - if (thisFlags.test(1)) { // 1:Bit 1 is true,The inputURL Default for protocol - flags_.set(1); // 1:Bit 1 set to true,The URL Default for protocol + if (thisFlags.test(BIT1)) { + flags_.set(BIT1); } urlData_.scheme = thisScheme; } @@ -1403,18 +1433,18 @@ void URL::SetUsername(const std::string& input) { if (input.size() == 0) { urlData_.username = ""; - flags_.set(2, 0); // 2:Bit 2 set to false,The URL not have username + flags_.set(BIT2, 0); } else { if (!input.empty()) { std::string usname = input; std::string oldstr = "@"; std::string newstr = "%40"; - ReplaceSpecialSymbols(usname,oldstr,newstr); + ReplaceSpecialSymbols(usname, oldstr, newstr); oldstr = "/"; newstr = "%2F"; - ReplaceSpecialSymbols(usname,oldstr,newstr); + ReplaceSpecialSymbols(usname, oldstr, newstr); urlData_.username = usname; - flags_.set(2); // 2:Bit 2 set to true,The URL have username + flags_.set(BIT2); } } } @@ -1423,51 +1453,47 @@ void URL::SetPassword(const std::string& input) { if (input.size() == 0) { urlData_.password = ""; - flags_.set(3, 0); // 3:Bit 3 set to false,The URL not have Password + flags_.set(BIT3, 0); } else { if (!input.empty()) { - std::string passwd = input; + std::string keyWord = input; std::string oldstr = "@"; std::string newstr = "%40"; - ReplaceSpecialSymbols(passwd,oldstr,newstr); + ReplaceSpecialSymbols(keyWord, oldstr, newstr); oldstr = "/"; newstr = "%2F"; - ReplaceSpecialSymbols(passwd,oldstr,newstr); - urlData_.password = passwd; - flags_.set(3); // 3:Bit 3 set to true,The URL have passwd + ReplaceSpecialSymbols(keyWord, oldstr, newstr); + urlData_.password = keyWord; + flags_.set(BIT3); } } } -void URL::InitOnlyInput(std::string& input, url_data& urlData, std::bitset<11>& flags) +void URL::InitOnlyInput(std::string& input, UrlData& urlData, std::bitset& flags) { if (input.empty()) { - flags.set(0); // 0:Bit 0 Set to true,The URL analysis failed + flags.set(BIT0); return; } if (input.find(':') != std::string::npos) { size_t pos = input.find(':'); pos++; - std::string scheme_ = input.substr(0, pos); - if (!AnalysisScheme(scheme_, urlData.scheme, flags)) { + std::string scheme = input.substr(0, pos); + if (!AnalysisScheme(scheme, urlData.scheme, flags)) { return; - } - + } if (input.find('#') != std::string::npos) { size_t i = input.find('#'); std::string fragment = input.substr(i); AnalysisFragment(fragment, urlData.fragment, flags); input = input.substr(0, i); - } if (input.find('?') != std::string::npos) { size_t i = input.find('?'); std::string query = input.substr(i); AnalysisQuery(query, urlData.query, flags); - input = input.substr(0, i); } - std::string str = input.substr(pos); if (urlData.scheme == "file:") { AnalysisFile(str, urlData, flags); @@ -1475,87 +1501,95 @@ void URL::InitOnlyInput(std::string& input, url_data& urlData, std::bitset<11>& AnalysisHostAndPath(str, urlData, flags); } } else { - flags.set(0); // 0:Bit 0 Set to true,The URL analysis failed + flags.set(BIT0); return; } } URLSearchParams::URLSearchParams(napi_env env) : env(env) -{ -} -std::wstring strToWstr(const std::string& str) +{} +std::wstring StrToWstr(const std::string& str) { setlocale(LC_ALL, ""); - const char* addressofS = str.c_str(); + std::wstring wstr = L""; size_t len = str.size() + 1; - wchar_t* wch = new wchar_t[len]; - wmemset(wch, 0, len); - mbstowcs(wch, addressofS, len); - std::wstring wstr = wch; - delete[]wch; - setlocale(LC_ALL, ""); + if (len > 0) { + auto wch = new wchar_t[len]; + mbstowcs(wch, str.c_str(), len); + wstr = wch; + delete[] wch; + setlocale(LC_ALL, ""); + return wstr; + } return wstr; } + +bool IsEscapeRange(const char charaEncode) +{ + if ((charaEncode > 0 && charaEncode < '*') || + (charaEncode > '*' && charaEncode < '-') || + (charaEncode == '/') || + (charaEncode > '9' && charaEncode < 'A') || + (charaEncode > 'Z' && charaEncode < '_') || + (charaEncode == '`') || + (charaEncode > 'z')) { + return true; + } + return false; +} + std::string ReviseStr(std::string str, std::string* reviseChar) { - const size_t len_Str = str.length(); - if (len_Str == 0) { + const size_t lenStr = str.length(); + if (lenStr == 0) { return ""; } std::string output = ""; int numOfAscii = 128; // 128:Number of ASCII characters size_t i = 0; - for (; i < len_Str; i++) - { + for (; i < lenStr; i++) { int charaEncode = static_cast(str[i]); if (charaEncode < 0 || charaEncode >= numOfAscii) { int bytOfSpeChar = 3; // 3:Bytes of special characters in Linux std::string subStr = str.substr(i, bytOfSpeChar); - i += 2; - const char* firstAddr = subStr.c_str(); - std::wstring wstr = strToWstr(firstAddr); + i += STEP_SZIE; + const char* ptrSbustr = subStr.c_str(); + std::wstring wstr = StrToWstr(ptrSbustr); wchar_t wch = wstr[0]; charaEncode = static_cast(wch); - } - if (0 <= charaEncode && charaEncode < numOfAscii) { - //2:Defines the escape range of ASCII characters - if ((0 < charaEncode && charaEncode < '*') || - ('*' < charaEncode && charaEncode < '-') || - (charaEncode == '/') || - ('9' < charaEncode && charaEncode < 'A') || - ('Z' < charaEncode && charaEncode < '_') || - (charaEncode == '`') || - (charaEncode > 'z')) { + } + if (charaEncode >= 0 && charaEncode < numOfAscii) { + // 2:Defines the escape range of ASCII characters + if (IsEscapeRange(charaEncode)) { output += reviseChar[charaEncode]; } else { output += str.substr(i, 1); } } else if (charaEncode <= 0x000007FF) { // Convert the Unicode code into two bytes std::string output1 = reviseChar[0x000000C0 | - (charaEncode / 64)]; // Acquisition method of the first byte + (charaEncode / 64)]; // 64:Acquisition method of the first byte std::string output2 = reviseChar[numOfAscii | (charaEncode & 0x0000003F)]; // Acquisition method of the second byte output += output1 + output2; } else if ((charaEncode < 0x0000D800) || - (charaEncode >= 0x0000E000)) { // Convert the Unicode code into three bytes + (charaEncode >= 0x0000E000)) { // Convert the Unicode code into three bytes std::string output1 = reviseChar[0x000000E0 | - (charaEncode / 4096)]; // Acquisition method of the first byte + (charaEncode / 4096)]; // 4096:Acquisition method of the first byte std::string output2 = reviseChar[numOfAscii | - ((charaEncode / 64) & 0x0000003F)]; // method of the second byte + ((charaEncode / 64) & 0x0000003F)]; // 64:method of the second byte std::string output3 = reviseChar[numOfAscii | (charaEncode & 0x0000003F)]; // Acquisition method of the third byte output += output1 + output2 + output3; } else { - // 1023:Convert codes greater than 0x000e000 into 4 bytes - const size_t charaEncode1 = static_cast(str[++i]) & 1023; + const size_t charaEncode1 = static_cast(str[++i]) & 1023; // 1023:Convert codes charaEncode = 65536 + (((charaEncode & 1023) << 10) | - charaEncode1); // Specific transcoding method + charaEncode1); // 65536:Specific transcoding method std::string output1 = reviseChar[0x000000F0 | - (charaEncode / 262144)]; // Acquisition method of the first byte + (charaEncode / 262144)]; // 262144:Acquisition method of the first byte std::string output2 = reviseChar[numOfAscii | - ((charaEncode / 4096) & 0x0000003F)]; // Acquisition method of the second byte + ((charaEncode / 4096) & 0x0000003F)]; // 4096:Acquisition method of the second byte std::string output3 = reviseChar[numOfAscii | - ((charaEncode / 64) & 0x0000003F)]; // Acquisition method of the third byte + ((charaEncode / 64) & 0x0000003F)]; // 64:Acquisition method of the third byte std::string output4 = reviseChar[numOfAscii | (charaEncode & 0x0000003F)]; // Acquisition method of the fourth byte output += output1 + output2 + output3 + output4; @@ -1567,8 +1601,8 @@ std::string ReviseStr(std::string str, std::string* reviseChar) napi_value URLSearchParams::ToString() { std::string output = ""; - std::string reviseChar[256]; - for (size_t i = 0; i < 256; ++i) { + std::string reviseChar[256]; // 256:Array length + for (size_t i = 0; i < 256; ++i) { // 256:Array length std::stringstream ioss; std::string str1; size_t j = i; @@ -1582,19 +1616,18 @@ napi_value URLSearchParams::ToString() } } reviseChar[0x20] = "+"; // 0x20:ASCII value of spaces - const size_t len_Str = searchParams.size(); - if (len_Str == 0) { + const size_t lenStr = searchParams.size(); + if (lenStr == 0) { napi_value result = nullptr; napi_create_string_utf8(env, output.c_str(), output.size(), &result); return result; } - std::string first_StrKey = ReviseStr(searchParams[0], reviseChar); - std::string first_StrValue = ReviseStr(searchParams[1], reviseChar); - output = first_StrKey + "=" + first_StrValue; - if (len_Str % 2 == 0) { - size_t couple = 2; - size_t i = 2; - for (; i < len_Str; i += couple) { + std::string firstStrKey = ReviseStr(searchParams[0], reviseChar); + std::string firstStrValue = ReviseStr(searchParams[1], reviseChar); + output = firstStrKey + "=" + firstStrValue; + if (lenStr % 2 == 0) { // 2:Divisible by 2 + size_t i = 2; // 2:Initial Position + for (; i < lenStr; i += STEP_SZIE) { std::string strKey = ReviseStr(searchParams[i], reviseChar); std::string strValue = ReviseStr(searchParams[i + 1], reviseChar); output += +"&" + strKey + "=" + strValue; @@ -1630,23 +1663,24 @@ std::string URLSearchParams::ToUSVString(std::string inputStr) { size_t strLen = strlen(inputStr.c_str()); wchar_t* strPtr = nullptr; + std::wstring winput = L""; int strSize = mbstowcs(strPtr, inputStr.c_str(), 0) + 1; - strPtr = new wchar_t[strSize]; - wmemset(strPtr, 0, strSize); - mbstowcs(strPtr, inputStr.c_str(), strLen); - + if (strSize > 0) { + strPtr = new wchar_t[strSize]; + mbstowcs(strPtr, inputStr.c_str(), strLen); + winput = strPtr; + } const char* expr = "(?:[^\\uD800-\\uDBFF]|^)[\\uDC00-\\uDFFF]|[\\uD800-\\uDBFF](?![\\uDC00-\\uDFFF])"; size_t exprLen = strlen(expr); wchar_t* exprPtr = nullptr; int exprSize = mbstowcs(exprPtr, expr, 0) + 1; - exprPtr = new wchar_t[exprSize]; - wmemset(exprPtr, 0, exprSize); - mbstowcs(exprPtr, expr, exprLen); - + if (exprSize > 0) { + exprPtr = new wchar_t[exprSize]; + mbstowcs(exprPtr, expr, exprLen); + } std::wregex wexpr(exprPtr); delete[] exprPtr; std::wsmatch result; - std::wstring winput = strPtr; delete[] strPtr; std::wstring::const_iterator iterStart = winput.begin(); std::wstring::const_iterator iterEnd = winput.end(); @@ -1656,11 +1690,19 @@ std::string URLSearchParams::ToUSVString(std::string inputStr) HandleIllegalChar(winput, result[0].first); size_t inputLen = wcslen(winput.c_str()); char* rePtr = nullptr; + std::string reStr = ""; int reSize = wcstombs(rePtr, winput.c_str(), 0) + 1; - rePtr = new char[reSize]; - memset(rePtr, 0, reSize); - wcstombs(rePtr, winput.c_str(), inputLen); - std::string reStr = rePtr; + if (reSize > 0) { + rePtr = new char[reSize]; + if (memset_s(rePtr, reSize, 0, reSize) != 0) { + HILOG_ERROR("ToUSVString memset_s failed"); + delete[] rePtr; + return reStr; + } else { + wcstombs(rePtr, winput.c_str(), inputLen); + reStr = rePtr; + } + } delete[] rePtr; return reStr; } @@ -1668,18 +1710,21 @@ napi_value URLSearchParams::Get(napi_value buffer) { char* name = nullptr; size_t nameSize = 0; + std::string temp = ""; napi_get_value_string_utf8(env, buffer, nullptr, 0, &nameSize); - name = new char[nameSize + 1]; - napi_get_value_string_utf8(env, buffer, name, nameSize + 1, &nameSize); - - std::string sname = ToUSVString(name); + if (nameSize > 0) { + name = new char[nameSize + 1]; + napi_get_value_string_utf8(env, buffer, name, nameSize + 1, &nameSize); + temp = name; + } + std::string sname = ToUSVString(temp); delete[] name; napi_value result = nullptr; - if(searchParams.size() == 0){ + if (searchParams.size() == 0) { return result; } size_t size = searchParams.size() - 1; - for (size_t i = 0; i < size; i += 2) { + for (size_t i = 0; i < size; i += STEP_SZIE) { if (searchParams[i] == sname) { std::string str = searchParams[i + 1]; napi_create_string_utf8(env, searchParams[i + 1].c_str(), searchParams[i + 1].length(), &result); @@ -1692,21 +1737,23 @@ napi_value URLSearchParams::GetAll(napi_value buffer) { char* name = nullptr; size_t nameSize = 0; + std::string sname = ""; napi_get_value_string_utf8(env, buffer, nullptr, 0, &nameSize); - name = new char[nameSize + 1]; - napi_get_value_string_utf8(env, buffer, name, nameSize + 1, &nameSize); - - std::string sname = ToUSVString(name); + if (nameSize > 0) { + name = new char[nameSize + 1]; + napi_get_value_string_utf8(env, buffer, name, nameSize + 1, &nameSize); + sname = ToUSVString(name); + } delete[] name; napi_value result = nullptr; napi_value napiStr = nullptr; NAPI_CALL(env, napi_create_array(env, &result)); size_t flag = 0; - if(searchParams.size() == 0){ + if (searchParams.size() == 0) { return result; } - size_t size = searchParams.size() -1 ; - for (size_t i = 0; i < size; i += 2) { + size_t size = searchParams.size() - 1; + for (size_t i = 0; i < size; i += STEP_SZIE) { if (searchParams[i] == sname) { napi_create_string_utf8(env, searchParams[i + 1].c_str(), searchParams[i + 1].length(), &napiStr); napi_status status = napi_set_element(env, result, flag, napiStr); @@ -1722,17 +1769,24 @@ void URLSearchParams::Append(napi_value buffer, napi_value temp) { char* name = nullptr; size_t nameSize = 0; + std::string tempName = ""; + std::string tempValue = ""; napi_get_value_string_utf8(env, buffer, nullptr, 0, &nameSize); - name = new char[nameSize + 1]; - napi_get_value_string_utf8(env, buffer, name, nameSize + 1, &nameSize); - + if (nameSize > 0) { + name = new char[nameSize + 1]; + napi_get_value_string_utf8(env, buffer, name, nameSize + 1, &nameSize); + tempName = name; + } char* value = nullptr; size_t valueSize = 0; napi_get_value_string_utf8(env, temp, nullptr, 0, &valueSize); - value = new char[valueSize + 1]; - napi_get_value_string_utf8(env, temp, value, valueSize + 1, &valueSize); - searchParams.push_back(name); - searchParams.push_back(value); + if (valueSize > 0) { + value = new char[valueSize + 1]; + napi_get_value_string_utf8(env, temp, value, valueSize + 1, &valueSize); + tempValue = value; + } + searchParams.push_back(tempName); + searchParams.push_back(tempValue); delete[] name; delete[] value; } @@ -1740,30 +1794,33 @@ void URLSearchParams::Delete(napi_value buffer) { char* name = nullptr; size_t nameSize = 0; + std::string sname = ""; napi_get_value_string_utf8(env, buffer, nullptr, 0, &nameSize); - name = new char[nameSize + 1]; - napi_get_value_string_utf8(env, buffer, name, nameSize + 1, &nameSize); - std::string sname = ToUSVString(name); + if (nameSize > 0) { + name = new char[nameSize + 1]; + napi_get_value_string_utf8(env, buffer, name, nameSize + 1, &nameSize); + sname = ToUSVString(name); + } delete[] name; for (std::vector::iterator iter = searchParams.begin(); iter != searchParams.end();) { if (*iter == sname) { - iter = searchParams.erase(iter, iter + 2); + iter = searchParams.erase(iter, iter + STEP_SZIE); } else { - iter += 2; + iter += STEP_SZIE; } } } -napi_value URLSearchParams::Entries() +napi_value URLSearchParams::Entries() const { napi_value resend = nullptr; napi_value firNapiStr = nullptr; napi_value secNapiStr = nullptr; napi_create_array(env, &resend); - if(searchParams.size() == 0) { - return resend; + if (searchParams.size() == 0) { + return resend; } size_t size = searchParams.size() - 1; - for (size_t i = 0; i < size; i += 2) { + for (size_t i = 0; i < size; i += STEP_SZIE) { napi_value result = nullptr; napi_create_array(env, &result); @@ -1771,45 +1828,47 @@ napi_value URLSearchParams::Entries() napi_create_string_utf8(env, searchParams[i + 1].c_str(), searchParams[i + 1].length(), &secNapiStr); napi_set_element(env, result, 0, firNapiStr); napi_set_element(env, result, 1, secNapiStr); - napi_set_element(env, resend, i / 2, result); + napi_set_element(env, resend, i / 2, result); // 2:Find the number of keys } return resend; } void URLSearchParams::ForEach(napi_value function, napi_value thisVar) { - if(searchParams.size() == 0){ - return ; + if (searchParams.size() == 0) { + return; } size_t size = searchParams.size() - 1; - for (size_t i = 0; i < size; i += 2) { - napi_value return_val = nullptr; + for (size_t i = 0; i < size; i += STEP_SZIE) { + napi_value returnVal = nullptr; size_t argc = 3; napi_value global = nullptr; napi_get_global(env, &global); - + napi_value key = nullptr; napi_create_string_utf8(env, searchParams[i].c_str(), strlen(searchParams[i].c_str()), &key); napi_value value = nullptr; napi_create_string_utf8(env, searchParams[i + 1].c_str(), strlen(searchParams[i + 1].c_str()), &value); napi_value argv[3] = {key, value, thisVar}; - napi_call_function(env, global, function, argc, argv, &return_val); + napi_call_function(env, global, function, argc, argv, &returnVal); } } -napi_value URLSearchParams::IsHas(napi_value name) +napi_value URLSearchParams::IsHas(napi_value name) const { char* buffer = nullptr; size_t bufferSize = 0; + std::string buf = ""; napi_get_value_string_utf8(env, name, nullptr, 0, &bufferSize); - buffer = new char[bufferSize + 1]; - napi_get_value_string_utf8(env, name, buffer, bufferSize + 1, &bufferSize); + if (bufferSize > 0) { + buffer = new char[bufferSize + 1]; + napi_get_value_string_utf8(env, name, buffer, bufferSize + 1, &bufferSize); + buf = buffer; + } bool flag = false; - napi_value result; - int lenStr = searchParams.size(); - int couple = 2; - for (size_t i = 0; i != lenStr; i += couple) { - std::string b = buffer; - if (searchParams[i] == b) { + napi_value result = nullptr; + size_t lenStr = searchParams.size(); + for (size_t i = 0; i != lenStr; i += STEP_SZIE) { + if (searchParams[i] == buf) { flag = true; napi_get_boolean(env, flag, &result); return result; @@ -1823,39 +1882,45 @@ void URLSearchParams::Set(napi_value name, napi_value value) { char* buffer0 = nullptr; size_t bufferSize0 = 0; + std::string cppName = ""; + std::string cppValue = ""; napi_get_value_string_utf8(env, name, nullptr, 0, &bufferSize0); - buffer0 = new char[bufferSize0 + 1]; - napi_get_value_string_utf8(env, name, buffer0, bufferSize0 + 1, &bufferSize0); + if (bufferSize0 > 0) { + buffer0 = new char[bufferSize0 + 1]; + napi_get_value_string_utf8(env, name, buffer0, bufferSize0 + 1, &bufferSize0); + cppName = buffer0; + delete[] buffer0; + } char* buffer1 = nullptr; size_t bufferSize1 = 0; napi_get_value_string_utf8(env, value, nullptr, 0, &bufferSize1); - buffer1 = new char[bufferSize1 + 1]; - napi_get_value_string_utf8(env, value, buffer1, bufferSize1 + 1, &bufferSize1); + if (bufferSize1 > 0) { + buffer1 = new char[bufferSize1 + 1]; + napi_get_value_string_utf8(env, value, buffer1, bufferSize1 + 1, &bufferSize1); + cppValue = buffer1; + delete[] buffer1; + } bool flag = false; - std::string cpp_name = buffer0; - std::string cpp_value = buffer1; - delete[] buffer0; - delete[] buffer1; for (std::vector::iterator it = searchParams.begin(); it < searchParams.end() - 1;) { - if (*it == cpp_name) { + if (*it == cppName) { if (!flag) { - *(it + 1) = cpp_value; + *(it + 1) = cppValue; flag = true; - it += 2; + it += STEP_SZIE; } else { - it = searchParams.erase(it, it + 2); + it = searchParams.erase(it, it + STEP_SZIE); } } else { - it += 2; + it += STEP_SZIE; } } if (!flag) { - searchParams.push_back(cpp_name); - searchParams.push_back(cpp_value); + searchParams.push_back(cppName); + searchParams.push_back(cppValue); } } -void SortMerge(std::vector out, unsigned int start, int mid, int end, - std::vector lBuffer, std::vector rBuffer) +void SortMerge(std::vector out, unsigned int start, int mid, int end, + std::vector lBuffer, std::vector rBuffer) { const unsigned int flag1 = mid - start; const unsigned int flag2 = end - mid; @@ -1864,10 +1929,10 @@ void SortMerge(std::vector out, unsigned int start, int mid, int en unsigned int flag5 = 0; for (flag3 = 0; flag3 < flag1; flag3++) { lBuffer[flag3] = out[start + flag3]; - } + } for (flag4 = 0; flag4 < flag2; flag4++) { rBuffer[flag4] = out[mid + flag4]; - } + } flag3 = 0; flag4 = 0; flag5 = start; @@ -1882,7 +1947,7 @@ void SortMerge(std::vector out, unsigned int start, int mid, int en } while (flag3 < flag1) { out[flag5++] = lBuffer[flag3++]; - } + } while (flag4 < flag2) { out[flag5++] = rBuffer[flag4++]; } @@ -1891,11 +1956,11 @@ void SortMerge(std::vector out, unsigned int start, int mid, int en void URLSearchParams::Sort() { unsigned int len = searchParams.size(); - if (2 < len && len < 100 && (len % 2 == 0)) { + if (len > 2 && len < 100 && (len % 2 == 0)) { // 2: 100:max size unsigned int i = 0; - for (; i < len - 2; i += 2) { - unsigned int j = i + 2; - for (; j < len; j += 2) { + for (; i < len - 2; i += STEP_SZIE) { // 2:Iterate over key-value pairs + unsigned int j = i + 2; // 2:Iterate over key-value pairs + for (; j < len; j += 2) { // 2:Iterate over key-value pairs if (searchParams[i] > searchParams[j]) { const std::string curKey = searchParams[i]; const std::string curVal = searchParams[i + 1]; @@ -1906,18 +1971,19 @@ void URLSearchParams::Sort() } } } - } if (len >= 100) { - const std::vector Buffer1; - const std::vector Buffer2; - for (unsigned int i = 2; i < searchParams.size(); i *= 2) { - for (unsigned int j = 0; j < searchParams.size() - 2; j += 2 * i) { + } + if (len >= 100) { // 100:max size + const std::vector buffer1; + const std::vector buffer2; + for (unsigned int i = 2; i < searchParams.size(); i *= 2) { // 2:Iterate over key-value pairs + for (unsigned int j = 0; j < searchParams.size() - 2; j += 2 * i) { // 2:Iterate over key-value pairs const unsigned int TempMid = j + i; unsigned int TempEnd = TempMid + i; - TempEnd = TempEnd < len ? TempEnd : len; + TempEnd = ((TempEnd < len) ? TempEnd : len); if (TempMid > TempEnd) { continue; - } - SortMerge(searchParams, j, TempMid, TempEnd, Buffer1, Buffer2); + } + SortMerge(searchParams, j, TempMid, TempEnd, buffer1, buffer2); } } } @@ -1926,15 +1992,14 @@ napi_value URLSearchParams::IterByKeys() { std::vector toKeys; napi_value result = nullptr; - napi_value napiStr; + napi_value napiStr = nullptr; napi_create_array(env, &result); - int lenStr = searchParams.size(); - if (lenStr % 2 == 0) { - int couple = 2; - for (std::vector::iterator it = searchParams.begin(); it != searchParams.end(); it += couple) { + size_t lenStr = searchParams.size(); + if (lenStr % 2 == 0) { // 2:Get the number of values + for (std::vector::iterator it = searchParams.begin(); it != searchParams.end(); it += STEP_SZIE) { toKeys.push_back(*it); } - int lenToKeys = toKeys.size(); + size_t lenToKeys = toKeys.size(); for (size_t i = 0; i < lenToKeys; i++) { napi_create_string_utf8(env, toKeys[i].c_str(), toKeys[i].length(), &napiStr); napi_set_element(env, result, i, napiStr); @@ -1946,15 +2011,14 @@ napi_value URLSearchParams::IterByValues() { std::vector toKeys; napi_value result = nullptr; - napi_value napiStr; + napi_value napiStr = nullptr; napi_create_array(env, &result); - int lenStr = searchParams.size(); - if (lenStr % 2 == 0) { - int couple = 2; - for (std::vector::iterator it = searchParams.begin(); it != searchParams.end(); it += couple) { + size_t lenStr = searchParams.size(); + if (lenStr % 2 == 0) { // 2:Get the number of values + for (std::vector::iterator it = searchParams.begin(); it != searchParams.end(); it += STEP_SZIE) { toKeys.push_back(*(it + 1)); } - int lenToKeys = toKeys.size(); + size_t lenToKeys = toKeys.size(); for (size_t i = 0; i < lenToKeys; i++) { napi_create_string_utf8(env, toKeys[i].c_str(), toKeys[i].length(), &napiStr); napi_set_element(env, result, i, napiStr); @@ -1966,7 +2030,7 @@ void URLSearchParams::SetArray(const std::vector vec) { searchParams = vec; } -napi_value URLSearchParams::GetArray() +napi_value URLSearchParams::GetArray() const { napi_value arr = nullptr; napi_create_array(env, &arr); diff --git a/url/js_url.h b/url/js_url.h old mode 100644 new mode 100755 index d506c7e7..c56c283a --- a/url/js_url.h +++ b/url/js_url.h @@ -12,34 +12,48 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include -#include -#include -#include -#include -#include +#ifndef COMPILERUNTIME_JS_API_URL_H +#define COMPILERUNTIME_JS_API_URL_H + #include -#include -#include +#include #include #include -#include +#include +#include +#include +#include #include "napi/native_api.h" #include "napi/native_node_api.h" -struct url_data { +static const int BIT_STATUS_11 = 11; // 11:Each bit of a BIT represents a different parsing state. + +enum BitsetStatusFlag { + BIT0, // 0:Bit 0 Set to true,The URL analysis failed + BIT1, // 1:Bit 1 Set to true,The protocol is the default protocol + BIT2, // 2:Bit 2 Set to true,The URL has username + BIT3, // 3:Bit 3 Set to true,The URL has password + BIT4, // 4:Bit 4 Set to true,The URL has hostname + BIT5, // 5:Bit 5 Set to true,The URL Port is the specially + BIT6, // 6:Bit 6 Set to true,The URL has pathname + BIT7, // 7:Bit 7 Set to true,The URL has query + BIT8, // 8:Bit 8 Set to true,The URL has fragment + BIT9, // 9:Bit 9 Set to true,The URL Can not be base + BIT10, // 10:Bit 10 Set to true,The host is IPV6 +}; + +struct UrlData { int port = -1; - std::string scheme; - std::string username; - std::string password; - std::string host; - std::string query; - std::string fragment; std::vector path; + std::string password = ""; + std::string scheme = ""; + std::string query = ""; + std::string username = ""; + std::string fragment = ""; + std::string host = ""; }; -class URL -{ +class URL { public: URL(napi_env env, const std::string& input); URL(napi_env env, const std::string& input, const std::string& base); @@ -68,37 +82,38 @@ public: napi_value GetIsIpv6() const; napi_value GetHost() const; - static void InitOnlyInput(std::string& input, url_data& urlData, std::bitset<11>& flags); + static void InitOnlyInput(std::string& input, UrlData& urlData, std::bitset& flags); virtual ~URL() {} private: - url_data urlData_; - std::bitset<11> flags_; + UrlData urlData_; + std::bitset flags_; // bitset<11>:Similar to bool array, each bit status represents the real-time status of current URL parsing napi_env env_ = nullptr; }; - + class URLSearchParams { public: - URLSearchParams(napi_env env); + explicit URLSearchParams(napi_env env); virtual ~URLSearchParams() {} - napi_value IsHas(napi_value name); + napi_value IsHas(napi_value name) const; napi_value Get(napi_value buffer); napi_value GetAll(napi_value buffer); void Append(napi_value buffer, napi_value temp); void Delete(napi_value buffer); void ForEach(napi_value function, napi_value thisVar); - napi_value Entries(); + napi_value Entries() const; void Set(napi_value name, napi_value value); void Sort(); napi_value ToString(); napi_value IterByKeys(); napi_value IterByValues(); void SetArray(std::vector input); - napi_value GetArray(); - std::vector StringParmas (std::string Stringpar); + napi_value GetArray() const; + std::vector StringParmas(std::string Stringpar); private: std::string ToUSVString(std::string inputStr); void HandleIllegalChar(std::wstring& inputStr, std::wstring::const_iterator it); std::vector searchParams; napi_env env; -}; \ No newline at end of file +}; +#endif /* COMPILERUNTIME_JS_API_URL_H */ diff --git a/url/js_url.js b/url/js_url.js old mode 100644 new mode 100755 index 18387e1f..c6c8ea6d --- a/url/js_url.js +++ b/url/js_url.js @@ -14,7 +14,7 @@ */ 'use strict'; -const urlUtil = requireInternal("url"); +const urlUtil = requireInternal('url'); let seachParamsArr = []; class URLSearchParams { @@ -40,7 +40,6 @@ class URLSearchParams { has(hasname) { return this.urlcalss.has(hasname); } - toString() { return this.urlcalss.toString(); } @@ -50,6 +49,7 @@ class URLSearchParams { } values() { + console.log('constructor failed'); return this.urlcalss.values(); } @@ -84,7 +84,6 @@ class URLSearchParams { this.urlcalss.array = out; } } - function toHleString(arg) { return arg.toString(); } @@ -99,38 +98,36 @@ function parameterProcessing(input) { return initToStringSeachParams(input); } } - function initObjectSeachParams(input) { if (typeof input[Symbol.iterator] === 'function') { return iteratorMethod(input); } return recordMethod(input); } - function recordMethod(input) { - const keys = Reflect.ownKeys(input); + const objectkeys = Reflect.ownKeys(input); seachParamsArr = []; - for (let i = 0; i <= keys.length; i++) { - const key = keys[i]; - const desc = Reflect.getOwnPropertyDescriptor(input, key); - if (desc !== undefined && desc.enumerable) { - const typedKey = toHleString(key); - const typedValue = toHleString(input[key]); - seachParamsArr.push(typedKey, typedValue); + let objectlength = objectkeys.length; + for (let i = 0; i <= objectlength; i++) { + const objectkey = objectkeys[i]; + const descry = Reflect.getOwnPropertyDescriptor(input, objectkey); + if (descry !== undefined && descry.enumerable) { + const inputkey = toHleString(objectkey); + const inputValue = toHleString(input[objectkey]); + seachParamsArr.push(inputkey, inputValue); } } return seachParamsArr; } - function iteratorMethod(input) { let pairs = []; seachParamsArr = []; for (const pair of input) { - const convertedPair = []; + const conversionsPair = []; for (let element of pair) { - convertedPair.push(element); + conversionsPair.push(element); } - pairs.push(convertedPair); + pairs.push(conversionsPair); } for (const pair of pairs) { @@ -150,7 +147,6 @@ function initToStringSeachParams(input) { seachParamsArr = urlUtil.stringParmas(strVal); return seachParamsArr; } - class URL { href_; search_; @@ -255,7 +251,6 @@ class URL { this.href_ = temp; } } - Object.defineProperties(URL.prototype, { to_string: { @@ -471,7 +466,6 @@ Object.defineProperties(URL.prototype, { } } }); - export default { URLSearchParams: URLSearchParams, URL: URL, diff --git a/url/native_module_url.cpp b/url/native_module_url.cpp old mode 100644 new mode 100755 index f4aaf0d1..6e14211d --- a/url/native_module_url.cpp +++ b/url/native_module_url.cpp @@ -1,4 +1,4 @@ - /* +/* * Copyright (c) 2021 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -12,10 +12,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "utils/log.h" +#include "js_url.h" #include "napi/native_api.h" #include "napi/native_node_api.h" -#include "js_url.h" -#include "utils/log.h" extern const char _binary_js_url_js_start[]; extern const char _binary_js_url_js_end[]; @@ -31,60 +31,70 @@ static napi_value UrlConstructor(napi_env env, napi_callback_info info) if (argc == 1) { NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data)); napi_valuetype valuetype; - NAPI_CALL(env,napi_typeof(env, argv[0], &valuetype)); + NAPI_CALL(env, napi_typeof(env, argv[0], &valuetype)); if (valuetype == napi_string) { char* type = nullptr; size_t typelen = 0; NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], nullptr, 0, &typelen)); - type = new char[typelen+1]; + if (typelen > 0) { + type = new char[typelen + 1]; + } NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], type, typelen + 1, &typelen)); - object = new URL(env,type); + object = new URL(env, type); delete[] type; } else { HILOG_INFO("Parameter error"); } - } else if (argc == 2) { + } else if (argc == 2) { // 2:When the input parameter is set to 2 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data)); napi_valuetype valuetype1; napi_valuetype valuetype2; - NAPI_CALL(env,napi_typeof(env, argv[0], &valuetype1)); + std::string input = ""; + NAPI_CALL(env, napi_typeof(env, argv[0], &valuetype1)); if (valuetype1 == napi_string) { char* tem = nullptr; size_t temlen = 0; NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], nullptr, 0, &temlen)); - tem = new char[temlen+1]; - NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], tem, temlen + 1, &temlen)); - std::string input_(tem); - delete[] tem; - NAPI_CALL(env,napi_typeof(env, argv[1], &valuetype2)); - if (valuetype2 == napi_string){ + if (temlen > 0) { + tem = new char[temlen + 1]; + NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], tem, temlen + 1, &temlen)); + input = tem; + delete[] tem; + } + NAPI_CALL(env, napi_typeof(env, argv[1], &valuetype2)); + if (valuetype2 == napi_string) { + std::string base = ""; char* type1 = nullptr; size_t typelen1 = 0; NAPI_CALL(env, napi_get_value_string_utf8(env, argv[1], nullptr, 0, &typelen1)); - type1 = new char[typelen1 + 1]; - NAPI_CALL(env, napi_get_value_string_utf8(env, argv[1], type1, typelen1 + 1, &typelen1)); - std::string base_(type1); - delete[] type1; - object = new URL(env, input_, base_); + if (typelen1 > 0) { + type1 = new char[typelen1 + 1]; + NAPI_CALL(env, napi_get_value_string_utf8(env, argv[1], type1, typelen1 + 1, &typelen1)); + base = type1; + delete[] type1; + } + object = new URL(env, input, base); } else if (valuetype2 == napi_object) { - URL * temp = nullptr; + URL* temp = nullptr; NAPI_CALL(env, napi_unwrap(env, argv[1], (void**)&temp)); - object = new URL(env,input_,*temp); + object = new URL(env, input, *temp); } else { HILOG_INFO("secondParameter error"); } } else { HILOG_INFO("firstParameter error"); - } + } } - NAPI_CALL(env, napi_wrap(env, thisVar, object, - [](napi_env env, void* data, void* hint) { - auto object = (URL*)data; - if (object != nullptr) { - delete object; - } - }, nullptr, nullptr)); - return thisVar; + NAPI_CALL(env, napi_wrap( + env, thisVar, object, + [](napi_env env, void* data, void* hint) { + auto object = (URL*)data; + if (object != nullptr) { + delete object; + } + }, + nullptr, nullptr)); + return thisVar; } static napi_value GetHostname(napi_env env, napi_callback_info info) @@ -202,13 +212,16 @@ static napi_value SetHref(napi_env env, napi_callback_info info) napi_value thisVar = nullptr; napi_value argv[1] = {0}; size_t argc = 1; + std::string input = ""; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr)); char* type = nullptr; size_t typelen = 0; NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], nullptr, 0, &typelen)); - type = new char[typelen+1]; - NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], type, typelen + 1, &typelen)); - std::string input(type); + if (typelen > 0) { + type = new char[typelen + 1]; + NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], type, typelen + 1, &typelen)); + input = type; + } if (type != nullptr) { delete[] type; type = nullptr; @@ -226,13 +239,16 @@ static napi_value SetHostname(napi_env env, napi_callback_info info) napi_value thisVar = nullptr; napi_value argv[1] = {0}; size_t argc = 1; + std::string input = ""; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr)); char* type = nullptr; size_t typelen = 0; NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], nullptr, 0, &typelen)); - type = new char[typelen+1]; - NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], type, typelen + 1, &typelen)); - std::string input(type); + if (typelen > 0) { + type = new char[typelen + 1]; + NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], type, typelen + 1, &typelen)); + input = type; + } if (type != nullptr) { delete[] type; type = nullptr; @@ -250,13 +266,16 @@ static napi_value SetPort(napi_env env, napi_callback_info info) napi_value thisVar = nullptr; napi_value argv[1] = {0}; size_t argc = 1; + std::string input = ""; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr)); char* type = nullptr; size_t typelen = 0; NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], nullptr, 0, &typelen)); - type = new char[typelen+1]; - NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], type, typelen + 1, &typelen)); - std::string input(type); + if (typelen > 0) { + type = new char[typelen + 1]; + NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], type, typelen + 1, &typelen)); + input = type; + } if (type != nullptr) { delete[] type; type = nullptr; @@ -274,13 +293,16 @@ static napi_value SetHost(napi_env env, napi_callback_info info) napi_value thisVar = nullptr; napi_value argv[1] = {0}; size_t argc = 1; + std::string input = ""; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr)); char* type = nullptr; size_t typelen = 0; NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], nullptr, 0, &typelen)); - type = new char[typelen+1]; - NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], type, typelen + 1, &typelen)); - std::string input(type); + if (typelen > 0) { + type = new char[typelen + 1]; + NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], type, typelen + 1, &typelen)); + input = type; + } if (type != nullptr) { delete[] type; type = nullptr; @@ -298,13 +320,16 @@ static napi_value SetSearch(napi_env env, napi_callback_info info) napi_value thisVar = nullptr; napi_value argv[1] = {0}; size_t argc = 1; + std::string input = ""; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr)); char* type = nullptr; size_t typelen = 0; NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], nullptr, 0, &typelen)); - type = new char[typelen+1]; - NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], type, typelen + 1, &typelen)); - std::string input(type); + if (typelen > 0) { + type = new char[typelen + 1]; + NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], type, typelen + 1, &typelen)); + input = type; + } if (type != nullptr) { delete[] type; type = nullptr; @@ -322,13 +347,16 @@ static napi_value SetScheme(napi_env env, napi_callback_info info) napi_value thisVar = nullptr; napi_value argv[1] = {0}; size_t argc = 1; + std::string input = ""; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr)); char* type = nullptr; size_t typelen = 0; NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], nullptr, 0, &typelen)); - type = new char[typelen+1]; - NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], type, typelen + 1, &typelen)); - std::string input(type); + if (typelen > 0) { + type = new char[typelen + 1]; + NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], type, typelen + 1, &typelen)); + input = type; + } if (type != nullptr) { delete[] type; type = nullptr; @@ -346,13 +374,16 @@ static napi_value SetFragment(napi_env env, napi_callback_info info) napi_value thisVar = nullptr; napi_value argv[1] = {0}; size_t argc = 1; + std::string input = ""; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr)); char* type = nullptr; size_t typelen = 0; NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], nullptr, 0, &typelen)); - type = new char[typelen+1]; - NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], type, typelen + 1, &typelen)); - std::string input(type); + if (typelen > 0) { + type = new char[typelen + 1]; + NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], type, typelen + 1, &typelen)); + input = type; + } if (type != nullptr) { delete[] type; type = nullptr; @@ -370,13 +401,16 @@ static napi_value SetUsername(napi_env env, napi_callback_info info) napi_value thisVar = nullptr; napi_value argv[1] = {0}; size_t argc = 1; + std::string input = ""; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr)); char* type = nullptr; size_t typelen = 0; NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], nullptr, 0, &typelen)); - type = new char[typelen+1]; - NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], type, typelen + 1, &typelen)); - std::string input(type); + if (typelen > 0) { + type = new char[typelen + 1]; + NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], type, typelen + 1, &typelen)); + input = type; + } if (type != nullptr) { delete[] type; type = nullptr; @@ -394,13 +428,16 @@ static napi_value SetPath(napi_env env, napi_callback_info info) napi_value thisVar = nullptr; napi_value argv[1] = {0}; size_t argc = 1; + std::string input = ""; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr)); char* type = nullptr; size_t typelen = 0; NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], nullptr, 0, &typelen)); - type = new char[typelen+1]; - NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], type, typelen + 1, &typelen)); - std::string input(type); + if (typelen > 0) { + type = new char[typelen + 1]; + NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], type, typelen + 1, &typelen)); + input = type; + } if (type != nullptr) { delete[] type; type = nullptr; @@ -418,13 +455,16 @@ static napi_value SetPassword(napi_env env, napi_callback_info info) napi_value thisVar = nullptr; napi_value argv[1] = {0}; size_t argc = 1; + std::string input = ""; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr)); char* type = nullptr; size_t typelen = 0; NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], nullptr, 0, &typelen)); - type = new char[typelen+1]; - NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], type, typelen + 1, &typelen)); - std::string input(type); + if (typelen > 0) { + type = new char[typelen + 1]; + NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], type, typelen + 1, &typelen)); + input = type; + } if (type != nullptr) { delete[] type; type = nullptr; @@ -464,17 +504,21 @@ static napi_value SetArray(napi_env env, napi_callback_info info) napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); napi_get_array_length(env, argv[0], &length); std::vector vec; - char* cstr = nullptr; size_t arraySize = 0; napi_value napiStr = nullptr; for (size_t i = 0; i < length; i++) { + char* cstr = nullptr; napi_get_element(env, argv[0], i, &napiStr); napi_get_value_string_utf8(env, napiStr, nullptr, 0, &arraySize); - cstr = new char[arraySize + 1]; - napi_get_value_string_utf8(env, napiStr, cstr, arraySize + 1, &arraySize); - vec.push_back(cstr); - delete []cstr; - cstr = nullptr; + if (arraySize > 0) { + cstr = new char[arraySize + 1]; + napi_get_value_string_utf8(env, napiStr, cstr, arraySize + 1, &arraySize); + vec.push_back(cstr); + delete []cstr; + cstr = nullptr; + } else { + vec.push_back(""); + } } URLSearchParams* murl = nullptr; NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&murl)); @@ -500,8 +544,7 @@ static napi_value Get(napi_env env, napi_callback_info info) size_t argc = 1; napi_value args = nullptr; napi_get_cb_info(env, info, &argc, &args, &thisVar, nullptr); - - if(argc != 1) { + if (argc != 1) { HILOG_INFO("One arg needs to be specified"); return nullptr; } @@ -517,8 +560,7 @@ static napi_value GetAll(napi_env env, napi_callback_info info) size_t argc = 1; napi_value args = nullptr; napi_get_cb_info(env, info, &argc, &args, &thisVar, nullptr); - - if(argc != 1) { + if (argc != 1) { HILOG_INFO("One arg needs to be specified"); return nullptr; } @@ -535,8 +577,7 @@ static napi_value Append(napi_env env, napi_callback_info info) napi_value args[2] = { 0 }; void* data = nullptr; napi_get_cb_info(env, info, &argc, args, &thisVar, &data); - - if(argc != 2) { + if (argc != 2) { // 2:If the input parameter is not set to 2, HILOG_INFO("Two args needs to be specified"); return nullptr; } @@ -552,8 +593,7 @@ static napi_value Delete(napi_env env, napi_callback_info info) size_t argc = 1; napi_value args = nullptr; napi_get_cb_info(env, info, &argc, &args, &thisVar, nullptr); - - if(argc != 1) { + if (argc != 1) { HILOG_INFO("One arg needs to be specified"); return nullptr; } @@ -569,7 +609,6 @@ static napi_value ForEach(napi_env env, napi_callback_info info) size_t argc = 1; napi_value args = nullptr; napi_get_cb_info(env, info, &argc, &args, &thisVar, nullptr); - URLSearchParams* object = nullptr; napi_unwrap(env, thisVar, (void**)&object); object->ForEach(args, thisVar); @@ -660,26 +699,26 @@ static napi_value IterByValues(napi_env env, napi_callback_info info) return result; } -std::vector stringParsing (std::string Stringpar) +std::vector StringParsing(std::string stringParm) { std::vector seachParasVec; size_t strStartPos = 0; size_t strLastPos = 0; bool isHasSpace = false; std::string buf = ""; - size_t i; - for(i = 0;i < Stringpar.length(); i++) { - char code = Stringpar[i]; - switch(code) { - case '&' : + size_t i = 0; + for (i = 0; i < stringParm.length(); i++) { + char code = stringParm[i]; + switch (code) { + case '&': { - if (strStartPos == i){ + if (strStartPos == i) { strLastPos = i + 1; strStartPos = i + 1; break; } if (strLastPos < i) { - buf += Stringpar.substr(strLastPos, i - strLastPos); + buf += stringParm.substr(strLastPos, i - strLastPos); } seachParasVec.push_back(buf); if (!isHasSpace) { @@ -691,13 +730,13 @@ std::vector stringParsing (std::string Stringpar) strStartPos = i + 1; break; } - case '=' : + case '=': { if (isHasSpace) { break; } if (strLastPos < i) { - buf += Stringpar.substr(strLastPos, i- strLastPos); + buf += stringParm.substr(strLastPos, i - strLastPos); } seachParasVec.push_back(buf); isHasSpace = true; @@ -705,10 +744,10 @@ std::vector stringParsing (std::string Stringpar) strLastPos = i + 1; break; } - case '+' : + case '+': { if (strLastPos < i) { - buf += Stringpar.substr(strLastPos, i - strLastPos); + buf += stringParm.substr(strLastPos, i - strLastPos); } buf += ""; strLastPos = i + 1; @@ -721,7 +760,7 @@ std::vector stringParsing (std::string Stringpar) return seachParasVec; } if (strLastPos < i) { - buf += Stringpar.substr(strLastPos, i - strLastPos); + buf += stringParm.substr(strLastPos, i - strLastPos); } seachParasVec.push_back(buf); if (!isHasSpace) { @@ -731,20 +770,23 @@ std::vector stringParsing (std::string Stringpar) } static napi_value StringParmas(napi_env env, napi_callback_info info) -{ +{ napi_value thisVar = nullptr; napi_value argv[1] = {0}; size_t argc = 1; + std::string input = ""; napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); char* type = nullptr; size_t typelen = 0; napi_get_value_string_utf8(env, argv[0], nullptr, 0, &typelen); - type = new char[typelen+1]; - napi_get_value_string_utf8(env, argv[0], type, typelen + 1, &typelen); - std::string input(type); - delete[] type; + if (typelen > 0) { + type = new char[typelen + 1]; + napi_get_value_string_utf8(env, argv[0], type, typelen + 1, &typelen); + input = type; + delete[] type; + } std::vector seachParasmsString; - seachParasmsString = stringParsing(input); + seachParasmsString = StringParsing(input); napi_value arr = nullptr; napi_create_array(env, &arr); for (size_t i = 0; i < seachParasmsString.size(); i++) { @@ -757,10 +799,9 @@ static napi_value StringParmas(napi_env env, napi_callback_info info) static napi_value SeachParamsInit(napi_env env, napi_value exports) { - const char* SeachParamsClassName = "URLSearchParams"; - napi_value SeachParamsInitClass = nullptr; - static napi_property_descriptor UrlDesc[] = - { + const char* seachParamsClassName = "URLSearchParams"; + napi_value seachParamsInitClass = nullptr; + static napi_property_descriptor UrlDesc[] = { DECLARE_NAPI_FUNCTION("has", IsHas), DECLARE_NAPI_FUNCTION("set", Set), DECLARE_NAPI_FUNCTION("sort", Sort), @@ -775,39 +816,38 @@ static napi_value SeachParamsInit(napi_env env, napi_value exports) DECLARE_NAPI_FUNCTION("entries", Entries), DECLARE_NAPI_GETTER_SETTER("array", GetArray, SetArray), }; - NAPI_CALL(env, napi_define_class(env, SeachParamsClassName, strlen(SeachParamsClassName), SeachParamsConstructor, - nullptr, sizeof(UrlDesc) / sizeof(UrlDesc[0]), UrlDesc, &SeachParamsInitClass)); + NAPI_CALL(env, napi_define_class(env, seachParamsClassName, strlen(seachParamsClassName), SeachParamsConstructor, + nullptr, sizeof(UrlDesc) / sizeof(UrlDesc[0]), UrlDesc, &seachParamsInitClass)); static napi_property_descriptor desc[] = { - DECLARE_NAPI_PROPERTY("URLSearchParams1", SeachParamsInitClass) + DECLARE_NAPI_PROPERTY("URLSearchParams1", seachParamsInitClass) }; napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); return exports; }; static napi_value UrlInit(napi_env env, napi_value exports) - { - const char* UrlClassName = "Url"; - napi_value UrlClass = nullptr; - static napi_property_descriptor UrlDesc[] = - { +{ + const char* urlClassName = "Url"; + napi_value urlClass = nullptr; + static napi_property_descriptor UrlDesc[] = { DECLARE_NAPI_GETTER_SETTER("hostname", GetHostname, SetHostname), DECLARE_NAPI_FUNCTION("href", SetHref), - DECLARE_NAPI_GETTER_SETTER("search", GetSearch, SetSearch), - DECLARE_NAPI_GETTER_SETTER("username", GetUsername, SetUsername), - DECLARE_NAPI_GETTER_SETTER("password", GetPassword, SetPassword), + DECLARE_NAPI_GETTER_SETTER("search", GetSearch, SetSearch), + DECLARE_NAPI_GETTER_SETTER("username", GetUsername, SetUsername), + DECLARE_NAPI_GETTER_SETTER("password", GetPassword, SetPassword), DECLARE_NAPI_GETTER_SETTER("host", GetHost, SetHost), - DECLARE_NAPI_GETTER_SETTER("hash", GetFragment, SetFragment), - DECLARE_NAPI_GETTER_SETTER("protocol", GetScheme, SetScheme), - DECLARE_NAPI_GETTER_SETTER("pathname", GetPath, SetPath), + DECLARE_NAPI_GETTER_SETTER("hash", GetFragment, SetFragment), + DECLARE_NAPI_GETTER_SETTER("protocol", GetScheme, SetScheme), + DECLARE_NAPI_GETTER_SETTER("pathname", GetPath, SetPath), DECLARE_NAPI_GETTER_SETTER("port", GetPort, SetPort), DECLARE_NAPI_GETTER("onOrOff", GetOnOrOff), DECLARE_NAPI_GETTER("GetIsIpv6", GetIsIpv6), }; - NAPI_CALL(env, napi_define_class(env, UrlClassName, strlen(UrlClassName), UrlConstructor, - nullptr, sizeof(UrlDesc) / sizeof(UrlDesc[0]), UrlDesc, - &UrlClass)); + NAPI_CALL(env, napi_define_class(env, urlClassName, strlen(urlClassName), UrlConstructor, + nullptr, sizeof(UrlDesc) / sizeof(UrlDesc[0]), UrlDesc, + &urlClass)); static napi_property_descriptor desc[] = { - DECLARE_NAPI_PROPERTY("Url", UrlClass) + DECLARE_NAPI_PROPERTY("Url", urlClass) }; napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); return exports; @@ -816,15 +856,15 @@ static napi_value UrlInit(napi_env env, napi_value exports) static napi_value Init(napi_env env, napi_value exports) { napi_property_descriptor desc[] = { - DECLARE_NAPI_FUNCTION("stringParmas",StringParmas), + DECLARE_NAPI_FUNCTION("stringParmas", StringParmas), }; NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc)); - SeachParamsInit(env, exports); + SeachParamsInit(env, exports); UrlInit(env, exports); return exports; } -extern "C" +extern "C" __attribute__((visibility("default"))) void NAPI_url_GetJSCode(const char** buf, int* bufLen) { if (buf != nullptr) { @@ -836,8 +876,7 @@ __attribute__((visibility("default"))) void NAPI_url_GetJSCode(const char** buf, } } -static napi_module UrlModule = -{ +static napi_module UrlModule = { .nm_version = 1, .nm_flags = 0, .nm_filename = nullptr, -- Gitee