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 
37 #ifndef XRDHTTPUTILS_HH
38 #define XRDHTTPUTILS_HH
39 
40 #include "XProtocol/XPtypes.hh"
41 #include "XProtocol/XProtocol.hh"
42 #include "XrdSec/XrdSecEntity.hh"
43 #include "XrdOuc/XrdOucIOVec.hh"
44 #include "XrdOuc/XrdOucTUtils.hh"
45 #include <string>
46 #include <cstring>
47 #include <vector>
48 #include <memory>
49 #include <sstream>
50 #include <cstdint>
51 
52 enum : int {
57 
58  // 2xx Success
59  HTTP_OK = 200,
60  HTTP_CREATED = 201,
68  HTTP_IM_USED = 226,
69 
70  // 3xx Redirection
73  HTTP_FOUND = 302,
79 
80  // 4xx Client Errors
91  HTTP_GONE = 410,
99  HTTP_IM_A_TEAPOT = 418, // RFC 2324
102  HTTP_LOCKED = 423,
110 
111  // 5xx Server Errors
123 };
124 
125 // Simple itoa function
126 std::string itos(long i);
127 
128 // Home made implementation of strchrnul
129 char *mystrchrnul(const char *s, int c);
130 
131 void calcHashes(
132  char *hash,
133 
134  const char *fn,
135 
136  kXR_int16 req,
137 
138  XrdSecEntity *secent,
139 
140  time_t tim,
141 
142  const char *key);
143 
144 
145 int compareHash(
146  const char *h1,
147  const char *h2);
148 
149 
150 bool Fromhexdigest(const std::string & hex, std::vector<uint8_t> & outputBytes);
151 
152 void Tobase64(const unsigned char *input, int length, char *out);
153 
154 void Tobase64(const std::vector<uint8_t> & input, std::string & base64Output);
155 
156 void base64ToBytes(const std::string & base64digest, std::vector<uint8_t> & outputBytes);
157 
158 void bytesToHex(const std::vector<uint8_t> & bytes, std::string & hexOutput);
159 
160 void base64DecodeHex(const std::string & base64, std::string & hexOutput);
161 
162 // Create a new quoted string
163 char *quote(const char *str);
164 // unquote a string and return a new one
165 char *unquote(char *str);
166 
175 inline char * decode_raw(const std::string & str) {
176  size_t strLength = str.length();
177  // uniquely own the temporary copy
178  std::unique_ptr<char[]> buf(new char[strLength + 1]);
179  std::memcpy(buf.get(), str.c_str(), strLength + 1);
180  // unquote returns a fresh malloc()'d pointer
181  return unquote(buf.get());
182 }
183 
190 inline char * encode_raw(const std::string & str) {
191  return quote(str.c_str());
192 }
193 
200 inline std::string encode_str(const std::string & str) {
201  char * encodedRaw = encode_raw(str);
202  std::string encoded { encodedRaw };
203  free(encodedRaw);
204  return encoded;
205 }
206 
213 inline std::string decode_str(const std::string & str) {
214  char * decodedRaw = decode_raw(str);
215  std::string decoded { decodedRaw };
216  free(decodedRaw);
217  return decoded;
218 }
219 
226 inline std::string encode_opaque(const std::string & opaque) {
227  std::ostringstream output;
228  std::vector<std::string> allKeyValues;
229  XrdOucTUtils::splitString(allKeyValues,opaque,"&");
230  bool first = true;
231  for(auto & kv: allKeyValues) {
232  size_t equal = kv.find('=');
233  if(equal != std::string::npos) {
234  std::string key = kv.substr(0, equal);
235  std::string value = kv.substr(equal + 1);
236  if(!first) {
237  output << "&";
238  }
239  output << encode_str(key) << "=" << encode_str(value);
240  first = false;
241  }
242  }
243  return output.str();
244 }
245 
246 // Escape a string and return a new one
247 char *escapeXML(const char *str);
248 
249 int mapXrdErrToHttp(XErrorCode xrdError);
250 
251 int mapErrNoToHttp(int err);
252 
253 std::string httpStatusToString(int status);
254 
255 typedef std::vector<XrdOucIOVec2> XrdHttpIOList;
256 
257 
258 
259 #endif /* XRDHTTPUTILS_HH */
XErrorCode
Definition: XProtocol.hh:1031
short kXR_int16
Definition: XPtypes.hh:66
std::string itos(long i)
void Tobase64(const unsigned char *input, int length, char *out)
Definition: XrdHttpUtils.cc:60
int compareHash(const char *h1, const char *h2)
void bytesToHex(const std::vector< uint8_t > &bytes, std::string &hexOutput)
char * unquote(char *str)
int mapErrNoToHttp(int err)
void base64ToBytes(const std::string &base64digest, std::vector< uint8_t > &outputBytes)
@ HTTP_HTTP_VERSION_NOT_SUPPORTED
@ HTTP_INSUFFICIENT_STORAGE
@ HTTP_PRECONDITION_FAILED
Definition: XrdHttpUtils.hh:93
@ HTTP_BAD_REQUEST
Definition: XrdHttpUtils.hh:81
@ HTTP_NOT_EXTENDED
@ HTTP_FAILED_DEPENDENCY
@ HTTP_VARIANT_ALSO_NEGOTIATES
@ HTTP_CREATED
Definition: XrdHttpUtils.hh:60
@ HTTP_IM_A_TEAPOT
Definition: XrdHttpUtils.hh:99
@ HTTP_MULTIPLE_CHOICES
Definition: XrdHttpUtils.hh:71
@ HTTP_LOCKED
@ HTTP_OK
Definition: XrdHttpUtils.hh:59
@ HTTP_NOT_MODIFIED
Definition: XrdHttpUtils.hh:75
@ HTTP_NON_AUTHORITATIVE_INFORMATION
Definition: XrdHttpUtils.hh:62
@ HTTP_LOOP_DETECTED
@ HTTP_EXPECTATION_FAILED
Definition: XrdHttpUtils.hh:98
@ HTTP_SERVICE_UNAVAILABLE
@ HTTP_PROXY_AUTHENTICATION_REQUIRED
Definition: XrdHttpUtils.hh:88
@ HTTP_LENGTH_REQUIRED
Definition: XrdHttpUtils.hh:92
@ HTTP_URI_TOO_LONG
Definition: XrdHttpUtils.hh:95
@ HTTP_UNAVAILABLE_FOR_LEGAL_REASONS
@ HTTP_UNAUTHORIZED
Definition: XrdHttpUtils.hh:82
@ HTTP_UNSUPPORTED_MEDIA_TYPE
Definition: XrdHttpUtils.hh:96
@ HTTP_NOT_FOUND
Definition: XrdHttpUtils.hh:85
@ HTTP_FORBIDDEN
Definition: XrdHttpUtils.hh:84
@ HTTP_CONTINUE
Definition: XrdHttpUtils.hh:53
@ HTTP_MULTI_STATUS
Definition: XrdHttpUtils.hh:66
@ HTTP_PERMANENT_REDIRECT
Definition: XrdHttpUtils.hh:78
@ HTTP_FOUND
Definition: XrdHttpUtils.hh:73
@ HTTP_RESET_CONTENT
Definition: XrdHttpUtils.hh:64
@ HTTP_TEMPORARY_REDIRECT
Definition: XrdHttpUtils.hh:77
@ HTTP_NO_CONTENT
Definition: XrdHttpUtils.hh:63
@ HTTP_MISDIRECTED_REQUEST
@ HTTP_PARTIAL_CONTENT
Definition: XrdHttpUtils.hh:65
@ HTTP_BAD_GATEWAY
@ HTTP_GATEWAY_TIMEOUT
@ HTTP_NETWORK_AUTHENTICATION_REQUIRED
@ HTTP_TOO_EARLY
@ HTTP_METHOD_NOT_ALLOWED
Definition: XrdHttpUtils.hh:86
@ HTTP_NOT_ACCEPTABLE
Definition: XrdHttpUtils.hh:87
@ HTTP_ALREADY_REPORTED
Definition: XrdHttpUtils.hh:67
@ HTTP_EARLY_HINTS
Definition: XrdHttpUtils.hh:56
@ HTTP_GONE
Definition: XrdHttpUtils.hh:91
@ HTTP_INTERNAL_SERVER_ERROR
@ HTTP_UPGRADE_REQUIRED
@ HTTP_IM_USED
Definition: XrdHttpUtils.hh:68
@ HTTP_TOO_MANY_REQUESTS
@ HTTP_PAYLOAD_TOO_LARGE
Definition: XrdHttpUtils.hh:94
@ HTTP_NOT_IMPLEMENTED
@ HTTP_SEE_OTHER
Definition: XrdHttpUtils.hh:74
@ HTTP_PAYMENT_REQUIRED
Definition: XrdHttpUtils.hh:83
@ HTTP_REQUEST_TIMEOUT
Definition: XrdHttpUtils.hh:89
@ HTTP_UNPROCESSABLE_ENTITY
@ HTTP_REQUEST_HEADER_FIELDS_TOO_LARGE
@ HTTP_SWITCHING_PROTOCOLS
Definition: XrdHttpUtils.hh:54
@ HTTP_MOVED_PERMANENTLY
Definition: XrdHttpUtils.hh:72
@ HTTP_USE_PROXY
Definition: XrdHttpUtils.hh:76
@ HTTP_PRECONDITION_REQUIRED
@ HTTP_RANGE_NOT_SATISFIABLE
Definition: XrdHttpUtils.hh:97
@ HTTP_ACCEPTED
Definition: XrdHttpUtils.hh:61
@ HTTP_CONFLICT
Definition: XrdHttpUtils.hh:90
@ HTTP_PROCESSING
Definition: XrdHttpUtils.hh:55
char * encode_raw(const std::string &str)
std::string encode_opaque(const std::string &opaque)
int mapXrdErrToHttp(XErrorCode xrdError)
bool Fromhexdigest(const std::string &hex, std::vector< uint8_t > &outputBytes)
char * decode_raw(const std::string &str)
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)
std::string httpStatusToString(int status)
void base64DecodeHex(const std::string &base64, std::string &hexOutput)
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
@ hex
Definition: XrdSysTrace.hh:42