| 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 "StandaloneCurlRequest.hpp" |
| 23 |
#include "CurlSessionFactory.hpp" |
| 24 |
#include "CurlSession.hpp" |
| 25 |
#include "HeaderlineParser.hpp" |
| 26 |
#include <utils/davix_logger_internal.hpp> |
| 27 |
#include <utils/stringutils.hpp> |
| 28 |
#include <core/ContentProvider.hpp> |
| 29 |
#include <curl/curl.h> |
| 30 |
#include <auth/davixx509cred_internal.hpp> |
| 31 |
|
| 32 |
#define SSTR(message) static_cast<std::ostringstream&>(std::ostringstream().flush() << message).str() |
| 33 |
#define DBG(message) std::cerr << __FILE__ << ":" << __LINE__ << " -- " << #message << " = " << message << std::endl; |
| 34 |
|
| 35 |
namespace Davix { |
| 36 |
|
| 37 |
static std::vector<std::string> split(std::string data, std::string token) { |
| 38 |
std::vector<std::string> output; |
| 39 |
size_t pos = std::string::npos; |
| 40 |
do { |
| 41 |
pos = data.find(token); |
| 42 |
output.push_back(data.substr(0, pos)); |
| 43 |
if(std::string::npos != pos) data = data.substr(pos + token.size()); |
| 44 |
} while (std::string::npos != pos); |
| 45 |
return output; |
| 46 |
} |
| 47 |
|
| 48 |
static std::string chopNewline(const std::string &line) { |
| 49 |
if(line.size() > 2 && line[line.size()-2] == '\r' && line[line.size()-1] == '\n') { |
| 50 |
return std::string(line.begin(), line.begin()+line.size()-2); |
| 51 |
} |
| 52 |
|
| 53 |
if(line.size() > 1 && line[line.size()-1] == '\n') { |
| 54 |
return std::string(line.begin(), line.begin()+line.size()-1); |
| 55 |
} |
| 56 |
|
| 57 |
return line; |
| 58 |
} |
| 59 |
|
| 60 |
static std::string filterAuthorizationHeader(const std::string& line) { |
| 61 |
size_t pos = StrUtil::findInsensitive(line, "Authorization: "); |
| 62 |
|
| 63 |
if (pos == std::string::npos) { |
| 64 |
return line; |
| 65 |
} |
| 66 |
|
| 67 |
std::string filtered(line); |
| 68 |
bool formatted = false; |
| 69 |
|
| 70 |
if (getenv("DAVIX_FORMAT_BEARER_TOKEN") != NULL) { |
| 71 |
std::string content = line.substr(pos + 15); |
| 72 |
|
| 73 |
// Format into "<header>: Bearer abcdefg...tuvwxyz" |
| 74 |
if ((content.find("Bearer ") == 0) && (content.size() > 100)) { |
| 75 |
filtered = line.substr(0, pos + 15) + "Bearer " + content.substr(7, 7) + "..." + content.substr(content.size() - 7); |
| 76 |
formatted = true; |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
for (size_t i = pos + 15 ; i < filtered.length() && !formatted; i++) { |
| 81 |
filtered[i] = 'x'; |
| 82 |
} |
| 83 |
|
| 84 |
return filtered; |
| 85 |
} |
| 86 |
|
| 87 |
static void logHeaderLines(const std::vector<std::string>& lines, char arrow) { |
| 88 |
for (size_t i = 0; i < lines.size(); i++) { |
| 89 |
std::string line = chopNewline(lines[i]); |
| 90 |
|
| 91 |
if (!line.empty()) { |
| 92 |
if (!(::Davix::getLogScope() & DAVIX_LOG_SENSITIVE)) { |
| 93 |
line = filterAuthorizationHeader(line); |
| 94 |
} |
| 95 |
|
| 96 |
DAVIX_SLOG(DAVIX_LOG_WARNING, DAVIX_LOG_HEADER,"{} {}", arrow, line); |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
//------------------------------------------------------------------------------ |
| 102 |
// Debug callback |
| 103 |
//------------------------------------------------------------------------------ |
| 104 |
int debug_callback(CURL *handle, curl_infotype type, char *data, size_t size, void *userptr) { |
| 105 |
static bool prevHeaderOut = false; |
| 106 |
static bool prevHeaderIn = false; |
| 107 |
|
| 108 |
switch(type) { |
| 109 |
case CURLINFO_HEADER_IN: { |
| 110 |
prevHeaderOut = false; |
| 111 |
|
| 112 |
if(!prevHeaderIn) { |
| 113 |
DAVIX_SLOG(DAVIX_LOG_WARNING, DAVIX_LOG_HEADER, ""); |
| 114 |
prevHeaderIn = true; |
| 115 |
} |
| 116 |
|
| 117 |
if(::Davix::getLogScope() & DAVIX_LOG_HEADER) { |
| 118 |
std::vector<std::string> lines = split(std::string(data, size), "\r\n"); |
| 119 |
logHeaderLines(lines, '<'); |
| 120 |
} |
| 121 |
|
| 122 |
break; |
| 123 |
} |
| 124 |
case CURLINFO_HEADER_OUT: { |
| 125 |
prevHeaderIn = false; |
| 126 |
|
| 127 |
if(!prevHeaderOut) { |
| 128 |
DAVIX_SLOG(DAVIX_LOG_WARNING, DAVIX_LOG_HEADER, ""); |
| 129 |
prevHeaderOut = true; |
| 130 |
} |
| 131 |
|
| 132 |
if(::Davix::getLogScope() & DAVIX_LOG_HEADER) { |
| 133 |
std::vector<std::string> lines = split(std::string(data, size), "\r\n"); |
| 134 |
logHeaderLines(lines, '>'); |
| 135 |
} |
| 136 |
|
| 137 |
break; |
| 138 |
} |
| 139 |
case CURLINFO_DATA_IN: { |
| 140 |
DAVIX_SLOG(DAVIX_LOG_WARNING, DAVIX_LOG_BODY, "Body block incoming ({} bytes): {}", size, std::string(data, size)); |
| 141 |
break; |
| 142 |
} |
| 143 |
case CURLINFO_DATA_OUT: { |
| 144 |
DAVIX_SLOG(DAVIX_LOG_WARNING, DAVIX_LOG_BODY, "Body block outgoing ({} bytes): {}", size, std::string(data, size)); |
| 145 |
break; |
| 146 |
} |
| 147 |
|
| 148 |
default: {} |
| 149 |
} |
| 150 |
|
| 151 |
return 0; |
| 152 |
} |
| 153 |
|
| 154 |
//------------------------------------------------------------------------------ |
| 155 |
// Header callback |
| 156 |
//------------------------------------------------------------------------------ |
| 157 |
static size_t header_callback(char *buffer, size_t size, size_t nitems, void *userdata) { |
| 158 |
size_t bytes = size * nitems; |
| 159 |
|
| 160 |
StandaloneCurlRequest* req = (StandaloneCurlRequest*) userdata; |
| 161 |
req->feedResponseHeader(std::string(buffer, bytes)); |
| 162 |
return bytes; |
| 163 |
} |
| 164 |
|
| 165 |
//------------------------------------------------------------------------------ |
| 166 |
// Write callback |
| 167 |
//------------------------------------------------------------------------------ |
| 168 |
static size_t write_callback(char *ptr, size_t size, size_t nmemb, void *userdata) { |
| 169 |
size_t bytes = size * nmemb; |
| 170 |
|
| 171 |
ResponseBuffer* buff = (ResponseBuffer*) userdata; |
| 172 |
buff->feed(ptr, bytes); |
| 173 |
return bytes; |
| 174 |
} |
| 175 |
|
| 176 |
//------------------------------------------------------------------------------ |
| 177 |
// Read callback |
| 178 |
//------------------------------------------------------------------------------ |
| 179 |
size_t read_callback(char *buffer, size_t size, size_t nitems, void *userdata) { |
| 180 |
size_t bytes = size * nitems; |
| 181 |
|
| 182 |
ContentProvider* provider = (ContentProvider*) userdata; |
| 183 |
ssize_t retval = provider->pullBytes(buffer, bytes); |
| 184 |
|
| 185 |
if(retval < 0) { |
| 186 |
DAVIX_SLOG(DAVIX_LOG_WARNING, DAVIX_LOG_HTTP, "Content provider reported an errc={}", retval); |
| 187 |
return 0; |
| 188 |
} |
| 189 |
|
| 190 |
return retval; |
| 191 |
} |
| 192 |
|
| 193 |
//------------------------------------------------------------------------------ |
| 194 |
// Constructor |
| 195 |
//------------------------------------------------------------------------------ |
| 196 |
StandaloneCurlRequest::StandaloneCurlRequest(CurlSessionFactory &sessionFactory, bool reuseSession, |
| 197 |
const BoundHooks &boundHooks, const Uri &uri, const std::string &verb, const RequestParams ¶ms, |
| 198 |
const std::vector<HeaderLine> &headers, int reqFlag, ContentProvider *contentProvider, |
| 199 |
Chrono::TimePoint deadline) |
| 200 |
: _session_factory(sessionFactory), _reuse_session(reuseSession), _bound_hooks(boundHooks), |
| 201 |
_uri(uri), _verb(verb), _params(params), _headers(headers), _req_flag(reqFlag), |
| 202 |
_content_provider(contentProvider), _deadline(deadline), _state(RequestState::kNotStarted), |
| 203 |
_chunklist(NULL), _received_headers(false) { |
| 204 |
name = "curl"; |
| 205 |
} |
| 206 |
|
| 207 |
//------------------------------------------------------------------------------ |
| 208 |
// Destructor |
| 209 |
//------------------------------------------------------------------------------ |
| 210 |
StandaloneCurlRequest::~StandaloneCurlRequest() { |
| 211 |
curl_slist_free_all(_chunklist); |
| 212 |
} |
| 213 |
|
| 214 |
//------------------------------------------------------------------------------ |
| 215 |
// Get a specific response header |
| 216 |
//------------------------------------------------------------------------------ |
| 217 |
bool StandaloneCurlRequest::getAnswerHeader(const std::string &header_name, std::string &value) const { |
| 218 |
auto transform_to_lower = [](const std::string& str) -> std::string { |
| 219 |
std::string lower(str); |
| 220 |
std::transform(lower.begin(), lower.end(), lower.begin(), ::tolower); |
| 221 |
return lower; |
| 222 |
}; |
| 223 |
|
| 224 |
auto header_lower = transform_to_lower(header_name); |
| 225 |
|
| 226 |
for (const auto& it: _response_headers) { |
| 227 |
if (header_lower == transform_to_lower(it.first)) { |
| 228 |
value = it.second; |
| 229 |
return true; |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
return false; |
| 234 |
} |
| 235 |
|
| 236 |
//------------------------------------------------------------------------------ |
| 237 |
// Get all response headers |
| 238 |
//------------------------------------------------------------------------------ |
| 239 |
size_t StandaloneCurlRequest::getAnswerHeaders(std::vector<std::pair<std::string, std::string > > & vec_headers) const { |
| 240 |
vec_headers = _response_headers; |
| 241 |
return vec_headers.size(); |
| 242 |
} |
| 243 |
|
| 244 |
//------------------------------------------------------------------------------ |
| 245 |
// Translate curl code into status |
| 246 |
//------------------------------------------------------------------------------ |
| 247 |
static Status curlCodeToStatus(CURLcode code) { |
| 248 |
std::ostringstream ss; |
| 249 |
if(code != CURLE_OK) { |
| 250 |
ss << "curl error (" << code << "): " << curl_easy_strerror(code); |
| 251 |
} |
| 252 |
|
| 253 |
switch(code) { |
| 254 |
case CURLE_OK: { |
| 255 |
return Status(); |
| 256 |
} |
| 257 |
case CURLE_COULDNT_RESOLVE_PROXY: |
| 258 |
case CURLE_COULDNT_RESOLVE_HOST: { |
| 259 |
return Status(davix_scope_http_request(), StatusCode::NameResolutionFailure, ss.str()); |
| 260 |
} |
| 261 |
case CURLE_LOGIN_DENIED: |
| 262 |
case CURLE_AUTH_ERROR: |
| 263 |
case CURLE_REMOTE_ACCESS_DENIED: { |
| 264 |
return Status(davix_scope_http_request(), StatusCode::AuthenticationError, ss.str()); |
| 265 |
} |
| 266 |
case CURLE_SSL_CONNECT_ERROR: |
| 267 |
case CURLE_SSL_ENGINE_NOTFOUND: |
| 268 |
case CURLE_SSL_ENGINE_SETFAILED: |
| 269 |
case CURLE_SSL_CERTPROBLEM: |
| 270 |
case CURLE_SSL_CIPHER: |
| 271 |
case CURLE_PEER_FAILED_VERIFICATION: |
| 272 |
case CURLE_SSL_SHUTDOWN_FAILED: |
| 273 |
case CURLE_SSL_CRL_BADFILE: |
| 274 |
case CURLE_SSL_ISSUER_ERROR: |
| 275 |
case CURLE_SSL_PINNEDPUBKEYNOTMATCH: |
| 276 |
case CURLE_SSL_INVALIDCERTSTATUS: { |
| 277 |
return Status(davix_scope_http_request(), StatusCode::SSLError, ss.str()); |
| 278 |
} |
| 279 |
case CURLE_SEND_ERROR: |
| 280 |
case CURLE_RECV_ERROR: |
| 281 |
case CURLE_COULDNT_CONNECT: |
| 282 |
case CURLE_GOT_NOTHING: { |
| 283 |
return Status(davix_scope_http_request(), StatusCode::ConnectionProblem, ss.str()); |
| 284 |
} |
| 285 |
case CURLE_OPERATION_TIMEDOUT: { |
| 286 |
return Status(davix_scope_http_request(), StatusCode::OperationTimeout, ss.str()); |
| 287 |
} |
| 288 |
default: { |
| 289 |
return Status(davix_scope_http_request(), StatusCode::UnknownError, ss.str()); |
| 290 |
} |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
//------------------------------------------------------------------------------ |
| 295 |
// Get curl version as string |
| 296 |
//------------------------------------------------------------------------------ |
| 297 |
std::string getCurlVersion() { |
| 298 |
curl_version_info_data* ver = curl_version_info(CURLVERSION_NOW); |
| 299 |
return ver->version; |
| 300 |
} |
| 301 |
|
| 302 |
//------------------------------------------------------------------------------ |
| 303 |
// Start request - calling this multiple times will do nothing. |
| 304 |
//------------------------------------------------------------------------------ |
| 305 |
Status StandaloneCurlRequest::startRequest() { |
| 306 |
if(_state != RequestState::kNotStarted) { |
| 307 |
return Status(); ; |
| 308 |
} |
| 309 |
|
| 310 |
//---------------------------------------------------------------------------- |
| 311 |
// Have we timed out already? |
| 312 |
//---------------------------------------------------------------------------- |
| 313 |
Status st = checkTimeout(); |
| 314 |
if(!st.ok()) { |
| 315 |
// markCompleted(); |
| 316 |
return st; |
| 317 |
} |
| 318 |
|
| 319 |
//---------------------------------------------------------------------------- |
| 320 |
// Retrieve a session, create request |
| 321 |
//---------------------------------------------------------------------------- |
| 322 |
_session = _session_factory.provideCurlSession(_uri, _params, st); |
| 323 |
if(!st.ok()) { |
| 324 |
// markCompleted(); |
| 325 |
return st; |
| 326 |
} |
| 327 |
|
| 328 |
//---------------------------------------------------------------------------- |
| 329 |
// Set request verb, target URL |
| 330 |
//---------------------------------------------------------------------------- |
| 331 |
CURL* handle = _session->getHandle()->handle; |
| 332 |
|
| 333 |
Uri uriCopy(_uri); |
| 334 |
uriCopy.httpizeProtocol(); |
| 335 |
|
| 336 |
curl_easy_setopt(handle, CURLOPT_CUSTOMREQUEST, _verb.c_str()); |
| 337 |
curl_easy_setopt(handle, CURLOPT_URL, uriCopy.getString().c_str()); |
| 338 |
|
| 339 |
//---------------------------------------------------------------------------- |
| 340 |
// Set up callback to consume response headers |
| 341 |
//---------------------------------------------------------------------------- |
| 342 |
curl_easy_setopt(handle, CURLOPT_HEADERFUNCTION, header_callback); |
| 343 |
curl_easy_setopt(handle, CURLOPT_HEADERDATA, this); |
| 344 |
|
| 345 |
//---------------------------------------------------------------------------- |
| 346 |
// Set up callback to consume response body |
| 347 |
//---------------------------------------------------------------------------- |
| 348 |
curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write_callback); |
| 349 |
curl_easy_setopt(handle, CURLOPT_WRITEDATA, &_response_buffer); |
| 350 |
|
| 351 |
//---------------------------------------------------------------------------- |
| 352 |
// Set up callback to provide request body |
| 353 |
//---------------------------------------------------------------------------- |
| 354 |
if(_content_provider) { |
| 355 |
_content_provider->rewind(); |
| 356 |
curl_easy_setopt(handle, CURLOPT_UPLOAD, 1L); |
| 357 |
curl_easy_setopt(handle, CURLOPT_READFUNCTION, read_callback); |
| 358 |
curl_easy_setopt(handle, CURLOPT_READDATA, _content_provider); |
| 359 |
curl_easy_setopt(handle, CURLOPT_INFILESIZE_LARGE, _content_provider->getSize()); |
| 360 |
|
| 361 |
} |
| 362 |
|
| 363 |
//---------------------------------------------------------------------------- |
| 364 |
// Set up CA store |
| 365 |
//---------------------------------------------------------------------------- |
| 366 |
if(_params.getSSLCACheck()) { |
| 367 |
for(auto it = _params.listCertificateAuthorityPath().begin(); it != _params.listCertificateAuthorityPath().end(); it++) { |
| 368 |
struct stat st; |
| 369 |
if (stat(it->c_str(), &st) < 0 || S_ISDIR(st.st_mode) == false) { |
| 370 |
DAVIX_SLOG(DAVIX_LOG_WARNING, DAVIX_LOG_HTTP, "CA Path invalid : {}, {} ", *it, ((errno != 0)?strerror(errno):strerror(ENOTDIR))); |
| 371 |
errno = 0; |
| 372 |
} else { |
| 373 |
DAVIX_SLOG(DAVIX_LOG_TRACE, DAVIX_LOG_HTTP, "add CA PATH {}", *it); |
| 374 |
curl_easy_setopt(handle, CURLOPT_CAPATH, it->c_str()); |
| 375 |
} |
| 376 |
} |
| 377 |
} |
| 378 |
else { |
| 379 |
curl_easy_setopt(handle, CURLOPT_SSL_VERIFYPEER, 0); |
| 380 |
curl_easy_setopt(handle, CURLOPT_SSL_VERIFYHOST, 0); |
| 381 |
} |
| 382 |
|
| 383 |
//---------------------------------------------------------------------------- |
| 384 |
// Set up client certificate |
| 385 |
//---------------------------------------------------------------------------- |
| 386 |
X509Credential clientCert = _params.getClientCertX509(); |
| 387 |
auto x509callback = _params.getClientCertCallbackX509(); |
| 388 |
|
| 389 |
if (x509callback.first) { |
| 390 |
DavixError* err = NULL; |
| 391 |
SessionInfo info; |
| 392 |
|
| 393 |
x509callback.first(x509callback.second, info, &clientCert, &err); |
| 394 |
|
| 395 |
if (err) { |
| 396 |
DAVIX_SLOG(DAVIX_LOG_WARNING, DAVIX_LOG_HTTP, "Failed to load certificate via callback: {}", err->getErrMsg()); |
| 397 |
return Status(davix_scope_http_request(), err->getStatus(), "Failed to load certificate"); |
| 398 |
} |
| 399 |
} |
| 400 |
|
| 401 |
std::string userCertificate; |
| 402 |
std::string userKey; |
| 403 |
std::string password; |
| 404 |
|
| 405 |
if(X509CredentialExtra::get_x509_info(clientCert, &userCertificate, &userKey, &password)) { |
| 406 |
curl_easy_setopt(handle, CURLOPT_SSLCERT, userCertificate.c_str()); |
| 407 |
curl_easy_setopt(handle, CURLOPT_SSLKEY, userKey.c_str()); |
| 408 |
} |
| 409 |
|
| 410 |
//---------------------------------------------------------------------------- |
| 411 |
// Set up headers |
| 412 |
//---------------------------------------------------------------------------- |
| 413 |
for(size_t i = 0; i < _headers.size(); i++) { |
| 414 |
_chunklist = curl_slist_append(_chunklist, SSTR(_headers[i].first << ": " << _headers[i].second).c_str()); |
| 415 |
|
| 416 |
if(_content_provider && _params.get100ContinueSupport() && (_req_flag & RequestFlag::SupportContinue100) ) { |
| 417 |
_chunklist = curl_slist_append(_chunklist, "Expect: 100-continue"); |
| 418 |
} |
| 419 |
else { |
| 420 |
_chunklist = curl_slist_append(_chunklist, "Expect:"); |
| 421 |
} |
| 422 |
} |
| 423 |
|
| 424 |
_chunklist = curl_slist_append(_chunklist, SSTR("User-Agent: " << Davix::RequestParams().getUserAgent() << " libcurl/" << getCurlVersion()).c_str()); |
| 425 |
|
| 426 |
curl_easy_setopt(handle, CURLOPT_HTTPHEADER, _chunklist); |
| 427 |
|
| 428 |
//---------------------------------------------------------------------------- |
| 429 |
// Special case for HEAD |
| 430 |
//---------------------------------------------------------------------------- |
| 431 |
if(strcasecmp(_verb.c_str(), "HEAD") == 0) { |
| 432 |
curl_easy_setopt(handle, CURLOPT_NOBODY, 1L); |
| 433 |
} |
| 434 |
|
| 435 |
//---------------------------------------------------------------------------- |
| 436 |
// Set up debugging |
| 437 |
//---------------------------------------------------------------------------- |
| 438 |
curl_easy_setopt(handle, CURLOPT_DEBUGFUNCTION, debug_callback); |
| 439 |
curl_easy_setopt(handle, CURLOPT_VERBOSE, 1L); |
| 440 |
|
| 441 |
//---------------------------------------------------------------------------- |
| 442 |
// Start request |
| 443 |
//---------------------------------------------------------------------------- |
| 444 |
_state = RequestState::kStarted; |
| 445 |
|
| 446 |
while(true) { |
| 447 |
int still_running = 1; |
| 448 |
Status st = performBlockingRound(still_running); |
| 449 |
|
| 450 |
if(!st.ok()) { |
| 451 |
_state = RequestState::kFinished; |
| 452 |
return st; |
| 453 |
} |
| 454 |
|
| 455 |
if(still_running == 0) { |
| 456 |
return checkErrors(); |
| 457 |
} |
| 458 |
|
| 459 |
if(_response_buffer.size() != 0u) { |
| 460 |
//------------------------------------------------------------------------ |
| 461 |
// We've dealt with the headers already, startRequest() is done. Switch |
| 462 |
// to readBlock() mode. |
| 463 |
//------------------------------------------------------------------------ |
| 464 |
_state = RequestState::kStarted; |
| 465 |
return checkErrors(); |
| 466 |
} |
| 467 |
} |
| 468 |
} |
| 469 |
|
| 470 |
//------------------------------------------------------------------------------ |
| 471 |
// Check internal mhandle errors |
| 472 |
//------------------------------------------------------------------------------ |
| 473 |
Status StandaloneCurlRequest::checkErrors() { |
| 474 |
CURLMsg *msg; |
| 475 |
int msgs_left = 0; |
| 476 |
while((msg = curl_multi_info_read(_session->getHandle()->mhandle, &msgs_left))) { |
| 477 |
if(msg->msg == CURLMSG_DONE && msg->data.result != CURLE_OK) { |
| 478 |
sessionError = curlCodeToStatus(msg->data.result); |
| 479 |
return sessionError; |
| 480 |
} |
| 481 |
} |
| 482 |
|
| 483 |
return Status(); |
| 484 |
} |
| 485 |
|
| 486 |
//------------------------------------------------------------------------------ |
| 487 |
// Get remaining number of milliseconds until deadline |
| 488 |
//------------------------------------------------------------------------------ |
| 489 |
uint64_t StandaloneCurlRequest::getRemainingMs() const { |
| 490 |
if(!_deadline.isValid()) { |
| 491 |
return std::numeric_limits<std::uint64_t>::max(); |
| 492 |
} |
| 493 |
|
| 494 |
Chrono::TimePoint now = Chrono::Clock(Chrono::Clock::Monolitic).now(); |
| 495 |
if(_deadline < now) { |
| 496 |
return 0; |
| 497 |
} |
| 498 |
|
| 499 |
return (_deadline - now).toMilliseconds(); |
| 500 |
} |
| 501 |
|
| 502 |
//------------------------------------------------------------------------------ |
| 503 |
// Perform a single blocking round of network I/O |
| 504 |
//------------------------------------------------------------------------------ |
| 505 |
Status StandaloneCurlRequest::performBlockingRound(int &still_running) { |
| 506 |
still_running = 0; |
| 507 |
|
| 508 |
if(!_session) { |
| 509 |
return Status(davix_scope_http_request(), StatusCode::InvalidArgument, "Request not active"); |
| 510 |
} |
| 511 |
|
| 512 |
while(true) { |
| 513 |
Status st = checkTimeout(); |
| 514 |
if(!st.ok()) { |
| 515 |
return st; |
| 516 |
} |
| 517 |
|
| 518 |
size_t prevSize = _response_buffer.size(); |
| 519 |
|
| 520 |
CURLM* mhandle = _session->getHandle()->mhandle; |
| 521 |
curl_multi_perform(mhandle, &still_running); |
| 522 |
|
| 523 |
//-------------------------------------------------------------------------- |
| 524 |
// Any errors from this round? |
| 525 |
//-------------------------------------------------------------------------- |
| 526 |
Status errs = checkErrors(); |
| 527 |
if(!errs.ok()) { |
| 528 |
return errs; |
| 529 |
} |
| 530 |
|
| 531 |
//-------------------------------------------------------------------------- |
| 532 |
// Are we done? |
| 533 |
//-------------------------------------------------------------------------- |
| 534 |
if(still_running == 0) { |
| 535 |
return Status(); |
| 536 |
} |
| 537 |
|
| 538 |
//-------------------------------------------------------------------------- |
| 539 |
// Was anything actually read? If so, our work here is done, we're |
| 540 |
// only supposed to do a single blocking round. |
| 541 |
//-------------------------------------------------------------------------- |
| 542 |
if(prevSize != _response_buffer.size()) { |
| 543 |
return Status(); |
| 544 |
} |
| 545 |
|
| 546 |
//-------------------------------------------------------------------------- |
| 547 |
// Nope, we made no progress during this round, retry |
| 548 |
//-------------------------------------------------------------------------- |
| 549 |
int numfds; |
| 550 |
curl_multi_poll(_session->getHandle()->mhandle, NULL, 0, getRemainingMs(), &numfds); |
| 551 |
} |
| 552 |
} |
| 553 |
|
| 554 |
//------------------------------------------------------------------------------ |
| 555 |
// Major read function - read a block of max_size bytes (at max) into buffer. |
| 556 |
//------------------------------------------------------------------------------ |
| 557 |
dav_ssize_t StandaloneCurlRequest::readBlock(char* buffer, dav_size_t max_size, Status& st) { |
| 558 |
if(!_session) { |
| 559 |
st = Status(davix_scope_http_request(), StatusCode::AlreadyRunning, "Request has not been started yet"); |
| 560 |
return -1; |
| 561 |
} |
| 562 |
|
| 563 |
if(max_size == 0) { |
| 564 |
return 0; |
| 565 |
} |
| 566 |
|
| 567 |
st = checkTimeout(); |
| 568 |
if(!st.ok()) { |
| 569 |
return -1; |
| 570 |
} |
| 571 |
|
| 572 |
//---------------------------------------------------------------------------- |
| 573 |
// Keep some data cached inside the response buffer, but not too much |
| 574 |
//---------------------------------------------------------------------------- |
| 575 |
if(_response_buffer.size() <= 33554432) { |
| 576 |
int still_running = 0; |
| 577 |
st = performBlockingRound(still_running); |
| 578 |
} |
| 579 |
|
| 580 |
return _response_buffer.consume(buffer, max_size); |
| 581 |
} |
| 582 |
|
| 583 |
//------------------------------------------------------------------------------ |
| 584 |
// Finish an already started request. |
| 585 |
//------------------------------------------------------------------------------ |
| 586 |
Status StandaloneCurlRequest::endRequest() { |
| 587 |
_state = RequestState::kFinished; |
| 588 |
return Status(); |
| 589 |
} |
| 590 |
|
| 591 |
//------------------------------------------------------------------------------ |
| 592 |
// Check request state |
| 593 |
//------------------------------------------------------------------------------ |
| 594 |
RequestState StandaloneCurlRequest::getState() const { |
| 595 |
return _state; |
| 596 |
} |
| 597 |
|
| 598 |
//------------------------------------------------------------------------------ |
| 599 |
// Check if timeout has passed |
| 600 |
//------------------------------------------------------------------------------ |
| 601 |
Status StandaloneCurlRequest::checkTimeout() { |
| 602 |
if(_deadline.isValid() && _deadline < Chrono::Clock(Chrono::Clock::Monolitic).now()) { |
| 603 |
std::ostringstream ss; |
| 604 |
ss << "timeout of " << _params.getOperationTimeout()->tv_sec << "s"; |
| 605 |
return Status(davix_scope_http_request(), StatusCode::OperationTimeout, ss.str()); |
| 606 |
} |
| 607 |
|
| 608 |
return Status(); |
| 609 |
} |
| 610 |
|
| 611 |
//------------------------------------------------------------------------------ |
| 612 |
// Get status code - returns 0 if impossible to determine |
| 613 |
//------------------------------------------------------------------------------ |
| 614 |
int StandaloneCurlRequest::getStatusCode() const { |
| 615 |
long response_code = 0; |
| 616 |
|
| 617 |
if(_session) { |
| 618 |
CURL* handle = _session->getHandle()->handle; |
| 619 |
curl_easy_getinfo(handle, CURLINFO_RESPONSE_CODE, &response_code); |
| 620 |
} |
| 621 |
|
| 622 |
return response_code; |
| 623 |
} |
| 624 |
|
| 625 |
//------------------------------------------------------------------------------ |
| 626 |
// Do not re-use underlying session |
| 627 |
//------------------------------------------------------------------------------ |
| 628 |
void StandaloneCurlRequest::doNotReuseSession() { |
| 629 |
|
| 630 |
} |
| 631 |
|
| 632 |
//------------------------------------------------------------------------------ |
| 633 |
// Has the underlying session been used before? |
| 634 |
//------------------------------------------------------------------------------ |
| 635 |
bool StandaloneCurlRequest::isRecycledSession() const { |
| 636 |
return false; |
| 637 |
} |
| 638 |
|
| 639 |
//------------------------------------------------------------------------------ |
| 640 |
// Obtain redirected location, store into the given Uri |
| 641 |
//------------------------------------------------------------------------------ |
| 642 |
Status StandaloneCurlRequest::obtainRedirectedLocation(Uri &out) { |
| 643 |
if(!_session) { |
| 644 |
return Status(davix_scope_http_request(), StatusCode::InvalidArgument, "Request not active, impossible to obtain redirected location"); |
| 645 |
} |
| 646 |
|
| 647 |
for(auto it = _response_headers.begin(); it != _response_headers.end(); it++) { |
| 648 |
if(strcasecmp("location", it->first.c_str()) == 0) { |
| 649 |
auto location = it->second; |
| 650 |
// Dealing with relative location [DMC-1209] |
| 651 |
if (!location.empty() && location[0] == '/') { |
| 652 |
out = Uri::fromRelativePath(_uri, location); |
| 653 |
} else { |
| 654 |
out = Uri(location); |
| 655 |
} |
| 656 |
if (out.getStatus() != StatusCode::OK) { |
| 657 |
return Status(davix_scope_http_request(), out.getStatus(), |
| 658 |
fmt::format("Failed to parse redirect location: {}", out.getString())); |
| 659 |
} |
| 660 |
return Status(); |
| 661 |
} |
| 662 |
} |
| 663 |
|
| 664 |
return Status(davix_scope_http_request(), StatusCode::InvalidArgument, "Could not find Location header in answer headers"); |
| 665 |
} |
| 666 |
|
| 667 |
//------------------------------------------------------------------------------ |
| 668 |
// Get session error, if available |
| 669 |
//------------------------------------------------------------------------------ |
| 670 |
std::string StandaloneCurlRequest::getSessionError() const { |
| 671 |
return sessionError.getErrorMessage(); |
| 672 |
} |
| 673 |
|
| 674 |
//------------------------------------------------------------------------------ |
| 675 |
// Block until all response headers have been received |
| 676 |
//------------------------------------------------------------------------------ |
| 677 |
Status StandaloneCurlRequest::readResponseHeaders() { |
| 678 |
return Status(); |
| 679 |
} |
| 680 |
|
| 681 |
//------------------------------------------------------------------------------ |
| 682 |
// Feed response header |
| 683 |
//------------------------------------------------------------------------------ |
| 684 |
void StandaloneCurlRequest::feedResponseHeader(const std::string &header) { |
| 685 |
if(header == "\r\n") { |
| 686 |
_received_headers = true; |
| 687 |
return; |
| 688 |
} |
| 689 |
|
| 690 |
HeaderlineParser parser(header); |
| 691 |
_response_headers.push_back(std::pair<std::string, std::string>(parser.getKey(), parser.getValue())); |
| 692 |
} |
| 693 |
|
| 694 |
|
| 695 |
} |