1 /* 2 * Copyright (c) 2021-2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef COMMUNICATIONNETSTACK_COMMON_UTILS_H 17 #define COMMUNICATIONNETSTACK_COMMON_UTILS_H 18 19 #include <iosfwd> 20 #include <list> 21 #include <vector> 22 #include <string> 23 #include <optional> 24 25 namespace OHOS::NetStack::CommonUtils { 26 static const std::string DOMAIN_TYPE_HTTP_REQUEST = "httpRequest"; 27 static const std::string DOMAIN_TYPE_WEBSOCKET_REQUEST = "webSocket"; 28 const int INVALID_IP_TYPE = -1; 29 const int MAX_PORT = 65535; 30 enum SdkVersion { 31 FIVE = 5, 32 SIX, 33 SEVEN, 34 EIGHT, 35 NINE, 36 TEN, 37 ELEVEN, 38 TWELVE 39 }; 40 41 std::vector<std::string> Split(const std::string &str, const std::string &sep); 42 43 std::vector<std::string> Split(const std::string &str, const std::string &sep, size_t size); 44 45 std::string Strip(const std::string &str, char ch = ' '); 46 47 std::string ToLower(const std::string &s); 48 49 std::string ToString(const std::list<std::string> &lists, char tab = ','); 50 51 bool HasInternetPermission(); 52 53 bool IsAtomicService(std::string &bundleName); 54 55 bool IsAllowedHostname(const std::string &bundleName, const std::string &domainType, const std::string &url); 56 57 bool EndsWith(const std::string &str, const std::string &suffix); 58 59 std::string Trim(std::string str); 60 61 bool IsMatch(const std::string &str, const std::string &patternStr); 62 63 std::string InsertCharBefore(const std::string &input, const char from, const char preChar, const char nextChar); 64 65 std::string ReplaceCharacters(const std::string &input); 66 67 bool IsRegexValid(const std::string ®ex); 68 69 std::string GetProtocolFromURL(const std::string &url); 70 71 std::string GetPortFromURL(const std::string &url); 72 73 std::string GetHostnameFromURL(const std::string& url); 74 75 std::string GetHostnameWithProtocolAndPortFromURL(const std::string& url); 76 77 bool IsExcluded(const std::string &str, const std::string &exclusions, const std::string &split); 78 79 bool IsHostNameExcluded(const std::string &url, const std::string &exclusions, const std::string &split); 80 81 int DetectIPType(const std::string &ip); 82 83 bool IsValidIP(const std::string& ip, int af); 84 85 bool IsValidIPV4(const std::string &ip); 86 87 bool IsValidIPV6(const std::string &ip); 88 89 std::string MaskIpv4(std::string &maskedResult); 90 91 std::string MaskIpv6(std::string &maskedResult); 92 93 std::string AnonymizeIp(std::string &input); 94 95 std::optional<std::string> GetBundleName(); 96 97 bool GetFileDataFromFilePath(const std::string& filePath, std::string& fileData); 98 99 bool Sha256sum(unsigned char *buf, size_t buflen, std::string &digestStr); 100 101 bool IsCertPubKeyInPinned(const std::string &certPubKeyDigest, const std::string &pinnedPubkey); 102 103 bool IsCleartextPermitted(const std::string &url, const std::string &protocol); 104 105 bool IsValidPort(const uint32_t &Port); 106 107 std::string ToAnonymousIp(const std::string &input); 108 } // namespace OHOS::NetStack::CommonUtils 109 #endif /* COMMUNICATIONNETSTACK_COMMON_UTILS_H */ 110