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
74  int httpStatusCode;
75  std::string httpStatusText;
76 
77  // The value of the user agent, if specified
78  std::string m_user_agent;
79 
80  // Whether transfer encoding was requested.
81  bool m_transfer_encoding_chunked;
82  long long m_current_chunk_offset;
83  long long m_current_chunk_size;
84 
85  // Whether trailer headers were enabled
86  bool m_trailer_headers{false};
87 
88  // Whether the client understands our special status trailer.
89  // The status trailer allows us to report when an IO error occurred
90  // after a response body has started
91  bool m_status_trailer{false};
92 
93  int parseHost(char *);
94 
95  void parseScitag(const std::string & val);
96 
97  //xmlDocPtr xmlbody; /* the resulting document tree */
98  XrdHttpProtocol *prot;
99 
100  void clientMarshallReadAheadList(int nitems);
101  void clientUnMarshallReadAheadList(int nitems);
102 
103 
104  void getfhandle();
105 
106  // Process the checksum response and return a header that should
107  // be included in the response.
108  int PostProcessChecksum(std::string &digest_header);
109 
110  // Process the listing request of a GET request against a directory
111  // - final_: True if this is the last entry in the listing.
112  int PostProcessListing(bool final_);
113 
114  // Send the response for a GET request for a file read (i.e., not a directory)
115  // Invoked after the open is successful but before the first read is issued.
116  int ReturnGetHeaders();
117 
123  int PostProcessHTTPReq(bool final = false);
124 
125  // Parse a resource string, typically a filename, setting the resource field and the opaque data
126  void parseResource(char *url);
127  // Map an XRootD error code to an appropriate HTTP status code and message
128  void mapXrdErrorToHttpStatus();
129 
130  // Sanitize the resource from http[s]://[host]/ questionable prefix
131  void sanitizeResourcePfx();
132 
133  // parses the iovN data pointers elements as either a kXR_read or kXR_readv
134  // response and fills out a XrdHttpIOList with the corresponding length and
135  // buffer pointers. File offsets from kXR_readv responses are not recorded.
136  void getReadResponse(XrdHttpIOList &received);
137 
138  // notifies the range handler of receipt of bytes and sends the client
139  // the data.
140  int sendReadResponseSingleRange(const XrdHttpIOList &received);
141 
142  // notifies the range handler of receipt of bytes and sends the client
143  // the data and necessary headers, assuming multipart/byteranges content type.
144  int sendReadResponsesMultiRanges(const XrdHttpIOList &received);
145 
146  // If requested by the client, sends any I/O errors that occur during the transfer
147  // into a footer.
148  int sendFooterError(const std::string &);
149 
150  // Set the age header from the file modification time
151  void addAgeHeader(std::string & headers);
157  static void extractChecksumFromList(const std::string & checksumList, std::vector<std::string> & extractedChecksum);
158 
165  static void determineXRootDChecksumFromUserDigest(const std::string & userDigest, std::vector<std::string> & xrootdChecksums);
166 
167 public:
169  readRangeHandler(rcfg), closeAfterError(false), keepalive(true) {
170 
171  prot = protinstance;
172  length = 0;
173  //xmlbody = 0;
174  depth = 0;
175  opaque = 0;
176  writtenbytes = 0;
177  fopened = false;
178  headerok = false;
179  mScitag = -1;
180  };
181 
182  virtual ~XrdHttpReq();
183 
184  virtual void reset();
185 
187  int parseLine(char *line, int len);
188 
190  int parseFirstLine(char *line, int len);
191 
193  int parseBody(char *body, long long len);
194 
196  int ReqReadV(const XrdHttpIOList &cl);
197  std::vector<readahead_list> ralist;
198 
200  std::string buildPartialHdr(long long bytestart, long long byteend, long long filesize, char *token);
201 
203  std::string buildPartialHdrEnd(char *token);
204 
205  // Appends the opaque info that we have
206  // NOTE: this function assumes that the strings are unquoted, and will quote them
207  void appendOpaque(XrdOucString &s, XrdSecEntity *secent, char *hash, time_t tnow);
208 
209  void addCgi(const std::string & key, const std::string & value);
210 
211  // Set the transfer status header, if requested by the client
212  void setTransferStatusHeader(std::string &header);
213 
214  // Return the current user agent; if none has been specified, returns an empty string
215  const std::string &userAgent() const {return m_user_agent;}
216 
217  // ----------------
218  // Description of the request. The header/body parsing
219  // is supposed to populate these fields, for fast access while
220  // processing the request
221 
223 
224  enum ReqType {
225  rtUnset = -1,
237  rtPOST
238  };
239 
242  std::string requestverb;
243 
244  // We have to keep the headers for possible further processing
245  // by external plugins
246  std::map<std::string, std::string> allheaders;
247 
254 
255 
257  bool headerok;
258 
262 
263  // Indication that there was a read error and the next
264  // request processing state should cleanly close the file.
266 
267  bool keepalive;
268  long long length; // Total size from client for PUT; total length of response TO client for GET.
269  int depth;
271 
273  std::string host;
275  std::string destination;
276 
278  std::string m_req_digest;
279 
282 
288  std::string m_digest_header;
289 
291  std::string hdr2cgistr;
294  bool m_appended_asize{false};
295 
296  //
297  // Area for coordinating request and responses to/from the bridge
298  //
299 
300 
302  unsigned int rwOpDone, rwOpPartialDone;
303 
306 
310  std::string etext;
312 
314  const struct iovec *iovP;
315  int iovN;
316  int iovL;
317  bool final;
318 
319  // The latest stat info got from the xrd layer
320  long long filesize;
321  long fileflags;
323  long filectime;
324  char fhandle[4];
325  bool fopened;
326 
328  std::string stringresp;
329 
331  int reqstate;
332 
334  long long writtenbytes;
335 
336  int mScitag;
337 
338 
339 
340 
341 
347  int ProcessHTTPReq();
348 
349 
350  // ------------
351  // Items inherited from the Bridge class
352  //
353 
354  //-----------------------------------------------------------------------------
380  //-----------------------------------------------------------------------------
381 
382  virtual bool Data(XrdXrootd::Bridge::Context &info,
383  const
384  struct iovec *iovP,
385  int iovN,
386  int iovL,
387  bool final
388  );
389 
390  //-----------------------------------------------------------------------------
400  //-----------------------------------------------------------------------------
401 
402  virtual bool Done(XrdXrootd::Bridge::Context &info);
403 
404 
405  //-----------------------------------------------------------------------------
418  //-----------------------------------------------------------------------------
419 
420  virtual bool Error(XrdXrootd::Bridge::Context &info,
421  int ecode,
422  const char *etext
423  );
424 
425  //-----------------------------------------------------------------------------
438  //-----------------------------------------------------------------------------
439 
440  virtual int File(XrdXrootd::Bridge::Context &info,
441  int dlen
442  );
443 
444  //-----------------------------------------------------------------------------
457  //-----------------------------------------------------------------------------
458 
459  virtual bool Redir(XrdXrootd::Bridge::Context &info,
460  int port,
461  const char *hname
462  );
463 
464 };
465 
466 
467 
468 void trim(std::string &str);
469 
470 #endif /* XRDHTTPREQ_HH */
471 
XErrorCode
Definition: XProtocol.hh:989
XResponseType
Definition: XProtocol.hh:898
long long size
Definition: XrdHttpReq.hh:61
void trim(std::string &str)
Definition: XrdHttpReq.cc:76
std::string path
Definition: XrdHttpReq.hh:60
long modtime
Definition: XrdHttpReq.hh:64
std::vector< XrdOucIOVec2 > XrdHttpIOList
Definition: XrdHttpUtils.hh:95
int reqstate
State machine to talk to the bridge.
Definition: XrdHttpReq.hh:331
char fhandle[4]
Definition: XrdHttpReq.hh:324
int ReqReadV(const XrdHttpIOList &cl)
Prepare the buffers for sending a readv request.
Definition: XrdHttpReq.cc:393
bool keepalive
Definition: XrdHttpReq.hh:267
unsigned int rwOpPartialDone
Definition: XrdHttpReq.hh:302
ReqType
These are the HTTP/DAV requests that we support.
Definition: XrdHttpReq.hh:224
int parseBody(char *body, long long len)
Parse the body of a request, assuming that it's XML and that it's entirely in memory.
Definition: XrdHttpReq.cc:94
std::vector< readahead_list > ralist
Definition: XrdHttpReq.hh:197
long long length
Definition: XrdHttpReq.hh:268
std::string destination
The destination field specified in the req.
Definition: XrdHttpReq.hh:275
XrdOucString resource
The resource specified by the request, stripped of opaque data.
Definition: XrdHttpReq.hh:249
bool headerok
Tells if we have finished reading the header.
Definition: XrdHttpReq.hh:257
std::string m_digest_header
The computed digest for the HTTP response header.
Definition: XrdHttpReq.hh:288
std::string etext
Definition: XrdHttpReq.hh:310
std::string stringresp
If we want to give a string as a response, we compose it here.
Definition: XrdHttpReq.hh:328
XResponseType xrdresp
The last response data we got.
Definition: XrdHttpReq.hh:308
std::string requestverb
Definition: XrdHttpReq.hh:242
ReqType request
The request we got.
Definition: XrdHttpReq.hh:241
int ProcessHTTPReq()
Definition: XrdHttpReq.cc:939
bool closeAfterError
Definition: XrdHttpReq.hh:265
long long writtenbytes
In a long write, we track where we have arrived.
Definition: XrdHttpReq.hh:334
XrdOucEnv * opaque
The opaque data, after parsing.
Definition: XrdHttpReq.hh:251
long fileflags
Definition: XrdHttpReq.hh:321
int iovL
byte count
Definition: XrdHttpReq.hh:316
bool fopened
Definition: XrdHttpReq.hh:325
const struct iovec * iovP
The latest data chunks got from the xrd layer. These are valid only inside the callbacks!
Definition: XrdHttpReq.hh:314
virtual ~XrdHttpReq()
Definition: XrdHttpReq.cc:110
std::string m_req_digest
The requested digest type.
Definition: XrdHttpReq.hh:278
XrdOucString resourceplusopaque
The resource specified by the request, including all the opaque data.
Definition: XrdHttpReq.hh:253
virtual bool Data(XrdXrootd::Bridge::Context &info, const struct iovec *iovP, int iovN, int iovL, bool final)
Definition: XrdHttpReq.cc:448
long filectime
Definition: XrdHttpReq.hh:323
std::string hdr2cgistr
Additional opaque info that may come from the hdr2cgi directive.
Definition: XrdHttpReq.hh:291
virtual bool Done(XrdXrootd::Bridge::Context &info)
the result context
Definition: XrdHttpReq.cc:494
std::string host
The host field specified in the req.
Definition: XrdHttpReq.hh:273
long filemodtime
Definition: XrdHttpReq.hh:322
int parseFirstLine(char *line, int len)
Parse the first line of the header.
Definition: XrdHttpReq.cc:261
XrdOucString redirdest
Definition: XrdHttpReq.hh:311
int parseLine(char *line, int len)
Parse the header.
Definition: XrdHttpReq.cc:116
std::string buildPartialHdrEnd(char *token)
Build the closing part for a multipart response.
Definition: XrdHttpReq.cc:440
XrdHttpChecksumHandler::XrdHttpChecksumRawPtr m_req_cksum
The checksum that was ran for this request.
Definition: XrdHttpReq.hh:281
void setTransferStatusHeader(std::string &header)
Definition: XrdHttpReq.cc:2117
bool m_appended_hdr2cgistr
Definition: XrdHttpReq.hh:292
void appendOpaque(XrdOucString &s, XrdSecEntity *secent, char *hash, time_t tnow)
Definition: XrdHttpReq.cc:637
int iovN
array count
Definition: XrdHttpReq.hh:315
XrdHttpReq(XrdHttpProtocol *protinstance, const XrdHttpReadRangeHandler::Configuration &rcfg)
Definition: XrdHttpReq.hh:168
bool m_appended_asize
Track whether we already appended the oss.asize argument for PUTs.
Definition: XrdHttpReq.hh:294
XrdOucString m_resource_with_digest
Definition: XrdHttpReq.hh:286
long long filesize
Definition: XrdHttpReq.hh:320
bool readClosing
Definition: XrdHttpReq.hh:261
virtual bool Redir(XrdXrootd::Bridge::Context &info, int port, const char *hname)
Definition: XrdHttpReq.cc:541
XErrorCode xrderrcode
Definition: XrdHttpReq.hh:309
virtual int File(XrdXrootd::Bridge::Context &info, int dlen)
Definition: XrdHttpReq.cc:470
std::map< std::string, std::string > allheaders
Definition: XrdHttpReq.hh:246
unsigned int rwOpDone
To coordinate multipart responses across multiple calls.
Definition: XrdHttpReq.hh:302
void addCgi(const std::string &key, const std::string &value)
Definition: XrdHttpReq.cc:794
bool sendcontinue
Definition: XrdHttpReq.hh:270
ClientRequest xrdreq
The last issued xrd request, often pending.
Definition: XrdHttpReq.hh:305
const std::string & userAgent() const
Definition: XrdHttpReq.hh:215
std::string buildPartialHdr(long long bytestart, long long byteend, long long filesize, char *token)
Build a partial header for a multipart response.
Definition: XrdHttpReq.cc:430
XrdHttpReadRangeHandler readRangeHandler
Tracking the next ranges of data to read during GET.
Definition: XrdHttpReq.hh:260
virtual void reset()
Definition: XrdHttpReq.cc:2815
virtual bool Error(XrdXrootd::Bridge::Context &info, int ecode, const char *etext)
Definition: XrdHttpReq.cc:511