XRootD
XrdHttpCorsHandler.cc
Go to the documentation of this file.
1 //------------------------------------------------------------------------------
2 // This file is part of the XRootD framework
3 //
4 // Copyright (c) 2025 by European Organization for Nuclear Research (CERN)
5 // Author: Cedric Caffy <cedric.caffy@cern.ch>
6 // File Date: Jun 2025
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 
22 #include "XrdHttpCorsHandler.hh"
24 #include "XrdVersion.hh"
25 #include "XrdOuc/XrdOucUtils.hh"
26 
27 extern "C"
28 {
30  return new XrdHttpCorsHandler();
31 }
32 }
33 
40 int XrdHttpCorsHandler::Configure(const char *configFN, XrdSysError *errP) {
41  // Get the cors.origin parameters which can be either space-delimited or a repetition of cors.origin
42  XrdOucGatherConf gconf("cors.origin",errP);
43  if(gconf.Gather(configFN,XrdOucGatherConf::only_body) < 0){
44  return 1;
45  }
46  if(gconf.GetLine()) {
47  while(char * val = gconf.GetToken()) {
48  // No need to check for correctness
49  addAllowedOrigin(val);
50  }
51  }
52  return 0;
53 }
54 
55 std::optional<std::string> XrdHttpCorsHandler::getCORSAllowOriginHeader(const std::string & origin) {
56  auto originItor = m_origins.find(origin);
57  if(originItor != m_origins.end()) {
58  return "Access-Control-Allow-Origin: " + origin;
59  }
60  // We did not find any allowed origin, return nullopt
61  return std::nullopt;
62 }
63 
64 void XrdHttpCorsHandler::addAllowedOrigin(const std::string & origin) {
65  std::string_view origin_sv(origin);
66  XrdOucUtils::trim(origin_sv);
67  if(!origin_sv.empty()) {
68  m_origins.emplace(origin_sv);
69  }
70 }
71 
XrdHttpCors * XrdHttpCorsGetHandler(XrdHttpCorsGetHandlerArgs)
XrdVERSIONINFO(XrdHttpCorsget, XrdHttpCorsHandler)
#define XrdHttpCorsGetHandlerArgs
Definition: XrdHttpCors.hh:70
int Configure(const char *configFN, XrdSysError *errP) override
void addAllowedOrigin(const std::string &origin) override
std::optional< std::string > getCORSAllowOriginHeader(const std::string &origin) override
char * GetToken(char **rest=0, int lowcase=0)
int Gather(const char *cfname, Level lvl, const char *parms=0)
@ only_body
Only directive bodies as a string blob.
static void trim(std::string &str)