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_content_length(-1),
30  m_stream(NULL),
31  m_curl(NULL),
32  m_headers(NULL)
33  {}
34 
35  // Note that we are "borrowing" a reference to the curl handle;
36  // it is not owned / freed by the State object. However, we use it
37  // as if there's only one handle per State.
38  State (off_t start_offset, Stream &stream, CURL *curl, bool push) :
39  m_push(push),
40  m_recv_status_line(false),
41  m_recv_all_headers(false),
42  m_offset(0),
43  m_start_offset(start_offset),
44  m_status_code(-1),
45  m_content_length(-1),
46  m_stream(&stream),
47  m_curl(curl),
48  m_headers(NULL)
49  {
50  InstallHandlers(curl);
51  }
52 
53  ~State();
54 
55  void SetTransferParameters(off_t offset, size_t size);
56 
57  void CopyHeaders(XrdHttpExtReq &req);
58 
59  off_t BytesTransferred() const {return m_offset;}
60 
61  off_t GetContentLength() const {return m_content_length;}
62 
63  int GetStatusCode() const {return m_status_code;}
64 
65  void ResetAfterRequest();
66 
67  CURL *GetHandle() const {return m_curl;}
68 
69  int AvailableBuffers() const;
70 
71  void DumpBuffers() const;
72 
73  // Returns true if at least one byte of the response has been received,
74  // but not the entire contents of the response.
76 
77  // Duplicate the current state; all settings are copied over, but those
78  // related to the transient state are reset as if from a constructor.
79  State *Duplicate();
80 
81  // Move the contents of a State object. To be replaced by a move
82  // constructor once C++11 is allowed in XRootD.
83  void Move (State &other);
84 
85  // Flush and finalize a transfer state. Eventually calls close() on the underlying
86  // file handle, which should hopefully synchronize the file metadata across
87  // all readers (even other load-balanced servers on the same distributed file
88  // system).
89  //
90  // Returns true on success; false otherwise. Failures can happen, for example, if
91  // not all buffers have been reordered by the underlying stream.
92  bool Finalize();
93 
94 private:
95  bool InstallHandlers(CURL *curl);
96 
97  State(const State&);
98  // Add back once C++11 is available
99  //State(State &&) noexcept;
100 
101  // libcurl callback functions, along with the corresponding class methods.
102  static size_t HeaderCB(char *buffer, size_t size, size_t nitems,
103  void *userdata);
104  int Header(const std::string &header);
105  static size_t WriteCB(void *buffer, size_t size, size_t nitems, void *userdata);
106  int Write(char *buffer, size_t size);
107  static size_t ReadCB(void *buffer, size_t size, size_t nitems, void *userdata);
108  int Read(char *buffer, size_t size);
109 
110  bool m_push; // whether we are transferring in "push-mode"
111  bool m_recv_status_line; // whether we have received a status line in the response from the remote host.
112  bool m_recv_all_headers; // true if we have seen the end of headers.
113  off_t m_offset; // number of bytes we have received.
114  off_t m_start_offset; // offset where we started in the file.
115  int m_status_code; // status code from HTTP response.
116  off_t m_content_length; // value of Content-Length header, if we received one.
117  Stream *m_stream; // stream corresponding to this transfer.
118  CURL *m_curl; // libcurl handle
119  struct curl_slist *m_headers; // any headers we set as part of the libcurl request.
120  std::vector<std::string> m_headers_copy; // Copies of custom headers.
121  std::string m_resp_protocol; // Response protocol in the HTTP status line.
122 };
123 
124 };
Definition: XrdTpcStream.hh:21
Definition: XrdTpcState.hh:19
bool InstallHandlers(CURL *curl)
bool m_push
Definition: XrdTpcState.hh:110
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()
State(off_t start_offset, Stream &stream, CURL *curl, bool push)
Definition: XrdTpcState.hh:38
void ResetAfterRequest()
State()
Definition: XrdTpcState.hh:22
CURL * m_curl
Definition: XrdTpcState.hh:118
bool BodyTransferInProgress() const
Definition: XrdTpcState.hh:75
off_t GetContentLength() const
Definition: XrdTpcState.hh:61
void SetTransferParameters(off_t offset, size_t size)
static size_t WriteCB(void *buffer, size_t size, size_t nitems, void *userdata)
Definition: XrdTpcState.hh:16
Stream * m_stream
Definition: XrdTpcState.hh:117
void Move(State &other)
off_t m_offset
Definition: XrdTpcState.hh:113
off_t BytesTransferred() const
Definition: XrdTpcState.hh:59
int m_status_code
Definition: XrdTpcState.hh:115
std::vector< std::string > m_headers_copy
Definition: XrdTpcState.hh:120
bool m_recv_status_line
Definition: XrdTpcState.hh:111
void DumpBuffers() const
std::string m_resp_protocol
Definition: XrdTpcState.hh:121
bool m_recv_all_headers
Definition: XrdTpcState.hh:112
off_t m_content_length
Definition: XrdTpcState.hh:116
int Header(const std::string &header)
State * Duplicate()
off_t m_start_offset
Definition: XrdTpcState.hh:114
Definition: XrdHttpExtHandler.hh:45
int Read(char *buffer, size_t size)
struct curl_slist * m_headers
Definition: XrdTpcState.hh:119
void CURL
Definition: XrdTpcState.hh:12
int GetStatusCode() const
Definition: XrdTpcState.hh:63
CURL * GetHandle() const
Definition: XrdTpcState.hh:67