diff --git a/url/js_url.cpp b/url/js_url.cpp index 1b53b24..6a2a426 100755 --- a/url/js_url.cpp +++ b/url/js_url.cpp @@ -19,24 +19,24 @@ #include "securec.h" #include "utils/log.h" namespace OHOS::Url { - static std::map g_head = { + std::map g_head = { {"ftp:", 21}, {"file:", -1}, {"gopher:", 70}, {"http:", 80}, {"https:", 443}, {"ws:", 80}, {"wss:", 443} }; - static std::vector g_doubleSegment = { + std::vector g_doubleSegment = { "..", ".%2e", ".%2E", "%2e.", "%2E.", "%2e%2e", "%2E%2E", "%2e%2E", "%2E%2e" }; - static std::vector g_singlesegment = { ".", "%2e", "%2E" }; + std::vector g_singlesegment = { ".", "%2e", "%2E" }; - static std::vector g_specialcharacter = { + std::vector g_specialcharacter = { '\0', '\t', '\n', '\r', ' ', '#', '%', '/', ':', '?', '@', '[', '\\', ']' }; - static void ReplaceSpecialSymbols(std::string& input, std::string& oldstr, std::string& newstr) + void ReplaceSpecialSymbols(std::string& input, std::string& oldstr, std::string& newstr) { size_t oldlen = oldstr.size(); while (true) { @@ -64,7 +64,7 @@ namespace OHOS::Url { return false; } - static unsigned AsciiToHex(const unsigned char pram) + unsigned AsciiToHex(const unsigned char pram) { if (pram >= '0' && pram <= '9') { return pram - '0'; @@ -78,7 +78,7 @@ namespace OHOS::Url { return static_cast(-1); } - static std::string DecodePercent(const char *input, size_t len) + std::string DecodePercent(const char *input, size_t len) { std::string temp; if (len == 0) { @@ -106,7 +106,7 @@ namespace OHOS::Url { return temp; } - static void DeleteC0OrSpace(std::string& str) + void DeleteC0OrSpace(std::string& str) { if (str.empty()) { return; @@ -131,7 +131,7 @@ namespace OHOS::Url { } } - static void DeleteTabOrNewline(std::string& str1) + void DeleteTabOrNewline(std::string& str1) { for (auto item = str1.begin(); item != str1.end();) { if (IsASCIITabOrNewline(*item)) { @@ -142,7 +142,7 @@ namespace OHOS::Url { } } - static bool IsSpecial(std::string scheme) + bool IsSpecial(std::string scheme) { auto temp = g_head.count(scheme); if (temp > 0) { @@ -151,7 +151,7 @@ namespace OHOS::Url { return false; } - static bool AnalysisScheme(std::string& input, std::string& scheme, + bool AnalysisScheme(std::string& input, std::string& scheme, std::bitset(BitsetStatusFlag::BIT_STATUS_11)>& flags) { if (!isalpha(input[0])) { @@ -178,20 +178,20 @@ namespace OHOS::Url { } } - static void AnalysisFragment(const std::string& input, std::string& fragment, + void AnalysisFragment(const std::string& input, std::string& fragment, std::bitset(BitsetStatusFlag::BIT_STATUS_11)>& flags) { fragment = input; flags.set(static_cast(BitsetStatusFlag::BIT8)); } - static void AnalysisQuery(const std::string& input, std::string& query, + void AnalysisQuery(const std::string& input, std::string& query, std::bitset(BitsetStatusFlag::BIT_STATUS_11)>& flags) { query = input; flags.set(static_cast(BitsetStatusFlag::BIT7)); } - static void AnalysisUsernameAndPasswd(std::string& input, std::string& username, std::string& password, + void AnalysisUsernameAndPasswd(std::string& input, std::string& username, std::string& password, std::bitset(BitsetStatusFlag::BIT_STATUS_11)>& flags) { int pos = input.size() - 1; @@ -215,7 +215,6 @@ namespace OHOS::Url { } } } - if (userAndPasswd.find(':') != std::string::npos) { size_t i = userAndPasswd.find(':'); std::string user = userAndPasswd.substr(0, i); @@ -234,7 +233,7 @@ namespace OHOS::Url { } } - static void AnalysisPath(std::string& input, std::vector& path, + void AnalysisPath(std::string& input, std::vector& path, std::bitset(BitsetStatusFlag::BIT_STATUS_11)>& flags, bool isSpecial) { std::vector temp; @@ -276,7 +275,7 @@ namespace OHOS::Url { } } - static void AnalysisPort(std::string input, UrlData& urlinfo, + void AnalysisPort(std::string input, UrlData& urlinfo, std::bitset(BitsetStatusFlag::BIT_STATUS_11)>& flags) { for (auto i : input) { @@ -302,7 +301,7 @@ namespace OHOS::Url { urlinfo.port = it; } - static void AnalysisOpaqueHost(std::string input, std::string& host, + void AnalysisOpaqueHost(std::string input, std::string& host, std::bitset(BitsetStatusFlag::BIT_STATUS_11)>& flags) { size_t strlen = input.size(); @@ -318,350 +317,329 @@ namespace OHOS::Url { flags.set(static_cast(BitsetStatusFlag::BIT4)); } - static std::string IPv6ZeroComperess(std::vector& tempIPV6, std::string& input, - int maxZeroIndex, int max) + std::string DealIpv4(std::string str) { - for (int i = 0; i < maxZeroIndex; ++i) { - input += tempIPV6[i]; - if (i != maxZeroIndex - 1) { - input += ":"; - } + std::vector temp; + size_t pos = str.rfind(":"); + size_t index = pos; + size_t left = pos + 1; + char hexVal[3] = { 0 }; + std::string val = ""; + while ((pos = str.find(".", left)) != std::string::npos) { + val = str.substr(left, pos - left); + snprintf(hexVal, sizeof(hexVal), "%02x", stoi(val)); + temp.push_back(hexVal); + left = pos + 1; } - 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; + val = str.substr(left); + snprintf(hexVal, sizeof(hexVal), "%02x", stoi(val)); + temp.push_back(hexVal); + std::string res = str.substr(0, index); + res = res + ":" + temp[0] + temp[1] + ":" + temp[2] + temp[3]; + return res; } - static std::string IPv6NoComperess(std::vector& tempIPV6, std::string& input) + std::string FormatIpv6(std::string ipv6String) { - size_t strlen = tempIPV6.size(); - for (size_t i = 0; i < strlen; ++i) { - if (tempIPV6[i][0] == '?' && tempIPV6[i].size() == 1) { - input += ":"; - } else { - input += tempIPV6[i]; - if (i != tempIPV6.size() - 1) { - input += ":"; - } - } - } - return input; + std::string str = ipv6String; + size_t pos = str.find("::"); + size_t index = pos; + if (pos == std::string::npos) { + return str; + } + size_t left = 0; + size_t count = 0; + while ((pos = str.find(":", left)) != std::string::npos) + { + count++; + left = pos + 1; + } + int size = 7 - (count - 2); // 7:point number 2:Continuous colon number + std::string temp = ""; + for (int i = 0; i < size - 1; ++i) { + temp += ":0"; + } + temp += ":"; + str.replace(index, 2, temp); // 2:jump"::" + if (index == 0) { + str = "0" + str; + } + return str; } - static std::string IPv6HostCompress(std::vector& tempIPV6, int flag) + void RemoveLeadingZeros(std::vector &ipv6) { - std::string input; - if (flag == 1) { - return IPv6NoComperess(tempIPV6, input); + size_t len = ipv6.size(); + for (size_t i = 0; i < len; ++i) { + size_t strLen = ipv6[i].size(); + size_t count = 0; + size_t j = 0; + for (j = 0; j < strLen; ++j) { + if (ipv6[i][j] != '0') { + break; + } + count++; + } + if (count == strLen) { + ipv6[i] = "0"; + } + else if (count != 0) { + ipv6[i] = ipv6[i].substr(j); + } } - int max = 0; - int count = 0; - size_t maxZeroIndex = 0; - size_t strlen = tempIPV6.size(); - for (size_t i = 0; i < strlen;) { - if (tempIPV6[i] == "0" && (i + 1 < strlen && tempIPV6[i + 1] == "0")) { - int index = i; - while (i < strlen && tempIPV6[i] == "0") { - i++; - count++; - } - if (max < count) { - max = count; - maxZeroIndex = index; - } - } else { - count = 0; + } + + std::string ZeroCompression(std::vector &ipv6) + { + size_t maxIndex = 0; + size_t maxSize = 0; + size_t index = 0; + size_t size = 0; + bool isNeedZeroCompression = false; + size_t len = ipv6.size(); + for (size_t i = 0; i < len; ++i) { + index = i; + size = 0; + while (i < len && ipv6[i] == "0") { + isNeedZeroCompression = true; + size++; i++; } + maxSize < size ? maxSize = size, maxIndex = index : 0; } - if (count == 8) { // 8:If IPv6 is all 0 - return "::"; - } else if (max == 0) { - strlen = tempIPV6.size(); - for (size_t i = 0; i < strlen; ++i) { - input += tempIPV6[i]; - if (i != strlen - 1) { - input += ":"; - } + std::string res = ""; + size_t ipv6Len = ipv6.size(); + for (size_t i = 0; i < ipv6Len; ++i) { + if (isNeedZeroCompression && i == maxIndex) { + maxIndex == 0 ? res += "::" : res += ":"; + i += maxSize - 1; + continue; } - return input; - } else if (maxZeroIndex == 0) { - input += "::"; - strlen = tempIPV6.size(); - for (size_t i = max; i < strlen; ++i) { - input += tempIPV6[i] + ":"; - } - input.pop_back(); - return input; - } else { - return IPv6ZeroComperess(tempIPV6, input, maxZeroIndex, max); + res += ipv6[i]; + i != (ipv6Len - 1) ? res += ":" : ""; } + return res; } - void DealWithtempIpv6(std::vector &tempIpv6, std::stringstream &ss, - std::string &numberHex, const int tempProt[4], int len) + void ToLower(std::string &str) { - tempIpv6.push_back(numberHex); - ss.clear(); - numberHex.clear(); - if (len == 0) { - return; - } - ss << std::hex << tempProt[2] * 0x100 + tempProt[3]; // 2: 3:subscript position - ss >> numberHex; - tempIpv6.push_back(numberHex); - ss.clear(); - numberHex.clear(); - tempIpv6.erase(tempIpv6.end() - 3); // 3:Remove redundant space - } - - void IPv6DealWithColon(int& flag, std::string& strInput, - std::bitset(BitsetStatusFlag::BIT_STATUS_11)>& flags, size_t &pos) - { - flag = 1; - if (strInput.find("::", pos + 2) != std::string::npos) { // 2:Subscript Move Right2 - flags.set(static_cast(BitsetStatusFlag::BIT0)); - } - return; - } - - void IsFlagExist(size_t &pos, std::vector &temp, std::vector &tempEnd, - std::bitset(BitsetStatusFlag::BIT_STATUS_11)> &flags, unsigned &numberFlag) - { - while (((pos = temp[numberFlag].find('.')) != std::string::npos)) { - tempEnd.push_back(temp[numberFlag].substr(0, pos)); - temp[numberFlag] = temp[numberFlag].substr(pos + 1); - } - tempEnd.push_back(temp[numberFlag]); - if (tempEnd.size() != 4) { // 4:The size is not 4 - flags.set(static_cast(BitsetStatusFlag::BIT0)); - } - } - void DealWithProt(std::vector &tempEnd, unsigned &val, - std::bitset(BitsetStatusFlag::BIT_STATUS_11)>& flags, - int &number, int tempProt[4]) - { - size_t strlen = tempEnd.size(); - for (size_t x = 0; x < strlen; ++x) { - val = stoi(tempEnd[x]); - if (val > 255) { // 255:The maximum value is 255 - flags.set(static_cast(BitsetStatusFlag::BIT0)); - return; - } - tempProt[number] = val; - number++; - val = 0; - } - } - - void DealWithElse(std::vector &temp, size_t &i, unsigned &numberFlag, - std::bitset(BitsetStatusFlag::BIT_STATUS_11)>& flags, unsigned &val) - { - size_t strlen = temp[i].size(); - for (size_t j = 0; j < strlen; ++j) { - if (((temp[i].find('.')) != std::string::npos)) { - numberFlag = i; - if (temp.size() == i || temp.size() > 7) { // 7:The size cannot be greater than 7 - flags.set(static_cast(BitsetStatusFlag::BIT0)); - return; - } - return; - } else if (IsHexDigit(temp[i][j])) { - val = val * 0x10 + AsciiToHex(temp[i][j]); + size_t strLen = str.size(); + for (size_t i = 0; i < strLen; ++i) { + if (isupper(str[i])) { + str[i] = tolower(str[i]); } } } - void DealWithStringStream(std::stringstream &ss, unsigned &val, - std::string &numberHex, std::vector &tempIpv6) + std::string Compress(std::string str) { - ss << std::hex << val; - ss >> numberHex; - tempIpv6.push_back(numberHex); - ss.clear(); - numberHex.clear(); - val = 0; + std::vector temp; + size_t pos = 0; + size_t left = 0; + while ((pos = str.find(":", left)) != std::string::npos) { + temp.push_back(str.substr(left, pos - left)); + left = pos + 1; + } + temp.push_back(str.substr(left)); + RemoveLeadingZeros(temp); + std::string res = ZeroCompression(temp); + ToLower(res); + return res; } - static void IPv6Host(std::string& input, std::string& host, + void IPv6Host(std::string& input, std::string& host, std::bitset(BitsetStatusFlag::BIT_STATUS_11)>& flags) { - if (input.size() == 0) { + std::regex ipv6("(::|(:((:[0-9A-Fa-f]{1,4}){1,7}))|(([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|" + "(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|:))|(([0-9A-Fa-f]{1,4}:){2}" + "(((:[0-9A-Fa-f]{1,4}){1,5})|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})" + "|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|:))|(([0-9A-Fa-f]{1,4}:){5}" + "(((:[0-9A-Fa-f]{1,4}){1,2})|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|:))|" + "(((:(:[0-9A-Fa-f]{1,4}){0,5}:)|(([0-9A-Fa-f]{1,4}:){1}(:[0-9A-Fa-f]{1,4}){0,4}:)" + "|(([0-9A-Fa-f]{1,4}:){2}(:[0-9A-Fa-f]{1,4}){0,3}:)|(([0-9A-Fa-f]{1,4}:){3}" + "(:[0-9A-Fa-f]{1,4}){0,2}:)|(([0-9A-Fa-f]{1,4}:){4}(:[0-9A-Fa-f]{1,4})?:)|" + "(([0-9A-Fa-f]{1,4}:){5}:)|(([0-9A-Fa-f]{1,4}:){6}))((25[0-5]|2[0-4]\\d|1\\d{2}|" + "[1-9]\\d|\\d)\\.){3}(25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d)))(%[a-zA-Z0-9._]+)?"); + if (!std::regex_match(input, ipv6)) { flags.set(static_cast(BitsetStatusFlag::BIT0)); return; } - std::string strInput = input; - std::stringstream ss; - std::string numberHex; - unsigned val = 0; - unsigned numberFlag = 0; - std::vector temp; - std::vector tempEnd; - std::vector tempIpv6; size_t pos = 0; - int tempProt[4] = { 0 }; - int number = 0; - int flag = 0; - if ((pos = strInput.find("::", 0)) != std::string::npos) { - IPv6DealWithColon(flag, strInput, flags, pos); + pos = input.find('.'); + if (pos != std::string::npos) { + input = DealIpv4(input); } - while (((pos = strInput.find(':')) != std::string::npos)) { - temp.push_back(strInput.substr(0, pos)); - strInput = strInput.substr(pos + 1); - } - temp.push_back(strInput); - if (temp.size() > 8) { // 8:The incoming value does not meet the criteria - flags.set(static_cast(BitsetStatusFlag::BIT0)); - return; - } - size_t length = temp.size(); - for (size_t i = 0; i < length; ++i) { - if (temp[i].empty()) { - tempIpv6.push_back("?"); - } else { - DealWithElse(temp, i, numberFlag, flags, val); - DealWithStringStream(ss, val, numberHex, tempIpv6); - } - } - if (numberFlag != 0) { - IsFlagExist(pos, temp, tempEnd, flags, numberFlag); - DealWithProt(tempEnd, val, flags, number, tempProt); - ss << std::hex << tempProt[0] * 0x100 + tempProt[1]; - ss >> numberHex; - DealWithtempIpv6(tempIpv6, ss, numberHex, tempProt, 4); // 4:tempProtlen - } - strInput = IPv6HostCompress(tempIpv6, flag); - host = '[' + strInput + ']'; + input = FormatIpv6(input); + input = Compress(input); + host = "[" + input + "]"; flags.set(static_cast(BitsetStatusFlag::BIT4)); flags.set(static_cast(BitsetStatusFlag::BIT10)); } - static bool CheckNunType(const char ch, const unsigned num) - { - enum class NUMERATION { - OCT = 8, // 8:Octal - DEC = 10, // 10:Decimal - HEX = 16 // 16:Hexadecimal - }; - if (NUMERATION(num) == NUMERATION::OCT) { - if (ch < '0' || ch > '7') { // 0~7:Octal - return false; - } - } else if (NUMERATION(num) == NUMERATION::DEC) { - if (ch < '0' || ch > '9') { // 0~9:Decimal - return false; - } - } else if (NUMERATION(num) == NUMERATION::HEX) { - if (!((ch >= '0' && ch <= '9') || // 0~9, a~f, A~F:Hexadecimal - (ch >= 'A' && ch <= 'F') || - (ch >= 'a' && ch <= 'f'))) { + bool IsRadix(std::string num, std::string radix) { + size_t len = num.size(); + for (size_t i = 0; i < len; ++i) { + if (radix.find(num[i]) == std::string::npos) { return false; } } return true; } - static int64_t AnalyseNum(std::string parts) + bool IsNumber(std::string num, int &radix) { - unsigned num = 10; // 10:Decimal - 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")) { // 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") { // 10:Conversion to Decimal Coefficient - num = 8; // 8:Convert to octal - parts = parts.substr(1); - } - for (size_t i = 0; i < parts.length(); i++) { - bool ret = CheckNunType(parts[i], num); - if (!ret) { - return -1; - } - } - return strtoll(parts.c_str(), nullptr, num); - } - - static bool OverHex(std::string input) - { - size_t size = input.size(); - for (size_t i = 0; i < size; i++) { - return !IsHexDigit(input[i]); + size_t len = num.size(); + if (len > 2 && num[0] == '0' && (num[1] == 'x' || num[1] == 'X')) { // 2:hex head length + radix = 16; // 16:hex + return IsRadix(num.substr(2), "0123456789abcdefABCDEF"); // 2:jump 0x + } else if (len > 1 && num[0] == '0') { + radix = 8; // 8:octal + return IsRadix(num.substr(1), "01234567"); + } else if (IsRadix(num, "0123456789")) { + radix = 10; // 10:decimal + return true; } return false; } - static bool NotAllNum(std::string input) + std::string BinaryConversion(std::string num, int radix) { - size_t size = input.size(); - for (size_t i = 0; i < size; i++) { - if (!isdigit(input[i])) { - return true; - } + int val = 0; + if (radix == 16) { // 16:hex + sscanf_s(num.c_str(),"%x", &val); + return std::to_string(val); + } else if (radix == 8) { // 8:octal + sscanf_s(num.c_str(), "%o", &val); + return std::to_string(val); + } else { + return num; } - return false; } - static bool AnalyseIPv4(const char *instr, std::string &host, - std::bitset(BitsetStatusFlag::BIT_STATUS_11)> &flags) + bool RemovalIpv4(std::vector &temp, std::string str) { - int count = 0; - for (const char *ptr = instr; *ptr != '\0'; ptr++) { - if (*ptr == '.') { - if (++count > 3) { // 3:The IPV4 address has only four segments - return false; - } - } - } - if (count != 3) { // 3:The IPV4 address has only four segments - return false; - } - size_t pos = 0; - std::vector strVec; - std::string input = static_cast(instr); - while (((pos = input.find('.')) != std::string::npos)) { - strVec.push_back(input.substr(0, pos)); - input = input.substr(pos + 1); + size_t left = 0; + while ((pos = str.find(".", left)) != std::string::npos) { + temp.push_back(str.substr(left, pos - left)); + left = pos + 1; } - strVec.push_back(input); - size_t size = strVec.size(); - for (size_t i = 0; i < size; i++) { - if (strVec[i].empty()) { - return false; - } - 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)))) { // 2:Intercept - continue; - } - 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(static_cast(BitsetStatusFlag::BIT0)); - return false; + temp.push_back(str.substr(left)); + size_t tmpLen = temp.size(); + std::vector res; + for (size_t i = 0; i < tmpLen; ++i) { + int radix = 0; + if (IsNumber(temp[i], radix)) { + res.push_back(BinaryConversion(temp[i], radix)); } else { - host += std::to_string(value); - if (i != size - 1) { - host += "."; - } + return false; } } - flags.set(static_cast(BitsetStatusFlag::BIT4)); + temp = res; return true; } - static void AnalysisHost(std::string& input, std::string& host, + + int IsFormatIpv4(std::vector nums) + { + size_t len = nums.size(); + for (size_t i = 0; i < len; ++i) { + if (stoi(nums[i]) > 255) { // 255:ipv4 max value + return i; + } + } + return -1; + } + + std::string SplitNum(std::string num, size_t &number) + { + int val = stoi(num); + std::vector nums; + std::string res = ""; + while (val > 0) { + int num = val % 256; // 256:ipv4 max value + nums.push_back(std::to_string(num)); + val /= 256; // 256:ipv4 max value + } + for (int i = nums.size() - 1; i >= 0; --i) { + res += nums[i] + "."; + } + number = nums.size(); + return res.substr(0, res.size() - 1); + } + + void FormatIpv4(std::vector nums, std::string& host, + std::bitset(BitsetStatusFlag::BIT_STATUS_11)>& flags) + { + size_t len = nums.size(); + int index = IsFormatIpv4(nums); + std::string res = ""; + if (index == -1) { + for (size_t i = 0; i < len - 1; ++i) { + res += nums[i] + "."; + } + for (size_t i = 0; i < 4 - len; ++i) { // 4:ipv4 max size + res += "0."; + } + res += nums[len - 1]; + host = res; + flags.set(static_cast(BitsetStatusFlag::BIT4)); + } else if (index == static_cast(len - 1)) { + for (size_t i = 0; i < len - 1; ++i) { + res += nums[i] + "."; + } + size_t number = 0; + std::string temp = SplitNum(nums[index], number); + if (number + (len - 1) > 4) { // 4:ipv4 max size + flags.set(static_cast(BitsetStatusFlag::BIT0)); + return; + } + for (size_t i = 0; i < 4 - (len - 1 + number); ++i) { // 4:ipv4 max size + temp = "0." + temp; + } + host = res + temp; + flags.set(static_cast(BitsetStatusFlag::BIT4)); + } else { + flags.set(static_cast(BitsetStatusFlag::BIT0)); + return; + } + + } + void AnalyseIPv4(const std::string input, std::string& host, + std::bitset(BitsetStatusFlag::BIT_STATUS_11)>& flags) + { + bool isipv4 = false; + std::vector temp; + isipv4 = RemovalIpv4(temp, input); + size_t tempLen = temp.size(); + std::string res = ""; + for (size_t i = 0; i < tempLen; ++i) { + res += temp[i]; + if (i != tempLen - 1) { + res += "."; + } + } + if (isipv4) { + if (tempLen > 4) { // 4:ipv4 max size + ToLower(res); + host = res; + flags.set(static_cast(BitsetStatusFlag::BIT4)); + } else if (tempLen == 4) { // 4:ipv4 max size + if (IsFormatIpv4(temp) == -1) { + host = res; + flags.set(static_cast(BitsetStatusFlag::BIT4)); + } else { + flags.set(static_cast(BitsetStatusFlag::BIT0)); + } + } else { + FormatIpv4(temp, host, flags); + } + } else { + ToLower(res); + host = res; + flags.set(static_cast(BitsetStatusFlag::BIT4)); + } + } + void AnalysisHost(std::string& input, std::string& host, std::bitset(BitsetStatusFlag::BIT_STATUS_11)>& flags, bool special) { if (input.empty()) { @@ -693,21 +671,16 @@ namespace OHOS::Url { return; } } - bool ipv4 = AnalyseIPv4(decodeInput.c_str(), host, flags); - if (ipv4) { - return; - } - host = decodeInput; - flags.set(static_cast(BitsetStatusFlag::BIT4)); + AnalyseIPv4(decodeInput, host, flags); } - static bool ISFileNohost(const std::string& input) + bool ISFileNohost(const std::string& input) { if ((isalpha(input[0]) && (input[1] == ':' || input[1] == '|'))) { return true; } return false; } - static void AnalysisFilePath(std::string& input, UrlData& urlinfo, + void AnalysisFilePath(std::string& input, UrlData& urlinfo, std::bitset(BitsetStatusFlag::BIT_STATUS_11)>& flags) { std::vector temp; @@ -752,7 +725,7 @@ namespace OHOS::Url { } } - static void AnalysisFile(std::string& input, UrlData& urlinfo, + void AnalysisFile(std::string& input, UrlData& urlinfo, std::bitset(BitsetStatusFlag::BIT_STATUS_11)>& flags) { bool special = true; @@ -793,7 +766,7 @@ namespace OHOS::Url { } } - static void AnalysisFilescheme(std::string& input, UrlData& urlinfo, + void AnalysisFilescheme(std::string& input, UrlData& urlinfo, std::bitset(BitsetStatusFlag::BIT_STATUS_11)>& flags) { std::string strPath = urlinfo.scheme + input; @@ -839,7 +812,7 @@ namespace OHOS::Url { } } - static void AnalysisNoDefaultProtocol(std::string& input, UrlData& urlinfo, + void AnalysisNoDefaultProtocol(std::string& input, UrlData& urlinfo, std::bitset(BitsetStatusFlag::BIT_STATUS_11)>& flags) { if (urlinfo.scheme.size() == 2) { // 2:The length is 2 @@ -890,7 +863,7 @@ namespace OHOS::Url { } } - static void AnalysisOnlyHost(std::string& input, UrlData& urlinfo, + void AnalysisOnlyHost(std::string& input, UrlData& urlinfo, std::bitset(BitsetStatusFlag::BIT_STATUS_11)>& flags, size_t pos) { std::string strHost = input; @@ -922,7 +895,7 @@ namespace OHOS::Url { } } } - static void AnalysisHostAndPath(std::string& input, UrlData& urlinfo, + void AnalysisHostAndPath(std::string& input, UrlData& urlinfo, std::bitset(BitsetStatusFlag::BIT_STATUS_11)>& flags) { if (flags.test(static_cast(BitsetStatusFlag::BIT1))) { @@ -973,7 +946,7 @@ namespace OHOS::Url { } } - static void AnalysisInput(std::string& input, UrlData& urlData, + void AnalysisInput(std::string& input, UrlData& urlData, std::bitset(BitsetStatusFlag::BIT_STATUS_11)>& flags) { if (input.find('#') != std::string::npos) { @@ -992,7 +965,7 @@ namespace OHOS::Url { AnalysisPath(input, urlData.path, flags, special); } - static void BaseInfoToUrl(const UrlData& baseInfo, + void BaseInfoToUrl(const UrlData& baseInfo, const std::bitset(BitsetStatusFlag::BIT_STATUS_11)> baseflags, UrlData& urlData, std::bitset(BitsetStatusFlag::BIT_STATUS_11)>& flags, bool inputIsEmpty) { @@ -1027,7 +1000,7 @@ namespace OHOS::Url { baseflags.test(static_cast(BitsetStatusFlag::BIT10))); } - static void ShorteningPath(UrlData& baseData, bool isFile) + void ShorteningPath(UrlData& baseData, bool isFile) { if (baseData.path.empty()) { return; @@ -1039,6 +1012,44 @@ namespace OHOS::Url { baseData.path.pop_back(); } + void InitOnlyInput(std::string& input, UrlData& urlData, + std::bitset(BitsetStatusFlag::BIT_STATUS_11)>& flags) + { + if (input.empty()) { + flags.set(static_cast(BitsetStatusFlag::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)) { + 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); + } else { + AnalysisHostAndPath(str, urlData, flags); + } + } else { + flags.set(static_cast(BitsetStatusFlag::BIT0)); + return; + } + } + URL::URL(napi_env env, const std::string& input) { env_ = env; @@ -1055,7 +1066,7 @@ namespace OHOS::Url { DeleteTabOrNewline(strBase); DeleteC0OrSpace(strInput); DeleteTabOrNewline(strInput); - URL::InitOnlyInput(strBase, baseInfo, baseflags); + InitOnlyInput(strBase, baseInfo, baseflags); } URL::URL(napi_env env, const std::string& input, const std::string& base) @@ -1591,44 +1602,6 @@ namespace OHOS::Url { } } - void URL::InitOnlyInput(std::string& input, UrlData& urlData, - std::bitset(BitsetStatusFlag::BIT_STATUS_11)>& flags) - { - if (input.empty()) { - flags.set(static_cast(BitsetStatusFlag::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)) { - 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); - } else { - AnalysisHostAndPath(str, urlData, flags); - } - } else { - flags.set(static_cast(BitsetStatusFlag::BIT0)); - return; - } - } - URLSearchParams::URLSearchParams(napi_env env) : env(env) {} std::wstring StrToWstr(const std::string& str) diff --git a/url/js_url.h b/url/js_url.h index ac55ff4..9a9e1ff 100755 --- a/url/js_url.h +++ b/url/js_url.h @@ -81,8 +81,6 @@ namespace OHOS::Url { napi_value GetIsIpv6() const; napi_value GetHost() const; - static void InitOnlyInput(std::string& input, UrlData& urlData, - std::bitset(BitsetStatusFlag::BIT_STATUS_11)>& flags); virtual ~URL() {} private: UrlData urlData_;