XRootD
XrdTpcUtils.cc
Go to the documentation of this file.
1 //------------------------------------------------------------------------------
2 // This file is part of XrdTpcTPC
3 //
4 // Copyright (c) 2023 by European Organization for Nuclear Research (CERN)
5 // Author: Cedric Caffy <ccaffy@cern.ch>
6 // File Date: Oct 2023
7 //------------------------------------------------------------------------------
8 // XRootD is free software: you can redistribute it and/or modify
9 // it under the terms of the GNU Lesser General Public License as published by
10 // the Free Software Foundation, either version 3 of the License, or
11 // (at your option) any later version.
12 //
13 // XRootD is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU Lesser General Public License
19 // along with XRootD. If not, see <http://www.gnu.org/licenses/>.
20 //------------------------------------------------------------------------------
21 #include "XrdTpcUtils.hh"
22 #include "XrdOuc/XrdOucTUtils.hh"
23 #include "XrdTpc/XrdTpcTPC.hh"
24 
25 #include <sstream>
26 
27 std::string XrdTpcUtils::prepareOpenURL(const std::string & reqResource, std::map<std::string,std::string> & reqHeaders, const std::map<std::string,std::string> & hdr2cgimap) {
28  auto iter = XrdOucTUtils::caseInsensitiveFind(reqHeaders,"xrd-http-query");
29  std::stringstream opaque;
30 
31  // https://github.com/xrootd/xrootd/issues/2427 we tell the oss layer that this transfer is a HTTP TPC one
32  opaque << "?" << TPC::TPCHandler::OSS_TASK_OPAQUE;
33 
34  if (iter != reqHeaders.end() && !iter->second.empty()) {
35  std::string token;
36  std::istringstream requestStream(iter->second);
37  auto has_authz_header = XrdOucTUtils::caseInsensitiveFind(reqHeaders,"authorization") != reqHeaders.end();
38  while (std::getline(requestStream, token, '&')) {
39  if (token.empty()) {
40  continue;
41  } else if (!strncmp(token.c_str(), "authz=", 6)) {
42  if (!has_authz_header) {
43  reqHeaders["Authorization"] = token.substr(6);
44  has_authz_header = true;
45  }
46  } else if (!strncmp(token.c_str(), "access_token=", 13) && !has_authz_header) {
47  reqHeaders["Authorization"] = token.substr(13);
48  has_authz_header = true;
49  } else {
50  opaque << "&" << token;
51  }
52  }
53  }
54 
55  // Append CGI coming from the tpc.header2cgi parameter
56  for(auto & hdr2cgi : hdr2cgimap) {
57  auto it = std::find_if(reqHeaders.begin(),reqHeaders.end(),[&hdr2cgi](const auto & elt){
58  return !strcasecmp(elt.first.c_str(),hdr2cgi.first.c_str());
59  });
60  if(it != reqHeaders.end()) {
61  opaque << "&" << hdr2cgi.second << "=" << it->second;
62  }
63  }
64 
65  return reqResource + opaque.str();
66 }
void getline(uchar *buff, int blen)
static constexpr std::string_view OSS_TASK_OPAQUE
Definition: XrdTpcTPC.hh:55
static std::map< std::string, T >::const_iterator caseInsensitiveFind(const std::map< std::string, T > &m, const std::string &lowerCaseSearchKey)
Definition: XrdOucTUtils.hh:79
static std::string prepareOpenURL(const std::string &reqResource, std::map< std::string, std::string > &reqHeaders, const std::map< std::string, std::string > &hdr2cgimap)
Definition: XrdTpcUtils.cc:27