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 28namespace OHOS { 29namespace NetManagerStandard { 30enum ProbeType : uint32_t { PROBE_HTTP = 1, PROBE_HTTPS = 2, PROBE_HTTP_HTTPS = 3 }; 31 32class NetHttpProbe { 33public: 34 NetHttpProbe(uint32_t netId, NetBearType bearType, const NetLinkInfo &netLinkInfo); 35 ~NetHttpProbe(); 36 37 int32_t SendProbe(ProbeType probeType, const std::string &httpUrl, const std::string &httpsUrl); 38 NetHttpProbeResult GetHttpProbeResult() const; 39 NetHttpProbeResult GetHttpsProbeResult() const; 40 void UpdateNetLinkInfo(const NetLinkInfo &netLinkInfo); 41 void UpdateGlobalHttpProxy(const HttpProxy &httpProxy); 42 bool HasProbeType(ProbeType inputProbeType, ProbeType hasProbeType); 43 void ProbeWithoutGlobalHttpProxy(); 44 45private: 46 static bool CurlGlobalInit(); 47 static void CurlGlobalCleanup(); 48 49 bool CheckCurlGlobalInitState(); 50 void CleanHttpCurl(); 51 void ClearProbeResult(); 52 std::string ExtractDomainFormUrl(const std::string &url); 53 std::string GetAddrInfo(const std::string &domain); 54 bool InitHttpCurl(ProbeType probeType); 55 bool SetCurlOptions(ProbeType probeType, const std::string &httpUrl, const std::string &httpsUrl); 56 bool SetHttpOptions(ProbeType probeType, CURL *curl, const std::string &url); 57 bool SetProxyOption(ProbeType probeType, bool &useProxy); 58 bool SetResolveOption(ProbeType probeType, const std::string &domain, const std::string &ipAddress, int32_t port); 59 bool SendDnsProbe(ProbeType probeType, const std::string &httpUrl, const std::string &httpsUrl, 60 const bool useProxy); 61 void SendHttpProbeRequest(); 62 void RecvHttpProbeResponse(); 63 int32_t LoadProxy(std::string &proxyHost, int32_t &proxyPort); 64 bool SetUserInfo(CURL *curlHandler); 65 bool SetProxyInfo(CURL *curlHandler, const std::string &proxyHost, int32_t proxyPort); 66 67private: 68 static std::mutex initCurlMutex_; 69 static int32_t useCurlCount_; 70 71 std::mutex proxyMtx_; 72 bool isCurlInit_ = false; 73 bool defaultUseGlobalHttpProxy_ = true; 74 uint32_t netId_ = 0; 75 NetBearType netBearType_ = BEARER_DEFAULT; 76 NetLinkInfo netLinkInfo_; 77 HttpProxy globalHttpProxy_; 78 CURLM *curlMulti_ = nullptr; 79 CURL *httpCurl_ = nullptr; 80 CURL *httpsCurl_ = nullptr; 81 curl_slist *httpResolveList_ = nullptr; 82 curl_slist *httpsResolveList_ = nullptr; 83 NetHttpProbeResult httpProbeResult_; 84 NetHttpProbeResult httpsProbeResult_; 85}; 86} // namespace NetManagerStandard 87} // namespace OHOS 88#endif // NET_HTTP_PROBE_H