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 <memory> 24 #include <map> 25 #include <vector> 26 #include <queue> 27 #include <atomic> 28 #include <iostream> 29 30 #include "http_client_request.h" 31 #include "http_client_error.h" 32 #include "http_client_task.h" 33 34 namespace OHOS { 35 namespace NetStack { 36 namespace HttpClient { 37 class HttpSession { 38 public: 39 /** 40 * Gets the singleton instance of HttpSession. 41 * @return The singleton instance of HttpSession. 42 */ 43 static HttpSession &GetInstance(); 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(); 69 ~HttpSession(); 70 71 /** 72 * Initializes the HttpSession. 73 * @return true if initialization succeeds, false otherwise. 74 */ 75 bool Init(); 76 77 /** 78 * HttpSession initialization ended. 79 */ 80 void Deinit(); 81 82 /** 83 * Runs the thread for handling HTTP tasks. 84 */ 85 void RunThread(); 86 87 /** 88 * Starts the specified HTTP client task. 89 * @param ptr A shared pointer to the HttpClientTask object. 90 */ 91 void StartTask(std::shared_ptr<HttpClientTask> ptr); 92 93 /** 94 * Stops the specified HTTP client task. 95 * @param ptr A shared pointer to the HttpClientTask object. 96 */ 97 void StopTask(std::shared_ptr<HttpClientTask> ptr); 98 99 /** 100 * Gets the HTTP client task with the specified task ID. 101 * @param taskId The ID of the task to retrieve. 102 * @return A shared pointer to the HttpClientTask object, or nullptr if not found. 103 */ 104 std::shared_ptr<HttpClientTask> GetTaskById(uint32_t taskId); 105 106 /** 107 * Gets the HTTP client task with the specified Curl handle. 108 * @param curlHandle The Curl handle of the task to retrieve. 109 * @return A shared pointer to the HttpClientTask object, or nullptr if not found. 110 */ 111 std::shared_ptr<HttpClientTask> GetTaskByCurlHandle(CURL *curlHandle); 112 113 std::mutex curlMultiMutex_; 114 CURLM *curlMulti_; 115 std::mutex taskMapMutex_; 116 std::mutex initMutex_; 117 std::map<CURL *, std::shared_ptr<HttpClientTask>> curlTaskMap_; 118 std::map<uint32_t, std::shared_ptr<HttpClientTask>> taskIdMap_; 119 std::mutex taskMutex_; 120 std::condition_variable conditionVariable_; 121 struct CompareTasks { operatorCompareTasks122 bool operator()(const std::shared_ptr<HttpClientTask> &task1, const std::shared_ptr<HttpClientTask> &task2) 123 { 124 if (task1->GetRequest().GetPriority() == task2->GetRequest().GetPriority()) { 125 return task1->GetTaskId() > task2->GetTaskId(); 126 } 127 128 return task1->GetRequest().GetPriority() < task2->GetRequest().GetPriority(); 129 } 130 }; 131 std::mutex taskQueueMutex_; 132 std::priority_queue<std::shared_ptr<HttpClientTask>, std::vector<std::shared_ptr<HttpClientTask>>, 133 HttpSession::CompareTasks> taskQueue_; 134 135 std::atomic_bool initialized_; 136 std::thread workThread_; 137 std::atomic_bool runThread_; 138 139 /** 140 * Add Request info 141 */ 142 void AddRequestInfo(); 143 144 /** 145 * Perform Http request 146 */ 147 CURLMcode PerformRequest(int &runningHandle); 148 149 /** 150 * Sends an HTTP request and handles the response. 151 */ 152 void RequestAndResponse(); 153 154 /** 155 * Reads the response received from the HTTP request. 156 */ 157 void ReadResponse(); 158 159 /** 160 * Resum Task 161 */ 162 void ResumTask(); 163 }; 164 } // namespace HttpClient 165 } // namespace NetStack 166 } // namespace OHOS 167 168 #endif // COMMUNICATIONNETSTACK_HTTP_CLIENT_H