This repository has been archived by the owner on Oct 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
curl.cc
executable file
·148 lines (115 loc) · 4.37 KB
/
curl.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Copyright 2020 Google LLC. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "curl.h"
#include <curl/curl.h>
#include <glog/logging.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "opensource/deps/base/integral_types.h"
//#include "util/http_util.h"
namespace plusfish {
// The parameter value to enable libcurl HTTP pipelining.
static const int kEnablePipeliningParam = 1;
// Maximum timeout value.
static const int kMaxTimeoutMs = 3000;
Curl::Curl() {}
Curl::~Curl() {}
const CURLMcode Curl::MultiAddHandle(CURLM* multi_handle,
CURL* easy_handle) const {
return curl_multi_add_handle(multi_handle, easy_handle);
}
CURLMsg* Curl::MultiInfoRead(CURLM* multi_handle, int* msgs_in_queue) const {
return curl_multi_info_read(multi_handle, msgs_in_queue);
}
CURLM* Curl::MultiInit() const { return curl_multi_init(); }
const CURLMcode Curl::MultiCleanup(CURLM* multi_handle) const {
return curl_multi_cleanup(multi_handle);
}
const CURLMcode Curl::MultiEnablePipelining(CURLM* multi_handle) const {
return curl_multi_setopt(multi_handle, CURLMOPT_PIPELINING,
&kEnablePipeliningParam);
}
const CURLMcode Curl::MultiPerform(CURLM* multi_handle,
int* running_handles) const {
return curl_multi_perform(multi_handle, running_handles);
}
const CURLMcode Curl::MultiSetOpt(CURLM* multi_handle, CURLMoption option,
const void* param) const {
return curl_multi_setopt(multi_handle, option, param);
}
const int Curl::GetTimeout(CURLM* multi_handle) const {
// The timeout value is set by the curl_multi_timeout call.
long timeout = -1; // NOLINT(runtime/int)
curl_multi_timeout(multi_handle, &timeout);
if (timeout > kMaxTimeoutMs) {
return kMaxTimeoutMs;
}
return static_cast<int>(timeout);
}
const int Curl::Select(CURLM* multi_handle, struct timeval* timeout) {
int maxfd_;
fd_set fdread_;
fd_set fdwrite_;
fd_set fdexcep_;
FD_ZERO(&fdread_);
FD_ZERO(&fdwrite_);
FD_ZERO(&fdexcep_);
curl_multi_fdset(multi_handle, &fdread_, &fdwrite_, &fdexcep_, &maxfd_);
if (maxfd_ == -1) {
return -1;
}
return select(maxfd_ + 1, &fdread_, &fdwrite_, &fdexcep_, timeout);
}
void Curl::EasyCleanup(CURL* handle) { return curl_easy_cleanup(handle); }
CURL* Curl::EasyInit() const { return curl_easy_init(); }
CurlHandleData* Curl::EasyGetHandleData(CURL* handle) const {
CurlHandleData* handle_data = nullptr;
curl_easy_getinfo(handle, CURLINFO_PRIVATE, &handle_data);
return handle_data;
}
const CURLcode Curl::EasyGetInfo(CURL* handle, CURLINFO info,
void* param) const {
return curl_easy_getinfo(handle, info, param);
}
const CURLcode Curl::EasySetOpt(CURL* handle, CURLoption option,
const void* param) const {
return curl_easy_setopt(handle, option, param);
}
const CURLcode Curl::EasySetOptInt64(CURL* handle, CURLoption option,
int64 param) const {
return curl_easy_setopt(handle, option, param);
}
const CURLcode Curl::EasySetWriteCallback(CURL* handle, CURLoption option,
WriteCallback callback) const {
return curl_easy_setopt(handle, option, callback);
}
CURLSH* Curl::ShareInit() const { return curl_share_init(); }
const CURLSHcode Curl::ShareData(CURLSH* share, curl_lock_data data) {
return curl_share_setopt(share, CURLSHOPT_SHARE, data);
}
void Curl::FreeSlist(struct curl_slist* list) const {
curl_slist_free_all(list);
}
bool Curl::AppendSlist(struct curl_slist** list,
const std::string& value) const {
struct curl_slist* tmp_list = nullptr;
tmp_list = curl_slist_append(*list, value.c_str());
if (tmp_list != nullptr) {
*list = tmp_list;
return true;
}
return false;
}
} // namespace plusfish