xrootd
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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"
46 
47 #include <vector>
48 #include <string>
49 #include <map>
50 
51 //#include <libxml/parser.h>
52 //#include <libxml/tree.h>
53 
54 
55 
56 #define READV_MAXCHUNKS 512
57 #define READV_MAXCHUNKSIZE (1024*128)
58 
59 struct ReadWriteOp {
60  // < 0 means "not specified"
61  long long bytestart;
62  long long byteend;
63 };
64 
65 struct DirListInfo {
66  std::string path;
67  long long size;
68  long id;
69  long flags;
70  long modtime;
71 };
72 
73 
74 class XrdHttpProtocol;
75 class XrdOucEnv;
76 
78 private:
79  // HTTP response parameters to be sent back to the user
81  std::string httpStatusText;
82 
83  // Whether transfer encoding was requested.
87 
88  int parseContentRange(char *);
89  int parseHost(char *);
90  int parseRWOp(char *);
91 
92  //xmlDocPtr xmlbody; /* the resulting document tree */
94 
95  void clientMarshallReadAheadList(int nitems);
96  void clientUnMarshallReadAheadList(int nitems);
97 
98 
99  void getfhandle();
100 
101  // Process the checksum response and return a header that should
102  // be included in the response.
103  int PostProcessChecksum(std::string &digest_header);
104 
110  int PostProcessHTTPReq(bool final = false);
111 
112  // Parse a resource string, typically a filename, setting the resource field and the opaque data
113  void parseResource(char *url);
114  // Map an XRootD error code to an appropriate HTTP status code and message
116 public:
117 
118  XrdHttpReq(XrdHttpProtocol *protinstance) : keepalive(true) {
119 
120  prot = protinstance;
121  length = 0;
122  //xmlbody = 0;
123  depth = 0;
124  ralist = 0;
125  opaque = 0;
126  writtenbytes = 0;
127  fopened = false;
128  headerok = false;
129  };
130 
131  virtual ~XrdHttpReq();
132 
133  virtual void reset();
134 
136  int parseLine(char *line, int len);
137 
139  int parseFirstLine(char *line, int len);
140 
142  int parseBody(char *body, long long len);
143 
145  int ReqReadV();
147 
149  std::string buildPartialHdr(long long bytestart, long long byteend, long long filesize, char *token);
150 
152  std::string buildPartialHdrEnd(char *token);
153 
154  // Appends the opaque info that we have
155  // NOTE: this function assumes that the strings are unquoted, and will quote them
156  void appendOpaque(XrdOucString &s, XrdSecEntity *secent, char *hash, time_t tnow);
157 
158  // ----------------
159  // Description of the request. The header/body parsing
160  // is supposed to populate these fields, for fast access while
161  // processing the request
162 
164 
165  enum ReqType {
166  rtUnset = -1,
179  };
180 
183  std::string requestverb;
184 
185  // We have to keep the headers for possible further processing
186  // by external plugins
187  std::map<std::string, std::string> allheaders;
188 
195 
196 
198  bool headerok;
199 
200 
201  // This can be largely optimized...
203  std::vector<ReadWriteOp> rwOps;
206  std::vector<ReadWriteOp> rwOps_split;
207 
208  bool keepalive;
209  long long length; // Total size from client for PUT; total length of response TO client for GET.
210  int depth;
212 
214  std::string host;
216  std::string destination;
217 
219  std::string m_req_digest;
225  std::string m_digest_header;
226 
228  std::string hdr2cgistr;
230 
231  //
232  // Area for coordinating request and responses to/from the bridge
233  //
234 
235 
237  unsigned int rwOpDone, rwOpPartialDone;
238 
241 
245  std::string etext;
247 
249  const struct iovec *iovP;
250  int iovN;
251  int iovL;
252  bool final;
253 
254  // The latest stat info got from the xrd layer
255  long long filesize;
256  long fileflags;
258  char fhandle[4];
259  bool fopened;
260 
262  std::string stringresp;
263 
265  int reqstate;
266 
268  long long writtenbytes;
269 
270 
271 
272 
273 
279  int ProcessHTTPReq();
280 
281 
282  // ------------
283  // Items inherited from the Bridge class
284  //
285 
286  //-----------------------------------------------------------------------------
312  //-----------------------------------------------------------------------------
313 
314  virtual bool Data(XrdXrootd::Bridge::Context &info,
315  const
316  struct iovec *iovP,
317  int iovN,
318  int iovL,
319  bool final
320  );
321 
322  //-----------------------------------------------------------------------------
332  //-----------------------------------------------------------------------------
333 
334  virtual bool Done(XrdXrootd::Bridge::Context &info);
335 
336 
337  //-----------------------------------------------------------------------------
350  //-----------------------------------------------------------------------------
351 
352  virtual bool Error(XrdXrootd::Bridge::Context &info,
353  int ecode,
354  const char *etext
355  );
356 
357  //-----------------------------------------------------------------------------
370  //-----------------------------------------------------------------------------
371 
372  virtual int File(XrdXrootd::Bridge::Context &info,
373  int dlen
374  );
375 
376  //-----------------------------------------------------------------------------
389  //-----------------------------------------------------------------------------
390 
391  virtual bool Redir(XrdXrootd::Bridge::Context &info,
392  int port,
393  const char *hname
394  );
395 
396 
397 
398 
399 
400 };
401 
402 
403 
404 void trim(std::string &str);
405 
406 #endif /* XRDHTTPREQ_HH */
407 
long filemodtime
Definition: XrdHttpReq.hh:257
virtual bool Data(XrdXrootd::Bridge::Context &info, const struct iovec *iovP, int iovN, int iovL, bool final)
int depth
Definition: XrdHttpReq.hh:210
XResponseType xrdresp
The last response data we got.
Definition: XrdHttpReq.hh:243
void clientMarshallReadAheadList(int nitems)
Definition: XrdHttpReq.hh:174
std::string requestverb
Definition: XrdHttpReq.hh:183
Definition: XrdHttpReq.hh:170
long modtime
Definition: XrdHttpReq.hh:70
long fileflags
Definition: XrdHttpReq.hh:256
std::string buildPartialHdr(long long bytestart, long long byteend, long long filesize, char *token)
Build a partial header for a multipart response.
int parseFirstLine(char *line, int len)
Parse the first line of the header.
long long writtenbytes
In a long write, we track where we have arrived.
Definition: XrdHttpReq.hh:268
Definition: XProtocol.hh:653
bool sendcontinue
Definition: XrdHttpReq.hh:211
XrdHttpProtocol * prot
Definition: XrdHttpReq.hh:93
std::string destination
The destination field specified in the req.
Definition: XrdHttpReq.hh:216
Definition: XrdXrootdBridge.hh:225
XrdOucString resourceplusopaque
The resource specified by the request, including all the opaque data.
Definition: XrdHttpReq.hh:194
XrdOucString m_resource_with_digest
Definition: XrdHttpReq.hh:223
Definition: XrdHttpReq.hh:77
void parseResource(char *url)
void trim(std::string &str)
int ProcessHTTPReq()
virtual bool Redir(XrdXrootd::Bridge::Context &info, int port, const char *hname)
Definition: XrdHttpReq.hh:177
ReqType
These are the HTTP/DAV requests that we support.
Definition: XrdHttpReq.hh:165
bool fopened
Definition: XrdHttpReq.hh:259
long long size
Definition: XrdHttpReq.hh:67
Definition: XrdHttpReq.hh:59
XrdOucEnv * opaque
The opaque data, after parsing.
Definition: XrdHttpReq.hh:192
XResponseType
Definition: XProtocol.hh:891
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:251
std::string etext
Definition: XrdHttpReq.hh:245
long long bytestart
Definition: XrdHttpReq.hh:61
virtual bool Error(XrdXrootd::Bridge::Context &info, int ecode, const char *etext)
Definition: XrdHttpReq.hh:178
bool headerok
Tells if we have finished reading the header.
Definition: XrdHttpReq.hh:198
bool keepalive
Definition: XrdHttpReq.hh:208
std::vector< ReadWriteOp > rwOps
The original list of multiple reads to perform.
Definition: XrdHttpReq.hh:203
char fhandle[4]
Definition: XrdHttpReq.hh:258
virtual int File(XrdXrootd::Bridge::Context &info, int dlen)
Definition: XrdHttpReq.hh:166
int httpStatusCode
Definition: XrdHttpReq.hh:80
int parseHost(char *)
virtual ~XrdHttpReq()
Definition: XrdHttpReq.hh:65
virtual bool Done(XrdXrootd::Bridge::Context &info)
the result context
Definition: XrdHttpReq.hh:176
long long m_current_chunk_size
Definition: XrdHttpReq.hh:86
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:228
int PostProcessHTTPReq(bool final=false)
XErrorCode
Definition: XProtocol.hh:982
readahead_list * ralist
Definition: XrdHttpReq.hh:146
Definition: XrdOucEnv.hh:41
std::string host
The host field specified in the req.
Definition: XrdHttpReq.hh:214
std::string httpStatusText
Definition: XrdHttpReq.hh:81
std::vector< ReadWriteOp > rwOps_split
Definition: XrdHttpReq.hh:206
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:219
void getfhandle()
unsigned int rwOpDone
To coordinate multipart responses across multiple calls.
Definition: XrdHttpReq.hh:237
bool m_transfer_encoding_chunked
Definition: XrdHttpReq.hh:84
std::string m_digest_header
The computed digest for the HTTP response header.
Definition: XrdHttpReq.hh:225
int PostProcessChecksum(std::string &digest_header)
int ReqReadV()
Prepare the buffers for sending a readv request.
void mapXrdErrorToHttpStatus()
Definition: XrdHttpProtocol.hh:76
long id
Definition: XrdHttpReq.hh:68
Definition: XrdXrootdBridge.hh:293
Definition: XrdHttpReq.hh:168
void clientUnMarshallReadAheadList(int nitems)
ClientRequest xrdreq
The last issued xrd request, often pending.
Definition: XrdHttpReq.hh:240
std::string stringresp
If we want to give a string as a response, we compose it here.
Definition: XrdHttpReq.hh:262
long long length
Definition: XrdHttpReq.hh:209
int iovN
array count
Definition: XrdHttpReq.hh:250
Definition: XrdHttpReq.hh:175
long long m_current_chunk_offset
Definition: XrdHttpReq.hh:85
int reqstate
State machine to talk to the bridge.
Definition: XrdHttpReq.hh:265
long long filesize
Definition: XrdHttpReq.hh:255
XrdHttpReq(XrdHttpProtocol *protinstance)
Definition: XrdHttpReq.hh:118
int parseRWOp(char *)
bool m_appended_hdr2cgistr
Definition: XrdHttpReq.hh:229
Definition: XrdHttpReq.hh:167
long long byteend
Definition: XrdHttpReq.hh:62
void appendOpaque(XrdOucString &s, XrdSecEntity *secent, char *hash, time_t tnow)
XErrorCode xrderrcode
Definition: XrdHttpReq.hh:244
Definition: XrdSecEntity.hh:63
Definition: XProtocol.hh:838
XrdOucString redirdest
Definition: XrdHttpReq.hh:246
virtual void reset()
ReqType request
The request we got.
Definition: XrdHttpReq.hh:182
XrdOucString resource
The resource specified by the request, stripped of opaque data.
Definition: XrdHttpReq.hh:190
Definition: XrdHttpReq.hh:171
Definition: XrdHttpReq.hh:169
struct iovec * iovP
The latest data chunks got from the xrd layer. These are valid only inside the callbacks! ...
Definition: XrdHttpReq.hh:249
std::map< std::string, std::string > allheaders
Definition: XrdHttpReq.hh:187
Definition: XrdHttpReq.hh:173
Definition: XrdOucString.hh:254
unsigned int rwOpPartialDone
Definition: XrdHttpReq.hh:237
long flags
Definition: XrdHttpReq.hh:69
Definition: XrdHttpReq.hh:172
std::string path
Definition: XrdHttpReq.hh:66
int parseContentRange(char *)