6#include "XrdVersion.hh"
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;
59bool 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;
136size_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);
143int 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);
152 if (!std::getline(ss, item,
' '))
return 0;
153 m_resp_protocol = item;
155 if (!std::getline(ss, item,
' '))
return 0;
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();
192size_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);
209ssize_t State::Write(
char *buffer,
size_t size) {
210 ssize_t retval = m_stream->Write(m_start_offset + m_offset, buffer, size,
false);
212 m_error_buf = m_stream->GetErrorMessage();
220void 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;
234 RecordFinalizeError(
errFlush, m_stream->GetErrorMessage());
240size_t State::ReadCB(
void *buffer,
size_t size,
size_t nitems,
void *userdata) {
244 return obj->Read(
static_cast<char*
>(buffer), size*nitems);
247int State::Read(
char *buffer,
size_t size) {
248 int retval = m_stream->Read(m_start_offset + m_offset, buffer, size);
258 CURL *curl = curl_easy_duphandle(m_curl);
260 throw std::runtime_error(
"Failed to duplicate existing curl handle.");
263 State *state =
new State(0, *m_stream, curl, m_push, tpcForwardCreds);
266 state->m_headers_copy.reserve(m_headers_copy.size());
267 for (std::vector<std::string>::const_iterator header_iter = m_headers_copy.begin();
268 header_iter != m_headers_copy.end();
270 state->m_headers = curl_slist_append(state->m_headers, header_iter->c_str());
271 state->m_headers_copy.push_back(*header_iter);
273 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, NULL);
274 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, state->m_headers);
281 m_start_offset = offset;
283 m_content_length = size;
284 std::stringstream ss;
285 ss << offset <<
"-" << (offset+size-1);
286 curl_easy_setopt(m_curl, CURLOPT_RANGE, ss.str().c_str());
291 return m_stream->AvailableBuffers();
296 m_stream->DumpBuffers();
301 if (!m_stream->Finalize()) {
302 RecordFinalizeError(
errClose, m_stream->GetErrorMessage());
312#if LIBCURL_VERSION_NUM >= 0x071500
313 char *curl_ip = NULL;
314 CURLcode rc = curl_easy_getinfo(m_curl, CURLINFO_PRIMARY_IP, &curl_ip);
315 if ((rc != CURLE_OK) || !curl_ip) {
319 rc = curl_easy_getinfo(m_curl, CURLINFO_PRIMARY_PORT, &curl_port);
320 if ((rc != CURLE_OK) || !curl_port) {
323 std::stringstream ss;
329 if (NULL == strchr(curl_ip,
':'))
330 ss <<
"tcp:" << curl_ip <<
":" << curl_port;
332 ss <<
"tcp:[" << curl_ip <<
"]:" << curl_port;
int GetStatusCode() const
void SetTransferParameters(off_t offset, size_t size)
std::string GetConnectionDescription()
void SetupHeaders(XrdHttpExtReq &req)
int AvailableBuffers() const
std::map< std::string, std::string > & headers