1 /*
2  * Copyright (c) 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 NETSTACK_I_NETWORK_MESSAGE_H
17 #define NETSTACK_I_NETWORK_MESSAGE_H
18 
19 #include <map>
20 
21 #if HAS_NETMANAGER_BASE
22 #include "curl/curl.h"
23 #endif
24 #include "tlv_utils.h"
25 
26 namespace OHOS::NetStack {
27 #if HAS_NETMANAGER_BASE
28 #define CURL_GET_INFO(curl, options, ptr)                           \
29     do {                                                            \
30         auto code = curl_easy_getinfo((curl), (options), (ptr));    \
31         if (code != CURLE_OK) {                                     \
32             return static_cast<uint32_t>(code);                     \
33         }                                                           \
34     } while (false)                                                 \
35 
36 #define CURL_GET_TIME_INFO(curl, options, time, timeInfo)           \
37     do {                                                            \
38         CURL_GET_INFO((curl), (options), &(time));                  \
39         (timeInfo).time = static_cast<uint64_t>(time);              \
40     } while (false)                                                 \
41 
42 #endif
43 
44 struct TimeInfo {
45     double dnsTime = 0;
46     double tcpConnectTime = 0;
47     double tlsHandshakeTime = 0;
48     double firstSendTime = 0;
49     double firstRecvTime = 0;
50     double redirectTime = 0;
51     double totalTime = 0;
52 };
53 
54 class INetworkMessage {
55 public:
56     INetworkMessage() = default;
57     explicit INetworkMessage(std::string requestId);
58     virtual ~INetworkMessage();
59     virtual DfxMessage Parse() = 0;
60     void SetRequestBeginTime(uint64_t bootTime);
61 
62 protected:
63 #if HAS_NETMANAGER_BASE
64     static uint32_t GetIpAddressFromCurlHandle(std::string &ip, CURL *handle);
65     static uint32_t GetEffectiveUrlFromCurlHandle(std::string &effectiveUrl, CURL *handle);
66     static uint32_t GetHttpVersionFromCurlHandle(std::string &httpVersion, CURL *handle);
67     static uint32_t GetTimeInfoFromCurlHandle(TimeInfo &timeInfo, CURL *handle);
68 #endif
69     static std::string GetReasonParse(const std::string &rawHeader);
70     static std::string GetRawHeader(const std::map<std::string, std::string> &headers);
71 
72 private:
73     static std::string GetHttpVersion(long httpVersion);
74 
75 protected:
76     std::string requestId_;
77     uint64_t requestBeginTime_ = 0;
78 };
79 }
80 
81 #endif //NETSTACK_I_NETWORK_MESSAGE_H
82