1b1b8bc3fSopenharmony_ci/*
2b1b8bc3fSopenharmony_ci * Copyright (C) 2023 Huawei Device Co., Ltd.
3b1b8bc3fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4b1b8bc3fSopenharmony_ci * you may not use this file except in compliance with the License.
5b1b8bc3fSopenharmony_ci * You may obtain a copy of the License at
6b1b8bc3fSopenharmony_ci *
7b1b8bc3fSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8b1b8bc3fSopenharmony_ci *
9b1b8bc3fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10b1b8bc3fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11b1b8bc3fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12b1b8bc3fSopenharmony_ci * See the License for the specific language governing permissions and
13b1b8bc3fSopenharmony_ci * limitations under the License.
14b1b8bc3fSopenharmony_ci */
15b1b8bc3fSopenharmony_ci
16b1b8bc3fSopenharmony_ci#ifndef NET_HTTP_PROBE_H
17b1b8bc3fSopenharmony_ci#define NET_HTTP_PROBE_H
18b1b8bc3fSopenharmony_ci
19b1b8bc3fSopenharmony_ci#include <curl/curl.h>
20b1b8bc3fSopenharmony_ci#include <curl/easy.h>
21b1b8bc3fSopenharmony_ci#include <map>
22b1b8bc3fSopenharmony_ci#include <mutex>
23b1b8bc3fSopenharmony_ci#include <string>
24b1b8bc3fSopenharmony_ci
25b1b8bc3fSopenharmony_ci#include "net_http_probe_result.h"
26b1b8bc3fSopenharmony_ci#include "net_link_info.h"
27b1b8bc3fSopenharmony_ci
28b1b8bc3fSopenharmony_cinamespace OHOS {
29b1b8bc3fSopenharmony_cinamespace NetManagerStandard {
30b1b8bc3fSopenharmony_cienum ProbeType : uint32_t { PROBE_HTTP = 1, PROBE_HTTPS = 2, PROBE_HTTP_HTTPS = 3 };
31b1b8bc3fSopenharmony_ci
32b1b8bc3fSopenharmony_ciclass NetHttpProbe {
33b1b8bc3fSopenharmony_cipublic:
34b1b8bc3fSopenharmony_ci    NetHttpProbe(uint32_t netId, NetBearType bearType, const NetLinkInfo &netLinkInfo);
35b1b8bc3fSopenharmony_ci    ~NetHttpProbe();
36b1b8bc3fSopenharmony_ci
37b1b8bc3fSopenharmony_ci    int32_t SendProbe(ProbeType probeType, const std::string &httpUrl, const std::string &httpsUrl);
38b1b8bc3fSopenharmony_ci    NetHttpProbeResult GetHttpProbeResult() const;
39b1b8bc3fSopenharmony_ci    NetHttpProbeResult GetHttpsProbeResult() const;
40b1b8bc3fSopenharmony_ci    void UpdateNetLinkInfo(const NetLinkInfo &netLinkInfo);
41b1b8bc3fSopenharmony_ci    void UpdateGlobalHttpProxy(const HttpProxy &httpProxy);
42b1b8bc3fSopenharmony_ci    bool HasProbeType(ProbeType inputProbeType, ProbeType hasProbeType);
43b1b8bc3fSopenharmony_ci    void ProbeWithoutGlobalHttpProxy();
44b1b8bc3fSopenharmony_ci
45b1b8bc3fSopenharmony_ciprivate:
46b1b8bc3fSopenharmony_ci    static bool CurlGlobalInit();
47b1b8bc3fSopenharmony_ci    static void CurlGlobalCleanup();
48b1b8bc3fSopenharmony_ci
49b1b8bc3fSopenharmony_ci    bool CheckCurlGlobalInitState();
50b1b8bc3fSopenharmony_ci    void CleanHttpCurl();
51b1b8bc3fSopenharmony_ci    void ClearProbeResult();
52b1b8bc3fSopenharmony_ci    std::string ExtractDomainFormUrl(const std::string &url);
53b1b8bc3fSopenharmony_ci    std::string GetAddrInfo(const std::string &domain);
54b1b8bc3fSopenharmony_ci    bool InitHttpCurl(ProbeType probeType);
55b1b8bc3fSopenharmony_ci    bool SetCurlOptions(ProbeType probeType, const std::string &httpUrl, const std::string &httpsUrl);
56b1b8bc3fSopenharmony_ci    bool SetHttpOptions(ProbeType probeType, CURL *curl, const std::string &url);
57b1b8bc3fSopenharmony_ci    bool SetProxyOption(ProbeType probeType, bool &useProxy);
58b1b8bc3fSopenharmony_ci    bool SetResolveOption(ProbeType probeType, const std::string &domain, const std::string &ipAddress, int32_t port);
59b1b8bc3fSopenharmony_ci    bool SendDnsProbe(ProbeType probeType, const std::string &httpUrl, const std::string &httpsUrl,
60b1b8bc3fSopenharmony_ci                      const bool useProxy);
61b1b8bc3fSopenharmony_ci    void SendHttpProbeRequest();
62b1b8bc3fSopenharmony_ci    void RecvHttpProbeResponse();
63b1b8bc3fSopenharmony_ci    int32_t LoadProxy(std::string &proxyHost, int32_t &proxyPort);
64b1b8bc3fSopenharmony_ci    bool SetUserInfo(CURL *curlHandler);
65b1b8bc3fSopenharmony_ci    bool SetProxyInfo(CURL *curlHandler, const std::string &proxyHost, int32_t proxyPort);
66b1b8bc3fSopenharmony_ci
67b1b8bc3fSopenharmony_ciprivate:
68b1b8bc3fSopenharmony_ci    static std::mutex initCurlMutex_;
69b1b8bc3fSopenharmony_ci    static int32_t useCurlCount_;
70b1b8bc3fSopenharmony_ci
71b1b8bc3fSopenharmony_ci    std::mutex proxyMtx_;
72b1b8bc3fSopenharmony_ci    bool isCurlInit_ = false;
73b1b8bc3fSopenharmony_ci    bool defaultUseGlobalHttpProxy_ = true;
74b1b8bc3fSopenharmony_ci    uint32_t netId_ = 0;
75b1b8bc3fSopenharmony_ci    NetBearType netBearType_ = BEARER_DEFAULT;
76b1b8bc3fSopenharmony_ci    NetLinkInfo netLinkInfo_;
77b1b8bc3fSopenharmony_ci    HttpProxy globalHttpProxy_;
78b1b8bc3fSopenharmony_ci    CURLM *curlMulti_ = nullptr;
79b1b8bc3fSopenharmony_ci    CURL *httpCurl_ = nullptr;
80b1b8bc3fSopenharmony_ci    CURL *httpsCurl_ = nullptr;
81b1b8bc3fSopenharmony_ci    curl_slist *httpResolveList_ = nullptr;
82b1b8bc3fSopenharmony_ci    curl_slist *httpsResolveList_ = nullptr;
83b1b8bc3fSopenharmony_ci    NetHttpProbeResult httpProbeResult_;
84b1b8bc3fSopenharmony_ci    NetHttpProbeResult httpsProbeResult_;
85b1b8bc3fSopenharmony_ci};
86b1b8bc3fSopenharmony_ci} // namespace NetManagerStandard
87b1b8bc3fSopenharmony_ci} // namespace OHOS
88b1b8bc3fSopenharmony_ci#endif // NET_HTTP_PROBE_H