XRootD
XrdHttpUtils.hh File Reference

Utility functions for XrdHTTP. More...

#include "XProtocol/XPtypes.hh"
#include "XrdSec/XrdSecEntity.hh"
#include "XrdOuc/XrdOucIOVec.hh"
#include "XrdOuc/XrdOucTUtils.hh"
#include <string>
#include <cstring>
#include <vector>
#include <memory>
#include <sstream>
+ Include dependency graph for XrdHttpUtils.hh:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Typedefs

typedef std::vector< XrdOucIOVec2XrdHttpIOList
 

Functions

void calcHashes (char *hash, const char *fn, kXR_int16 req, XrdSecEntity *secent, time_t tim, const char *key)
 
int compareHash (const char *h1, const char *h2)
 
char * decode_raw (const std::string &str)
 
std::string decode_str (const std::string &str)
 
std::string encode_opaque (const std::string &opaque)
 
char * encode_raw (const std::string &str)
 
std::string encode_str (const std::string &str)
 
char * escapeXML (const char *str)
 
bool Fromhexdigest (const unsigned char *input, int length, unsigned char *out)
 
std::string itos (long i)
 
char * mystrchrnul (const char *s, int c)
 
int parseURL (char *url, char *host, int &port, char **path)
 
char * quote (const char *str)
 
void Tobase64 (const unsigned char *input, int length, char *out)
 
char * unquote (char *str)
 

Detailed Description

Utility functions for XrdHTTP.

Author
Fabrizio Furano
Date
April 2013

Definition in file XrdHttpUtils.hh.

Typedef Documentation

◆ XrdHttpIOList

typedef std::vector<XrdOucIOVec2> XrdHttpIOList

Definition at line 173 of file XrdHttpUtils.hh.

Function Documentation

◆ calcHashes()

void calcHashes ( char *  hash,
const char *  fn,
kXR_int16  req,
XrdSecEntity secent,
time_t  tim,
const char *  key 
)

Definition at line 219 of file XrdHttpUtils.cc.

230  {
231 
232 
233 #if OPENSSL_VERSION_NUMBER >= 0x30000000L
234  EVP_MAC *mac;
235  EVP_MAC_CTX *ctx;
236  size_t len;
237 #else
238  HMAC_CTX *ctx;
239  unsigned int len;
240 #endif
241  unsigned char mdbuf[EVP_MAX_MD_SIZE];
242  char buf[64];
243  struct tm tms;
244 
245 
246  if (!hash) {
247  return;
248  }
249  hash[0] = '\0';
250 
251  if (!key) {
252  return;
253  }
254 
255  if (!fn || !secent) {
256  return;
257  }
258 
259 #if OPENSSL_VERSION_NUMBER >= 0x30000000L
260 
261  mac = EVP_MAC_fetch(0, "sha256", 0);
262  ctx = EVP_MAC_CTX_new(mac);
263 
264  if (!ctx) {
265  return;
266  }
267 
268 
269  EVP_MAC_init(ctx, (const unsigned char *) key, strlen(key), 0);
270 
271 
272  if (fn)
273  EVP_MAC_update(ctx, (const unsigned char *) fn,
274  strlen(fn) + 1);
275 
276  EVP_MAC_update(ctx, (const unsigned char *) &request,
277  sizeof (request));
278 
279  if (secent->name)
280  EVP_MAC_update(ctx, (const unsigned char *) secent->name,
281  strlen(secent->name) + 1);
282 
283  if (secent->vorg)
284  EVP_MAC_update(ctx, (const unsigned char *) secent->vorg,
285  strlen(secent->vorg) + 1);
286 
287  if (secent->host)
288  EVP_MAC_update(ctx, (const unsigned char *) secent->host,
289  strlen(secent->host) + 1);
290 
291  if (secent->moninfo)
292  EVP_MAC_update(ctx, (const unsigned char *) secent->moninfo,
293  strlen(secent->moninfo) + 1);
294 
295  localtime_r(&tim, &tms);
296  strftime(buf, sizeof (buf), "%s", &tms);
297  EVP_MAC_update(ctx, (const unsigned char *) buf,
298  strlen(buf) + 1);
299 
300  EVP_MAC_final(ctx, mdbuf, &len, EVP_MAX_MD_SIZE);
301 
302  EVP_MAC_CTX_free(ctx);
303  EVP_MAC_free(mac);
304 
305 #else
306 
307  ctx = HMAC_CTX_new();
308 
309  if (!ctx) {
310  return;
311  }
312 
313 
314 
315  HMAC_Init_ex(ctx, (const void *) key, strlen(key), EVP_sha256(), 0);
316 
317 
318  if (fn)
319  HMAC_Update(ctx, (const unsigned char *) fn,
320  strlen(fn) + 1);
321 
322  HMAC_Update(ctx, (const unsigned char *) &request,
323  sizeof (request));
324 
325  if (secent->name)
326  HMAC_Update(ctx, (const unsigned char *) secent->name,
327  strlen(secent->name) + 1);
328 
329  if (secent->vorg)
330  HMAC_Update(ctx, (const unsigned char *) secent->vorg,
331  strlen(secent->vorg) + 1);
332 
333  if (secent->host)
334  HMAC_Update(ctx, (const unsigned char *) secent->host,
335  strlen(secent->host) + 1);
336 
337  if (secent->moninfo)
338  HMAC_Update(ctx, (const unsigned char *) secent->moninfo,
339  strlen(secent->moninfo) + 1);
340 
341  localtime_r(&tim, &tms);
342  strftime(buf, sizeof (buf), "%s", &tms);
343  HMAC_Update(ctx, (const unsigned char *) buf,
344  strlen(buf) + 1);
345 
346  HMAC_Final(ctx, mdbuf, &len);
347 
348  HMAC_CTX_free(ctx);
349 
350 #endif
351 
352  Tobase64(mdbuf, len / 2, hash);
353 }
void Tobase64(const unsigned char *input, int length, char *out)
static void HMAC_CTX_free(HMAC_CTX *ctx)
Definition: XrdHttpUtils.cc:65
static HMAC_CTX * HMAC_CTX_new()
Definition: XrdHttpUtils.cc:59
char * vorg
Entity's virtual organization(s)
Definition: XrdSecEntity.hh:71
char * name
Entity's name.
Definition: XrdSecEntity.hh:69
char * moninfo
Information for monitoring.
Definition: XrdSecEntity.hh:76
char * host
Entity's host name dnr dependent.
Definition: XrdSecEntity.hh:70

References HMAC_CTX_free(), HMAC_CTX_new(), XrdSecEntity::host, XrdSecEntity::moninfo, XrdSecEntity::name, Tobase64(), and XrdSecEntity::vorg.

Referenced by XrdHttpProtocol::Process(), and XrdHttpReq::Redir().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ compareHash()

int compareHash ( const char *  h1,
const char *  h2 
)

Definition at line 355 of file XrdHttpUtils.cc.

357  {
358 
359  if (h1 == h2) return 0;
360 
361  if (!h1 || !h2)
362  return 1;
363 
364  return strcmp(h1, h2);
365 
366 }

Referenced by XrdHttpProtocol::Process().

+ Here is the caller graph for this function:

◆ decode_raw()

char* decode_raw ( const std::string &  str)
inline

Creates a non-const copy of the string passed in parameter and calls unquote() on it before returning the pointer to the unquoted string

Parameters
strthe string to unquote
Returns
the malloc'd and unquoted string !!! IT MUST BE FREED AFTER USAGE USING free(...) !!!

Definition at line 99 of file XrdHttpUtils.hh.

99  {
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 }
char * unquote(char *str)

References unquote().

Referenced by decode_str().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ decode_str()

std::string decode_str ( const std::string &  str)
inline

Decodes the string passed in parameter (converts all XX codes to their 8bit versions) Calls unquote() Returns the std::string containing the decoded string.

Definition at line 137 of file XrdHttpUtils.hh.

137  {
138  char * decodedRaw = decode_raw(str);
139  std::string decoded { decodedRaw };
140  free(decodedRaw);
141  return decoded;
142 }
char * decode_raw(const std::string &str)
Definition: XrdHttpUtils.hh:99

References decode_raw().

Referenced by XrdHttpProtocol::Process().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ encode_opaque()

std::string encode_opaque ( const std::string &  opaque)
inline

Encodes opaque query string parameters example: authz=Bearer token --> authz=Bearer%20token

Parameters
opaquethe opaque query string to encode
Returns
the opaque query string with encoded values

Definition at line 150 of file XrdHttpUtils.hh.

150  {
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 }
std::string encode_str(const std::string &str)
static void splitString(Container &result, const std::string &input, const std::string &delimiter)
Split a string.
Definition: XrdOucTUtils.hh:51

References encode_str(), and XrdOucTUtils::splitString().

Referenced by XrdHttpReq::appendOpaque(), XrdHttpReq::ProcessHTTPReq(), and XrdHttpReq::Redir().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ encode_raw()

char* encode_raw ( const std::string &  str)
inline

Calls quote() on the string passed in parameter

Parameters
strthe string to quote
Returns
the pointer to the quoted string !!! IT MUST BE FREED AFTER USAGE USING free(...) !!!

Definition at line 114 of file XrdHttpUtils.hh.

114  {
115  return quote(str.c_str());
116 }
char * quote(const char *str)

References quote().

Referenced by encode_str().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ encode_str()

std::string encode_str ( const std::string &  str)
inline

Encodes the URL passed in parameter (converts all letters consider illegal in URLs to their XX versions). Calls quote() Returns a std::string containing the encoded string

Definition at line 124 of file XrdHttpUtils.hh.

124  {
125  char * encodedRaw = encode_raw(str);
126  std::string encoded { encodedRaw };
127  free(encodedRaw);
128  return encoded;
129 }
char * encode_raw(const std::string &str)

References encode_raw().

Referenced by XrdHttpReq::appendOpaque(), encode_opaque(), and XrdHttpReq::Redir().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ escapeXML()

char* escapeXML ( const char *  str)

Definition at line 458 of file XrdHttpUtils.cc.

458  {
459  int l = strlen(str);
460  char *r = (char *) malloc(l*6 + 1);
461  r[0] = '\0';
462  int i, j = 0;
463 
464  for (i = 0; i < l; i++) {
465  char c = str[i];
466 
467  switch (c) {
468  case '"':
469  strcpy(r + j, "&quot;");
470  j += 6;
471  break;
472  case '&':
473  strcpy(r + j, "&amp;");
474  j += 5;
475  break;
476  case '<':
477  strcpy(r + j, "&lt;");
478  j += 4;
479  break;
480  case '>':
481  strcpy(r + j, "&gt;");
482  j += 4;
483  break;
484  case '\'':
485  strcpy(r + j, "&apos;");
486  j += 6;
487  break;
488 
489  default:
490  r[j++] = c;
491  }
492  }
493 
494  r[j] = '\0';
495 
496  return r;
497 }

Referenced by XrdHttpReq::Error().

+ Here is the caller graph for this function:

◆ Fromhexdigest()

bool Fromhexdigest ( const unsigned char *  input,
int  length,
unsigned char *  out 
)

Definition at line 169 of file XrdHttpUtils.cc.

169  {
170  for (int idx=0; idx < length; idx += 2) {
171  int upper = char_to_int(input[idx]);
172  int lower = char_to_int(input[idx+1]);
173  if ((upper < 0) || (lower < 0)) {
174  return false;
175  }
176  out[idx/2] = (upper << 4) + lower;
177  }
178  return true;
179 }
static int char_to_int(int c)

References char_to_int().

+ Here is the call graph for this function:

◆ itos()

std::string itos ( long  i)

Definition at line 183 of file XrdHttpUtils.cc.

183  {
184  char buf[128];
185  sprintf(buf, "%ld", i);
186 
187  return buf;
188 }

◆ mystrchrnul()

char* mystrchrnul ( const char *  s,
int  c 
)

Definition at line 193 of file XrdHttpUtils.cc.

193  {
194  char *ptr = strchr((char *)s, c);
195 
196  if (!ptr)
197  return strchr((char *)s, '\0');
198 
199  return ptr;
200 }

◆ parseURL()

int parseURL ( char *  url,
char *  host,
int &  port,
char **  path 
)

Definition at line 77 of file XrdHttpUtils.cc.

77  {
78  // http://x.y.z.w:p/path
79 
80  *path = 0;
81 
82  // look for the second slash
83  char *p = strstr(url, "//");
84  if (!p) return -1;
85 
86 
87  p += 2;
88 
89  // look for the end of the host:port
90  char *p2 = strchr(p, '/');
91  if (!p2) return -1;
92 
93  *path = p2;
94 
95  char buf[256];
96  int l = std::min((int)(p2 - p), (int)sizeof (buf));
97  strncpy(buf, p, l);
98  buf[l] = '\0';
99 
100  // Now look for :
101  p = strchr(buf, ':');
102  if (p) {
103  int l = std::min((int)(p - buf), (int)sizeof (buf));
104  strncpy(host, buf, l);
105  host[l] = '\0';
106 
107  port = atoi(p + 1);
108  } else {
109  port = 0;
110 
111 
112  strcpy(host, buf);
113  }
114 
115  return 0;
116 }

Referenced by XrdHttpReq::ProcessHTTPReq().

+ Here is the caller graph for this function:

◆ quote()

char* quote ( const char *  str)

Definition at line 399 of file XrdHttpUtils.cc.

399  {
400  int l = strlen(str);
401  char *r = (char *) malloc(l*3 + 1);
402  r[0] = '\0';
403  int i, j = 0;
404 
405  for (i = 0; i < l; i++) {
406  char c = str[i];
407 
408  switch (c) {
409  case ' ':
410  strcpy(r + j, "%20");
411  j += 3;
412  break;
413  case '[':
414  strcpy(r + j, "%5B");
415  j += 3;
416  break;
417  case ']':
418  strcpy(r + j, "%5D");
419  j += 3;
420  break;
421  case ':':
422  strcpy(r + j, "%3A");
423  j += 3;
424  break;
425  // case '/':
426  // strcpy(r + j, "%2F");
427  // j += 3;
428  // break;
429  case '#':
430  strcpy(r + j, "%23");
431  j += 3;
432  break;
433  case '\n':
434  strcpy(r + j, "%0A");
435  j += 3;
436  break;
437  case '\r':
438  strcpy(r + j, "%0D");
439  j += 3;
440  break;
441  case '=':
442  strcpy(r + j, "%3D");
443  j += 3;
444  break;
445  default:
446  r[j++] = c;
447  }
448  }
449 
450  r[j] = '\0';
451 
452  return r;
453 }

Referenced by encode_raw().

+ Here is the caller graph for this function:

◆ Tobase64()

void Tobase64 ( const unsigned char *  input,
int  length,
char *  out 
)

Definition at line 121 of file XrdHttpUtils.cc.

121  {
122  BIO *bmem, *b64;
123  BUF_MEM *bptr;
124 
125  if (!out) return;
126 
127  out[0] = '\0';
128 
129  b64 = BIO_new(BIO_f_base64());
130  BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
131  bmem = BIO_new(BIO_s_mem());
132  BIO_push(b64, bmem);
133  BIO_write(b64, input, length);
134 
135  if (BIO_flush(b64) <= 0) {
136  BIO_free_all(b64);
137  return;
138  }
139 
140  BIO_get_mem_ptr(b64, &bptr);
141 
142 
143  memcpy(out, bptr->data, bptr->length);
144  out[bptr->length] = '\0';
145 
146  BIO_free_all(b64);
147 
148  return;
149 }
void BIO_set_flags(BIO *bio, int flags)

References BIO_set_flags().

Referenced by calcHashes().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ unquote()

char* unquote ( char *  str)

Definition at line 370 of file XrdHttpUtils.cc.

370  {
371  int l = strlen(str);
372  char *r = (char *) malloc(l + 1);
373  r[0] = '\0';
374  int i, j = 0;
375 
376  for (i = 0; i < l; i++) {
377 
378  if (str[i] == '%') {
379  char savec = str[i + 3];
380  str[i + 3] = '\0';
381 
382  r[j] = strtol(str + i + 1, 0, 16);
383  str[i + 3] = savec;
384 
385  i += 2;
386  } else r[j] = str[i];
387 
388  j++;
389  }
390 
391  r[j] = '\0';
392 
393  return r;
394 
395 }

Referenced by decode_raw().

+ Here is the caller graph for this function: