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