XRootD
XrdClHttpOpChecksum.cc
Go to the documentation of this file.
1 /******************************************************************************/
2 /* Copyright (C) 2025, Pelican Project, Morgridge Institute for Research */
3 /* */
4 /* This file is part of the XrdClHttp client plugin for XRootD. */
5 /* */
6 /* XRootD is free software: you can redistribute it and/or modify it under */
7 /* the terms of the GNU Lesser General Public License as published by the */
8 /* Free Software Foundation, either version 3 of the License, or (at your */
9 /* option) any later version. */
10 /* */
11 /* XRootD is distributed in the hope that it will be useful, but WITHOUT */
12 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
13 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
14 /* License for more details. */
15 /* */
16 /* The copyright holder's institutional names and contributor's names may not */
17 /* be used to endorse or promote products derived from this software without */
18 /* specific prior written permission of the institution or contributor. */
19 /******************************************************************************/
20 
21 #include "XrdClHttpOps.hh"
22 #include "XrdClHttpResponses.hh"
23 #include "XrdClHttpUtil.hh"
24 
25 #include <XrdCl/XrdClLog.hh>
27 
28 #include <cerrno>
29 #include <iomanip>
30 #include <sstream>
31 
32 using namespace XrdClHttp;
33 
35  struct timespec timeout, XrdCl::Log *logger, bool response_info, CreateConnCalloutType callout,
36  HeaderCallout *header_callout)
37 :
38  CurlStatOp(handler, url, timeout, logger, response_info, callout, header_callout),
39  m_preferred_cksum(preferred)
40 {}
41 
42 // Override to prevent the parent CurlStatOp from switching verb to PROPFIND
43 void
45 {}
46 
47 bool
49 {
50  auto rv = CurlStatOp::Setup(curl, worker);
51  if (!rv) return false;
52 
53  curl_easy_setopt(m_curl.get(), CURLOPT_NOBODY, 1L);
54  curl_easy_setopt(m_curl.get(), CURLOPT_CUSTOMREQUEST, nullptr);
55 
56  m_headers_list.emplace_back("Want-Digest", XrdClHttp::HeaderParser::ChecksumTypeToDigestName(m_preferred_cksum));
57 
58  return true;
59 }
60 
62 CurlChecksumOp::Redirect(std::string &target)
63 {
64  auto result = CurlOperation::Redirect(target);
65  curl_easy_setopt(m_curl.get(), CURLOPT_NOBODY, 1L);
66  curl_easy_setopt(m_curl.get(), CURLOPT_CUSTOMREQUEST, nullptr);
67  return result;
68 }
69 
70 void
72 {
73  if (m_curl == nullptr) return;
74  curl_easy_setopt(m_curl.get(), CURLOPT_HTTPHEADER, nullptr);
75 
77 }
78 
79 void
81 {
82  SetDone(false);
83  auto checksums = m_headers.GetChecksums();
84 
85  const auto fail = [&](uint16_t code, uint32_t errNo,
86  const std::string &message) {
87  m_logger->Error(kLogXrdClHttp, "%s for %s", message.c_str(),
88  m_url.c_str());
89  auto handle = m_handler;
90  m_handler = nullptr;
92  XrdCl::stError, code, errNo, message), nullptr);
93  };
94 
95  std::array<unsigned char, XrdClHttp::g_max_checksum_length> value;
96  auto type = XrdClHttp::ChecksumType::kUnknown;
97  if (m_preferred_cksum != XrdClHttp::ChecksumType::kAll) {
98  if (!checksums.IsSet(m_preferred_cksum)) {
99  fail(XrdCl::errNotSupported, ENOTSUP,
100  "server did not return the requested checksum type '" +
101  XrdClHttp::GetTypeString(m_preferred_cksum) + "'");
102  return;
103  }
104  value = checksums.Get(m_preferred_cksum);
105  type = m_preferred_cksum;
106  } else {
107  bool isset;
108  std::tie(type, value, isset) = checksums.GetFirst();
109  if (!isset) {
110  fail(XrdCl::errCheckSumError, ENOMSG,
111  "server did not return a supported checksum");
112  return;
113  }
114  }
115  std::stringstream ss;
116  for (size_t idx = 0; idx < XrdClHttp::GetChecksumLength(type); ++idx) {
117  ss << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(value[idx]);
118  }
119 
120  std::string response = XrdClHttp::GetTypeString(type) + " " + ss.str();
121  auto buf = new XrdClHttp::QueryResponse();
122  buf->FromString(response);
123  buf->SetResponseInfo(MoveResponseInfo());
124 
125  auto obj = new XrdCl::AnyObject();
126  obj->Set(static_cast<XrdCl::Buffer*>(buf));
127 
128  auto handle = m_handler;
129  m_handler = nullptr;
130  handle->HandleResponse(new XrdCl::XRootDStatus(), obj);
131  // Does not call CurlStatOp::Success() as we don't need to invoke a stat info callback
132 }
void CURL
virtual void OptionsDone() override
RedirectAction Redirect(std::string &target) override
CurlChecksumOp(XrdCl::ResponseHandler *handler, const std::string &url, XrdClHttp::ChecksumType preferred, struct timespec timeout, XrdCl::Log *logger, bool response_info, CreateConnCalloutType callout, HeaderCallout *header_callout)
bool Setup(CURL *curl, CurlWorker &) override
void SetDone(bool has_failed)
const std::string m_url
std::unique_ptr< CURL, void(*)(CURL *)> m_curl
std::vector< std::pair< std::string, std::string > > m_headers_list
virtual RedirectAction Redirect(std::string &target)
XrdCl::ResponseHandler * m_handler
std::unique_ptr< ResponseInfo > MoveResponseInfo()
void ReleaseHandle() override
bool Setup(CURL *curl, CurlWorker &) override
const XrdClHttp::ChecksumInfo & GetChecksums() const
static std::string ChecksumTypeToDigestName(XrdClHttp::ChecksumType type)
Binary blob representation.
Definition: XrdClBuffer.hh:34
Handle diagnostics.
Definition: XrdClLog.hh:101
void Error(uint64_t topic, const char *format,...)
Report an error.
Definition: XrdClLog.cc:231
Handle an async response.
virtual void HandleResponse(XRootDStatus *status, AnyObject *response)
size_t GetChecksumLength(ChecksumType ctype)
const std::string GetTypeString(ChecksumType ctype)
const uint16_t stError
An error occurred that could potentially be retried.
Definition: XrdClStatus.hh:32
const uint16_t errNotSupported
Definition: XrdClStatus.hh:62
const uint16_t errCheckSumError
Definition: XrdClStatus.hh:101
ConnectionCallout *(*)(const std::string &, const ResponseInfo &) CreateConnCalloutType
const uint64_t kLogXrdClHttp