| 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 |
#include "BackendRequest.hpp" |
| 23 |
#include <core/ContentProvider.hpp> |
| 24 |
#include <utils/davix_s3_utils.hpp> |
| 25 |
#include <utils/davix_azure_utils.hpp> |
| 26 |
#include <utils/davix_gcloud_utils.hpp> |
| 27 |
#include <utils/davix_swift_utils.hpp> |
| 28 |
#include <utils/davix_logger_internal.hpp> |
| 29 |
#include <utils/stringutils.hpp> |
| 30 |
#include <fileops/fileutils.hpp> |
| 31 |
#include <string> |
| 32 |
|
| 33 |
namespace Davix { |
| 34 |
|
| 35 |
//------------------------------------------------------------------------------ |
| 36 |
// Default constructor |
| 37 |
//------------------------------------------------------------------------------ |
| 38 |
BackendRequest::BackendRequest(Context &c, const Uri &uri) |
| 39 |
: _context(c), _current( new Uri(uri)), |
| 40 |
_orig(_current), |
| 41 |
_params(), |
| 42 |
_request_type("GET"), |
| 43 |
_req_flag(RequestFlag::IdempotentRequest), |
| 44 |
_deadline(), |
| 45 |
_content_provider(NULL), |
| 46 |
_ans_size(-1), |
| 47 |
_early_termination(false), |
| 48 |
_early_termination_error(NULL) {} |
| 49 |
|
| 50 |
//------------------------------------------------------------------------------ |
| 51 |
// Virtual destructor |
| 52 |
//------------------------------------------------------------------------------ |
| 53 |
BackendRequest::~BackendRequest() {} |
| 54 |
|
| 55 |
//------------------------------------------------------------------------------ |
| 56 |
// Several different ways to provide the request body. |
| 57 |
//------------------------------------------------------------------------------ |
| 58 |
void BackendRequest::setRequestBody(const std::string & body) { |
| 59 |
_owned_content_provider.reset(new OwnedBufferContentProvider(body)); |
| 60 |
_content_provider = _owned_content_provider.get(); |
| 61 |
} |
| 62 |
|
| 63 |
void BackendRequest::setRequestBody(const void * buffer, dav_size_t len) { |
| 64 |
_owned_content_provider.reset(new BufferContentProvider( (const char*) buffer, len)); |
| 65 |
_content_provider = _owned_content_provider.get(); |
| 66 |
} |
| 67 |
|
| 68 |
void BackendRequest::setRequestBody(int fd, dav_off_t offset, dav_size_t len) { |
| 69 |
_owned_content_provider.reset(new FdContentProvider(fd, offset, len)); |
| 70 |
_content_provider = _owned_content_provider.get(); |
| 71 |
} |
| 72 |
|
| 73 |
void BackendRequest::setRequestBody(HttpBodyProvider provider, dav_size_t len, void* udata) { |
| 74 |
_owned_content_provider.reset(new CallbackContentProvider(provider, len, udata)); |
| 75 |
_content_provider = _owned_content_provider.get(); |
| 76 |
} |
| 77 |
|
| 78 |
void BackendRequest::setRequestBody(ContentProvider &provider) { |
| 79 |
_content_provider = &provider; |
| 80 |
} |
| 81 |
|
| 82 |
//------------------------------------------------------------------------------ |
| 83 |
// Set request parameters. |
| 84 |
//------------------------------------------------------------------------------ |
| 85 |
void BackendRequest::setParameters(const RequestParams &p) { |
| 86 |
_params = p; |
| 87 |
} |
| 88 |
|
| 89 |
//------------------------------------------------------------------------------ |
| 90 |
// Get request parameters. |
| 91 |
//------------------------------------------------------------------------------ |
| 92 |
RequestParams& BackendRequest::getParameters() { |
| 93 |
return _params; |
| 94 |
} |
| 95 |
|
| 96 |
//------------------------------------------------------------------------------ |
| 97 |
// Configure request for S3. |
| 98 |
//------------------------------------------------------------------------------ |
| 99 |
void BackendRequest::configureS3params() { |
| 100 |
// strange workaround to get S3 compatibility on gcloud to work |
| 101 |
if(_params.getAwsRegion().empty()) { |
| 102 |
HeaderVec vec = _headers_field; |
| 103 |
S3::signRequest(_params, _request_type, *_current, vec); |
| 104 |
vec.swap(_headers_field); |
| 105 |
} |
| 106 |
else { |
| 107 |
Uri signed_url = S3::signURI(_params, _request_type, *_current, _headers_field, DEFAULT_REQUEST_SIGNING_DURATION); |
| 108 |
_current= std::shared_ptr<Uri>(new Uri(signed_url)); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
//------------------------------------------------------------------------------ |
| 113 |
// Configure request for Azure. |
| 114 |
//------------------------------------------------------------------------------ |
| 115 |
void BackendRequest::configureAzureParams() { |
| 116 |
Uri signed_url = Azure::signURI(_params.getAzureKey(), _request_type, *_current, DEFAULT_REQUEST_SIGNING_DURATION); |
| 117 |
_current.reset(new Uri(signed_url)); |
| 118 |
} |
| 119 |
|
| 120 |
//------------------------------------------------------------------------------ |
| 121 |
// Configure request for Gcloud. |
| 122 |
//------------------------------------------------------------------------------ |
| 123 |
void BackendRequest::configureGcloudParams() { |
| 124 |
Uri signed_url = gcloud::signURI(_params.getGcloudCredentials(), _request_type, *_current, _headers_field, DEFAULT_REQUEST_SIGNING_DURATION); |
| 125 |
_current.reset(new Uri(signed_url)); |
| 126 |
} |
| 127 |
|
| 128 |
//------------------------------------------------------------------------------ |
| 129 |
// Configure request for Swift. |
| 130 |
//------------------------------------------------------------------------------ |
| 131 |
void BackendRequest::configureSwiftParams() { |
| 132 |
_headers_field.emplace_back("X-Auth-Token", _params.getOSToken()); |
| 133 |
Uri signed_url = Swift::signURI(_params, *_current); |
| 134 |
_current.reset(new Uri(signed_url)); |
| 135 |
} |
| 136 |
|
| 137 |
//------------------------------------------------------------------------------ |
| 138 |
// Set-up deadline, but only if uninitialized |
| 139 |
//------------------------------------------------------------------------------ |
| 140 |
void BackendRequest::setupDeadlineIfUnset() { |
| 141 |
if(_deadline.isValid() == false && _params.getOperationTimeout()->tv_sec != 0){ |
| 142 |
using namespace Chrono; |
| 143 |
_deadline = Clock(Clock::Monolitic).now() + Duration(_params.getOperationTimeout()->tv_sec); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
//------------------------------------------------------------------------------ |
| 148 |
// Check if deadline has already passed |
| 149 |
//------------------------------------------------------------------------------ |
| 150 |
bool BackendRequest::checkTimeout(DavixError **err){ |
| 151 |
if(_deadline.isValid() && _deadline < Chrono::Clock(Chrono::Clock::Monolitic).now()) { |
| 152 |
std::ostringstream ss; |
| 153 |
ss << "timeout of " << _params.getOperationTimeout()->tv_sec << "s"; |
| 154 |
DavixError::setupError(err, davix_scope_http_request(), StatusCode::OperationTimeout, ss.str()); |
| 155 |
return true; |
| 156 |
} |
| 157 |
|
| 158 |
return false; |
| 159 |
} |
| 160 |
|
| 161 |
//------------------------------------------------------------------------------ |
| 162 |
// Get answer size from headers. |
| 163 |
//------------------------------------------------------------------------------ |
| 164 |
dav_ssize_t BackendRequest::getAnswerSizeFromHeaders() const { |
| 165 |
std::string str_file_size=""; |
| 166 |
|
| 167 |
long size=-1; |
| 168 |
if(getAnswerHeader(ans_header_content_length, str_file_size)) { |
| 169 |
StrUtil::trim(str_file_size); |
| 170 |
try{ |
| 171 |
size = toType<long, std::string>()(str_file_size); |
| 172 |
} catch(...){ |
| 173 |
size = -1; |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
if(size == -1){ |
| 178 |
DAVIX_SLOG(DAVIX_LOG_TRACE, DAVIX_LOG_HTTP, "Bad server answer: {} Invalid, impossible to determine answer size", ans_header_content_length); |
| 179 |
} |
| 180 |
|
| 181 |
return static_cast<dav_ssize_t>(size); |
| 182 |
} |
| 183 |
|
| 184 |
//------------------------------------------------------------------------------ |
| 185 |
// Get this requests' context. |
| 186 |
//------------------------------------------------------------------------------ |
| 187 |
Context& BackendRequest::getContext() const { |
| 188 |
return _context; |
| 189 |
} |
| 190 |
|
| 191 |
//------------------------------------------------------------------------------ |
| 192 |
// Get original URL, before any redirections |
| 193 |
//------------------------------------------------------------------------------ |
| 194 |
std::shared_ptr<Uri> BackendRequest::getOriginalUri() const { |
| 195 |
return _orig; |
| 196 |
} |
| 197 |
|
| 198 |
//------------------------------------------------------------------------------ |
| 199 |
// Helper read members - implemented in terms of readBlock, and an internal |
| 200 |
// buffer. |
| 201 |
//------------------------------------------------------------------------------ |
| 202 |
dav_ssize_t BackendRequest::readSegment(char* p_buff, dav_size_t size_read, bool stop_at_line_boundary, DavixError**err) { |
| 203 |
DavixError* tmp_err=NULL; |
| 204 |
dav_ssize_t ret, tmp_ret; |
| 205 |
dav_size_t s_read= size_read; |
| 206 |
ret = tmp_ret = 0; |
| 207 |
DAVIX_SLOG(DAVIX_LOG_TRACE, DAVIX_LOG_HTTP, "Davix::BackendRequest::readSegment: want to read {} bytes ", size_read); |
| 208 |
bool early_stop = false; |
| 209 |
|
| 210 |
do { |
| 211 |
tmp_ret= readBlock(p_buff, s_read, &tmp_err); |
| 212 |
|
| 213 |
if(tmp_ret > 0 && stop_at_line_boundary && std::find(p_buff, p_buff+tmp_ret, '\n') != p_buff+tmp_ret) { |
| 214 |
early_stop = true; |
| 215 |
} |
| 216 |
|
| 217 |
if(tmp_ret > 0){ // tmp_ret bytes read |
| 218 |
ret += tmp_ret; |
| 219 |
} |
| 220 |
|
| 221 |
if(ret > 0 && ret < (dav_ssize_t) size_read){ |
| 222 |
p_buff+= tmp_ret; |
| 223 |
s_read -= tmp_ret; |
| 224 |
} |
| 225 |
|
| 226 |
} while( !early_stop && tmp_ret > 0 && ret < (dav_ssize_t) size_read); |
| 227 |
|
| 228 |
if(tmp_err){ |
| 229 |
DavixError::propagateError(err, tmp_err); |
| 230 |
return -1; |
| 231 |
} |
| 232 |
|
| 233 |
return ret; |
| 234 |
} |
| 235 |
|
| 236 |
dav_ssize_t BackendRequest::readToFd(int fd, dav_size_t read_size, DavixError** err){ |
| 237 |
dav_ssize_t ret=1, total=0; |
| 238 |
dav_size_t chunk_size = DAVIX_BLOCK_SIZE; |
| 239 |
read_size = (read_size==0)?(std::numeric_limits<dav_size_t>::max()):read_size; |
| 240 |
std::vector<char> buffer(chunk_size); |
| 241 |
|
| 242 |
while( (ret = readBlock(&buffer[0], |
| 243 |
std::min<dav_size_t>(chunk_size,read_size), |
| 244 |
err)) >0 && ( read_size >0 )){ |
| 245 |
if(((dav_size_t)ret) == chunk_size && chunk_size < DAVIX_MAX_BLOCK_SIZE){ // increase buffer size |
| 246 |
chunk_size = std::min<dav_size_t>(chunk_size << 1, DAVIX_MAX_BLOCK_SIZE); |
| 247 |
buffer.resize(chunk_size); |
| 248 |
} |
| 249 |
|
| 250 |
dav_ssize_t write_len = ret; |
| 251 |
read_size -= ret; |
| 252 |
total += ret; |
| 253 |
|
| 254 |
do { |
| 255 |
ret = write(fd, &buffer[0], write_len); |
| 256 |
|
| 257 |
if (ret == -1 && errno == EINTR) { |
| 258 |
continue; |
| 259 |
} else if (ret < 0) { |
| 260 |
DavixError::setupError(err, davix_scope_http_request(), |
| 261 |
StatusCode::SystemError, std::string("Impossible to write to fd").append(strerror(errno))); |
| 262 |
return -1; |
| 263 |
} else { |
| 264 |
write_len -= ret; |
| 265 |
} |
| 266 |
} while(write_len >0); |
| 267 |
} |
| 268 |
|
| 269 |
if(total > 0) return total; |
| 270 |
return ret; |
| 271 |
} |
| 272 |
|
| 273 |
dav_ssize_t BackendRequest::readLine(char* buffer, dav_size_t max_size, DavixError** err){ |
| 274 |
dav_ssize_t ret=-1; |
| 275 |
|
| 276 |
if( _vec_line.size() > 0){ |
| 277 |
std::vector<char>::iterator it; |
| 278 |
it = std::find(_vec_line.begin(), _vec_line.end(), '\n'); |
| 279 |
|
| 280 |
if( it != _vec_line.end()){ |
| 281 |
it++; |
| 282 |
const dav_ssize_t read_size = it - _vec_line.begin(); |
| 283 |
std::copy(_vec_line.begin(), it, buffer); |
| 284 |
_vec_line.erase(_vec_line.begin(), it); |
| 285 |
return read_size; |
| 286 |
} |
| 287 |
|
| 288 |
std::copy(_vec_line.begin(), _vec_line.end(), buffer); |
| 289 |
const dav_ssize_t n_bytes = _vec_line.size(); |
| 290 |
_vec_line.clear(); |
| 291 |
ret = readLine(buffer + n_bytes, max_size - n_bytes, err); |
| 292 |
return (ret >= 0)?(ret + n_bytes):-1; |
| 293 |
} |
| 294 |
|
| 295 |
if( ( ret = readSegment(buffer, max_size, true, err)) >= 0){ |
| 296 |
// search for crlf |
| 297 |
char* p_endline; |
| 298 |
p_endline = std::find(buffer, buffer+ret, '\n'); |
| 299 |
if( p_endline < buffer+ret) p_endline++; |
| 300 |
_vec_line.reserve(ret - ( p_endline - buffer )); |
| 301 |
std::copy(p_endline, buffer + ret, std::back_inserter(_vec_line)); |
| 302 |
*p_endline = '\0'; |
| 303 |
return p_endline - buffer; |
| 304 |
} |
| 305 |
|
| 306 |
return -1; |
| 307 |
} |
| 308 |
|
| 309 |
//------------------------------------------------------------------------------ |
| 310 |
// Access response buffer. |
| 311 |
//------------------------------------------------------------------------------ |
| 312 |
const char* BackendRequest::getAnswerContent() { |
| 313 |
if(_vec.size() != 0) { |
| 314 |
return (const char*) &(_vec.at(0)); |
| 315 |
} |
| 316 |
|
| 317 |
return NULL; |
| 318 |
} |
| 319 |
|
| 320 |
std::vector<char>& BackendRequest::getAnswerContentVec() { |
| 321 |
return _vec; |
| 322 |
} |
| 323 |
|
| 324 |
//------------------------------------------------------------------------------ |
| 325 |
// Clear response buffer. |
| 326 |
//------------------------------------------------------------------------------ |
| 327 |
void BackendRequest::clearAnswerContent() { |
| 328 |
_vec.clear(); |
| 329 |
} |
| 330 |
|
| 331 |
//------------------------------------------------------------------------------ |
| 332 |
// Parse "Last-Modified" response header, return as time_t. |
| 333 |
//------------------------------------------------------------------------------ |
| 334 |
time_t BackendRequest::getLastModified() const { |
| 335 |
time_t t=0; |
| 336 |
|
| 337 |
std::string str_lastmodified; |
| 338 |
if(getAnswerHeader("Last-Modified", str_lastmodified)) { |
| 339 |
StrUtil::trim(str_lastmodified); |
| 340 |
try{ |
| 341 |
t = S3::s3TimeConverter(str_lastmodified); |
| 342 |
} catch(...){ |
| 343 |
str_lastmodified.clear(); |
| 344 |
} |
| 345 |
} |
| 346 |
|
| 347 |
if(str_lastmodified.empty()){ |
| 348 |
DAVIX_SLOG(DAVIX_LOG_TRACE, DAVIX_LOG_HTTP, "Bad server answer: {} Invalid, impossible to determine last modified time"); |
| 349 |
} |
| 350 |
|
| 351 |
return t; |
| 352 |
} |
| 353 |
|
| 354 |
//------------------------------------------------------------------------------ |
| 355 |
// Get answer size |
| 356 |
//------------------------------------------------------------------------------ |
| 357 |
dav_ssize_t BackendRequest::getAnswerSize() const { |
| 358 |
if(_ans_size < 0) { |
| 359 |
_ans_size = getAnswerSizeFromHeaders(); |
| 360 |
} |
| 361 |
|
| 362 |
return _ans_size; |
| 363 |
} |
| 364 |
|
| 365 |
|
| 366 |
} |