XRootD
Loading...
Searching...
No Matches
XrdHttpUtils.hh File Reference

Utility functions for XrdHTTP. More...

#include "XProtocol/XPtypes.hh"
#include "XProtocol/XProtocol.hh"
#include "XrdSec/XrdSecEntity.hh"
#include "XrdOuc/XrdOucIOVec.hh"
#include "XrdOuc/XrdOucTUtils.hh"
#include <string>
#include <cstring>
#include <vector>
#include <memory>
#include <sstream>
#include <cstdint>
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

Enumerations

enum  : int {
  HTTP_CONTINUE = 100 ,
  HTTP_SWITCHING_PROTOCOLS = 101 ,
  HTTP_PROCESSING = 102 ,
  HTTP_EARLY_HINTS = 103 ,
  HTTP_OK = 200 ,
  HTTP_CREATED = 201 ,
  HTTP_ACCEPTED = 202 ,
  HTTP_NON_AUTHORITATIVE_INFORMATION = 203 ,
  HTTP_NO_CONTENT = 204 ,
  HTTP_RESET_CONTENT = 205 ,
  HTTP_PARTIAL_CONTENT = 206 ,
  HTTP_MULTI_STATUS = 207 ,
  HTTP_ALREADY_REPORTED = 208 ,
  HTTP_IM_USED = 226 ,
  HTTP_MULTIPLE_CHOICES = 300 ,
  HTTP_MOVED_PERMANENTLY = 301 ,
  HTTP_FOUND = 302 ,
  HTTP_SEE_OTHER = 303 ,
  HTTP_NOT_MODIFIED = 304 ,
  HTTP_USE_PROXY = 305 ,
  HTTP_TEMPORARY_REDIRECT = 307 ,
  HTTP_PERMANENT_REDIRECT = 308 ,
  HTTP_BAD_REQUEST = 400 ,
  HTTP_UNAUTHORIZED = 401 ,
  HTTP_PAYMENT_REQUIRED = 402 ,
  HTTP_FORBIDDEN = 403 ,
  HTTP_NOT_FOUND = 404 ,
  HTTP_METHOD_NOT_ALLOWED = 405 ,
  HTTP_NOT_ACCEPTABLE = 406 ,
  HTTP_PROXY_AUTHENTICATION_REQUIRED = 407 ,
  HTTP_REQUEST_TIMEOUT = 408 ,
  HTTP_CONFLICT = 409 ,
  HTTP_GONE = 410 ,
  HTTP_LENGTH_REQUIRED = 411 ,
  HTTP_PRECONDITION_FAILED = 412 ,
  HTTP_PAYLOAD_TOO_LARGE = 413 ,
  HTTP_URI_TOO_LONG = 414 ,
  HTTP_UNSUPPORTED_MEDIA_TYPE = 415 ,
  HTTP_RANGE_NOT_SATISFIABLE = 416 ,
  HTTP_EXPECTATION_FAILED = 417 ,
  HTTP_IM_A_TEAPOT = 418 ,
  HTTP_MISDIRECTED_REQUEST = 421 ,
  HTTP_UNPROCESSABLE_ENTITY = 422 ,
  HTTP_LOCKED = 423 ,
  HTTP_FAILED_DEPENDENCY = 424 ,
  HTTP_TOO_EARLY = 425 ,
  HTTP_UPGRADE_REQUIRED = 426 ,
  HTTP_PRECONDITION_REQUIRED = 428 ,
  HTTP_TOO_MANY_REQUESTS = 429 ,
  HTTP_REQUEST_HEADER_FIELDS_TOO_LARGE = 431 ,
  HTTP_UNAVAILABLE_FOR_LEGAL_REASONS = 451 ,
  HTTP_INTERNAL_SERVER_ERROR = 500 ,
  HTTP_NOT_IMPLEMENTED = 501 ,
  HTTP_BAD_GATEWAY = 502 ,
  HTTP_SERVICE_UNAVAILABLE = 503 ,
  HTTP_GATEWAY_TIMEOUT = 504 ,
  HTTP_HTTP_VERSION_NOT_SUPPORTED = 505 ,
  HTTP_VARIANT_ALSO_NEGOTIATES = 506 ,
  HTTP_INSUFFICIENT_STORAGE = 507 ,
  HTTP_LOOP_DETECTED = 508 ,
  HTTP_NOT_EXTENDED = 510 ,
  HTTP_NETWORK_AUTHENTICATION_REQUIRED = 511
}

Functions

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 &hexOutput)
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 std::string &hex, std::vector< uint8_t > &outputBytes)
std::string httpStatusToString (int status)
std::string itos (long i)
int mapErrNoToHttp (int err)
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)

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 255 of file XrdHttpUtils.hh.

Enumeration Type Documentation

◆ anonymous enum

anonymous enum : int
Enumerator
HTTP_CONTINUE 
HTTP_SWITCHING_PROTOCOLS 
HTTP_PROCESSING 
HTTP_EARLY_HINTS 
HTTP_OK 
HTTP_CREATED 
HTTP_ACCEPTED 
HTTP_NON_AUTHORITATIVE_INFORMATION 
HTTP_NO_CONTENT 
HTTP_RESET_CONTENT 
HTTP_PARTIAL_CONTENT 
HTTP_MULTI_STATUS 
HTTP_ALREADY_REPORTED 
HTTP_IM_USED 
HTTP_MULTIPLE_CHOICES 
HTTP_MOVED_PERMANENTLY 
HTTP_FOUND 
HTTP_SEE_OTHER 
HTTP_NOT_MODIFIED 
HTTP_USE_PROXY 
HTTP_TEMPORARY_REDIRECT 
HTTP_PERMANENT_REDIRECT 
HTTP_BAD_REQUEST 
HTTP_UNAUTHORIZED 
HTTP_PAYMENT_REQUIRED 
HTTP_FORBIDDEN 
HTTP_NOT_FOUND 
HTTP_METHOD_NOT_ALLOWED 
HTTP_NOT_ACCEPTABLE 
HTTP_PROXY_AUTHENTICATION_REQUIRED 
HTTP_REQUEST_TIMEOUT 
HTTP_CONFLICT 
HTTP_GONE 
HTTP_LENGTH_REQUIRED 
HTTP_PRECONDITION_FAILED 
HTTP_PAYLOAD_TOO_LARGE 
HTTP_URI_TOO_LONG 
HTTP_UNSUPPORTED_MEDIA_TYPE 
HTTP_RANGE_NOT_SATISFIABLE 
HTTP_EXPECTATION_FAILED 
HTTP_IM_A_TEAPOT 
HTTP_MISDIRECTED_REQUEST 
HTTP_UNPROCESSABLE_ENTITY 
HTTP_LOCKED 
HTTP_FAILED_DEPENDENCY 
HTTP_TOO_EARLY 
HTTP_UPGRADE_REQUIRED 
HTTP_PRECONDITION_REQUIRED 
HTTP_TOO_MANY_REQUESTS 
HTTP_REQUEST_HEADER_FIELDS_TOO_LARGE 
HTTP_UNAVAILABLE_FOR_LEGAL_REASONS 
HTTP_INTERNAL_SERVER_ERROR 
HTTP_NOT_IMPLEMENTED 
HTTP_BAD_GATEWAY 
HTTP_SERVICE_UNAVAILABLE 
HTTP_GATEWAY_TIMEOUT 
HTTP_HTTP_VERSION_NOT_SUPPORTED 
HTTP_VARIANT_ALSO_NEGOTIATES 
HTTP_INSUFFICIENT_STORAGE 
HTTP_LOOP_DETECTED 
HTTP_NOT_EXTENDED 
HTTP_NETWORK_AUTHENTICATION_REQUIRED 

Definition at line 52 of file XrdHttpUtils.hh.

52 : int {
53 HTTP_CONTINUE = 100,
55 HTTP_PROCESSING = 102,
56 HTTP_EARLY_HINTS = 103,
57
58 // 2xx Success
59 HTTP_OK = 200,
60 HTTP_CREATED = 201,
61 HTTP_ACCEPTED = 202,
63 HTTP_NO_CONTENT = 204,
68 HTTP_IM_USED = 226,
69
70 // 3xx Redirection
73 HTTP_FOUND = 302,
74 HTTP_SEE_OTHER = 303,
76 HTTP_USE_PROXY = 305,
79
80 // 4xx Client Errors
81 HTTP_BAD_REQUEST = 400,
84 HTTP_FORBIDDEN = 403,
85 HTTP_NOT_FOUND = 404,
90 HTTP_CONFLICT = 409,
91 HTTP_GONE = 410,
99 HTTP_IM_A_TEAPOT = 418, // RFC 2324
102 HTTP_LOCKED = 423,
104 HTTP_TOO_EARLY = 425,
110
111 // 5xx Server Errors
114 HTTP_BAD_GATEWAY = 502,
120 HTTP_LOOP_DETECTED = 508,
121 HTTP_NOT_EXTENDED = 510,
123};
@ HTTP_HTTP_VERSION_NOT_SUPPORTED
@ HTTP_INSUFFICIENT_STORAGE
@ HTTP_PRECONDITION_FAILED
@ HTTP_BAD_REQUEST
@ HTTP_NOT_EXTENDED
@ HTTP_FAILED_DEPENDENCY
@ HTTP_VARIANT_ALSO_NEGOTIATES
@ HTTP_CREATED
@ HTTP_IM_A_TEAPOT
@ HTTP_MULTIPLE_CHOICES
@ HTTP_LOCKED
@ HTTP_OK
@ HTTP_NOT_MODIFIED
@ HTTP_NON_AUTHORITATIVE_INFORMATION
@ HTTP_LOOP_DETECTED
@ HTTP_EXPECTATION_FAILED
@ HTTP_SERVICE_UNAVAILABLE
@ HTTP_PROXY_AUTHENTICATION_REQUIRED
@ HTTP_LENGTH_REQUIRED
@ HTTP_URI_TOO_LONG
@ HTTP_UNAVAILABLE_FOR_LEGAL_REASONS
@ HTTP_UNAUTHORIZED
@ HTTP_UNSUPPORTED_MEDIA_TYPE
@ HTTP_NOT_FOUND
@ HTTP_FORBIDDEN
@ HTTP_CONTINUE
@ HTTP_MULTI_STATUS
@ HTTP_PERMANENT_REDIRECT
@ HTTP_FOUND
@ HTTP_RESET_CONTENT
@ HTTP_TEMPORARY_REDIRECT
@ HTTP_NO_CONTENT
@ HTTP_MISDIRECTED_REQUEST
@ HTTP_PARTIAL_CONTENT
@ HTTP_BAD_GATEWAY
@ HTTP_GATEWAY_TIMEOUT
@ HTTP_NETWORK_AUTHENTICATION_REQUIRED
@ HTTP_TOO_EARLY
@ HTTP_METHOD_NOT_ALLOWED
@ HTTP_NOT_ACCEPTABLE
@ HTTP_ALREADY_REPORTED
@ HTTP_EARLY_HINTS
@ HTTP_GONE
@ HTTP_INTERNAL_SERVER_ERROR
@ HTTP_UPGRADE_REQUIRED
@ HTTP_IM_USED
@ HTTP_TOO_MANY_REQUESTS
@ HTTP_PAYLOAD_TOO_LARGE
@ HTTP_NOT_IMPLEMENTED
@ HTTP_SEE_OTHER
@ HTTP_PAYMENT_REQUIRED
@ HTTP_REQUEST_TIMEOUT
@ HTTP_UNPROCESSABLE_ENTITY
@ HTTP_REQUEST_HEADER_FIELDS_TOO_LARGE
@ HTTP_SWITCHING_PROTOCOLS
@ HTTP_MOVED_PERMANENTLY
@ HTTP_USE_PROXY
@ HTTP_PRECONDITION_REQUIRED
@ HTTP_RANGE_NOT_SATISFIABLE
@ HTTP_ACCEPTED
@ HTTP_CONFLICT
@ HTTP_PROCESSING

Function Documentation

◆ base64DecodeHex()

void base64DecodeHex ( const std::string & base64,
std::string & hexOutput )

Definition at line 155 of file XrdHttpUtils.cc.

155 {
156 std::vector<uint8_t> bytes;
157 base64ToBytes(base64,bytes);
158 bytesToHex(bytes, hexOutput);
159}
void base64ToBytes(const std::string &base64digest, std::vector< uint8_t > &outputBytes)
void bytesToHex(const std::vector< uint8_t > &bytes, std::string &output)

References base64ToBytes(), and bytesToHex().

Referenced by XrdHttpHeaderUtils::parseReprDigest().

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

◆ base64ToBytes()

void base64ToBytes ( const std::string & base64digest,
std::vector< uint8_t > & outputBytes )

Definition at line 118 of file XrdHttpUtils.cc.

118 {
119 outputBytes.clear();
120
121 if (base64digest.empty()) {
122 return;
123 }
124
125 BIO *b64 = BIO_new(BIO_f_base64());
126 BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL); // match your encoder
127
128 BIO *bmem = BIO_new_mem_buf(base64digest.data(), static_cast<int>(base64digest.size()));
129 bmem = BIO_push(b64, bmem);
130
131 // Estimate maximum size (base64 expands data by ~33%)
132 std::vector<uint8_t> buffer(base64digest.size());
133
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);
138 } else {
139 outputBytes.clear(); // decoding failed
140 }
141
142 BIO_free_all(bmem);
143}

Referenced by base64DecodeHex().

Here is the caller graph for this function:

◆ bytesToHex()

void bytesToHex ( const std::vector< uint8_t > & bytes,
std::string & hexOutput )

Definition at line 145 of file XrdHttpUtils.cc.

145 {
146 static const char* lut = "0123456789abcdef";
147 output.clear();
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]);
152 }
153}

Referenced by base64DecodeHex().

Here is the caller graph for this function:

◆ calcHashes()

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

Definition at line 231 of file XrdHttpUtils.cc.

242 {
243
244
245#if OPENSSL_VERSION_NUMBER >= 0x30000000L
246 EVP_MAC *mac;
247 EVP_MAC_CTX *ctx;
248 size_t len;
249#else
250 HMAC_CTX *ctx;
251 unsigned int len;
252#endif
253 unsigned char mdbuf[EVP_MAX_MD_SIZE];
254 char buf[64];
255 struct tm tms;
256
257
258 if (!hash) {
259 return;
260 }
261 hash[0] = '\0';
262
263 if (!key) {
264 return;
265 }
266
267 if (!fn || !secent) {
268 return;
269 }
270
271#if OPENSSL_VERSION_NUMBER >= 0x30000000L
272
273 if (!(mac = EVP_MAC_fetch(nullptr, "HMAC", nullptr))) {
274 return;
275 }
276
277 if (!(ctx = EVP_MAC_CTX_new(mac))) {
278 EVP_MAC_free(mac);
279 return;
280 }
281
282 OSSL_PARAM params[2] = {
283 OSSL_PARAM_construct_utf8_string("digest", (char*)"SHA256", 0),
284 OSSL_PARAM_construct_end()
285 };
286
287 if (!EVP_MAC_init(ctx, (const unsigned char *) key, strlen(key), params)) {
288 EVP_MAC_CTX_free(ctx);
289 EVP_MAC_free(mac);
290 return;
291 }
292
293 if (fn)
294 EVP_MAC_update(ctx, (const unsigned char *) fn,
295 strlen(fn) + 1);
296
297 EVP_MAC_update(ctx, (const unsigned char *) &request,
298 sizeof (request));
299
300 if (secent->name)
301 EVP_MAC_update(ctx, (const unsigned char *) secent->name,
302 strlen(secent->name) + 1);
303
304 if (secent->vorg)
305 EVP_MAC_update(ctx, (const unsigned char *) secent->vorg,
306 strlen(secent->vorg) + 1);
307
308 if (secent->host)
309 EVP_MAC_update(ctx, (const unsigned char *) secent->host,
310 strlen(secent->host) + 1);
311
312 if (secent->moninfo)
313 EVP_MAC_update(ctx, (const unsigned char *) secent->moninfo,
314 strlen(secent->moninfo) + 1);
315
316 localtime_r(&tim, &tms);
317 strftime(buf, sizeof (buf), "%s", &tms);
318 EVP_MAC_update(ctx, (const unsigned char *) buf,
319 strlen(buf) + 1);
320
321 EVP_MAC_final(ctx, mdbuf, &len, EVP_MAX_MD_SIZE);
322
323 EVP_MAC_CTX_free(ctx);
324 EVP_MAC_free(mac);
325
326#else
327
328 ctx = HMAC_CTX_new();
329
330 if (!ctx) {
331 return;
332 }
333
334
335
336 HMAC_Init_ex(ctx, (const void *) key, strlen(key), EVP_sha256(), 0);
337
338
339 if (fn)
340 HMAC_Update(ctx, (const unsigned char *) fn,
341 strlen(fn) + 1);
342
343 HMAC_Update(ctx, (const unsigned char *) &request,
344 sizeof (request));
345
346 if (secent->name)
347 HMAC_Update(ctx, (const unsigned char *) secent->name,
348 strlen(secent->name) + 1);
349
350 if (secent->vorg)
351 HMAC_Update(ctx, (const unsigned char *) secent->vorg,
352 strlen(secent->vorg) + 1);
353
354 if (secent->host)
355 HMAC_Update(ctx, (const unsigned char *) secent->host,
356 strlen(secent->host) + 1);
357
358 if (secent->moninfo)
359 HMAC_Update(ctx, (const unsigned char *) secent->moninfo,
360 strlen(secent->moninfo) + 1);
361
362 localtime_r(&tim, &tms);
363 strftime(buf, sizeof (buf), "%s", &tms);
364 HMAC_Update(ctx, (const unsigned char *) buf,
365 strlen(buf) + 1);
366
367 HMAC_Final(ctx, mdbuf, &len);
368
369 HMAC_CTX_free(ctx);
370
371#endif
372
373 Tobase64(mdbuf, len / 2, hash);
374}
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().

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 376 of file XrdHttpUtils.cc.

378 {
379
380 if (h1 == h2) return 0;
381
382 if (!h1 || !h2)
383 return 1;
384
385 return strcmp(h1, h2);
386
387}

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 175 of file XrdHttpUtils.hh.

175 {
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}
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 213 of file XrdHttpUtils.hh.

213 {
214 char * decodedRaw = decode_raw(str);
215 std::string decoded { decodedRaw };
216 free(decodedRaw);
217 return decoded;
218}
char * decode_raw(const std::string &str)

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 226 of file XrdHttpUtils.hh.

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

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 190 of file XrdHttpUtils.hh.

190 {
191 return quote(str.c_str());
192}
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 200 of file XrdHttpUtils.hh.

200 {
201 char * encodedRaw = encode_raw(str);
202 std::string encoded { encodedRaw };
203 free(encodedRaw);
204 return encoded;
205}
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 482 of file XrdHttpUtils.cc.

482 {
483 int l = strlen(str);
484 char *r = (char *) malloc(l*6 + 1);
485 r[0] = '\0';
486 int i, j = 0;
487
488 for (i = 0; i < l; i++) {
489 char c = str[i];
490
491 switch (c) {
492 case '"':
493 strcpy(r + j, "&quot;");
494 j += 6;
495 break;
496 case '&':
497 strcpy(r + j, "&amp;");
498 j += 5;
499 break;
500 case '<':
501 strcpy(r + j, "&lt;");
502 j += 4;
503 break;
504 case '>':
505 strcpy(r + j, "&gt;");
506 j += 4;
507 break;
508 case '\'':
509 strcpy(r + j, "&apos;");
510 j += 6;
511 break;
512
513 default:
514 r[j++] = c;
515 }
516 }
517
518 r[j] = '\0';
519
520 return r;
521}

Referenced by XrdHttpReq::Error().

Here is the caller graph for this function:

◆ Fromhexdigest()

bool Fromhexdigest ( const std::string & hex,
std::vector< uint8_t > & outputBytes )

Definition at line 177 of file XrdHttpUtils.cc.

177 {
178 if(hex.size() % 2 != 0) {
179 return false;
180 }
181
182 outputBytes.reserve(hex.size() / 2);
183
184 for(size_t i = 0; i < hex.size(); i += 2) {
185 int upper = char_to_int(hex[i]);
186 int lower = char_to_int(hex[i + 1]);
187 if (upper < 0 || lower < 0) return false;
188 outputBytes.push_back(static_cast<uint8_t>((upper << 4) + lower));
189 }
190 return true;
191}
static int char_to_int(int ch)

References char_to_int().

Here is the call graph for this function:

◆ httpStatusToString()

std::string httpStatusToString ( int status)

Definition at line 603 of file XrdHttpUtils.cc.

603 {
604 switch (status) {
605 // 1xx Informational
606 case 100: return "Continue";
607 case 101: return "Switching Protocols";
608 case 102: return "Processing";
609 case 103: return "Early Hints";
610
611 // 2xx Success
612 case 200: return "OK";
613 case 201: return "Created";
614 case 202: return "Accepted";
615 case 203: return "Non-Authoritative Information";
616 case 204: return "No Content";
617 case 205: return "Reset Content";
618 case 206: return "Partial Content";
619 case 207: return "Multi-Status";
620 case 208: return "Already Reported";
621 case 226: return "IM Used";
622
623 // 3xx Redirection
624 case 300: return "Multiple Choices";
625 case 301: return "Moved Permanently";
626 case 302: return "Found";
627 case 303: return "See Other";
628 case 304: return "Not Modified";
629 case 305: return "Use Proxy";
630 case 307: return "Temporary Redirect";
631 case 308: return "Permanent Redirect";
632
633 // 4xx Client Errors
634 case 400: return "Bad Request";
635 case 401: return "Unauthorized";
636 case 402: return "Payment Required";
637 case 403: return "Forbidden";
638 case 404: return "Not Found";
639 case 405: return "Method Not Allowed";
640 case 406: return "Not Acceptable";
641 case 407: return "Proxy Authentication Required";
642 case 408: return "Request Timeout";
643 case 409: return "Conflict";
644 case 410: return "Gone";
645 case 411: return "Length Required";
646 case 412: return "Precondition Failed";
647 case 413: return "Payload Too Large";
648 case 414: return "URI Too Long";
649 case 415: return "Unsupported Media Type";
650 case 416: return "Range Not Satisfiable";
651 case 417: return "Expectation Failed";
652 case 418: return "I'm a teapot";
653 case 421: return "Misdirected Request";
654 case 422: return "Unprocessable Entity";
655 case 423: return "Locked";
656 case 424: return "Failed Dependency";
657 case 425: return "Too Early";
658 case 426: return "Upgrade Required";
659 case 428: return "Precondition Required";
660 case 429: return "Too Many Requests";
661 case 431: return "Request Header Fields Too Large";
662 case 451: return "Unavailable For Legal Reasons";
663
664 // 5xx Server Errors
665 case 500: return "Internal Server Error";
666 case 501: return "Not Implemented";
667 case 502: return "Bad Gateway";
668 case 503: return "Service Unavailable";
669 case 504: return "Gateway Timeout";
670 case 505: return "HTTP Version Not Supported";
671 case 506: return "Variant Also Negotiates";
672 case 507: return "Insufficient Storage";
673 case 508: return "Loop Detected";
674 case 510: return "Not Extended";
675 case 511: return "Network Authentication Required";
676
677 default:
678 switch (status) {
679 case 100 ... 199: return "Informational";
680 case 200 ... 299: return "Success";
681 case 300 ... 399: return "Redirection";
682 case 400 ... 499: return "Client Error";
683 case 500 ... 599: return "Server Error";
684 default: return "Unknown";
685 }
686 }
687}

◆ itos()

std::string itos ( long i)

Definition at line 195 of file XrdHttpUtils.cc.

195 {
196 char buf[128];
197 sprintf(buf, "%ld", i);
198
199 return buf;
200}

◆ mapErrNoToHttp()

int mapErrNoToHttp ( int err)

Definition at line 530 of file XrdHttpUtils.cc.

530 {
531
532 switch (errNo) {
533
534 case EACCES:
535 case EROFS:
536 case EPERM:
537 return HTTP_FORBIDDEN;
538
539 case EAUTH:
540 return HTTP_UNAUTHORIZED;
541
542 case ENOENT:
543 return HTTP_NOT_FOUND;
544
545 case EEXIST:
546 case EISDIR:
547 case ENOTDIR:
548 case ENOTEMPTY:
549 return HTTP_CONFLICT;
550
551 case EXDEV:
553
554 case ENAMETOOLONG:
555 return HTTP_URI_TOO_LONG;
556
557 case ELOOP:
558 return HTTP_LOOP_DETECTED;
559
560 case ENOSPC:
561 case EDQUOT:
563
564 case EFBIG:
566
567 case EINVAL:
568 case EBADF:
569 case EFAULT:
570 case ENXIO:
571 case ESPIPE:
572 case EOVERFLOW:
573 return HTTP_BAD_REQUEST;
574
575 case ENOTSUP: // EOPNOTSUPP
577
578 case EBUSY:
579 case EAGAIN:
580 case EINTR:
581 case ENOMEM:
582 case EMFILE:
583 case ENFILE:
584 case ETXTBSY:
586
587 case ETIMEDOUT:
589
590 case ECONNREFUSED:
591 case ECONNRESET:
592 case ENETDOWN:
593 case ENETUNREACH:
594 case EHOSTUNREACH:
595 case EPIPE:
596 return HTTP_BAD_GATEWAY;
597
598 default:
600 }
601}
#define EAUTH

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().

Here is the caller graph for this function:

◆ mapXrdErrToHttp()

int mapXrdErrToHttp ( XErrorCode xrdError)

Definition at line 523 of file XrdHttpUtils.cc.

523 {
524
525 int errNo = XProtocol::toErrno(xrdError);
526 return mapErrNoToHttp(errNo);
527
528}
int mapErrNoToHttp(int errNo)
static int toErrno(int xerr)

References mapErrNoToHttp(), and XProtocol::toErrno().

Here is the call graph for this function:

◆ mystrchrnul()

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

Definition at line 205 of file XrdHttpUtils.cc.

205 {
206 char *ptr = strchr((char *)s, c);
207
208 if (!ptr)
209 return strchr((char *)s, '\0');
210
211 return ptr;
212}

◆ quote()

char * quote ( const char * str)

Definition at line 423 of file XrdHttpUtils.cc.

423 {
424 int l = strlen(str);
425 char *r = (char *) malloc(l*3 + 1);
426 r[0] = '\0';
427 int i, j = 0;
428
429 for (i = 0; i < l; i++) {
430 char c = str[i];
431
432 switch (c) {
433 case ' ':
434 strcpy(r + j, "%20");
435 j += 3;
436 break;
437 case '[':
438 strcpy(r + j, "%5B");
439 j += 3;
440 break;
441 case ']':
442 strcpy(r + j, "%5D");
443 j += 3;
444 break;
445 case ':':
446 strcpy(r + j, "%3A");
447 j += 3;
448 break;
449 // case '/':
450 // strcpy(r + j, "%2F");
451 // j += 3;
452 // break;
453 case '#':
454 strcpy(r + j, "%23");
455 j += 3;
456 break;
457 case '\n':
458 strcpy(r + j, "%0A");
459 j += 3;
460 break;
461 case '\r':
462 strcpy(r + j, "%0D");
463 j += 3;
464 break;
465 case '=':
466 strcpy(r + j, "%3D");
467 j += 3;
468 break;
469 default:
470 r[j++] = c;
471 }
472 }
473
474 r[j] = '\0';
475
476 return r;
477}

Referenced by encode_raw().

Here is the caller graph for this function:

◆ Tobase64() [1/2]

void Tobase64 ( const std::vector< uint8_t > & input,
std::string & base64Output )

Definition at line 90 of file XrdHttpUtils.cc.

90 {
91 BIO *bmem, *b64;
92 BUF_MEM *bptr;
93
94 base64Output.clear();
95
96 if(input.empty()) {
97 return;
98 }
99
100 b64 = BIO_new(BIO_f_base64());
101 BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
102 bmem = BIO_new(BIO_s_mem());
103 BIO_push(b64, bmem);
104 BIO_write(b64, input.data(), input.size());
105
106 if (BIO_flush(b64) <= 0) {
107 BIO_free_all(b64);
108 return;
109 }
110
111 BIO_get_mem_ptr(b64, &bptr);
112
113 base64Output.assign(bptr->data,bptr->length);
114
115 BIO_free_all(b64);
116}

◆ Tobase64() [2/2]

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

Definition at line 60 of file XrdHttpUtils.cc.

60 {
61 BIO *bmem, *b64;
62 BUF_MEM *bptr;
63
64 if (!out) return;
65
66 out[0] = '\0';
67
68 b64 = BIO_new(BIO_f_base64());
69 BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
70 bmem = BIO_new(BIO_s_mem());
71 BIO_push(b64, bmem);
72 BIO_write(b64, input, length);
73
74 if (BIO_flush(b64) <= 0) {
75 BIO_free_all(b64);
76 return;
77 }
78
79 BIO_get_mem_ptr(b64, &bptr);
80
81
82 memcpy(out, bptr->data, bptr->length);
83 out[bptr->length] = '\0';
84
85 BIO_free_all(b64);
86
87 return;
88}

Referenced by calcHashes().

Here is the caller graph for this function:

◆ unquote()

char * unquote ( char * str)

Definition at line 391 of file XrdHttpUtils.cc.

391 {
392 int l = strlen(str);
393 char *r = (char *) malloc(l + 1);
394 r[0] = '\0';
395 int i, j = 0;
396
397 for (i = 0; i < l; i++) {
398 if (str[i] == '%') {
399 if (i + 3 > l) {
400 r[j] = '\0';
401 return r;
402 }
403 char savec = str[i + 3];
404 str[i + 3] = '\0';
405
406 r[j] = strtol(str + i + 1, 0, 16);
407 str[i + 3] = savec;
408
409 i += 2;
410 } else r[j] = str[i];
411
412 j++;
413 }
414
415 r[j] = '\0';
416
417 return r;
418
419}

Referenced by decode_raw().

Here is the caller graph for this function: