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 // Forward dec'ls
13 class XrdSfsFile;
14 class XrdHttpExtReq;
15 typedef void CURL;
16 struct curl_slist;
17 
18 namespace TPC {
19 class Stream;
20 
21 class State {
22 public:
23 
24  State() :
25  m_push(true),
26  m_recv_status_line(false),
27  m_recv_all_headers(false),
28  m_offset(0),
29  m_start_offset(0),
30  m_status_code(-1),
31  m_error_code(0),
32  m_content_length(-1),
33  m_stream(NULL),
34  m_curl(NULL),
35  m_headers(NULL),
36  m_is_transfer_state(true)
37  {}
38 
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 
93 
94  off_t BytesTransferred() const {return m_offset;}
95 
96  void SetContentLength(const off_t content_length) { m_content_length = content_length; }
97 
98  off_t GetContentLength() const {return m_content_length;}
99 
100  const std::map<std::string, std::string> & GetReprDigest() const { return m_repr_digests; }
101 
102  int GetErrorCode() const {return m_error_code;}
103 
104  void SetErrorCode(int error_code) {m_error_code = error_code;}
105 
106  int GetStatusCode() const {return m_status_code;}
107 
108  std::string GetErrorMessage() const {return m_error_buf;}
109 
110  void SetErrorMessage(const std::string &error_msg) {m_error_buf = error_msg;}
111 
112  void ResetAfterRequest();
113 
114  CURL *GetHandle() const {return m_curl;}
115 
116  int AvailableBuffers() const;
117 
118  void DumpBuffers() const;
119 
120  // Returns true if at least one byte of the response has been received,
121  // but not the entire contents of the response.
122  bool BodyTransferInProgress() const {return m_offset && (m_offset != m_content_length);}
123 
124  // Duplicate the current state; all settings are copied over, but those
125  // related to the transient state are reset as if from a constructor.
126  State *Duplicate();
127 
128  // Move the contents of a State object. To be replaced by a move
129  // constructor once C++11 is allowed in XRootD.
130  void Move (State &other);
131 
132  // Flush and finalize a transfer state. Eventually calls close() on the underlying
133  // file handle, which should hopefully synchronize the file metadata across
134  // all readers (even other load-balanced servers on the same distributed file
135  // system).
136  //
137  // Returns true on success; false otherwise. Failures can happen, for example, if
138  // not all buffers have been reordered by the underlying stream.
139  bool Finalize();
140 
141  // Flush the data in memory to disk, even if it may cause unaligned or short
142  // writes. Typically, only done while shutting down the transfer (note some
143  // backends may be unable to handle unaligned writes unless it's the last write).
144  int Flush();
145 
146  // Retrieve the description of the remote connection; is of the form:
147  // tcp:129.93.3.4:1234
148  // tcp:[2600:900:6:1301:268a:7ff:fef6:a590]:2345
149  // This is meant to facilitate the monitoring via the performance markers.
150  std::string GetConnectionDescription();
151 
152 private:
153  bool InstallHandlers(CURL *curl);
154 
155  State(const State&);
156  // Add back once C++11 is available
157  //State(State &&) noexcept;
158 
159  // libcurl callback functions, along with the corresponding class methods.
160  static size_t HeaderCB(char *buffer, size_t size, size_t nitems,
161  void *userdata);
162  int Header(const std::string &header);
163  static size_t WriteCB(void *buffer, size_t size, size_t nitems, void *userdata);
164  ssize_t Write(char *buffer, size_t size);
169  static size_t PushRespCB(void *buffer, size_t size, size_t nitems, void *userdata);
170  static size_t ReadCB(void *buffer, size_t size, size_t nitems, void *userdata);
171  int Read(char *buffer, size_t size);
172 
173  bool m_push; // whether we are transferring in "push-mode"
174  bool m_recv_status_line; // whether we have received a status line in the response from the remote host.
175  bool m_recv_all_headers; // true if we have seen the end of headers.
176  off_t m_offset; // number of bytes we have received.
177  off_t m_start_offset; // offset where we started in the file.
178  int m_status_code; // status code from HTTP response.
179  int m_error_code; // error code from underlying stream operations.
180  off_t m_content_length; // value of Content-Length header, if we received one.
181  off_t m_push_length; // For push transfers, the size of the file on our server.
182  Stream *m_stream; // stream corresponding to this transfer.
183  CURL *m_curl; // libcurl handle
184  struct curl_slist *m_headers; // any headers we set as part of the libcurl request.
185  std::vector<std::string> m_headers_copy; // Copies of custom headers.
186  std::string m_resp_protocol; // Response protocol in the HTTP status line.
187  std::string m_error_buf; // Any error associated with a response.
188  bool m_is_transfer_state; // If set to true, this state will be used to perform some transfers
189  bool tpcForwardCreds = false; // if set to true, the redirection will send user credentials to the redirection host
190  std::map<std::string, std::string> m_repr_digests; // Repr-Digest values received from the passive server (PULL)
191 };
192 
193 };
194 
195 #endif
void CURL
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)
const std::map< std::string, std::string > & GetReprDigest() const
void SetupHeadersForHEAD(XrdHttpExtReq &req)
int AvailableBuffers() const