XRootD
XrdHttpHeaderUtils.cc
Go to the documentation of this file.
1 //------------------------------------------------------------------------------
2 // This file is part of XrdHTTP: A pragmatic implementation of the
3 // HTTP/WebDAV protocol for the Xrootd framework
4 //
5 // Copyright (c) 2025 by European Organization for Nuclear Research (CERN)
6 // Author: Cedric Caffy <ccaffy@cern.ch>
7 // File Date: Jun 2025
8 //------------------------------------------------------------------------------
9 // XRootD is free software: you can redistribute it and/or modify
10 // it under the terms of the GNU Lesser General Public License as published by
11 // the Free Software Foundation, either version 3 of the License, or
12 // (at your option) any later version.
13 //
14 // XRootD is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public License
20 // along with XRootD. If not, see <http://www.gnu.org/licenses/>.
21 //------------------------------------------------------------------------------
22 #include "XrdHttpHeaderUtils.hh"
23 #include "XrdOuc/XrdOucTUtils.hh"
24 #include "XrdOuc/XrdOucUtils.hh"
25 
26 #include <vector>
27 #include <algorithm>
28 
29 
30 void XrdHttpHeaderUtils::parseReprDigest(const std::string &header, std::map<std::string, std::string> &output) {
31  // Expected format per entry: <cksumType>=:<digestValue>:
32  std::vector<std::string> digestNameValuePairs;
33  XrdOucTUtils::splitString(digestNameValuePairs, header, ",");
34 
35  for (const auto &digestNameValue : digestNameValuePairs) {
36  std::string_view digestNameValueSV {digestNameValue};
37  auto equalPos = digestNameValueSV.find('=');
38  if (equalPos == std::string::npos || equalPos >= digestNameValueSV.size() - 1)
39  continue;
40 
41  std::string_view cksumTypeSV = digestNameValueSV.substr(0, equalPos);
42  XrdOucUtils::trim(cksumTypeSV);
43  if (cksumTypeSV.empty())
44  continue;
45 
46  std::string_view cksumValueInSV = digestNameValueSV.substr(equalPos + 1);
47  size_t beginCksumPos = cksumValueInSV.find(':');
48  size_t endCksumPos = cksumValueInSV.rfind(':');
49 
50  // Check that the string starts with ':' and contains two distinct colons
51  if (beginCksumPos == 0 && endCksumPos > beginCksumPos + 1 && endCksumPos < cksumValueInSV.size()) {
52  std::string_view cksumValue = cksumValueInSV.substr(beginCksumPos + 1, endCksumPos - beginCksumPos - 1);
53  XrdOucUtils::trim(cksumValue);
54  if (!cksumValue.empty())
55  output[std::string(cksumTypeSV)] = cksumValue;
56  }
57  // Malformed entries are silently ignored
58  }
59 }
static void parseReprDigest(const std::string &header, std::map< std::string, std::string > &output)
static void splitString(Container &result, const std::string &input, const std::string &delimiter)
Split a string.
Definition: XrdOucTUtils.hh:51
static void trim(std::string &str)