6 #include "XrdVersion.hh"
10 #include <curl/curl.h>
20 curl_slist_free_all(m_headers);
22 if (m_curl) {curl_easy_setopt(m_curl, CURLOPT_HTTPHEADER, m_headers);}
29 m_push = other.m_push;
30 m_recv_status_line = other.m_recv_status_line;
31 m_recv_all_headers = other.m_recv_all_headers;
32 m_offset = other.m_offset;
33 m_start_offset = other.m_start_offset;
34 m_status_code = other.m_status_code;
35 m_content_length = other.m_content_length;
36 m_push_length = other.m_push_length;
37 m_stream = other.m_stream;
38 m_curl = other.m_curl;
39 m_headers = other.m_headers;
40 m_headers_copy = other.m_headers_copy;
41 m_resp_protocol = other.m_resp_protocol;
42 m_is_transfer_state = other.m_is_transfer_state;
43 curl_easy_setopt(m_curl, CURLOPT_HEADERDATA,
this);
44 if (m_is_transfer_state) {
46 curl_easy_setopt(m_curl, CURLOPT_READDATA,
this);
48 curl_easy_setopt(m_curl, CURLOPT_WRITEDATA,
this);
51 tpcForwardCreds = other.tpcForwardCreds;
52 other.m_headers_copy.clear();
54 other.m_headers = NULL;
55 other.m_stream = NULL;
59 bool State::InstallHandlers(
CURL *curl) {
60 curl_easy_setopt(curl, CURLOPT_USERAGENT,
"xrootd-tpc/" XrdVERSION);
61 curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, &State::HeaderCB);
62 curl_easy_setopt(curl, CURLOPT_HEADERDATA,
this);
63 if(m_is_transfer_state) {
65 curl_easy_setopt(curl, CURLOPT_UPLOAD, 1);
66 curl_easy_setopt(curl, CURLOPT_READFUNCTION, &State::ReadCB);
67 curl_easy_setopt(curl, CURLOPT_READDATA,
this);
70 m_push_length = buf.st_size;
71 curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, buf.st_size);
74 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &State::WriteCB);
75 curl_easy_setopt(curl, CURLOPT_WRITEDATA,
this);
78 curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
80 curl_easy_setopt(curl,CURLOPT_UNRESTRICTED_AUTH,1L);
94 struct curl_slist *list = NULL;
95 for (std::map<std::string, std::string>::const_iterator hdr_iter = req.
headers.begin();
98 if (!strcasecmp(hdr_iter->first.c_str(),
"copy-header")) {
99 list = curl_slist_append(list, hdr_iter->second.c_str());
100 m_headers_copy.emplace_back(hdr_iter->second);
103 if (!strncasecmp(hdr_iter->first.c_str(),
"transferheader",14)) {
104 std::stringstream ss;
105 ss << hdr_iter->first.substr(14) <<
": " << hdr_iter->second;
106 list = curl_slist_append(list, ss.str().c_str());
107 m_headers_copy.emplace_back(ss.str());
111 if (m_is_transfer_state && m_push && m_push_length > 0) {
118 list = curl_slist_append(list,
"Expect: 100-continue");
122 curl_easy_setopt(m_curl, CURLOPT_HTTPHEADER, list);
130 m_content_length = -1;
132 m_recv_all_headers =
false;
133 m_recv_status_line =
false;
136 size_t State::HeaderCB(
char *buffer,
size_t size,
size_t nitems,
void *userdata)
139 std::string header(buffer, size*nitems);
140 return obj->Header(header);
143 int State::Header(
const std::string &header) {
145 if (m_recv_all_headers) {
146 m_recv_all_headers =
false;
147 m_recv_status_line =
false;
149 if (!m_recv_status_line) {
150 std::stringstream ss(header);
153 m_resp_protocol = item;
157 m_status_code = std::stol(item);
161 m_recv_status_line =
true;
162 }
else if (header.size() == 0 || header ==
"\n" || header ==
"\r\n") {
163 m_recv_all_headers =
true;
165 else if (header !=
"\r\n") {
167 std::size_t found = header.find(
":");
168 if (found != std::string::npos) {
169 std::string header_name = header.substr(0, found);
170 std::transform(header_name.begin(), header_name.end(), header_name.begin(), ::tolower);
171 std::string header_value = header.substr(found+1);
172 if (header_name ==
"content-length")
175 m_content_length = std::stoll(header_value);
189 return header.size();
192 size_t State::WriteCB(
void *buffer,
size_t size,
size_t nitems,
void *userdata) {
198 obj->m_error_buf += std::string(
static_cast<char*
>(buffer),
199 std::min(
static_cast<size_t>(1024), size*nitems));
201 if (obj->m_error_buf.size() >= 1024)
206 return obj->Write(
static_cast<char*
>(buffer), size*nitems);
209 ssize_t State::Write(
char *buffer,
size_t size) {
210 ssize_t retval = m_stream->
Write(m_start_offset + m_offset, buffer, size,
false);
220 void State::RecordFinalizeError(
int error_code,
const std::string &error_msg) {
221 if (m_finalize_error_code) {
224 m_finalize_error_code = error_code;
225 m_finalize_error_buf = error_msg;
233 ssize_t retval = m_stream->
Write(m_start_offset + m_offset, 0, 0,
true);
242 size_t State::ReadCB(
void *buffer,
size_t size,
size_t nitems,
void *userdata) {
246 return obj->Read(
static_cast<char*
>(buffer), size*nitems);
249 int State::Read(
char *buffer,
size_t size) {
250 int retval = m_stream->
Read(m_start_offset + m_offset, buffer, size);
260 CURL *curl = curl_easy_duphandle(m_curl);
262 throw std::runtime_error(
"Failed to duplicate existing curl handle.");
265 State *state =
new State(0, *m_stream, curl, m_push, tpcForwardCreds);
268 state->m_headers_copy.reserve(m_headers_copy.size());
269 for (std::vector<std::string>::const_iterator header_iter = m_headers_copy.begin();
270 header_iter != m_headers_copy.end();
272 state->m_headers = curl_slist_append(state->m_headers, header_iter->c_str());
273 state->m_headers_copy.push_back(*header_iter);
275 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, NULL);
276 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, state->m_headers);
283 m_start_offset = offset;
285 m_content_length = size;
286 std::stringstream ss;
287 ss << offset <<
"-" << (offset+size-1);
288 curl_easy_setopt(m_curl, CURLOPT_RANGE, ss.str().c_str());
314 #if LIBCURL_VERSION_NUM >= 0x071500
315 char *curl_ip = NULL;
316 CURLcode rc = curl_easy_getinfo(m_curl, CURLINFO_PRIMARY_IP, &curl_ip);
317 if ((rc != CURLE_OK) || !curl_ip) {
321 rc = curl_easy_getinfo(m_curl, CURLINFO_PRIMARY_PORT, &curl_port);
322 if ((rc != CURLE_OK) || !curl_port) {
325 std::stringstream ss;
331 if (NULL == strchr(curl_ip,
':'))
332 ss <<
"tcp:" << curl_ip <<
":" << curl_port;
334 ss <<
"tcp:[" << curl_ip <<
"]:" << curl_port;
void getline(uchar *buff, int blen)
int GetStatusCode() const
void SetTransferParameters(off_t offset, size_t size)
std::string GetConnectionDescription()
void SetupHeaders(XrdHttpExtReq &req)
int AvailableBuffers() const
int Read(off_t offset, char *buffer, size_t size)
ssize_t Write(off_t offset, const char *buffer, size_t size, bool force)
std::string GetErrorMessage() const
size_t AvailableBuffers() const
std::map< std::string, std::string > & headers