XRootD
XrdHttpTpcState.hh
Go to the documentation of this file.
1 
6 #ifndef __XRD_TPC_STATE_HH__
7 #define __XRD_TPC_STATE_HH__
8 
9 #include <memory>
10 #include <vector>
11 
12 #include "XrdSys/XrdSysRAtomic.hh"
13 
14 // Forward dec'ls
15 class XrdSfsFile;
16 class XrdHttpExtReq;
17 typedef void CURL;
18 struct curl_slist;
19 
20 namespace TPC {
21 class Stream;
22 
23 class State {
24 public:
25 
26  State() :
27  m_push(true),
28  m_recv_status_line(false),
29  m_recv_all_headers(false),
30  m_offset(0),
31  m_start_offset(0),
32  m_status_code(-1),
33  m_error_code(0),
34  m_content_length(-1),
35  m_stream(NULL),
36  m_curl(NULL),
37  m_headers(NULL),
38  m_is_transfer_state(true)
39  {}
40 
45  State(CURL * curl, bool tpcForwardCreds):
46  m_push(true),
47  m_recv_status_line(false),
48  m_recv_all_headers(false),
49  m_offset(0),
50  m_start_offset(0),
51  m_status_code(-1),
52  m_error_code(0),
53  m_content_length(-1),
54  m_push_length(-1),
55  m_stream(NULL),
56  m_curl(curl),
57  m_headers(NULL),
58  m_is_transfer_state(false),
59  tpcForwardCreds(tpcForwardCreds)
60  {
61  InstallHandlers(curl);
62  }
63 
64  // Note that we are "borrowing" a reference to the curl handle;
65  // it is not owned / freed by the State object. However, we use it
66  // as if there's only one handle per State.
67  State (off_t start_offset, Stream &stream, CURL *curl, bool push, bool tpcForwardCreds) :
68  m_push(push),
69  m_recv_status_line(false),
70  m_recv_all_headers(false),
71  m_offset(0),
72  m_start_offset(start_offset),
73  m_status_code(-1),
74  m_error_code(0),
75  m_content_length(-1),
76  m_push_length(-1),
77  m_stream(&stream),
78  m_curl(curl),
79  m_headers(NULL),
80  m_is_transfer_state(true),
81  tpcForwardCreds(tpcForwardCreds)
82  {
83  InstallHandlers(curl);
84  }
85 
86  ~State();
87 
88  void SetTransferParameters(off_t offset, size_t size);
89 
90  void SetupHeaders(XrdHttpExtReq &req);
91 
92  off_t BytesTransferred() const {return m_offset;}
93 
94  void SetContentLength(const off_t content_length) { m_content_length = content_length; }
95 
96  off_t GetContentLength() const {return m_content_length;}
97 
98  int GetErrorCode() const {return m_error_code;}
99 
100  void SetErrorCode(int error_code) {m_error_code = error_code;}
101 
102  int GetStatusCode() const {return m_status_code;}
103 
104  std::string GetErrorMessage() const {return m_error_buf;}
105 
106  void SetErrorMessage(const std::string &error_msg) {m_error_buf = error_msg;}
107 
108  void ResetAfterRequest();
109 
110  CURL *GetHandle() const {return m_curl;}
111 
112  int AvailableBuffers() const;
113 
114  void DumpBuffers() const;
115 
116  // Returns true if at least one byte of the response has been received,
117  // but not the entire contents of the response.
118  bool BodyTransferInProgress() const {return m_offset && (m_offset != m_content_length);}
119 
120  // Duplicate the current state; all settings are copied over, but those
121  // related to the transient state are reset as if from a constructor.
122  State *Duplicate();
123 
124  // Move the contents of a State object. To be replaced by a move
125  // constructor once C++11 is allowed in XRootD.
126  void Move (State &other);
127 
128  // Flush and finalize a transfer state. Eventually calls close() on the underlying
129  // file handle, which should hopefully synchronize the file metadata across
130  // all readers (even other load-balanced servers on the same distributed file
131  // system).
132  //
133  // Returns true on success; false otherwise. Failures can happen, for example, if
134  // not all buffers have been reordered by the underlying stream.
135  bool Finalize();
136 
137  // Flush the data in memory to disk, even if it may cause unaligned or short
138  // writes. Typically, only done while shutting down the transfer (note some
139  // backends may be unable to handle unaligned writes unless it's the last write).
140  int Flush();
141 
142  // Retrieve the description of the remote connection; is of the form:
143  // tcp:129.93.3.4:1234
144  // tcp:[2600:900:6:1301:268a:7ff:fef6:a590]:2345
145  // This is meant to facilitate the monitoring via the performance markers.
146  std::string GetConnectionDescription();
147 
148 private:
149  bool InstallHandlers(CURL *curl);
150 
151  State(const State&);
152  // Add back once C++11 is available
153  //State(State &&) noexcept;
154 
155  // libcurl callback functions, along with the corresponding class methods.
156  static size_t HeaderCB(char *buffer, size_t size, size_t nitems,
157  void *userdata);
158  int Header(const std::string &header);
159  static size_t WriteCB(void *buffer, size_t size, size_t nitems, void *userdata);
160  ssize_t Write(char *buffer, size_t size);
161  static size_t ReadCB(void *buffer, size_t size, size_t nitems, void *userdata);
162  int Read(char *buffer, size_t size);
163 
164  bool m_push; // whether we are transferring in "push-mode"
165  bool m_recv_status_line; // whether we have received a status line in the response from the remote host.
166  bool m_recv_all_headers; // true if we have seen the end of headers.
167  XrdSys::RAtomic<off_t> m_offset; // number of bytes we have received.
168  off_t m_start_offset; // offset where we started in the file.
169  int m_status_code; // status code from HTTP response.
170  int m_error_code; // error code from underlying stream operations.
171  off_t m_content_length; // value of Content-Length header, if we received one.
172  off_t m_push_length; // For push transfers, the size of the file on our server.
173  Stream *m_stream; // stream corresponding to this transfer.
174  CURL *m_curl; // libcurl handle
175  struct curl_slist *m_headers; // any headers we set as part of the libcurl request.
176  std::vector<std::string> m_headers_copy; // Copies of custom headers.
177  std::string m_resp_protocol; // Response protocol in the HTTP status line.
178  std::string m_error_buf; // Any error associated with a response.
179  bool m_is_transfer_state; // If set to true, this state will be used to perform some transfers
180  bool tpcForwardCreds = false; // if set to true, the redirection will send user credentials to the redirection host
181 };
182 
183 };
184 
185 #endif
void CURL
State(off_t start_offset, Stream &stream, CURL *curl, bool push, bool tpcForwardCreds)
State * Duplicate()
void Move(State &other)
int GetStatusCode() const
CURL * GetHandle() const
void DumpBuffers() const
off_t BytesTransferred() const
bool BodyTransferInProgress() const
void SetErrorMessage(const std::string &error_msg)
void ResetAfterRequest()
int GetErrorCode() const
void SetTransferParameters(off_t offset, size_t size)
std::string GetErrorMessage() const
std::string GetConnectionDescription()
void SetupHeaders(XrdHttpExtReq &req)
void SetContentLength(const off_t content_length)
off_t GetContentLength() const
void SetErrorCode(int error_code)
State(CURL *curl, bool tpcForwardCreds)
int AvailableBuffers() const