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 NET_HTTP_UTILS_H
17 #define NET_HTTP_UTILS_H
18 
19 #include <cstddef>
20 #include <cstring>
21 #include <iostream>
22 #include <memory>
23 #include <string>
24 #include <chrono>
25 #include <map>
26 
27 #include "constant.h"
28 #include "cj_ffi/cj_common_ffi.h"
29 
30 namespace OHOS::NetStack::Http {
31 const double MICROSECONDS_TO_MILLISECONDS = 1000.0;
32 char* MallocCString(const std::string& origin);
33 
34 class Timer {
35 public:
36     const char* timerName_{nullptr};
37     Timer();
38     void Start(time_t time);
39     void Start();
40     void Stop();
41     double Elapsed() const;
42 
43 private:
44     time_t startTime_{0};
45     time_t endTime_{0};
46 };
47 
48 class TimerMap {
49 public:
50     Timer& RecieveTimer(const char *const type);
51 
52 private:
53     std::map<const char *const, Timer> timerMap_;
54 };
55 
56 class TimeUtils {
57 public:
58     static time_t GetNowTimeMicroseconds();
59 
60     static double Microseconds2Milliseconds(time_t microseconds);
61 };
62 
63 time_t StrTimeToTimestamp(const std::string &time_str);
64 
65 time_t GetNowTimeSeconds();
66 
67 std::string GetNowTimeGMT();
68 
69 std::string Encode(const std::string &source);
70 
71 std::string Decode(const std::string &encoded);
72 
73 class SecureChar {
74 public:
75     SecureChar();
76     ~SecureChar();
77     explicit SecureChar(const std::string &secureChar);
78     SecureChar(const uint8_t *secureChar, size_t length);
79     SecureChar(const SecureChar &secureChar);
80     SecureChar &operator=(const SecureChar &secureChar);
81 
82     const char *Data() const;
83     size_t Length() const;
84 
85 private:
86     size_t length_ = 0;
87     std::unique_ptr<char[]> data_ = nullptr;
88 };
89 
90 CArrString g_map2CArrString(std::map<std::string, std::string> map);
91 } // namespace OHOS::NetStack::Http
92 #endif // NET_HTTP_UTILS_H
93