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