1 /* 2 * Copyright (c) 2021-2023 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 24 namespace OHOS::NetStack::CommonUtils { 25 static const std::string DOMAIN_TYPE_HTTP_REQUEST = "httpRequest"; 26 static const std::string DOMAIN_TYPE_WEBSOCKET_REQUEST = "webSocket"; 27 enum SdkVersion { 28 FIVE = 5, 29 SIX, 30 SEVEN, 31 EIGHT, 32 NINE, 33 TEN, 34 ELEVEN, 35 TWELVE 36 }; 37 38 std::vector<std::string> Split(const std::string &str, const std::string &sep); 39 40 std::vector<std::string> Split(const std::string &str, const std::string &sep, size_t size); 41 42 std::string Strip(const std::string &str, char ch = ' '); 43 44 std::string ToLower(const std::string &s); 45 46 std::string ToString(const std::list<std::string> &lists, char tab = ','); 47 48 bool HasInternetPermission(); 49 50 bool IsAtomicService(std::string &bundleName); 51 52 bool IsAllowedHostname(const std::string &bundleName, const std::string &domainType, const std::string &url); 53 54 bool EndsWith(const std::string &str, const std::string &suffix); 55 56 std::string Trim(std::string str); 57 58 bool IsMatch(const std::string &str, const std::string &patternStr); 59 60 std::string InsertCharBefore(const std::string &input, const char from, const char preChar, const char nextChar); 61 62 std::string ReplaceCharacters(const std::string &input); 63 64 bool IsRegexValid(const std::string ®ex); 65 66 std::string GetProtocolFromURL(const std::string &url); 67 68 std::string GetPortFromURL(const std::string &url); 69 70 std::string GetHostnameFromURL(const std::string& url); 71 72 std::string GetHostnameWithProtocolAndPortFromURL(const std::string& url); 73 74 bool IsExcluded(const std::string &str, const std::string &exclusions, const std::string &split); 75 76 bool IsHostNameExcluded(const std::string &url, const std::string &exclusions, const std::string &split); 77 78 bool IsValidIP(const std::string& ip, int af); 79 80 bool IsValidIPV4(const std::string &ip); 81 82 bool IsValidIPV6(const std::string &ip); 83 84 std::string MaskIpv4(std::string &maskedResult); 85 86 std::string MaskIpv6(std::string &maskedResult); 87 88 std::string AnonymizeIp(std::string &input); 89 90 bool GetFileDataFromFilePath(const std::string& filePath, std::string& fileData); 91 } // namespace OHOS::NetStack::CommonUtils 92 #endif /* COMMUNICATIONNETSTACK_COMMON_UTILS_H */ 93