xrootd
XrdHttpReq.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: Nov 2012
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 
38 #ifndef XRDHTTPREQ_HH
39 #define XRDHTTPREQ_HH
40 
41 
42 #include "XrdOuc/XrdOucString.hh"
43 
44 #include "XProtocol/XProtocol.hh"
48 
49 #include <vector>
50 #include <string>
51 #include <map>
52 
53 //#include <libxml/parser.h>
54 //#include <libxml/tree.h>
55 
56 
57 
58 
59 struct DirListInfo {
60  std::string path;
61  long long size;
62  long id;
63  long flags;
64  long modtime;
65 };
66 
67 
68 class XrdHttpProtocol;
69 class XrdOucEnv;
70 
72 private:
73  // HTTP response parameters to be sent back to the user
75  std::string httpStatusText;
76 
77  // Whether transfer encoding was requested.
81 
82  // Whether trailer headers were enabled
83  bool m_trailer_headers{false};
84 
85  // Whether the client understands our special status trailer.
86  // The status trailer allows us to report when an IO error occurred
87  // after a response body has started
88  bool m_status_trailer{false};
89 
90  int parseHost(char *);
91 
92  void parseScitag(const std::string & val);
93 
94  //xmlDocPtr xmlbody; /* the resulting document tree */
96 
97  void clientMarshallReadAheadList(int nitems);
98  void clientUnMarshallReadAheadList(int nitems);
99 
100 
101  void getfhandle();
102 
103  // Process the checksum response and return a header that should
104  // be included in the response.
105  int PostProcessChecksum(std::string &digest_header);
106 
112  int PostProcessHTTPReq(bool final = false);
113 
114  // Parse a resource string, typically a filename, setting the resource field and the opaque data
115  void parseResource(char *url);
116  // Map an XRootD error code to an appropriate HTTP status code and message
118 
119  // Sanitize the resource from http[s]://[host]/ questionable prefix
120  void sanitizeResourcePfx();
121 
122  // parses the iovN data pointers elements as either a kXR_read or kXR_readv
123  // response and fills out a XrdHttpIOList with the corresponding length and
124  // buffer pointers. File offsets from kXR_readv responses are not recorded.
125  void getReadResponse(XrdHttpIOList &received);
126 
127  // notifies the range handler of receipt of bytes and sends the client
128  // the data.
129  int sendReadResponseSingleRange(const XrdHttpIOList &received);
130 
131  // notifies the range handler of receipt of bytes and sends the client
132  // the data and necessary headers, assuming multipart/byteranges content type.
133  int sendReadResponsesMultiRanges(const XrdHttpIOList &received);
134 
140  static void extractChecksumFromList(const std::string & checksumList, std::vector<std::string> & extractedChecksum);
141 
148  static void determineXRootDChecksumFromUserDigest(const std::string & userDigest, std::vector<std::string> & xrootdChecksums);
149 
150 public:
152  readRangeHandler(rcfg), keepalive(true) {
153 
154  prot = protinstance;
155  length = 0;
156  //xmlbody = 0;
157  depth = 0;
158  opaque = 0;
159  writtenbytes = 0;
160  fopened = false;
161  headerok = false;
162  mScitag = -1;
163  };
164 
165  virtual ~XrdHttpReq();
166 
167  virtual void reset();
168 
170  int parseLine(char *line, int len);
171 
173  int parseFirstLine(char *line, int len);
174 
176  int parseBody(char *body, long long len);
177 
179  int ReqReadV(const XrdHttpIOList &cl);
180  std::vector<readahead_list> ralist;
181 
183  std::string buildPartialHdr(long long bytestart, long long byteend, long long filesize, char *token);
184 
186  std::string buildPartialHdrEnd(char *token);
187 
188  // Appends the opaque info that we have
189  // NOTE: this function assumes that the strings are unquoted, and will quote them
190  void appendOpaque(XrdOucString &s, XrdSecEntity *secent, char *hash, time_t tnow);
191 
192  void addCgi(const std::string & key, const std::string & value);
193 
194  // ----------------
195  // Description of the request. The header/body parsing
196  // is supposed to populate these fields, for fast access while
197  // processing the request
198 
200 
201  enum ReqType {
202  rtUnset = -1,
215  };
216 
219  std::string requestverb;
220 
221  // We have to keep the headers for possible further processing
222  // by external plugins
223  std::map<std::string, std::string> allheaders;
224 
231 
232 
234  bool headerok;
235 
239 
240  bool keepalive;
241  long long length; // Total size from client for PUT; total length of response TO client for GET.
242  int depth;
244 
246  std::string host;
248  std::string destination;
249 
251  std::string m_req_digest;
252 
255 
261  std::string m_digest_header;
262 
264  std::string hdr2cgistr;
266 
267  //
268  // Area for coordinating request and responses to/from the bridge
269  //
270 
271 
273  unsigned int rwOpDone, rwOpPartialDone;
274 
277 
281  std::string etext;
283 
285  const struct iovec *iovP;
286  int iovN;
287  int iovL;
288  bool final;
289 
290  // The latest stat info got from the xrd layer
291  long long filesize;
292  long fileflags;
294  long filectime;
295  char fhandle[4];
296  bool fopened;
297 
299  std::string stringresp;
300 
302  int reqstate;
303 
305  long long writtenbytes;
306 
307  int mScitag;
308 
309 
310 
311 
312 
318  int ProcessHTTPReq();
319 
320 
321  // ------------
322  // Items inherited from the Bridge class
323  //
324 
325  //-----------------------------------------------------------------------------
351  //-----------------------------------------------------------------------------
352 
353  virtual bool Data(XrdXrootd::Bridge::Context &info,
354  const
355  struct iovec *iovP,
356  int iovN,
357  int iovL,
358  bool final
359  );
360 
361  //-----------------------------------------------------------------------------
371  //-----------------------------------------------------------------------------
372 
373  virtual bool Done(XrdXrootd::Bridge::Context &info);
374 
375 
376  //-----------------------------------------------------------------------------
389  //-----------------------------------------------------------------------------
390 
391  virtual bool Error(XrdXrootd::Bridge::Context &info,
392  int ecode,
393  const char *etext
394  );
395 
396  //-----------------------------------------------------------------------------
409  //-----------------------------------------------------------------------------
410 
411  virtual int File(XrdXrootd::Bridge::Context &info,
412  int dlen
413  );
414 
415  //-----------------------------------------------------------------------------
428  //-----------------------------------------------------------------------------
429 
430  virtual bool Redir(XrdXrootd::Bridge::Context &info,
431  int port,
432  const char *hname
433  );
434 
435 };
436 
437 
438 
439 void trim(std::string &str);
440 
441 #endif /* XRDHTTPREQ_HH */
442 
long filemodtime
Definition: XrdHttpReq.hh:293
virtual bool Data(XrdXrootd::Bridge::Context &info, const struct iovec *iovP, int iovN, int iovL, bool final)
bool readClosing
Definition: XrdHttpReq.hh:238
int depth
Definition: XrdHttpReq.hh:242
XResponseType xrdresp
The last response data we got.
Definition: XrdHttpReq.hh:279
void clientMarshallReadAheadList(int nitems)
Definition: XrdHttpReq.hh:210
std::string requestverb
Definition: XrdHttpReq.hh:219
Definition: XrdHttpReq.hh:206
long modtime
Definition: XrdHttpReq.hh:64
long fileflags
Definition: XrdHttpReq.hh:292
std::string buildPartialHdr(long long bytestart, long long byteend, long long filesize, char *token)
Build a partial header for a multipart response.
int ReqReadV(const XrdHttpIOList &cl)
Prepare the buffers for sending a readv request.
int parseFirstLine(char *line, int len)
Parse the first line of the header.
XrdHttpReq(XrdHttpProtocol *protinstance, const XrdHttpReadRangeHandler::Configuration &rcfg)
Definition: XrdHttpReq.hh:151
std::vector< XrdOucIOVec2 > XrdHttpIOList
Definition: XrdHttpUtils.hh:95
long long writtenbytes
In a long write, we track where we have arrived.
Definition: XrdHttpReq.hh:305
void addCgi(const std::string &key, const std::string &value)
bool sendcontinue
Definition: XrdHttpReq.hh:243
XrdHttpProtocol * prot
Definition: XrdHttpReq.hh:95
std::string destination
The destination field specified in the req.
Definition: XrdHttpReq.hh:248
Definition: XrdXrootdBridge.hh:225
XrdOucString resourceplusopaque
The resource specified by the request, including all the opaque data.
Definition: XrdHttpReq.hh:230
XrdOucString m_resource_with_digest
Definition: XrdHttpReq.hh:259
Definition: XrdHttpReq.hh:71
void parseResource(char *url)
bool m_status_trailer
Definition: XrdHttpReq.hh:88
int sendReadResponseSingleRange(const XrdHttpIOList &received)
static void determineXRootDChecksumFromUserDigest(const std::string &userDigest, std::vector< std::string > &xrootdChecksums)
void trim(std::string &str)
int ProcessHTTPReq()
virtual bool Redir(XrdXrootd::Bridge::Context &info, int port, const char *hname)
Definition: XrdHttpReq.hh:213
ReqType
These are the HTTP/DAV requests that we support.
Definition: XrdHttpReq.hh:201
XrdHttpReadRangeHandler readRangeHandler
Tracking the next ranges of data to read during GET.
Definition: XrdHttpReq.hh:237
bool fopened
Definition: XrdHttpReq.hh:296
long long size
Definition: XrdHttpReq.hh:61
static void extractChecksumFromList(const std::string &checksumList, std::vector< std::string > &extractedChecksum)
int mScitag
Definition: XrdHttpReq.hh:307
std::vector< readahead_list > ralist
Definition: XrdHttpReq.hh:180
XrdOucEnv * opaque
The opaque data, after parsing.
Definition: XrdHttpReq.hh:228
XResponseType
Definition: XProtocol.hh:896
std::size_t hash(const BasicJsonType &j)
hash a JSON value
Definition: XrdOucJson.hh:5221
int parseLine(char *line, int len)
Parse the header.
int iovL
byte count
Definition: XrdHttpReq.hh:287
XrdHttpChecksumHandler::XrdHttpChecksumRawPtr m_req_cksum
The checksum that was ran for this request.
Definition: XrdHttpReq.hh:254
std::string etext
Definition: XrdHttpReq.hh:281
long filectime
Definition: XrdHttpReq.hh:294
virtual bool Error(XrdXrootd::Bridge::Context &info, int ecode, const char *etext)
Definition: XrdHttpReq.hh:214
const struct iovec * iovP
The latest data chunks got from the xrd layer. These are valid only inside the callbacks! ...
Definition: XrdHttpReq.hh:285
bool headerok
Tells if we have finished reading the header.
Definition: XrdHttpReq.hh:234
bool m_trailer_headers
Definition: XrdHttpReq.hh:83
bool keepalive
Definition: XrdHttpReq.hh:240
char fhandle[4]
Definition: XrdHttpReq.hh:295
virtual int File(XrdXrootd::Bridge::Context &info, int dlen)
Definition: XrdHttpReq.hh:202
int httpStatusCode
Definition: XrdHttpReq.hh:74
int parseHost(char *)
virtual ~XrdHttpReq()
Definition: XrdHttpReq.hh:59
void getReadResponse(XrdHttpIOList &received)
virtual bool Done(XrdXrootd::Bridge::Context &info)
the result context
Definition: XrdHttpReq.hh:212
long long m_current_chunk_size
Definition: XrdHttpReq.hh:80
std::string buildPartialHdrEnd(char *token)
Build the closing part for a multipart response.
std::string hdr2cgistr
Additional opaque info that may come from the hdr2cgi directive.
Definition: XrdHttpReq.hh:264
int PostProcessHTTPReq(bool final=false)
XErrorCode
Definition: XProtocol.hh:987
Definition: XrdOucEnv.hh:41
std::string host
The host field specified in the req.
Definition: XrdHttpReq.hh:246
std::string httpStatusText
Definition: XrdHttpReq.hh:75
int parseBody(char *body, long long len)
Parse the body of a request, assuming that it&#39;s XML and that it&#39;s entirely in memory.
std::string m_req_digest
The requested digest type.
Definition: XrdHttpReq.hh:251
void getfhandle()
unsigned int rwOpDone
To coordinate multipart responses across multiple calls.
Definition: XrdHttpReq.hh:273
bool m_transfer_encoding_chunked
Definition: XrdHttpReq.hh:78
std::string m_digest_header
The computed digest for the HTTP response header.
Definition: XrdHttpReq.hh:261
Definition: XrdHttpReadRangeHandler.hh:54
int PostProcessChecksum(std::string &digest_header)
void mapXrdErrorToHttpStatus()
Definition: XrdHttpProtocol.hh:79
long id
Definition: XrdHttpReq.hh:62
Definition: XrdXrootdBridge.hh:293
Definition: XrdHttpReq.hh:204
void clientUnMarshallReadAheadList(int nitems)
ClientRequest xrdreq
The last issued xrd request, often pending.
Definition: XrdHttpReq.hh:276
std::string stringresp
If we want to give a string as a response, we compose it here.
Definition: XrdHttpReq.hh:299
long long length
Definition: XrdHttpReq.hh:241
Definition: XrdHttpChecksum.hh:32
int iovN
array count
Definition: XrdHttpReq.hh:286
Definition: XrdHttpReq.hh:211
long long m_current_chunk_offset
Definition: XrdHttpReq.hh:79
int reqstate
State machine to talk to the bridge.
Definition: XrdHttpReq.hh:302
long long filesize
Definition: XrdHttpReq.hh:291
bool m_appended_hdr2cgistr
Definition: XrdHttpReq.hh:265
Definition: XrdHttpReq.hh:203
void appendOpaque(XrdOucString &s, XrdSecEntity *secent, char *hash, time_t tnow)
XErrorCode xrderrcode
Definition: XrdHttpReq.hh:280
Definition: XrdSecEntity.hh:64
Definition: XProtocol.hh:843
int sendReadResponsesMultiRanges(const XrdHttpIOList &received)
XrdOucString redirdest
Definition: XrdHttpReq.hh:282
virtual void reset()
ReqType request
The request we got.
Definition: XrdHttpReq.hh:218
void parseScitag(const std::string &val)
XrdOucString resource
The resource specified by the request, stripped of opaque data.
Definition: XrdHttpReq.hh:226
Definition: XrdHttpReq.hh:207
Definition: XrdHttpReq.hh:205
std::map< std::string, std::string > allheaders
Definition: XrdHttpReq.hh:223
Definition: XrdHttpReq.hh:209
void sanitizeResourcePfx()
Definition: XrdOucString.hh:254
unsigned int rwOpPartialDone
Definition: XrdHttpReq.hh:273
long flags
Definition: XrdHttpReq.hh:63
Definition: XrdHttpReq.hh:208
std::string path
Definition: XrdHttpReq.hh:60
Definition: XrdHttpReadRangeHandler.hh:36