XRootD
XrdHttpUtils.hh
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) 2013 by European Organization for Nuclear Research (CERN)
6 // Author: Fabrizio Furano <furano@cern.ch>
7 // File Date: Apr 2013
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 
23 
24 
25 
26 
27 
28 
38 #include "XProtocol/XPtypes.hh"
39 #include "XrdSec/XrdSecEntity.hh"
40 #include "XrdOuc/XrdOucIOVec.hh"
41 #include "XrdOuc/XrdOucTUtils.hh"
42 #include <string>
43 #include <cstring>
44 #include <vector>
45 #include <memory>
46 #include <sstream>
47 
48 #ifndef XRDHTTPUTILS_HH
49 #define XRDHTTPUTILS_HH
50 
51 
52 // GetHost from URL
53 // Parse an URL and extract the host name and port
54 // Return 0 if OK
55 int parseURL(char *url, char *host, int &port, char **path);
56 
57 // Simple itoa function
58 std::string itos(long i);
59 
60 // Home made implementation of strchrnul
61 char *mystrchrnul(const char *s, int c);
62 
63 void calcHashes(
64  char *hash,
65 
66  const char *fn,
67 
68  kXR_int16 req,
69 
70  XrdSecEntity *secent,
71 
72  time_t tim,
73 
74  const char *key);
75 
76 
77 int compareHash(
78  const char *h1,
79  const char *h2);
80 
81 
82 bool Fromhexdigest(const unsigned char *input, int length, unsigned char *out);
83 
84 void Tobase64(const unsigned char *input, int length, char *out);
85 
86 // Create a new quoted string
87 char *quote(const char *str);
88 // unquote a string and return a new one
89 char *unquote(char *str);
90 
99 inline char * decode_raw(const std::string & str) {
100  size_t strLength = str.length();
101  // uniquely own the temporary copy
102  std::unique_ptr<char[]> buf(new char[strLength + 1]);
103  std::memcpy(buf.get(), str.c_str(), strLength + 1);
104  // unquote returns a fresh malloc()'d pointer
105  return unquote(buf.get());
106 }
107 
114 inline char * encode_raw(const std::string & str) {
115  return quote(str.c_str());
116 }
117 
124 inline std::string encode_str(const std::string & str) {
125  char * encodedRaw = encode_raw(str);
126  std::string encoded { encodedRaw };
127  free(encodedRaw);
128  return encoded;
129 }
130 
137 inline std::string decode_str(const std::string & str) {
138  char * decodedRaw = decode_raw(str);
139  std::string decoded { decodedRaw };
140  free(decodedRaw);
141  return decoded;
142 }
143 
150 inline std::string encode_opaque(const std::string & opaque) {
151  std::ostringstream output;
152  std::vector<std::string> allKeyValues;
153  XrdOucTUtils::splitString(allKeyValues,opaque,"&");
154  bool first = true;
155  for(auto & kv: allKeyValues) {
156  size_t equal = kv.find('=');
157  if(equal != std::string::npos) {
158  std::string key = kv.substr(0, equal);
159  std::string value = kv.substr(equal + 1);
160  if(!first) {
161  output << "&";
162  }
163  output << key << "=" << encode_str(value);
164  first = false;
165  }
166  }
167  return output.str();
168 }
169 
170 // Escape a string and return a new one
171 char *escapeXML(const char *str);
172 
173 typedef std::vector<XrdOucIOVec2> XrdHttpIOList;
174 
175 
176 
177 #endif /* XRDHTTPUTILS_HH */
178 
short kXR_int16
Definition: XPtypes.hh:66
int parseURL(char *url, char *host, int &port, char **path)
Definition: XrdHttpUtils.cc:77
std::string itos(long i)
void Tobase64(const unsigned char *input, int length, char *out)
int compareHash(const char *h1, const char *h2)
char * unquote(char *str)
bool Fromhexdigest(const unsigned char *input, int length, unsigned char *out)
char * encode_raw(const std::string &str)
std::string encode_opaque(const std::string &opaque)
char * decode_raw(const std::string &str)
Definition: XrdHttpUtils.hh:99
void calcHashes(char *hash, const char *fn, kXR_int16 req, XrdSecEntity *secent, time_t tim, const char *key)
std::string encode_str(const std::string &str)
std::vector< XrdOucIOVec2 > XrdHttpIOList
char * escapeXML(const char *str)
std::string decode_str(const std::string &str)
char * mystrchrnul(const char *s, int c)
char * quote(const char *str)
static void splitString(Container &result, const std::string &input, const std::string &delimiter)
Split a string.
Definition: XrdOucTUtils.hh:51