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