XRootD
Loading...
Searching...
No Matches
XrdHttpUtils.hh
Go to the documentation of this file.
1//------------------------------------------------------------------------------
2// This file is part of XrdHTTP: A pragmatic implementation of the
3// HTTP/WebDAV protocol for the Xrootd framework
4//
5// Copyright (c) 2013 by European Organization for Nuclear Research (CERN)
6// Author: Fabrizio Furano <furano@cern.ch>
7// File Date: Apr 2013
8//------------------------------------------------------------------------------
9// XRootD is free software: you can redistribute it and/or modify
10// it under the terms of the GNU Lesser General Public License as published by
11// the Free Software Foundation, either version 3 of the License, or
12// (at your option) any later version.
13//
14// XRootD is distributed in the hope that it will be useful,
15// but WITHOUT ANY WARRANTY; without even the implied warranty of
16// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17// GNU General Public License for more details.
18//
19// You should have received a copy of the GNU Lesser General Public License
20// along with XRootD. If not, see <http://www.gnu.org/licenses/>.
21//------------------------------------------------------------------------------
22
23
24
25
26
27
28
37
38#include "XProtocol/XPtypes.hh"
41#include "XrdOuc/XrdOucIOVec.hh"
43#include <string>
44#include <cstring>
45#include <vector>
46#include <memory>
47#include <sstream>
48
49#ifndef XRDHTTPUTILS_HH
50#define XRDHTTPUTILS_HH
51
52enum : int {
57
58 // 2xx Success
59 HTTP_OK = 200,
69
70 // 3xx Redirection
79
80 // 4xx Client Errors
91 HTTP_GONE = 410,
99 HTTP_IM_A_TEAPOT = 418, // RFC 2324
110
111 // 5xx Server Errors
123};
124
125// GetHost from URL
126// Parse an URL and extract the host name and port
127// Return 0 if OK
128int parseURL(char *url, char *host, int &port, char **path);
129
130// Simple itoa function
131std::string itos(long i);
132
133// Home made implementation of strchrnul
134char *mystrchrnul(const char *s, int c);
135
136void calcHashes(
137 char *hash,
138
139 const char *fn,
140
141 kXR_int16 req,
142
143 XrdSecEntity *secent,
144
145 time_t tim,
146
147 const char *key);
148
149
150int compareHash(
151 const char *h1,
152 const char *h2);
153
154
155bool Fromhexdigest(const unsigned char *input, int length, unsigned char *out);
156
157void Tobase64(const unsigned char *input, int length, char *out);
158
159// Create a new quoted string
160char *quote(const char *str);
161// unquote a string and return a new one
162char *unquote(char *str);
163
172inline char * decode_raw(const std::string & str) {
173 size_t strLength = str.length();
174 // uniquely own the temporary copy
175 std::unique_ptr<char[]> buf(new char[strLength + 1]);
176 std::memcpy(buf.get(), str.c_str(), strLength + 1);
177 // unquote returns a fresh malloc()'d pointer
178 return unquote(buf.get());
179}
180
187inline char * encode_raw(const std::string & str) {
188 return quote(str.c_str());
189}
190
197inline std::string encode_str(const std::string & str) {
198 char * encodedRaw = encode_raw(str);
199 std::string encoded { encodedRaw };
200 free(encodedRaw);
201 return encoded;
202}
203
210inline std::string decode_str(const std::string & str) {
211 char * decodedRaw = decode_raw(str);
212 std::string decoded { decodedRaw };
213 free(decodedRaw);
214 return decoded;
215}
216
223inline std::string encode_opaque(const std::string & opaque) {
224 std::ostringstream output;
225 std::vector<std::string> allKeyValues;
226 XrdOucTUtils::splitString(allKeyValues,opaque,"&");
227 bool first = true;
228 for(auto & kv: allKeyValues) {
229 size_t equal = kv.find('=');
230 if(equal != std::string::npos) {
231 std::string key = kv.substr(0, equal);
232 std::string value = kv.substr(equal + 1);
233 if(!first) {
234 output << "&";
235 }
236 output << key << "=" << encode_str(value);
237 first = false;
238 }
239 }
240 return output.str();
241}
242
243// Escape a string and return a new one
244char *escapeXML(const char *str);
245
246int mapXrdErrToHttp(XErrorCode xrdError);
247
248int mapErrNoToHttp(int err);
249
250std::string httpStatusToString(int status);
251
252typedef std::vector<XrdOucIOVec2> XrdHttpIOList;
253
254
255
256#endif /* XRDHTTPUTILS_HH */
XErrorCode
Definition XProtocol.hh:989
short kXR_int16
Definition XPtypes.hh:66
int parseURL(char *url, char *host, int &port, char **path)
std::string itos(long i)
void Tobase64(const unsigned char *input, int length, char *out)
int compareHash(const char *h1, const char *h2)
int mapErrNoToHttp(int err)
bool Fromhexdigest(const unsigned char *input, int length, unsigned char *out)
char * unquote(char *str)
@ 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
std::string encode_opaque(const std::string &opaque)
int mapXrdErrToHttp(XErrorCode xrdError)
char * quote(const char *str)
char * encode_raw(const std::string &str)
char * escapeXML(const char *str)
void calcHashes(char *hash, const char *fn, kXR_int16 req, XrdSecEntity *secent, time_t tim, const char *key)
std::string encode_str(const std::string &str)
std::vector< XrdOucIOVec2 > XrdHttpIOList
char * mystrchrnul(const char *s, int c)
char * decode_raw(const std::string &str)
std::string decode_str(const std::string &str)
std::string httpStatusToString(int status)
static void splitString(Container &result, const std::string &input, const std::string &delimiter)
Split a string.