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