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