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 
115  int PostProcessHTTPReq(bool final = false);
116 
117  // Parse a resource string, typically a filename, setting the resource field and the opaque data
118  void parseResource(char *url);
119  // Map an XRootD error code to an appropriate HTTP status code and message
120  void mapXrdErrorToHttpStatus();
121 
122  // Sanitize the resource from http[s]://[host]/ questionable prefix
123  void sanitizeResourcePfx();
124 
125  // parses the iovN data pointers elements as either a kXR_read or kXR_readv
126  // response and fills out a XrdHttpIOList with the corresponding length and
127  // buffer pointers. File offsets from kXR_readv responses are not recorded.
128  void getReadResponse(XrdHttpIOList &received);
129 
130  // notifies the range handler of receipt of bytes and sends the client
131  // the data.
132  int sendReadResponseSingleRange(const XrdHttpIOList &received);
133 
134  // notifies the range handler of receipt of bytes and sends the client
135  // the data and necessary headers, assuming multipart/byteranges content type.
136  int sendReadResponsesMultiRanges(const XrdHttpIOList &received);
137 
143  static void extractChecksumFromList(const std::string & checksumList, std::vector<std::string> & extractedChecksum);
144 
151  static void determineXRootDChecksumFromUserDigest(const std::string & userDigest, std::vector<std::string> & xrootdChecksums);
152 
153 public:
155  readRangeHandler(rcfg), keepalive(true) {
156 
157  prot = protinstance;
158  length = 0;
159  //xmlbody = 0;
160  depth = 0;
161  opaque = 0;
162  writtenbytes = 0;
163  fopened = false;
164  headerok = false;
165  mScitag = -1;
166  };
167 
168  virtual ~XrdHttpReq();
169 
170  virtual void reset();
171 
173  int parseLine(char *line, int len);
174 
176  int parseFirstLine(char *line, int len);
177 
179  int parseBody(char *body, long long len);
180 
182  int ReqReadV(const XrdHttpIOList &cl);
183  std::vector<readahead_list> ralist;
184 
186  std::string buildPartialHdr(long long bytestart, long long byteend, long long filesize, char *token);
187 
189  std::string buildPartialHdrEnd(char *token);
190 
191  // Appends the opaque info that we have
192  // NOTE: this function assumes that the strings are unquoted, and will quote them
193  void appendOpaque(XrdOucString &s, XrdSecEntity *secent, char *hash, time_t tnow);
194 
195  void addCgi(const std::string & key, const std::string & value);
196 
197  // Return the current user agent; if none has been specified, returns an empty string
198  const std::string &userAgent() const {return m_user_agent;}
199 
200  // ----------------
201  // Description of the request. The header/body parsing
202  // is supposed to populate these fields, for fast access while
203  // processing the request
204 
206 
207  enum ReqType {
208  rtUnset = -1,
220  rtPOST
221  };
222 
225  std::string requestverb;
226 
227  // We have to keep the headers for possible further processing
228  // by external plugins
229  std::map<std::string, std::string> allheaders;
230 
237 
238 
240  bool headerok;
241 
245 
246  bool keepalive;
247  long long length; // Total size from client for PUT; total length of response TO client for GET.
248  int depth;
250 
252  std::string host;
254  std::string destination;
255 
257  std::string m_req_digest;
258 
261 
267  std::string m_digest_header;
268 
270  std::string hdr2cgistr;
272 
273  //
274  // Area for coordinating request and responses to/from the bridge
275  //
276 
277 
279  unsigned int rwOpDone, rwOpPartialDone;
280 
283 
287  std::string etext;
289 
291  const struct iovec *iovP;
292  int iovN;
293  int iovL;
294  bool final;
295 
296  // The latest stat info got from the xrd layer
297  long long filesize;
298  long fileflags;
300  long filectime;
301  char fhandle[4];
302  bool fopened;
303 
305  std::string stringresp;
306 
308  int reqstate;
309 
311  long long writtenbytes;
312 
313  int mScitag;
314 
315 
316 
317 
318 
324  int ProcessHTTPReq();
325 
326 
327  // ------------
328  // Items inherited from the Bridge class
329  //
330 
331  //-----------------------------------------------------------------------------
357  //-----------------------------------------------------------------------------
358 
359  virtual bool Data(XrdXrootd::Bridge::Context &info,
360  const
361  struct iovec *iovP,
362  int iovN,
363  int iovL,
364  bool final
365  );
366 
367  //-----------------------------------------------------------------------------
377  //-----------------------------------------------------------------------------
378 
379  virtual bool Done(XrdXrootd::Bridge::Context &info);
380 
381 
382  //-----------------------------------------------------------------------------
395  //-----------------------------------------------------------------------------
396 
397  virtual bool Error(XrdXrootd::Bridge::Context &info,
398  int ecode,
399  const char *etext
400  );
401 
402  //-----------------------------------------------------------------------------
415  //-----------------------------------------------------------------------------
416 
417  virtual int File(XrdXrootd::Bridge::Context &info,
418  int dlen
419  );
420 
421  //-----------------------------------------------------------------------------
434  //-----------------------------------------------------------------------------
435 
436  virtual bool Redir(XrdXrootd::Bridge::Context &info,
437  int port,
438  const char *hname
439  );
440 
441 };
442 
443 
444 
445 void trim(std::string &str);
446 
447 #endif /* XRDHTTPREQ_HH */
448 
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:308
char fhandle[4]
Definition: XrdHttpReq.hh:301
int ReqReadV(const XrdHttpIOList &cl)
Prepare the buffers for sending a readv request.
Definition: XrdHttpReq.cc:388
bool keepalive
Definition: XrdHttpReq.hh:246
unsigned int rwOpPartialDone
Definition: XrdHttpReq.hh:279
ReqType
These are the HTTP/DAV requests that we support.
Definition: XrdHttpReq.hh:207
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:183
long long length
Definition: XrdHttpReq.hh:247
std::string destination
The destination field specified in the req.
Definition: XrdHttpReq.hh:254
XrdOucString resource
The resource specified by the request, stripped of opaque data.
Definition: XrdHttpReq.hh:232
bool headerok
Tells if we have finished reading the header.
Definition: XrdHttpReq.hh:240
std::string m_digest_header
The computed digest for the HTTP response header.
Definition: XrdHttpReq.hh:267
std::string etext
Definition: XrdHttpReq.hh:287
std::string stringresp
If we want to give a string as a response, we compose it here.
Definition: XrdHttpReq.hh:305
XResponseType xrdresp
The last response data we got.
Definition: XrdHttpReq.hh:285
std::string requestverb
Definition: XrdHttpReq.hh:225
ReqType request
The request we got.
Definition: XrdHttpReq.hh:224
int ProcessHTTPReq()
Definition: XrdHttpReq.cc:926
long long writtenbytes
In a long write, we track where we have arrived.
Definition: XrdHttpReq.hh:311
XrdOucEnv * opaque
The opaque data, after parsing.
Definition: XrdHttpReq.hh:234
long fileflags
Definition: XrdHttpReq.hh:298
int iovL
byte count
Definition: XrdHttpReq.hh:293
bool fopened
Definition: XrdHttpReq.hh:302
const struct iovec * iovP
The latest data chunks got from the xrd layer. These are valid only inside the callbacks!
Definition: XrdHttpReq.hh:291
virtual ~XrdHttpReq()
Definition: XrdHttpReq.cc:110
std::string m_req_digest
The requested digest type.
Definition: XrdHttpReq.hh:257
XrdOucString resourceplusopaque
The resource specified by the request, including all the opaque data.
Definition: XrdHttpReq.hh:236
virtual bool Data(XrdXrootd::Bridge::Context &info, const struct iovec *iovP, int iovN, int iovL, bool final)
Definition: XrdHttpReq.cc:443
long filectime
Definition: XrdHttpReq.hh:300
std::string hdr2cgistr
Additional opaque info that may come from the hdr2cgi directive.
Definition: XrdHttpReq.hh:270
virtual bool Done(XrdXrootd::Bridge::Context &info)
the result context
Definition: XrdHttpReq.cc:489
std::string host
The host field specified in the req.
Definition: XrdHttpReq.hh:252
long filemodtime
Definition: XrdHttpReq.hh:299
int parseFirstLine(char *line, int len)
Parse the first line of the header.
Definition: XrdHttpReq.cc:256
XrdOucString redirdest
Definition: XrdHttpReq.hh:288
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:435
XrdHttpChecksumHandler::XrdHttpChecksumRawPtr m_req_cksum
The checksum that was ran for this request.
Definition: XrdHttpReq.hh:260
bool m_appended_hdr2cgistr
Definition: XrdHttpReq.hh:271
void appendOpaque(XrdOucString &s, XrdSecEntity *secent, char *hash, time_t tnow)
Definition: XrdHttpReq.cc:627
int iovN
array count
Definition: XrdHttpReq.hh:292
XrdHttpReq(XrdHttpProtocol *protinstance, const XrdHttpReadRangeHandler::Configuration &rcfg)
Definition: XrdHttpReq.hh:154
XrdOucString m_resource_with_digest
Definition: XrdHttpReq.hh:265
long long filesize
Definition: XrdHttpReq.hh:297
bool readClosing
Definition: XrdHttpReq.hh:244
virtual bool Redir(XrdXrootd::Bridge::Context &info, int port, const char *hname)
Definition: XrdHttpReq.cc:531
XErrorCode xrderrcode
Definition: XrdHttpReq.hh:286
virtual int File(XrdXrootd::Bridge::Context &info, int dlen)
Definition: XrdHttpReq.cc:465
std::map< std::string, std::string > allheaders
Definition: XrdHttpReq.hh:229
unsigned int rwOpDone
To coordinate multipart responses across multiple calls.
Definition: XrdHttpReq.hh:279
void addCgi(const std::string &key, const std::string &value)
Definition: XrdHttpReq.cc:784
bool sendcontinue
Definition: XrdHttpReq.hh:249
ClientRequest xrdreq
The last issued xrd request, often pending.
Definition: XrdHttpReq.hh:282
const std::string & userAgent() const
Definition: XrdHttpReq.hh:198
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:425
XrdHttpReadRangeHandler readRangeHandler
Tracking the next ranges of data to read during GET.
Definition: XrdHttpReq.hh:243
virtual void reset()
Definition: XrdHttpReq.cc:2750
virtual bool Error(XrdXrootd::Bridge::Context &info, int ecode, const char *etext)
Definition: XrdHttpReq.cc:506