1 /*
2 * Copyright (c) 2023-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 #include <atomic>
17 #include <curl/curl.h>
18 #include <functional>
19 #include <iostream>
20 #include <sstream>
21
22 #include "epoll_request_handler.h"
23 #include "request_tracer.h"
24 #include "trace_events.h"
25 #include "http_client.h"
26 #include "netstack_log.h"
27
28 namespace OHOS {
29 namespace NetStack {
30 namespace HttpClient {
31 class HttpGlobal {
32 public:
HttpGlobal()33 HttpGlobal()
34 {
35 if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
36 NETSTACK_LOGE("Failed to initialize 'curl'");
37 }
38 }
~HttpGlobal()39 ~HttpGlobal()
40 {
41 NETSTACK_LOGD("Deinit curl_global_cleanup()");
42 curl_global_cleanup();
43 }
44 };
45 static HttpGlobal g_httpGlobal;
46
47 HttpSession::HttpSession() = default;
48
~HttpSession()49 HttpSession::~HttpSession()
50 {
51 NETSTACK_LOGD("~HttpSession::enter");
52 }
53
GetInstance()54 HttpSession &HttpSession::GetInstance()
55 {
56 static HttpSession gInstance;
57 return gInstance;
58 }
59
CreateTask(const HttpClientRequest &request)60 std::shared_ptr<HttpClientTask> HttpSession::CreateTask(const HttpClientRequest &request)
61 {
62 std::shared_ptr<HttpClientTask> ptr = std::make_shared<HttpClientTask>(request);
63 if (ptr->GetCurlHandle() == nullptr) {
64 NETSTACK_LOGE("CreateTask A error!");
65 return nullptr;
66 }
67
68 return ptr;
69 }
70
CreateTask(const HttpClientRequest &request, TaskType type, const std::string &filePath)71 std::shared_ptr<HttpClientTask> HttpSession::CreateTask(const HttpClientRequest &request, TaskType type,
72 const std::string &filePath)
73 {
74 std::shared_ptr<HttpClientTask> ptr = std::make_shared<HttpClientTask>(request, type, filePath);
75 if (ptr->GetCurlHandle() == nullptr) {
76 NETSTACK_LOGE("CreateTask B error!");
77 return nullptr;
78 }
79 return ptr;
80 }
81
StartTask(const std::shared_ptr<HttpClientTask> &ptr)82 void HttpSession::StartTask(const std::shared_ptr<HttpClientTask> &ptr)
83 {
84 if (nullptr == ptr) {
85 NETSTACK_LOGE("HttpSession::StartTask shared_ptr = nullptr! Error!");
86 return;
87 }
88
89 static HttpOverCurl::EpollRequestHandler requestHandler;
90
91 ptr->SetStatus(TaskStatus::RUNNING);
92
93 auto startedCallback = [ptr](CURL *, void *) {
94 ptr->GetTrace().Tracepoint(TraceEvents::QUEUE);
95 };
96 auto responseCallback = [ptr](CURLMsg *curlMessage, void *) {
97 ptr->GetTrace().Tracepoint(TraceEvents::NATIVE);
98 ptr->ProcessResponse(curlMessage);
99 ptr->SetStatus(TaskStatus::IDLE);
100 };
101 requestHandler.Process(ptr->GetCurlHandle(), startedCallback, responseCallback);
102 }
103
104 } // namespace HttpClient
105 } // namespace NetStack
106 } // namespace OHOS
107