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