Loading...
Searching...
No Matches
XrdHttpUtils.hh
Go to the documentation of this file.
//------------------------------------------------------------------------------
// This file is part of XrdHTTP: A pragmatic implementation of the
// HTTP/WebDAV protocol for the Xrootd framework
//
// Copyright (c) 2013 by European Organization for Nuclear Research (CERN)
// Author: Fabrizio Furano <furano@cern.ch>
// File Date: Apr 2013
//------------------------------------------------------------------------------
// XRootD is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// XRootD is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with XRootD. If not, see <http://www.gnu.org/licenses/>.
//------------------------------------------------------------------------------
#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>
#ifndef XRDHTTPUTILS_HH
#define XRDHTTPUTILS_HH
enum : int {
HTTP_CONTINUE = 100,
HTTP_SWITCHING_PROTOCOLS = 101,
HTTP_PROCESSING = 102,
HTTP_EARLY_HINTS = 103,
// 2xx Success
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,
// 3xx Redirection
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,
// 4xx Client Errors
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, // RFC 2324
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,
// 5xx Server Errors
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,
};
// GetHost from URL
// Parse an URL and extract the host name and port
// Return 0 if OK
int parseURL(char *url, char *host, int &port, char **path);
// Simple itoa function
std::string itos(long i);
// Home made implementation of strchrnul
char *mystrchrnul(const char *s, int c);
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);
bool Fromhexdigest(const unsigned char *input, int length, unsigned char *out);
void Tobase64(const unsigned char *input, int length, char *out);
// Create a new quoted string
char *quote(const char *str);
// unquote a string and return a new one
char *unquote(char *str);
inline char * decode_raw(const std::string & str) {
size_t strLength = str.length();
// uniquely own the temporary copy
std::unique_ptr<char[]> buf(new char[strLength + 1]);
std::memcpy(buf.get(), str.c_str(), strLength + 1);
// unquote returns a fresh malloc()'d pointer
return unquote(buf.get());
}
inline char * encode_raw(const std::string & str) {
return quote(str.c_str());
}
inline std::string encode_str(const std::string & str) {
char * encodedRaw = encode_raw(str);
std::string encoded { encodedRaw };
free(encodedRaw);
return encoded;
}
inline std::string decode_str(const std::string & str) {
char * decodedRaw = decode_raw(str);
std::string decoded { decodedRaw };
free(decodedRaw);
return decoded;
}
inline std::string encode_opaque(const std::string & opaque) {
std::ostringstream output;
std::vector<std::string> allKeyValues;
XrdOucTUtils::splitString(allKeyValues,opaque,"&");
bool first = true;
for(auto & kv: allKeyValues) {
size_t equal = kv.find('=');
if(equal != std::string::npos) {
std::string key = kv.substr(0, equal);
std::string value = kv.substr(equal + 1);
if(!first) {
output << "&";
}
output << key << "=" << encode_str(value);
first = false;
}
}
return output.str();
}
// Escape a string and return a new one
char *escapeXML(const char *str);
int mapXrdErrToHttp(XErrorCode xrdError);
int mapErrNoToHttp(int err);
std::string httpStatusToString(int status);
typedef std::vector<XrdOucIOVec2> XrdHttpIOList;
#endif /* XRDHTTPUTILS_HH */
XProtocol.hh
XErrorCode
XErrorCode
Definition XProtocol.hh:989
XPtypes.hh
kXR_int16
short kXR_int16
Definition XPtypes.hh:66
parseURL
int parseURL(char *url, char *host, int &port, char **path)
Definition XrdHttpUtils.cc:77
itos
std::string itos(long i)
Definition XrdHttpUtils.cc:183
Tobase64
void Tobase64(const unsigned char *input, int length, char *out)
Definition XrdHttpUtils.cc:121
compareHash
int compareHash(const char *h1, const char *h2)
Definition XrdHttpUtils.cc:355
mapErrNoToHttp
int mapErrNoToHttp(int err)
Definition XrdHttpUtils.cc:506
Fromhexdigest
bool Fromhexdigest(const unsigned char *input, int length, unsigned char *out)
Definition XrdHttpUtils.cc:169
unquote
char * unquote(char *str)
Definition XrdHttpUtils.cc:370
HTTP_HTTP_VERSION_NOT_SUPPORTED
@ HTTP_HTTP_VERSION_NOT_SUPPORTED
Definition XrdHttpUtils.hh:117
HTTP_INSUFFICIENT_STORAGE
@ HTTP_INSUFFICIENT_STORAGE
Definition XrdHttpUtils.hh:119
HTTP_PRECONDITION_FAILED
@ HTTP_PRECONDITION_FAILED
Definition XrdHttpUtils.hh:93
HTTP_BAD_REQUEST
@ HTTP_BAD_REQUEST
Definition XrdHttpUtils.hh:81
HTTP_NOT_EXTENDED
@ HTTP_NOT_EXTENDED
Definition XrdHttpUtils.hh:121
HTTP_FAILED_DEPENDENCY
@ HTTP_FAILED_DEPENDENCY
Definition XrdHttpUtils.hh:103
HTTP_VARIANT_ALSO_NEGOTIATES
@ HTTP_VARIANT_ALSO_NEGOTIATES
Definition XrdHttpUtils.hh:118
HTTP_CREATED
@ HTTP_CREATED
Definition XrdHttpUtils.hh:60
HTTP_IM_A_TEAPOT
@ HTTP_IM_A_TEAPOT
Definition XrdHttpUtils.hh:99
HTTP_MULTIPLE_CHOICES
@ HTTP_MULTIPLE_CHOICES
Definition XrdHttpUtils.hh:71
HTTP_LOCKED
@ HTTP_LOCKED
Definition XrdHttpUtils.hh:102
HTTP_OK
@ HTTP_OK
Definition XrdHttpUtils.hh:59
HTTP_NOT_MODIFIED
@ HTTP_NOT_MODIFIED
Definition XrdHttpUtils.hh:75
HTTP_NON_AUTHORITATIVE_INFORMATION
@ HTTP_NON_AUTHORITATIVE_INFORMATION
Definition XrdHttpUtils.hh:62
HTTP_LOOP_DETECTED
@ HTTP_LOOP_DETECTED
Definition XrdHttpUtils.hh:120
HTTP_EXPECTATION_FAILED
@ HTTP_EXPECTATION_FAILED
Definition XrdHttpUtils.hh:98
HTTP_SERVICE_UNAVAILABLE
@ HTTP_SERVICE_UNAVAILABLE
Definition XrdHttpUtils.hh:115
HTTP_PROXY_AUTHENTICATION_REQUIRED
@ HTTP_PROXY_AUTHENTICATION_REQUIRED
Definition XrdHttpUtils.hh:88
HTTP_LENGTH_REQUIRED
@ HTTP_LENGTH_REQUIRED
Definition XrdHttpUtils.hh:92
HTTP_URI_TOO_LONG
@ HTTP_URI_TOO_LONG
Definition XrdHttpUtils.hh:95
HTTP_UNAVAILABLE_FOR_LEGAL_REASONS
@ HTTP_UNAVAILABLE_FOR_LEGAL_REASONS
Definition XrdHttpUtils.hh:109
HTTP_UNAUTHORIZED
@ HTTP_UNAUTHORIZED
Definition XrdHttpUtils.hh:82
HTTP_UNSUPPORTED_MEDIA_TYPE
@ HTTP_UNSUPPORTED_MEDIA_TYPE
Definition XrdHttpUtils.hh:96
HTTP_NOT_FOUND
@ HTTP_NOT_FOUND
Definition XrdHttpUtils.hh:85
HTTP_FORBIDDEN
@ HTTP_FORBIDDEN
Definition XrdHttpUtils.hh:84
HTTP_CONTINUE
@ HTTP_CONTINUE
Definition XrdHttpUtils.hh:53
HTTP_MULTI_STATUS
@ HTTP_MULTI_STATUS
Definition XrdHttpUtils.hh:66
HTTP_PERMANENT_REDIRECT
@ HTTP_PERMANENT_REDIRECT
Definition XrdHttpUtils.hh:78
HTTP_FOUND
@ HTTP_FOUND
Definition XrdHttpUtils.hh:73
HTTP_RESET_CONTENT
@ HTTP_RESET_CONTENT
Definition XrdHttpUtils.hh:64
HTTP_TEMPORARY_REDIRECT
@ HTTP_TEMPORARY_REDIRECT
Definition XrdHttpUtils.hh:77
HTTP_NO_CONTENT
@ HTTP_NO_CONTENT
Definition XrdHttpUtils.hh:63
HTTP_MISDIRECTED_REQUEST
@ HTTP_MISDIRECTED_REQUEST
Definition XrdHttpUtils.hh:100
HTTP_PARTIAL_CONTENT
@ HTTP_PARTIAL_CONTENT
Definition XrdHttpUtils.hh:65
HTTP_BAD_GATEWAY
@ HTTP_BAD_GATEWAY
Definition XrdHttpUtils.hh:114
HTTP_GATEWAY_TIMEOUT
@ HTTP_GATEWAY_TIMEOUT
Definition XrdHttpUtils.hh:116
HTTP_NETWORK_AUTHENTICATION_REQUIRED
@ HTTP_NETWORK_AUTHENTICATION_REQUIRED
Definition XrdHttpUtils.hh:122
HTTP_TOO_EARLY
@ HTTP_TOO_EARLY
Definition XrdHttpUtils.hh:104
HTTP_METHOD_NOT_ALLOWED
@ HTTP_METHOD_NOT_ALLOWED
Definition XrdHttpUtils.hh:86
HTTP_NOT_ACCEPTABLE
@ HTTP_NOT_ACCEPTABLE
Definition XrdHttpUtils.hh:87
HTTP_ALREADY_REPORTED
@ HTTP_ALREADY_REPORTED
Definition XrdHttpUtils.hh:67
HTTP_EARLY_HINTS
@ HTTP_EARLY_HINTS
Definition XrdHttpUtils.hh:56
HTTP_GONE
@ HTTP_GONE
Definition XrdHttpUtils.hh:91
HTTP_INTERNAL_SERVER_ERROR
@ HTTP_INTERNAL_SERVER_ERROR
Definition XrdHttpUtils.hh:112
HTTP_UPGRADE_REQUIRED
@ HTTP_UPGRADE_REQUIRED
Definition XrdHttpUtils.hh:105
HTTP_IM_USED
@ HTTP_IM_USED
Definition XrdHttpUtils.hh:68
HTTP_TOO_MANY_REQUESTS
@ HTTP_TOO_MANY_REQUESTS
Definition XrdHttpUtils.hh:107
HTTP_PAYLOAD_TOO_LARGE
@ HTTP_PAYLOAD_TOO_LARGE
Definition XrdHttpUtils.hh:94
HTTP_NOT_IMPLEMENTED
@ HTTP_NOT_IMPLEMENTED
Definition XrdHttpUtils.hh:113
HTTP_SEE_OTHER
@ HTTP_SEE_OTHER
Definition XrdHttpUtils.hh:74
HTTP_PAYMENT_REQUIRED
@ HTTP_PAYMENT_REQUIRED
Definition XrdHttpUtils.hh:83
HTTP_REQUEST_TIMEOUT
@ HTTP_REQUEST_TIMEOUT
Definition XrdHttpUtils.hh:89
HTTP_UNPROCESSABLE_ENTITY
@ HTTP_UNPROCESSABLE_ENTITY
Definition XrdHttpUtils.hh:101
HTTP_REQUEST_HEADER_FIELDS_TOO_LARGE
@ HTTP_REQUEST_HEADER_FIELDS_TOO_LARGE
Definition XrdHttpUtils.hh:108
HTTP_SWITCHING_PROTOCOLS
@ HTTP_SWITCHING_PROTOCOLS
Definition XrdHttpUtils.hh:54
HTTP_MOVED_PERMANENTLY
@ HTTP_MOVED_PERMANENTLY
Definition XrdHttpUtils.hh:72
HTTP_USE_PROXY
@ HTTP_USE_PROXY
Definition XrdHttpUtils.hh:76
HTTP_PRECONDITION_REQUIRED
@ HTTP_PRECONDITION_REQUIRED
Definition XrdHttpUtils.hh:106
HTTP_RANGE_NOT_SATISFIABLE
@ HTTP_RANGE_NOT_SATISFIABLE
Definition XrdHttpUtils.hh:97
HTTP_ACCEPTED
@ HTTP_ACCEPTED
Definition XrdHttpUtils.hh:61
HTTP_CONFLICT
@ HTTP_CONFLICT
Definition XrdHttpUtils.hh:90
HTTP_PROCESSING
@ HTTP_PROCESSING
Definition XrdHttpUtils.hh:55
encode_opaque
std::string encode_opaque(const std::string &opaque)
Definition XrdHttpUtils.hh:223
mapXrdErrToHttp
int mapXrdErrToHttp(XErrorCode xrdError)
Definition XrdHttpUtils.cc:499
quote
char * quote(const char *str)
Definition XrdHttpUtils.cc:399
encode_raw
char * encode_raw(const std::string &str)
Definition XrdHttpUtils.hh:187
escapeXML
char * escapeXML(const char *str)
Definition XrdHttpUtils.cc:458
calcHashes
void calcHashes(char *hash, const char *fn, kXR_int16 req, XrdSecEntity *secent, time_t tim, const char *key)
Definition XrdHttpUtils.cc:219
encode_str
std::string encode_str(const std::string &str)
Definition XrdHttpUtils.hh:197
XrdHttpIOList
std::vector< XrdOucIOVec2 > XrdHttpIOList
Definition XrdHttpUtils.hh:252
mystrchrnul
char * mystrchrnul(const char *s, int c)
Definition XrdHttpUtils.cc:193
decode_raw
char * decode_raw(const std::string &str)
Definition XrdHttpUtils.hh:172
decode_str
std::string decode_str(const std::string &str)
Definition XrdHttpUtils.hh:210
httpStatusToString
std::string httpStatusToString(int status)
Definition XrdHttpUtils.cc:579
XrdOucIOVec.hh
XrdOucTUtils.hh
XrdSecEntity.hh
XrdOucTUtils::splitString
static void splitString(Container &result, const std::string &input, const std::string &delimiter)
Split a string.
Definition XrdOucTUtils.hh:51
XrdSecEntity
Definition XrdSecEntity.hh:65