XRootD
Loading...
Searching...
No Matches
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"
24#include "XrdOuc/XrdOucUtils.hh"
25#include "XrdHttpUtils.hh"
26
27#include <vector>
28#include <algorithm>
29
30
31void XrdHttpHeaderUtils::parseReprDigest(const std::string &value, std::map<std::string, std::string> &output) {
32 // Expected format per entry: <cksumType>=:<digestValue>:
33 std::vector<std::string> digestNameValuePairs;
34 XrdOucTUtils::splitString(digestNameValuePairs, value, ",");
35
36 for (const auto &digestNameValue : digestNameValuePairs) {
37 std::string_view digestNameValueSV {digestNameValue};
38 auto equalPos = digestNameValueSV.find('=');
39 if (equalPos == std::string_view::npos || equalPos >= digestNameValueSV.size() - 1)
40 continue;
41
42 std::string_view cksumTypeSV = digestNameValueSV.substr(0, equalPos);
43 XrdOucUtils::trim(cksumTypeSV);
44 if (cksumTypeSV.empty())
45 continue;
46
47 std::string_view cksumValueInSV = digestNameValueSV.substr(equalPos + 1);
48 size_t beginCksumPos = cksumValueInSV.find(':');
49 size_t endCksumPos = cksumValueInSV.rfind(':');
50
51 // Check that the string starts with ':' and contains two distinct colons
52 if (beginCksumPos == 0 && endCksumPos > beginCksumPos + 1 && endCksumPos < cksumValueInSV.size()) {
53 std::string_view cksumValue = cksumValueInSV.substr(beginCksumPos + 1, endCksumPos - beginCksumPos - 1);
54 XrdOucUtils::trim(cksumValue);
55 if (!cksumValue.empty()) {
56 //What we get as checksum value is a base64-encoded hexadecimal bytes
57 //Let's decode that.
58 std::string chksumDecoded;
59 base64DecodeHex(std::string(cksumValue), chksumDecoded);
60 std::string cksumTypeLC {cksumTypeSV};
61 std::transform(cksumTypeLC.begin(), cksumTypeLC.end(), cksumTypeLC.begin(), ::tolower);
62 output[cksumTypeLC] = chksumDecoded;
63 }
64 }
65 // Malformed entries are silently ignored
66 }
67}
68
69void XrdHttpHeaderUtils::parseWantReprDigest(const std::string & value, std::map<std::string, uint8_t> &output) {
70 size_t pos = 0;
71 std::string_view value_sv {value};
72 while(pos <= value_sv.size()) {
73 // find comma
74 size_t comma = value.find(',',pos);
75 // extract item, no comma means the item is the full string
76 std::string_view item = (comma == std::string_view::npos) ? value_sv.substr(pos) : value_sv.substr(pos, comma - pos);
77 // move current cursor to 'comma + 1' or after the string end
78 pos = (comma == std::string_view::npos) ? value.size() + 1 : comma + 1;
79 // trim the item
81 if(item.empty()) continue;
82
83 size_t eq = item.find('=');
84 // If no '=' sign, we discard this entry as it is malformed
85 if(eq == std::string_view::npos) continue;
86 // We found the equal sign on the item
87 std::string_view digestName {item.substr(0, eq)};
88 XrdOucUtils::trim(digestName);
89 std::string_view preference {item.substr(eq+1)};
90 XrdOucUtils::trim(preference);
91
92 std::string key_lower {digestName};
93 std::transform(key_lower.begin(),key_lower.end(),key_lower.begin(),::tolower);
94
95 try {
96 uint8_t preference_us = XrdOucUtils::touint8_t(preference);
97 // Max allowed value for Repr-Digest is 10
98 preference_us = std::min(preference_us,(uint8_t)10);
99 output[key_lower] = preference_us;
100 } catch (...) {
101 // discard invalid values
102 }
103 }
104}
void base64DecodeHex(const std::string &base64, std::string &hexOutput)
Utility functions for XrdHTTP.
static void parseWantReprDigest(const std::string &value, std::map< std::string, uint8_t > &output)
static void parseReprDigest(const std::string &value, std::map< std::string, std::string > &output)
static void splitString(Container &result, const std::string &input, const std::string &delimiter)
Split a string.
static uint8_t touint8_t(const std::string_view sv)
static void trim(std::string &str)