9 #include "XrdVersion.hh"
15 #include <curl/curl.h>
35 uint64_t TPCHandler::m_monid{0};
36 int TPCHandler::m_marker_period = 5;
37 size_t TPCHandler::m_block_size = 16*1024*1024;
38 size_t TPCHandler::m_small_block_size = 1*1024*1024;
47 TPCHandler::TPCLogRecord::~TPCLogRecord()
54 monInfo.
clID = clID.c_str();
56 gettimeofday(&monInfo.
endT, 0);
59 {monInfo.
dstURL = local.c_str();
60 monInfo.
srcURL = remote.c_str();
62 monInfo.
dstURL = remote.c_str();
63 monInfo.
srcURL = local.c_str();
67 if (!status) monInfo.
endRC = 0;
68 else if (tpc_status > 0) monInfo.
endRC = tpc_status;
69 else monInfo.
endRC = 1;
70 monInfo.
strm =
static_cast<unsigned char>(streams);
71 monInfo.
fSize = (bytes_transferred < 0 ? 0 : bytes_transferred);
74 tpcMonitor->Report(monInfo);
84 if (curl) curl_easy_cleanup(curl);
108 std::stringstream parser(opaque);
109 std::string sequence;
110 std::stringstream output;
112 while (
getline(parser, sequence,
'&')) {
113 if (sequence.empty()) {
continue;}
114 size_t equal_pos = sequence.find(
'=');
116 if (equal_pos != std::string::npos)
117 val = curl_easy_escape(curl, sequence.c_str() + equal_pos + 1, sequence.size() - equal_pos - 1);
119 if (!val && equal_pos != std::string::npos) {
continue;}
121 if (!first) output <<
"&";
123 output << sequence.substr(0, equal_pos);
125 output <<
"=" << val;
137 TPCHandler::ConfigureCurlCA(
CURL *curl)
139 auto ca_filename = m_ca_file ? m_ca_file->CAFilename() :
"";
140 auto crl_filename = m_ca_file ? m_ca_file->CRLFilename() :
"";
141 if (!ca_filename.empty() && !crl_filename.empty()) {
142 curl_easy_setopt(curl, CURLOPT_CAINFO, ca_filename.c_str());
146 std::ifstream in(crl_filename, std::ifstream::ate | std::ifstream::binary);
147 if(in.tellg() > 0 && m_ca_file->atLeastOneValidCRLFound()){
148 curl_easy_setopt(curl, CURLOPT_CRLFILE, crl_filename.c_str());
150 std::ostringstream oss;
151 oss <<
"No valid CRL file has been found in the file " << crl_filename <<
". Disabling CRL checking.";
152 m_log.
Log(
Warning,
"TpcHandler",oss.str().c_str());
155 else if (!m_cadir.empty()) {
156 curl_easy_setopt(curl, CURLOPT_CAPATH, m_cadir.c_str());
158 if (!m_cafile.empty()) {
159 curl_easy_setopt(curl, CURLOPT_CAINFO, m_cafile.c_str());
165 return !strcmp(verb,
"COPY") || !strcmp(verb,
"OPTIONS");
173 if (!strncmp(input.c_str(),
"davs://", 7)) {
174 return "https://" + input.substr(7);
184 if (req.
verb ==
"OPTIONS") {
185 return ProcessOptionsReq(req);
188 if (header != req.
headers.end()) {
189 if (header->second !=
"none") {
190 m_log.
Emsg(
"ProcessReq",
"COPY requested an unsupported credential type: ", header->second.c_str());
191 return req.
SendSimpleResp(400, NULL, NULL,
"COPY requestd an unsupported Credential type", 0);
195 if (header != req.
headers.end()) {
197 return ProcessPullReq(src, req);
200 if (header != req.
headers.end()) {
201 return ProcessPushReq(header->second, req);
203 m_log.
Emsg(
"ProcessReq",
"COPY verb requested but no source or destination specified.");
204 return req.
SendSimpleResp(400, NULL, NULL,
"No Source or Destination specified", 0);
221 m_fixed_route(false),
223 m_first_timeout(120),
224 m_log(log->logger(),
"TPC_"),
226 m_request_manager(*myEnv, *log)
228 if (!Configure(config, myEnv)) {
229 throw std::runtime_error(
"Failed to configure the HTTP third-party-copy handler.");
247 return req.
SendSimpleResp(200, NULL, (
char *)
"DAV: 1\r\nDAV: <http://apache.org/dav/propset/fs/1>\r\nAllow: HEAD,GET,PUT,PROPFIND,DELETE,OPTIONS,COPY", NULL, 0);
257 if (authz_header != req.
headers.end()) {
258 std::stringstream ss;
259 ss <<
"authz=" <<
encode_str(authz_header->second);
269 int TPCHandler::RedirectTransfer(
CURL *curl,
const std::string &redirect_resource,
274 if ((ptr == NULL) || (*ptr ==
'\0') || (port == 0)) {
276 std::stringstream ss;
277 ss <<
"Internal error: redirect without hostname";
278 logTransferEvent(
LogMask::Error, rec,
"REDIRECT_INTERNAL_ERROR", ss.str());
279 return req.
SendSimpleResp(rec.status, NULL, NULL, generateClientErr(ss, rec).c_str(), 0);
283 std::string rdr_info = ptr;
284 std::string host, opaque;
285 size_t pos = rdr_info.find(
'?');
286 host = rdr_info.substr(0, pos);
288 if (pos != std::string::npos) {
289 opaque = rdr_info.substr(pos + 1);
292 std::stringstream ss;
293 ss <<
"Location: http" << (m_desthttps ?
"s" :
"") <<
"://" << host <<
":" << port <<
"/" << redirect_resource;
295 if (!opaque.empty()) {
301 return req.
SendSimpleResp(rec.status, NULL,
const_cast<char *
>(ss.str().c_str()),
309 int TPCHandler::OpenWaitStall(
XrdSfsFile &fh,
const std::string &resource,
311 const std::string &authz)
318 size_t pos = resource.find(
'?');
320 std::string path = resource.substr(0, pos);
322 if (pos != std::string::npos) {
323 opaque = resource.substr(pos + 1);
328 opaque += (opaque.empty() ?
"" :
"&");
331 open_result = fh.
open(path.c_str(), mode, openMode, &sec, opaque.c_str());
335 if (open_result ==
SFS_STARTED) {secs_to_stall = secs_to_stall/2 + 5;}
336 std::this_thread::sleep_for (std::chrono::seconds(secs_to_stall));
353 bool &success, TPCLogRecord &rec,
bool shouldReturnErrorToClient) {
355 curl_easy_setopt(curl, CURLOPT_NOBODY, 1);
357 curl_easy_setopt(curl, CURLOPT_TIMEOUT, CONNECT_TIMEOUT);
359 res = curl_easy_perform(curl);
362 curl_easy_setopt(curl, CURLOPT_NOBODY, 0);
364 curl_easy_setopt(curl, CURLOPT_TIMEOUT, 0L);
365 if (res == CURLE_HTTP_RETURNED_ERROR) {
366 std::stringstream ss;
367 ss <<
"Remote server failed request while fetching remote size";
368 std::stringstream ss2;
369 ss2 << ss.str() <<
": " << curl_easy_strerror(res);
372 return shouldReturnErrorToClient ? req.
SendSimpleResp(rec.status, NULL, NULL, generateClientErr(ss, rec, res).c_str(), 0) : -1;
374 std::stringstream ss;
375 ss <<
"Remote side " << req.
clienthost <<
" failed with status code " << state.
GetStatusCode() <<
" while fetching remote size";
378 return shouldReturnErrorToClient ? req.
SendSimpleResp(rec.status, NULL, NULL, generateClientErr(ss, rec).c_str(), 0) : -1;
380 std::stringstream ss;
381 ss <<
"Internal transfer failure while fetching remote size";
382 std::stringstream ss2;
383 ss2 << ss.str() <<
" - HTTP library failed: " << curl_easy_strerror(res);
386 return shouldReturnErrorToClient ? req.
SendSimpleResp(rec.status, NULL, NULL, generateClientErr(ss, rec, res).c_str(), 0) : -1;
388 std::stringstream ss;
389 ss <<
"Successfully determined remote size for pull request: "
396 int TPCHandler::GetContentLengthTPCPull(
CURL *curl,
XrdHttpExtReq &req, uint64_t &contentLength,
bool & success, TPCLogRecord &rec) {
403 if ((result = DetermineXferSize(curl, req, state, success, rec)) || !success) {
415 std::stringstream ss;
416 const std::string crlf =
"\n";
417 ss <<
"Perf Marker" << crlf;
418 ss <<
"Timestamp: " << time(NULL) << crlf;
419 ss <<
"Stripe Index: 0" << crlf;
421 ss <<
"Total Stripe Count: 1" << crlf;
422 if (!desc.empty()) ss <<
"RemoteConnections: " << desc << crlf;
426 return req.
ChunkResp(ss.str().c_str(), 0);
434 std::stringstream ss;
435 const std::string crlf =
"\n";
436 ss <<
"Perf Marker" << crlf;
437 ss <<
"Timestamp: " << time(NULL) << crlf;
438 ss <<
"Stripe Index: 0" << crlf;
440 ss <<
"Total Stripe Count: 1" << crlf;
445 ss <<
"RemoteConnections: " << desc << crlf;
450 return req.
ChunkResp(ss.str().c_str(), 0);
457 int TPCHandler::SendPerfMarker(
XrdHttpExtReq &req, TPCLogRecord &rec, std::vector<State*> &state,
458 off_t bytes_transferred)
472 std::stringstream ss;
473 const std::string crlf =
"\n";
474 ss <<
"Perf Marker" << crlf;
475 ss <<
"Timestamp: " << time(NULL) << crlf;
476 ss <<
"Stripe Index: 0" << crlf;
477 ss <<
"Stripe Bytes Transferred: " << bytes_transferred << crlf;
478 ss <<
"Total Stripe Count: 1" << crlf;
482 std::stringstream ss2;
483 for (std::vector<State*>::const_iterator iter = state.begin();
484 iter != state.end(); iter++)
486 std::string desc = (*iter)->GetConnectionDescription();
488 ss2 << (first ?
"" :
",") << desc;
493 ss <<
"RemoteConnections: " << ss2.str() << crlf;
495 rec.bytes_transferred = bytes_transferred;
498 return req.
ChunkResp(ss.str().c_str(), 0);
510 if (!m_request_manager.
Produce(request)) {
512 "Unable to accept HTTP-TPC requests "
513 "because server is too busy. Try again later");
515 logTransferEvent(
LogMask::Error, rec,
"RESPONSE_FAIL",
"Failed to send the initial response to the TPC client");
523 int retval = req.
StartChunkedResp(201,
"Created",
"Content-Type: text/plain");
527 "Failed to send the initial response to the TPC client");
530 "Initial transfer response sent to the TPC client");
534 off_t last_advance_bytes = 0;
535 time_t last_advance_time = time(NULL);
536 time_t transfer_start = last_advance_time;
537 CURLcode res =
static_cast<CURLcode
>(-1);
542 while ((res = (CURLcode)request.WaitFor(std::chrono::seconds(m_marker_period))) < 0) {
543 auto now = time(NULL);
544 std::string conn_desc = request.GetRemoteConnDesc();
546 if (bytes_xfer > last_advance_bytes) {
547 last_advance_bytes = bytes_xfer;
548 last_advance_time = now;
550 if (SendPerfMarker(req, rec, state, conn_desc)) {
552 logTransferEvent(
LogMask::Error, rec,
"PERFMARKER_FAIL",
"Failed to send a perf marker to the TPC client");
554 int timeout = (transfer_start == last_advance_time) ? m_first_timeout : m_timeout;
555 if (now > last_advance_time + timeout) {
556 const char *log_prefix = rec.log_prefix.c_str();
557 bool tpc_pull = strncmp(
"Pull", log_prefix, 4) == 0;
560 std::stringstream ss;
561 ss <<
"Transfer failed because no bytes have been "
562 << (tpc_pull ?
"received from the source (pull mode) in " :
"transmitted to the destination (push mode) in ") << timeout
580 std::stringstream ss;
581 bool success =
false;
584 std::stringstream ss2;
585 ss2 <<
"Remote side failed with status code " << state.
GetStatusCode();
587 std::replace(err.begin(), err.end(),
'\n',
' ');
588 ss2 <<
"; error message: \"" << err <<
"\"";
590 logTransferEvent(
LogMask::Error, rec,
"TRANSFER_FAIL", ss2.str());
591 ss << generateClientErr(ss2, rec);
594 if (err.empty()) {err =
"(no error message provided)";}
595 else {std::replace(err.begin(), err.end(),
'\n',
' ');}
596 std::stringstream ss2;
597 ss2 <<
"Error when interacting with local filesystem: " << err;
598 logTransferEvent(
LogMask::Error, rec,
"TRANSFER_FAIL", ss2.str());
599 ss << generateClientErr(ss2, rec);
600 }
else if (res != CURLE_OK) {
601 std::stringstream ss2;
602 ss2 <<
"Internal transfer failure";
603 std::stringstream ss3;
604 ss3 << ss2.str() <<
": " << curl_easy_strerror(res);
605 logTransferEvent(
LogMask::Error, rec,
"TRANSFER_FAIL", ss3.str());
606 ss << generateClientErr(ss2, rec, res);
608 ss <<
"success: Created";
612 if ((retval = req.
ChunkResp(ss.str().c_str(), 0))) {
614 "Failed to send last update to remote client");
616 }
else if (success) {
627 int TPCHandler::ProcessPushReq(
const std::string & resource,
XrdHttpExtReq &req) {
629 rec.log_prefix =
"PushRequest";
631 rec.remote = resource;
635 if (name) rec.name = name;
636 logTransferEvent(
LogMask::Info, rec,
"PUSH_START",
"Starting a push request");
639 auto curl = curlPtr.get();
641 std::stringstream ss;
642 ss <<
"Failed to initialize internal transfer resources";
645 return req.
SendSimpleResp(rec.status, NULL, NULL, generateClientErr(ss, rec).c_str(), 0);
647 curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
648 curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, (
long) CURL_HTTP_VERSION_1_1);
649 curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, CONNECT_TIMEOUT);
651 std::string redirect_resource = req.
resource;
652 if (query_header != req.
headers.end()) {
653 redirect_resource = query_header->second;
657 uint64_t file_monid =
AtomicInc(m_monid);
659 std::unique_ptr<XrdSfsFile> fh(m_sfs->
newFile(name, file_monid));
662 std::stringstream ss;
663 ss <<
"Failed to initialize internal transfer file handle";
666 return req.
SendSimpleResp(rec.status, NULL, NULL, generateClientErr(ss, rec).c_str(), 0);
668 std::string full_url = prepareURL(req);
670 std::string authz = GetAuthz(req);
672 int open_results = OpenWaitStall(*fh, full_url,
SFS_O_RDONLY, 0644,
675 int result = RedirectTransfer(curl, redirect_resource, req, fh->
error, rec);
677 }
else if (
SFS_OK != open_results) {
679 std::stringstream ss;
681 if (msg == NULL) ss <<
"Failed to open local resource";
684 if (code == EACCES) rec.status = 401;
685 else if (code == EEXIST) rec.status = 412;
687 int resp_result = req.
SendSimpleResp(rec.status, NULL, NULL, generateClientErr(ss, rec).c_str(), 0);
691 ConfigureCurlCA(curl);
692 curl_easy_setopt(curl, CURLOPT_URL, resource.c_str());
694 Stream stream(std::move(fh), 0, 0, m_log);
698 return RunCurlWithUpdates(curl, req, state, rec);
705 int TPCHandler::ProcessPullReq(
const std::string &resource,
XrdHttpExtReq &req) {
707 rec.log_prefix =
"PullRequest";
709 rec.remote = resource;
713 if (name) rec.name = name;
714 logTransferEvent(
LogMask::Info, rec,
"PULL_START",
"Starting a pull request");
717 auto curl = curlPtr.get();
719 std::stringstream ss;
720 ss <<
"Failed to initialize internal transfer resources";
723 return req.
SendSimpleResp(rec.status, NULL, NULL, generateClientErr(ss, rec).c_str(), 0);
740 std::string host_used;
741 if (host_header != req.
headers.end()) {
742 host_used = host_header->second;
748 ip = (
char *)malloc(ip_size-1);
751 memcpy(ip, buff+1, ip_size-2);
755 curl_easy_setopt(curl, CURLOPT_INTERFACE, ip);
757 curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
758 curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, (
long) CURL_HTTP_VERSION_1_1);
759 curl_easy_setopt(curl, CURLOPT_SOCKOPTDATA , &rec);
760 curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, CONNECT_TIMEOUT);
761 std::unique_ptr<XrdSfsFile> fh(m_sfs->
newFile(name, m_monid++));
763 std::stringstream ss;
764 ss <<
"Failed to initialize internal transfer file handle";
767 return req.
SendSimpleResp(rec.status, NULL, NULL, generateClientErr(ss, rec).c_str(), 0);
770 std::string redirect_resource = req.
resource;
771 if (query_header != req.
headers.end()) {
772 redirect_resource = query_header->second;
776 if ((overwrite_header == req.
headers.end()) || (overwrite_header->second ==
"T")) {
782 if (streams_header != req.
headers.end()) {
785 stream_req = std::stol(streams_header->second);
788 if (stream_req < 0 || stream_req > 100) {
789 std::stringstream ss;
790 ss <<
"Invalid request for number of streams";
792 logTransferEvent(
LogMask::Info, rec,
"INVALID_REQUEST", ss.str());
793 return req.
SendSimpleResp(rec.status, NULL, NULL, generateClientErr(ss, rec).c_str(), 0);
795 streams = stream_req == 0 ? 1 : stream_req;
798 rec.streams = streams;
799 std::string full_url = prepareURL(req);
800 std::string authz = GetAuthz(req);
801 curl_easy_setopt(curl, CURLOPT_URL, resource.c_str());
802 ConfigureCurlCA(curl);
803 uint64_t sourceFileContentLength = 0;
808 GetContentLengthTPCPull(curl, req, sourceFileContentLength, success, rec);
812 full_url +=
"&oss.asize=" + std::to_string(sourceFileContentLength);
819 int open_result = OpenWaitStall(*fh, full_url, mode|
SFS_O_WRONLY,
823 int result = RedirectTransfer(curl, redirect_resource, req, fh->
error, rec);
825 }
else if (
SFS_OK != open_result) {
827 std::stringstream ss;
829 if ((msg == NULL) || (*msg ==
'\0')) ss <<
"Failed to open local resource";
832 if (code == EACCES) rec.status = 401;
833 else if (code == EEXIST) rec.status = 412;
836 generateClientErr(ss, rec).c_str(), 0);
840 Stream stream(std::move(fh), streams * m_pipelining_multiplier, streams > 1 ? m_block_size : m_small_block_size, m_log);
846 return RunCurlWithStreams(req, state, streams, rec);
848 return RunCurlWithUpdates(curl, req, state, rec);
856 void TPCHandler::logTransferEvent(
LogMask mask,
const TPCLogRecord &rec,
857 const std::string &event,
const std::string &message)
861 std::stringstream ss;
862 ss <<
"event=" <<
event <<
", local=" << rec.local <<
", remote=" << rec.remote;
863 if (rec.name.empty())
864 ss <<
", user=(anonymous)";
866 ss <<
", user=" << rec.name;
867 if (rec.streams != 1)
868 ss <<
", streams=" << rec.streams;
869 if (rec.bytes_transferred >= 0)
870 ss <<
", bytes_transferred=" << rec.bytes_transferred;
872 ss <<
", status=" << rec.status;
873 if (rec.tpc_status >= 0)
874 ss <<
", tpc_status=" << rec.tpc_status;
875 if (!message.empty())
876 ss <<
"; " << message;
877 m_log.
Log(mask, rec.log_prefix.c_str(), ss.str().c_str());
880 std::string TPCHandler::generateClientErr(std::stringstream &err_ss,
const TPCLogRecord &rec, CURLcode cCode) {
881 std::stringstream ssret;
882 ssret <<
"failure: " << err_ss.str() <<
", local=" << rec.local <<
", remote=" << rec.remote;
883 if(cCode != CURLcode::CURLE_OK) {
884 ssret <<
", HTTP library failure=" << curl_easy_strerror(cCode);
895 if (curl_global_init(CURL_GLOBAL_DEFAULT)) {
896 log->
Emsg(
"TPCInitialize",
"libcurl failed to initialize");
902 log->
Emsg(
"TPCInitialize",
"TPC handler requires a config filename in order to load");
906 log->
Emsg(
"TPCInitialize",
"Will load configuration for the TPC handler from", config);
908 }
catch (std::runtime_error &re) {
909 log->
Emsg(
"TPCInitialize",
"Encountered a runtime failure when loading ", re.what());
static std::string PrepareURL(const std::string &input)
XrdVERSIONINFO(XrdHttpGetExtHandler, HttpTPC)
XrdHttpExtHandler * XrdHttpGetExtHandler(XrdSysError *log, const char *config, const char *, XrdOucEnv *myEnv)
std::string encode_xrootd_opaque_to_uri(CURL *curl, const std::string &opaque)
std::string encode_str(const std::string &str)
void getline(uchar *buff, int blen)
int GetStatusCode() const
off_t BytesTransferred() const
void SetErrorMessage(const std::string &error_msg)
std::string GetErrorMessage() const
std::string GetConnectionDescription()
void SetupHeaders(XrdHttpExtReq &req)
void SetContentLength(const off_t content_length)
off_t GetContentLength() const
void SetErrorCode(int error_code)
TPCHandler(XrdSysError *log, const char *config, XrdOucEnv *myEnv)
virtual int ProcessReq(XrdHttpExtReq &req)
virtual bool MatchesPath(const char *verb, const char *path)
Tells if the incoming path is recognized as one of the paths that have to be processed.
static std::string GenerateIdentifier(const std::string &label, const char *vorg, const int scitag)
bool Produce(TPCRequest &handler)
int ChunkResp(const char *body, long long bodylen)
Send a (potentially partial) body in a chunked response; invoking with NULL body.
void GetClientID(std::string &clid)
std::map< std::string, std::string > & headers
int StartChunkedResp(int code, const char *desc, const char *header_to_add)
Starts a chunked response; body of request is sent over multiple parts using the SendChunkResp.
const XrdSecEntity & GetSecEntity() const
int SendSimpleResp(int code, const char *desc, const char *header_to_add, const char *body, long long bodylen)
Sends a basic response. If the length is < 0 then it is calculated internally.
static std::string prepareOpenURL(const std::string &reqResource, std::map< std::string, std::string > &reqHeaders, const std::map< std::string, std::string > &hdr2cgimap)
static const int noPort
Do not add port number.
int Format(char *bAddr, int bLen, fmtUse fmtType=fmtAuto, int fmtOpts=0)
@ fmtAddr
Address using suitable ipv4 or ipv6 format.
static const char * GetAddrs(const char *hSpec, XrdNetAddr *aListP[], int &aListN, AddrOpts opts=allIPMap, int pNum=PortInSpec)
void * GetPtr(const char *varname)
const char * getErrText()
void setUCap(int ucval)
Set user capabilties.
static std::map< std::string, T >::const_iterator caseInsensitiveFind(const std::map< std::string, T > &m, const std::string &lowerCaseSearchKey)
char * vorg
Entity's virtual organization(s)
char * name
Entity's name.
virtual XrdSfsFile * newFile(char *user=0, int MonID=0)=0
virtual int open(const char *fileName, XrdSfsFileOpenMode openMode, mode_t createMode, const XrdSecEntity *client=0, const char *opaque=0)=0
int Emsg(const char *esfx, int ecode, const char *text1, const char *text2=0)
XrdSysLogger * logger(XrdSysLogger *lp=0)
void Log(int mask, const char *esfx, const char *text1, const char *text2=0, const char *text3=0)
std::unique_ptr< CURL, CurlDeleter > ManagedCurlHandle
void operator()(CURL *curl)
static const int uIPv64
ucap: Supports only IPv4 info