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