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