Debian Codegraph @

src/backend/BackendRequest.hpp

raw
1 /*
2 * This File is part of Davix, The IO library for HTTP based protocols
3 * Copyright (C) CERN 2019
4 * Author: Georgios Bitzes <[email protected]>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 *
20 */
21
22 #ifndef BACKEND_REQUEST_HPP
23 #define BACKEND_REQUEST_HPP
24
25 #include <davix_internal.hpp>
26 #include <request/httprequest.hpp>
27 #include <utils/davix_uri.hpp>
28 #include <memory>
29
30 #define DEFAULT_REQUEST_SIGNING_DURATION 3600
31
32 namespace Davix{
33
34 //------------------------------------------------------------------------------
35 // Describe current request status.
36 //------------------------------------------------------------------------------
37 enum class RequestStatus {
38 kNotStarted = 0, // request is not active
39 kStarted, // request has been started
40 kCompletedOneShot, // request completed through 'executeRequest', not begin/end
41 };
42
43 //------------------------------------------------------------------------------
44 // Abstract HTTP request type towards a backend.
45 //------------------------------------------------------------------------------
46 class BackendRequest {
47 public:
48 //----------------------------------------------------------------------------
49 // Default constructor
50 //----------------------------------------------------------------------------
51 BackendRequest(Context &c, const Uri &uri);
52
53 //----------------------------------------------------------------------------
54 // Virtual destructor
55 //----------------------------------------------------------------------------
56 virtual ~BackendRequest();
57
58 //----------------------------------------------------------------------------
59 // No evil constructors, no move semantics.
60 //----------------------------------------------------------------------------
61 BackendRequest(const BackendRequest& other) = delete;
62 BackendRequest(BackendRequest&& other) = delete;
63 BackendRequest& operator=(BackendRequest&& other) = delete;
64 BackendRequest& operator=(const BackendRequest& other) = delete;
65
66 //----------------------------------------------------------------------------
67 // Major read member - implementations need to override.
68 // Read a block of max_size bytes (at max) into buffer.
69 //----------------------------------------------------------------------------
70 virtual dav_ssize_t readBlock(char* buffer, dav_size_t max_size, DavixError** err) = 0;
71
72 //----------------------------------------------------------------------------
73 // Get a specific response header
74 //----------------------------------------------------------------------------
75 virtual bool getAnswerHeader(const std::string &header_name, std::string &value) const = 0;
76
77 //----------------------------------------------------------------------------
78 // Get all response headers
79 //----------------------------------------------------------------------------
80 virtual size_t getAnswerHeaders(std::vector<std::pair<std::string, std::string > > & vec_headers) const = 0;
81
82 //----------------------------------------------------------------------------
83 // Start request.
84 //----------------------------------------------------------------------------
85 virtual int beginRequest(DavixError** err) = 0;
86
87 //----------------------------------------------------------------------------
88 // Finish an already started request.
89 //----------------------------------------------------------------------------
90 virtual int endRequest(DavixError** err) = 0;
91
92 //----------------------------------------------------------------------------
93 // Execute request synchronously, and store result in internal buffer.
94 //----------------------------------------------------------------------------
95 virtual int executeRequest(DavixError** err) = 0;
96
97 //----------------------------------------------------------------------------
98 // Get response status.
99 //----------------------------------------------------------------------------
100 virtual int getRequestCode() = 0;
101
102 //----------------------------------------------------------------------------
103 // Helper read members - implemented in terms of readBlock, and an internal
104 // buffer.
105 //----------------------------------------------------------------------------
106 dav_ssize_t readSegment(char* buffer, dav_size_t size_read, bool stop_at_line_boundary, DavixError** err);
107 dav_ssize_t readLine(char* buffer, dav_size_t max_size, DavixError** err);
108 dav_ssize_t readToFd(int fd, dav_size_t read_size, DavixError** err);
109
110 //----------------------------------------------------------------------------
111 // Add custom header to the request, replace an existing one if already
112 // exists. If value is empty, the entire header line is removed.
113 //----------------------------------------------------------------------------
114 void addHeaderField(const std::string & field, const std::string & value){
115 _headers_field.emplace_back(field, value);
116 }
117
118 //----------------------------------------------------------------------------
119 // Set the request verb. (such as GET, POST, PUT, PROPFIND, etc)
120 //----------------------------------------------------------------------------
121 void setRequestMethod(const std::string &val){
122 _request_type = val;
123 }
124
125 //----------------------------------------------------------------------------
126 // Get the request verb. (such as GET, POST, PUT, PROPFIND, etc)
127 //----------------------------------------------------------------------------
128 std::string getRequestMethod() {
129 return _request_type;
130 }
131
132 //----------------------------------------------------------------------------
133 // Set the value of a request flag
134 //----------------------------------------------------------------------------
135 void setFlag(const RequestFlag::RequestFlag flag, bool value) {
136 if(value) {
137 _req_flag |= flag;
138 }
139 else {
140 _req_flag &= ~(flag);
141 }
142 }
143
144 //----------------------------------------------------------------------------
145 // Get the value of a request flag
146 //----------------------------------------------------------------------------
147 bool getFlag(const RequestFlag::RequestFlag flag) const {
148 return _req_flag & ((int) flag);
149 }
150
151 //----------------------------------------------------------------------------
152 // Several different ways to provide the request body.
153 //----------------------------------------------------------------------------
154 void setRequestBody(const std::string & body);
155 void setRequestBody(const void * buffer, dav_size_t len);
156 void setRequestBody(int fd, dav_off_t offset, dav_size_t len);
157 void setRequestBody(HttpBodyProvider provider, dav_size_t len, void* udata);
158 void setRequestBody(ContentProvider &provider);
159
160 //----------------------------------------------------------------------------
161 // Set request parameters.
162 //----------------------------------------------------------------------------
163 void setParameters(const RequestParams &p);
164
165 //----------------------------------------------------------------------------
166 // Get request parameters.
167 //----------------------------------------------------------------------------
168 RequestParams& getParameters();
169
170 //----------------------------------------------------------------------------
171 // Get this requests' context.
172 //----------------------------------------------------------------------------
173 Context& getContext() const;
174
175 //----------------------------------------------------------------------------
176 // Get original URL, before any redirections
177 //----------------------------------------------------------------------------
178 std::shared_ptr<Uri> getOriginalUri() const;
179
180 //----------------------------------------------------------------------------
181 // Access response buffer.
182 //----------------------------------------------------------------------------
183 const char* getAnswerContent();
184 std::vector<char> & getAnswerContentVec();
185
186 //----------------------------------------------------------------------------
187 // Clear response buffer.
188 //----------------------------------------------------------------------------
189 void clearAnswerContent();
190
191 //----------------------------------------------------------------------------
192 // Parse "Last-Modified" response header, return as time_t.
193 //----------------------------------------------------------------------------
194 time_t getLastModified() const;
195
196 //----------------------------------------------------------------------------
197 // Get answer size
198 //----------------------------------------------------------------------------
199 dav_ssize_t getAnswerSize() const;
200
201 protected:
202 //----------------------------------------------------------------------------
203 // Configure request for S3.
204 //----------------------------------------------------------------------------
205 void configureS3params();
206
207 //----------------------------------------------------------------------------
208 // Configure request for Azure.
209 //----------------------------------------------------------------------------
210 void configureAzureParams();
211
212 //----------------------------------------------------------------------------
213 // Configure request for Gcloud.
214 //----------------------------------------------------------------------------
215 void configureGcloudParams();
216
217 //----------------------------------------------------------------------------
218 // Configure request for Swift.
219 //----------------------------------------------------------------------------
220 void configureSwiftParams();
221
222 //----------------------------------------------------------------------------
223 // Set-up deadline, but only if uninitialized
224 //----------------------------------------------------------------------------
225 void setupDeadlineIfUnset();
226
227 //----------------------------------------------------------------------------
228 // Check if deadline has already passed
229 //----------------------------------------------------------------------------
230 bool checkTimeout(DavixError **err);
231
232 //----------------------------------------------------------------------------
233 // Get answer size from headers.
234 //----------------------------------------------------------------------------
235 dav_ssize_t getAnswerSizeFromHeaders() const;
236
237 //----------------------------------------------------------------------------
238 // Member variables common to all implementations.
239 //----------------------------------------------------------------------------
240 Context& _context;
241 std::shared_ptr<Uri> _current, _orig;
242 RequestParams _params;
243 std::vector<std::pair<std::string, std::string>> _headers_field;
244 std::string _request_type;
245 int _req_flag;
246 Chrono::TimePoint _deadline;
247
248 //----------------------------------------------------------------------------
249 // Request content.
250 //----------------------------------------------------------------------------
251 std::unique_ptr<ContentProvider> _owned_content_provider;
252 ContentProvider *_content_provider;
253
254 //----------------------------------------------------------------------------
255 // Answer length.
256 //----------------------------------------------------------------------------
257 mutable dav_ssize_t _ans_size;
258
259 //----------------------------------------------------------------------------
260 // Answer buffers.
261 //----------------------------------------------------------------------------
262 std::vector<char> _vec;
263 std::vector<char> _vec_line;
264
265 //----------------------------------------------------------------------------
266 // Early termination flag and status.
267 //----------------------------------------------------------------------------
268 bool _early_termination;
269 DavixError* _early_termination_error;
270
271 };
272
273
274 }
275
276
277 #endif