Utility functions for XrdHTTP.
More...
#include "XrdHttpUtils.hh"
#include <cstring>
#include <openssl/hmac.h>
#include <openssl/bio.h>
#include <openssl/buffer.h>
#include <openssl/err.h>
#include <openssl/ssl.h>
#include "sys/param.h"
#include <pthread.h>
#include <memory>
#include <vector>
#include <algorithm>
#include "XrdSec/XrdSecEntity.hh"
#include "XrdOuc/XrdOucString.hh"
Go to the source code of this file.
|
| void | base64DecodeHex (const std::string &base64, std::string &hexOutput) |
| |
| void | base64ToBytes (const std::string &base64digest, std::vector< uint8_t > &outputBytes) |
| |
| void | bytesToHex (const std::vector< uint8_t > &bytes, std::string &output) |
| |
| void | calcHashes (char *hash, const char *fn, kXR_int16 request, XrdSecEntity *secent, time_t tim, const char *key) |
| |
| static int | char_to_int (int ch) |
| |
| int | compareHash (const char *h1, const char *h2) |
| |
| char * | escapeXML (const char *str) |
| |
| bool | Fromhexdigest (const std::string &hex, std::vector< uint8_t > &outputBytes) |
| |
| std::string | httpStatusToString (int status) |
| |
| std::string | itos (long i) |
| |
| int | mapErrNoToHttp (int errNo) |
| |
| int | mapXrdErrToHttp (XErrorCode xrdError) |
| |
| char * | mystrchrnul (const char *s, int c) |
| |
| char * | quote (const char *str) |
| |
| void | Tobase64 (const std::vector< uint8_t > &input, std::string &base64Output) |
| |
| void | Tobase64 (const unsigned char *input, int length, char *out) |
| |
| char * | unquote (char *str) |
| |
Utility functions for XrdHTTP.
- Author
- Fabrizio Furano
- Date
- April 2013
Definition in file XrdHttpUtils.cc.
◆ base64DecodeHex()
| void base64DecodeHex |
( |
const std::string & |
base64, |
|
|
std::string & |
hexOutput |
|
) |
| |
◆ base64ToBytes()
| void base64ToBytes |
( |
const std::string & |
base64digest, |
|
|
std::vector< uint8_t > & |
outputBytes |
|
) |
| |
Definition at line 118 of file XrdHttpUtils.cc.
121 if (base64digest.empty()) {
125 BIO *b64 = BIO_new(BIO_f_base64());
126 BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
128 BIO *bmem = BIO_new_mem_buf(base64digest.data(),
static_cast<int>(base64digest.size()));
129 bmem = BIO_push(b64, bmem);
132 std::vector<uint8_t> buffer(base64digest.size());
134 int decodedLen = BIO_read(bmem, buffer.data(),
static_cast<int>(buffer.size()));
135 if (decodedLen > 0) {
136 buffer.resize(decodedLen);
137 outputBytes.swap(buffer);
Referenced by base64DecodeHex().
◆ bytesToHex()
| void bytesToHex |
( |
const std::vector< uint8_t > & |
bytes, |
|
|
std::string & |
output |
|
) |
| |
Definition at line 145 of file XrdHttpUtils.cc.
146 static const char* lut =
"0123456789abcdef";
148 output.reserve(bytes.size() * 2);
149 for (uint8_t b : bytes) {
150 output.push_back(lut[b >> 4]);
151 output.push_back(lut[b & 0x0F]);
Referenced by base64DecodeHex().
◆ calcHashes()
| void calcHashes |
( |
char * |
hash, |
|
|
const char * |
fn, |
|
|
kXR_int16 |
request, |
|
|
XrdSecEntity * |
secent, |
|
|
time_t |
tim, |
|
|
const char * |
key |
|
) |
| |
Definition at line 231 of file XrdHttpUtils.cc.
245 #if OPENSSL_VERSION_NUMBER >= 0x30000000L
253 unsigned char mdbuf[EVP_MAX_MD_SIZE];
267 if (!fn || !secent) {
271 #if OPENSSL_VERSION_NUMBER >= 0x30000000L
273 mac = EVP_MAC_fetch(0,
"sha256", 0);
274 ctx = EVP_MAC_CTX_new(mac);
281 EVP_MAC_init(ctx, (
const unsigned char *) key, strlen(key), 0);
285 EVP_MAC_update(ctx, (
const unsigned char *) fn,
288 EVP_MAC_update(ctx, (
const unsigned char *) &request,
292 EVP_MAC_update(ctx, (
const unsigned char *) secent->
name,
293 strlen(secent->
name) + 1);
296 EVP_MAC_update(ctx, (
const unsigned char *) secent->
vorg,
297 strlen(secent->
vorg) + 1);
300 EVP_MAC_update(ctx, (
const unsigned char *) secent->
host,
301 strlen(secent->
host) + 1);
304 EVP_MAC_update(ctx, (
const unsigned char *) secent->
moninfo,
307 localtime_r(&tim, &tms);
308 strftime(buf,
sizeof (buf),
"%s", &tms);
309 EVP_MAC_update(ctx, (
const unsigned char *) buf,
312 EVP_MAC_final(ctx, mdbuf, &len, EVP_MAX_MD_SIZE);
314 EVP_MAC_CTX_free(ctx);
319 ctx = HMAC_CTX_new();
327 HMAC_Init_ex(ctx, (
const void *) key, strlen(key), EVP_sha256(), 0);
331 HMAC_Update(ctx, (
const unsigned char *) fn,
334 HMAC_Update(ctx, (
const unsigned char *) &request,
338 HMAC_Update(ctx, (
const unsigned char *) secent->
name,
339 strlen(secent->
name) + 1);
342 HMAC_Update(ctx, (
const unsigned char *) secent->
vorg,
343 strlen(secent->
vorg) + 1);
346 HMAC_Update(ctx, (
const unsigned char *) secent->
host,
347 strlen(secent->
host) + 1);
350 HMAC_Update(ctx, (
const unsigned char *) secent->
moninfo,
353 localtime_r(&tim, &tms);
354 strftime(buf,
sizeof (buf),
"%s", &tms);
355 HMAC_Update(ctx, (
const unsigned char *) buf,
358 HMAC_Final(ctx, mdbuf, &len);
void Tobase64(const unsigned char *input, int length, char *out)
char * vorg
Entity's virtual organization(s)
char * name
Entity's name.
char * moninfo
Information for monitoring.
char * host
Entity's host name dnr dependent.
References XrdSecEntity::host, XrdSecEntity::moninfo, XrdSecEntity::name, Tobase64(), and XrdSecEntity::vorg.
Referenced by XrdHttpProtocol::Process(), and XrdHttpReq::Redir().
◆ char_to_int()
| static int char_to_int |
( |
int |
ch | ) |
|
|
static |
Definition at line 163 of file XrdHttpUtils.cc.
165 unsigned char c =
static_cast<unsigned char>(ch);
166 if (std::isdigit(c)) {
170 if (c >=
'a' && c <=
'f') {
Referenced by Fromhexdigest().
◆ compareHash()
| int compareHash |
( |
const char * |
h1, |
|
|
const char * |
h2 |
|
) |
| |
◆ escapeXML()
| char* escapeXML |
( |
const char * |
str | ) |
|
Definition at line 473 of file XrdHttpUtils.cc.
475 char *r = (
char *) malloc(l*6 + 1);
479 for (i = 0; i < l; i++) {
484 strcpy(r + j,
""");
488 strcpy(r + j,
"&");
492 strcpy(r + j,
"<");
496 strcpy(r + j,
">");
500 strcpy(r + j,
"'");
Referenced by XrdHttpReq::Error().
◆ Fromhexdigest()
| bool Fromhexdigest |
( |
const std::string & |
hex, |
|
|
std::vector< uint8_t > & |
outputBytes |
|
) |
| |
Definition at line 177 of file XrdHttpUtils.cc.
178 if(
hex.size() % 2 != 0) {
182 outputBytes.reserve(
hex.size() / 2);
184 for(
size_t i = 0; i <
hex.size(); i += 2) {
187 if (upper < 0 || lower < 0)
return false;
188 outputBytes.push_back(
static_cast<uint8_t
>((upper << 4) + lower));
static int char_to_int(int ch)
References char_to_int(), and Xrd::hex.
◆ httpStatusToString()
| std::string httpStatusToString |
( |
int |
status | ) |
|
Definition at line 594 of file XrdHttpUtils.cc.
597 case 100:
return "Continue";
598 case 101:
return "Switching Protocols";
599 case 102:
return "Processing";
600 case 103:
return "Early Hints";
603 case 200:
return "OK";
604 case 201:
return "Created";
605 case 202:
return "Accepted";
606 case 203:
return "Non-Authoritative Information";
607 case 204:
return "No Content";
608 case 205:
return "Reset Content";
609 case 206:
return "Partial Content";
610 case 207:
return "Multi-Status";
611 case 208:
return "Already Reported";
612 case 226:
return "IM Used";
615 case 300:
return "Multiple Choices";
616 case 301:
return "Moved Permanently";
617 case 302:
return "Found";
618 case 303:
return "See Other";
619 case 304:
return "Not Modified";
620 case 305:
return "Use Proxy";
621 case 307:
return "Temporary Redirect";
622 case 308:
return "Permanent Redirect";
625 case 400:
return "Bad Request";
626 case 401:
return "Unauthorized";
627 case 402:
return "Payment Required";
628 case 403:
return "Forbidden";
629 case 404:
return "Not Found";
630 case 405:
return "Method Not Allowed";
631 case 406:
return "Not Acceptable";
632 case 407:
return "Proxy Authentication Required";
633 case 408:
return "Request Timeout";
634 case 409:
return "Conflict";
635 case 410:
return "Gone";
636 case 411:
return "Length Required";
637 case 412:
return "Precondition Failed";
638 case 413:
return "Payload Too Large";
639 case 414:
return "URI Too Long";
640 case 415:
return "Unsupported Media Type";
641 case 416:
return "Range Not Satisfiable";
642 case 417:
return "Expectation Failed";
643 case 418:
return "I'm a teapot";
644 case 421:
return "Misdirected Request";
645 case 422:
return "Unprocessable Entity";
646 case 423:
return "Locked";
647 case 424:
return "Failed Dependency";
648 case 425:
return "Too Early";
649 case 426:
return "Upgrade Required";
650 case 428:
return "Precondition Required";
651 case 429:
return "Too Many Requests";
652 case 431:
return "Request Header Fields Too Large";
653 case 451:
return "Unavailable For Legal Reasons";
656 case 500:
return "Internal Server Error";
657 case 501:
return "Not Implemented";
658 case 502:
return "Bad Gateway";
659 case 503:
return "Service Unavailable";
660 case 504:
return "Gateway Timeout";
661 case 505:
return "HTTP Version Not Supported";
662 case 506:
return "Variant Also Negotiates";
663 case 507:
return "Insufficient Storage";
664 case 508:
return "Loop Detected";
665 case 510:
return "Not Extended";
666 case 511:
return "Network Authentication Required";
670 case 100 ... 199:
return "Informational";
671 case 200 ... 299:
return "Success";
672 case 300 ... 399:
return "Redirection";
673 case 400 ... 499:
return "Client Error";
674 case 500 ... 599:
return "Server Error";
675 default:
return "Unknown";
◆ itos()
| std::string itos |
( |
long |
i | ) |
|
◆ mapErrNoToHttp()
| int mapErrNoToHttp |
( |
int |
errNo | ) |
|
Definition at line 521 of file XrdHttpUtils.cc.
@ HTTP_INSUFFICIENT_STORAGE
@ HTTP_SERVICE_UNAVAILABLE
@ HTTP_INTERNAL_SERVER_ERROR
@ HTTP_UNPROCESSABLE_ENTITY
References EAUTH, HTTP_BAD_GATEWAY, HTTP_BAD_REQUEST, HTTP_CONFLICT, HTTP_FORBIDDEN, HTTP_GATEWAY_TIMEOUT, HTTP_INSUFFICIENT_STORAGE, HTTP_INTERNAL_SERVER_ERROR, HTTP_LOOP_DETECTED, HTTP_NOT_FOUND, HTTP_NOT_IMPLEMENTED, HTTP_PAYLOAD_TOO_LARGE, HTTP_SERVICE_UNAVAILABLE, HTTP_UNAUTHORIZED, HTTP_UNPROCESSABLE_ENTITY, and HTTP_URI_TOO_LONG.
Referenced by mapXrdErrToHttp().
◆ mapXrdErrToHttp()
◆ mystrchrnul()
| char* mystrchrnul |
( |
const char * |
s, |
|
|
int |
c |
|
) |
| |
Definition at line 205 of file XrdHttpUtils.cc.
206 char *ptr = strchr((
char *)s, c);
209 return strchr((
char *)s,
'\0');
◆ quote()
| char* quote |
( |
const char * |
str | ) |
|
Definition at line 414 of file XrdHttpUtils.cc.
416 char *r = (
char *) malloc(l*3 + 1);
420 for (i = 0; i < l; i++) {
425 strcpy(r + j,
"%20");
429 strcpy(r + j,
"%5B");
433 strcpy(r + j,
"%5D");
437 strcpy(r + j,
"%3A");
445 strcpy(r + j,
"%23");
449 strcpy(r + j,
"%0A");
453 strcpy(r + j,
"%0D");
457 strcpy(r + j,
"%3D");
Referenced by encode_raw().
◆ Tobase64() [1/2]
| void Tobase64 |
( |
const std::vector< uint8_t > & |
input, |
|
|
std::string & |
base64Output |
|
) |
| |
Definition at line 90 of file XrdHttpUtils.cc.
100 b64 = BIO_new(BIO_f_base64());
101 BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
102 bmem = BIO_new(BIO_s_mem());
104 BIO_write(b64, input.data(), input.size());
106 if (BIO_flush(b64) <= 0) {
111 BIO_get_mem_ptr(b64, &bptr);
113 base64Output.assign(bptr->data,bptr->length);
◆ Tobase64() [2/2]
| void Tobase64 |
( |
const unsigned char * |
input, |
|
|
int |
length, |
|
|
char * |
out |
|
) |
| |
Definition at line 60 of file XrdHttpUtils.cc.
68 b64 = BIO_new(BIO_f_base64());
69 BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
70 bmem = BIO_new(BIO_s_mem());
72 BIO_write(b64, input, length);
74 if (BIO_flush(b64) <= 0) {
79 BIO_get_mem_ptr(b64, &bptr);
82 memcpy(out, bptr->data, bptr->length);
83 out[bptr->length] =
'\0';
Referenced by calcHashes().
◆ unquote()
| char* unquote |
( |
char * |
str | ) |
|
Definition at line 382 of file XrdHttpUtils.cc.
384 char *r = (
char *) malloc(l + 1);
388 for (i = 0; i < l; i++) {
394 char savec = str[i + 3];
397 r[j] = strtol(str + i + 1, 0, 16);
401 }
else r[j] = str[i];
Referenced by decode_raw().