1 /* 2 * Copyright (C) 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 NET_HTTP_PROBE_H 17 #define NET_HTTP_PROBE_H 18 19 #include <curl/curl.h> 20 #include <curl/easy.h> 21 #include <map> 22 #include <mutex> 23 #include <string> 24 25 #include "net_http_probe_result.h" 26 #include "net_link_info.h" 27 28 namespace OHOS { 29 namespace NetManagerStandard { 30 enum ProbeType : uint32_t { 31 PROBE_HTTP = 1, 32 PROBE_HTTPS = 2, 33 PROBE_HTTP_HTTPS = 3, 34 PROBE_HTTP_FALLBACK = 4, 35 PROBE_HTTPS_FALLBACK = 5 36 }; 37 38 class NetHttpProbe { 39 public: 40 NetHttpProbe(uint32_t netId, NetBearType bearType, const NetLinkInfo &netLinkInfo, ProbeType probeType); 41 ~NetHttpProbe(); 42 43 int32_t SendProbe(ProbeType probeType, const std::string &httpUrl, const std::string &httpsUrl); 44 NetHttpProbeResult GetHttpProbeResult() const; 45 NetHttpProbeResult GetHttpsProbeResult() const; 46 void UpdateNetLinkInfo(const NetLinkInfo &netLinkInfo); 47 void UpdateGlobalHttpProxy(const HttpProxy &httpProxy); 48 bool IsHttpDetect(ProbeType probeType); 49 bool IsHttpsDetect(ProbeType probeType); 50 void ProbeWithoutGlobalHttpProxy(); 51 52 private: 53 static bool CurlGlobalInit(); 54 static void CurlGlobalCleanup(); 55 56 bool CheckCurlGlobalInitState(); 57 void CleanHttpCurl(); 58 void ClearProbeResult(); 59 std::string ExtractDomainFormUrl(const std::string &url); 60 std::string GetAddrInfo(const std::string &domain); 61 bool InitHttpCurl(ProbeType probeType); 62 bool SetCurlOptions(ProbeType probeType, const std::string &httpUrl, const std::string &httpsUrl); 63 bool SetHttpOptions(ProbeType probeType, CURL *curl, const std::string &url); 64 bool SetProxyOption(ProbeType probeType, bool &useProxy); 65 bool SetResolveOption(ProbeType probeType, const std::string &domain, const std::string &ipAddress, int32_t port); 66 bool SendDnsProbe(ProbeType probeType, const std::string &httpUrl, const std::string &httpsUrl, 67 const bool useProxy); 68 void SendHttpProbeRequest(); 69 void RecvHttpProbeResponse(); 70 int32_t LoadProxy(std::string &proxyHost, int32_t &proxyPort); 71 bool SetUserInfo(CURL *curlHandler); 72 bool SetProxyInfo(CURL *curlHandler, const std::string &proxyHost, int32_t proxyPort); 73 static size_t HeaderCallback(char* buffer, size_t size, size_t nitems, void* userdata); 74 int64_t CheckRespCode(int64_t respCode); 75 std::string GetHeaderField(std::string key); 76 int64_t CheckClientErrorRespCode(int64_t respCode); 77 78 private: 79 static std::mutex initCurlMutex_; 80 static int32_t useCurlCount_; 81 82 std::mutex proxyMtx_; 83 bool isCurlInit_ = false; 84 bool defaultUseGlobalHttpProxy_ = true; 85 uint32_t netId_ = 0; 86 NetBearType netBearType_ = BEARER_DEFAULT; 87 NetLinkInfo netLinkInfo_; 88 HttpProxy globalHttpProxy_; 89 CURLM *curlMulti_ = nullptr; 90 CURL *httpCurl_ = nullptr; 91 CURL *httpsCurl_ = nullptr; 92 curl_slist *httpResolveList_ = nullptr; 93 curl_slist *httpsResolveList_ = nullptr; 94 NetHttpProbeResult httpProbeResult_; 95 NetHttpProbeResult httpsProbeResult_; 96 std::string respHeader_; 97 ProbeType probeType_; 98 char errBuffer[CURL_ERROR_SIZE] = {0}; 99 }; 100 } // namespace NetManagerStandard 101 } // namespace OHOS 102 #endif // NET_HTTP_PROBE_H