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 | calcHashes (char *hash, const char *fn, kXR_int16 request, XrdSecEntity *secent, time_t tim, const char *key) |
| |
| static int | char_to_int (int c) |
| |
| int | compareHash (const char *h1, const char *h2) |
| |
| char * | escapeXML (const char *str) |
| |
| bool | Fromhexdigest (const unsigned char *input, int length, unsigned char *out) |
| |
| static void | HMAC_CTX_free (HMAC_CTX *ctx) |
| |
| static HMAC_CTX * | HMAC_CTX_new () |
| |
| 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) |
| |
| 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) |
| |
Utility functions for XrdHTTP.
- Author
- Fabrizio Furano
- Date
- April 2013
Definition in file XrdHttpUtils.cc.
◆ calcHashes()
| void calcHashes |
( |
char * |
hash, |
|
|
const char * |
fn, |
|
|
kXR_int16 |
request, |
|
|
XrdSecEntity * |
secent, |
|
|
time_t |
tim, |
|
|
const char * |
key |
|
) |
| |
Definition at line 219 of file XrdHttpUtils.cc.
233 #if OPENSSL_VERSION_NUMBER >= 0x30000000L
241 unsigned char mdbuf[EVP_MAX_MD_SIZE];
255 if (!fn || !secent) {
259 #if OPENSSL_VERSION_NUMBER >= 0x30000000L
261 if (!(mac = EVP_MAC_fetch(
nullptr,
"HMAC",
nullptr))) {
265 if (!(ctx = EVP_MAC_CTX_new(mac))) {
270 OSSL_PARAM params[2] = {
271 OSSL_PARAM_construct_utf8_string(
"digest", (
char*)
"SHA256", 0),
272 OSSL_PARAM_construct_end()
275 if (!EVP_MAC_init(ctx, (
const unsigned char *) key, strlen(key), params)) {
276 EVP_MAC_CTX_free(ctx);
282 EVP_MAC_update(ctx, (
const unsigned char *) fn,
285 EVP_MAC_update(ctx, (
const unsigned char *) &request,
289 EVP_MAC_update(ctx, (
const unsigned char *) secent->
name,
290 strlen(secent->
name) + 1);
293 EVP_MAC_update(ctx, (
const unsigned char *) secent->
vorg,
294 strlen(secent->
vorg) + 1);
297 EVP_MAC_update(ctx, (
const unsigned char *) secent->
host,
298 strlen(secent->
host) + 1);
301 EVP_MAC_update(ctx, (
const unsigned char *) secent->
moninfo,
304 localtime_r(&tim, &tms);
305 strftime(buf,
sizeof (buf),
"%s", &tms);
306 EVP_MAC_update(ctx, (
const unsigned char *) buf,
309 EVP_MAC_final(ctx, mdbuf, &len, EVP_MAX_MD_SIZE);
311 EVP_MAC_CTX_free(ctx);
324 HMAC_Init_ex(ctx, (
const void *) key, strlen(key), EVP_sha256(), 0);
328 HMAC_Update(ctx, (
const unsigned char *) fn,
331 HMAC_Update(ctx, (
const unsigned char *) &request,
335 HMAC_Update(ctx, (
const unsigned char *) secent->
name,
336 strlen(secent->
name) + 1);
339 HMAC_Update(ctx, (
const unsigned char *) secent->
vorg,
340 strlen(secent->
vorg) + 1);
343 HMAC_Update(ctx, (
const unsigned char *) secent->
host,
344 strlen(secent->
host) + 1);
347 HMAC_Update(ctx, (
const unsigned char *) secent->
moninfo,
350 localtime_r(&tim, &tms);
351 strftime(buf,
sizeof (buf),
"%s", &tms);
352 HMAC_Update(ctx, (
const unsigned char *) buf,
355 HMAC_Final(ctx, mdbuf, &len);
void Tobase64(const unsigned char *input, int length, char *out)
static void HMAC_CTX_free(HMAC_CTX *ctx)
static HMAC_CTX * HMAC_CTX_new()
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 HMAC_CTX_free(), HMAC_CTX_new(), 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 |
c | ) |
|
|
static |
◆ compareHash()
| int compareHash |
( |
const char * |
h1, |
|
|
const char * |
h2 |
|
) |
| |
◆ escapeXML()
| char* escapeXML |
( |
const char * |
str | ) |
|
Definition at line 470 of file XrdHttpUtils.cc.
472 char *r = (
char *) malloc(l*6 + 1);
476 for (i = 0; i < l; i++) {
481 strcpy(r + j,
""");
485 strcpy(r + j,
"&");
489 strcpy(r + j,
"<");
493 strcpy(r + j,
">");
497 strcpy(r + j,
"'");
Referenced by XrdHttpReq::Error().
◆ Fromhexdigest()
| bool Fromhexdigest |
( |
const unsigned char * |
input, |
|
|
int |
length, |
|
|
unsigned char * |
out |
|
) |
| |
Definition at line 169 of file XrdHttpUtils.cc.
170 for (
int idx=0; idx < length; idx += 2) {
173 if ((upper < 0) || (lower < 0)) {
176 out[idx/2] = (upper << 4) + lower;
static int char_to_int(int c)
References char_to_int().
◆ HMAC_CTX_free()
| static void HMAC_CTX_free |
( |
HMAC_CTX * |
ctx | ) |
|
|
static |
◆ HMAC_CTX_new()
| static HMAC_CTX* HMAC_CTX_new |
( |
| ) |
|
|
static |
Definition at line 59 of file XrdHttpUtils.cc.
60 HMAC_CTX *ctx = (HMAC_CTX *)OPENSSL_malloc(
sizeof(HMAC_CTX));
61 if (ctx) HMAC_CTX_init(ctx);
Referenced by calcHashes().
◆ httpStatusToString()
| std::string httpStatusToString |
( |
int |
status | ) |
|
Definition at line 591 of file XrdHttpUtils.cc.
594 case 100:
return "Continue";
595 case 101:
return "Switching Protocols";
596 case 102:
return "Processing";
597 case 103:
return "Early Hints";
600 case 200:
return "OK";
601 case 201:
return "Created";
602 case 202:
return "Accepted";
603 case 203:
return "Non-Authoritative Information";
604 case 204:
return "No Content";
605 case 205:
return "Reset Content";
606 case 206:
return "Partial Content";
607 case 207:
return "Multi-Status";
608 case 208:
return "Already Reported";
609 case 226:
return "IM Used";
612 case 300:
return "Multiple Choices";
613 case 301:
return "Moved Permanently";
614 case 302:
return "Found";
615 case 303:
return "See Other";
616 case 304:
return "Not Modified";
617 case 305:
return "Use Proxy";
618 case 307:
return "Temporary Redirect";
619 case 308:
return "Permanent Redirect";
622 case 400:
return "Bad Request";
623 case 401:
return "Unauthorized";
624 case 402:
return "Payment Required";
625 case 403:
return "Forbidden";
626 case 404:
return "Not Found";
627 case 405:
return "Method Not Allowed";
628 case 406:
return "Not Acceptable";
629 case 407:
return "Proxy Authentication Required";
630 case 408:
return "Request Timeout";
631 case 409:
return "Conflict";
632 case 410:
return "Gone";
633 case 411:
return "Length Required";
634 case 412:
return "Precondition Failed";
635 case 413:
return "Payload Too Large";
636 case 414:
return "URI Too Long";
637 case 415:
return "Unsupported Media Type";
638 case 416:
return "Range Not Satisfiable";
639 case 417:
return "Expectation Failed";
640 case 418:
return "I'm a teapot";
641 case 421:
return "Misdirected Request";
642 case 422:
return "Unprocessable Entity";
643 case 423:
return "Locked";
644 case 424:
return "Failed Dependency";
645 case 425:
return "Too Early";
646 case 426:
return "Upgrade Required";
647 case 428:
return "Precondition Required";
648 case 429:
return "Too Many Requests";
649 case 431:
return "Request Header Fields Too Large";
650 case 451:
return "Unavailable For Legal Reasons";
653 case 500:
return "Internal Server Error";
654 case 501:
return "Not Implemented";
655 case 502:
return "Bad Gateway";
656 case 503:
return "Service Unavailable";
657 case 504:
return "Gateway Timeout";
658 case 505:
return "HTTP Version Not Supported";
659 case 506:
return "Variant Also Negotiates";
660 case 507:
return "Insufficient Storage";
661 case 508:
return "Loop Detected";
662 case 510:
return "Not Extended";
663 case 511:
return "Network Authentication Required";
667 case 100 ... 199:
return "Informational";
668 case 200 ... 299:
return "Success";
669 case 300 ... 399:
return "Redirection";
670 case 400 ... 499:
return "Client Error";
671 case 500 ... 599:
return "Server Error";
672 default:
return "Unknown";
◆ itos()
| std::string itos |
( |
long |
i | ) |
|
◆ mapErrNoToHttp()
| int mapErrNoToHttp |
( |
int |
errNo | ) |
|
Definition at line 518 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 193 of file XrdHttpUtils.cc.
194 char *ptr = strchr((
char *)s, c);
197 return strchr((
char *)s,
'\0');
◆ parseURL()
| int parseURL |
( |
char * |
url, |
|
|
char * |
host, |
|
|
int & |
port, |
|
|
char ** |
path |
|
) |
| |
Definition at line 77 of file XrdHttpUtils.cc.
83 char *p = strstr(url,
"//");
90 char *p2 = strchr(p,
'/');
96 int l = std::min((
int)(p2 - p), (
int)
sizeof (buf) - 1);
101 p = strchr(buf,
':');
103 int l = std::min((
int)(p - buf), (
int)
sizeof (buf) - 1);
104 strncpy(host, buf, l);
◆ quote()
| char* quote |
( |
const char * |
str | ) |
|
Definition at line 411 of file XrdHttpUtils.cc.
413 char *r = (
char *) malloc(l*3 + 1);
417 for (i = 0; i < l; i++) {
422 strcpy(r + j,
"%20");
426 strcpy(r + j,
"%5B");
430 strcpy(r + j,
"%5D");
434 strcpy(r + j,
"%3A");
442 strcpy(r + j,
"%23");
446 strcpy(r + j,
"%0A");
450 strcpy(r + j,
"%0D");
454 strcpy(r + j,
"%3D");
Referenced by encode_raw().
◆ Tobase64()
| void Tobase64 |
( |
const unsigned char * |
input, |
|
|
int |
length, |
|
|
char * |
out |
|
) |
| |
Definition at line 121 of file XrdHttpUtils.cc.
129 b64 = BIO_new(BIO_f_base64());
131 bmem = BIO_new(BIO_s_mem());
133 BIO_write(b64, input, length);
135 if (BIO_flush(b64) <= 0) {
140 BIO_get_mem_ptr(b64, &bptr);
143 memcpy(out, bptr->data, bptr->length);
144 out[bptr->length] =
'\0';
void BIO_set_flags(BIO *bio, int flags)
References BIO_set_flags().
Referenced by calcHashes().
◆ unquote()
| char* unquote |
( |
char * |
str | ) |
|
Definition at line 379 of file XrdHttpUtils.cc.
381 char *r = (
char *) malloc(l + 1);
385 for (i = 0; i < l; i++) {
391 char savec = str[i + 3];
394 r[j] = strtol(str + i + 1, 0, 16);
398 }
else r[j] = str[i];
Referenced by decode_raw().