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 COMMUNICATIONNETSTACK_HTTP_CLIENT_H 17 #define COMMUNICATIONNETSTACK_HTTP_CLIENT_H 18 19 #include <string> 20 #include <mutex> 21 #include <thread> 22 #include <condition_variable> 23 #include <queue> 24 #include <atomic> 25 #include <iostream> 26 27 #include "http_client_request.h" 28 #include "http_client_error.h" 29 #include "http_client_task.h" 30 31 namespace OHOS { 32 namespace NetStack { 33 namespace HttpClient { 34 class HttpSession { 35 public: 36 /** 37 * Gets the singleton instance of HttpSession. 38 * @return The singleton instance of HttpSession. 39 */ GetInstance()40 static HttpSession &GetInstance() 41 { 42 return instance_; 43 } 44 45 /** 46 * Creates an HTTP client task with the provided request. 47 * @param request The HTTP request to be executed. 48 * @return A shared pointer to the created HttpClientTask object. 49 */ 50 [[nodiscard]] std::shared_ptr<HttpClientTask> CreateTask(const HttpClientRequest &request); 51 52 /** 53 * Creates an HTTP client task with the provided request and file path. 54 * @param request The HTTP request to be executed. 55 * @param type The type of the task. 56 * @param filePath The file path to read the uploaded file (applicable for upload tasks). 57 * @return A shared pointer to the created HttpClientTask object. 58 */ 59 [[nodiscard]] std::shared_ptr<HttpClientTask> CreateTask(const HttpClientRequest &request, TaskType type, 60 const std::string &filePath); 61 62 private: 63 friend class HttpClientTask; 64 65 /** 66 * Default constructor. 67 */ 68 HttpSession() = default; 69 ~HttpSession(); 70 71 /** 72 * Initializes the HttpSession. 73 * @return true if initialization succeeds, false otherwise. 74 */ 75 bool Init(); 76 77 /** 78 * Checks if HttpSession is initialized. 79 * @return true if HttpSession is initialized, false otherwise. 80 */ 81 bool IsInited(); 82 83 /** 84 * HttpSession initialization ended. 85 */ 86 void Deinit(); 87 88 /** 89 * Runs the thread for handling HTTP tasks. 90 */ 91 static void RunThread(); 92 93 /** 94 * Starts the specified HTTP client task. 95 * @param ptr A shared pointer to the HttpClientTask object. 96 */ 97 void StartTask(std::shared_ptr<HttpClientTask> ptr); 98 99 /** 100 * Stops the specified HTTP client task. 101 * @param ptr A shared pointer to the HttpClientTask object. 102 */ 103 void StopTask(std::shared_ptr<HttpClientTask> ptr); 104 105 /** 106 * Gets the HTTP client task with the specified task ID. 107 * @param taskId The ID of the task to retrieve. 108 * @return A shared pointer to the HttpClientTask object, or nullptr if not found. 109 */ 110 std::shared_ptr<HttpClientTask> GetTaskById(uint32_t taskId); 111 112 /** 113 * Gets the HTTP client task with the specified Curl handle. 114 * @param curlHandle The Curl handle of the task to retrieve. 115 * @return A shared pointer to the HttpClientTask object, or nullptr if not found. 116 */ 117 std::shared_ptr<HttpClientTask> GetTaskByCurlHandle(CURL *curlHandle); 118 119 static HttpSession instance_; 120 static std::mutex curlMultiMutex_; 121 static CURLM *curlMulti_; 122 std::mutex taskMapMutex_; 123 std::map<CURL *, std::shared_ptr<HttpClientTask>> curlTaskMap_; 124 std::map<uint32_t, std::shared_ptr<HttpClientTask>> taskIdMap_; 125 static std::condition_variable conditionVariable_; 126 struct CompareTasks { operatorCompareTasks127 bool operator()(const std::shared_ptr<HttpClientTask> &task1, const std::shared_ptr<HttpClientTask> &task2) 128 { 129 if (task1->GetRequest().GetPriority() == task2->GetRequest().GetPriority()) { 130 return task1->GetTaskId() > task2->GetTaskId(); 131 } 132 133 return task1->GetRequest().GetPriority() < task2->GetRequest().GetPriority(); 134 } 135 }; 136 static std::mutex taskQueueMutex_; 137 static std::priority_queue<std::shared_ptr<HttpClientTask>, std::vector<std::shared_ptr<HttpClientTask>>, 138 CompareTasks> 139 taskQueue_; 140 static std::atomic_bool initialized_; 141 static std::thread workThread_; 142 static std::atomic_bool runThread_; 143 144 /** 145 * Sends an HTTP request and handles the response. 146 */ 147 void RequestAndResponse(); 148 149 /** 150 * Reads the response received from the HTTP request. 151 */ 152 void ReadResponse(); 153 154 /** 155 * Adds additional information to the HTTP request. 156 */ 157 void AddRequestInfo(); 158 159 /** 160 * Executes the HTTP request. 161 */ 162 void ExecRequest(); 163 }; 164 } // namespace HttpClient 165 } // namespace NetStack 166 } // namespace OHOS 167 168 #endif // COMMUNICATIONNETSTACK_HTTP_CLIENT_H 169