| 402 | } |
| 403 | |
| 404 | Status CurlHttpRequest::Send() { |
| 405 | CheckNotSent(); |
| 406 | CHECK(is_uri_set_) << "URI has not been set."; |
| 407 | |
| 408 | is_sent_ = true; |
| 409 | |
| 410 | if (curl_headers_) { |
| 411 | CHECK_CURL_OK( |
| 412 | libcurl_->curl_easy_setopt(curl_, CURLOPT_HTTPHEADER, curl_headers_)); |
| 413 | } |
| 414 | if (resolve_list_) { |
| 415 | CHECK_CURL_OK( |
| 416 | libcurl_->curl_easy_setopt(curl_, CURLOPT_RESOLVE, resolve_list_)); |
| 417 | } |
| 418 | CHECK_CURL_OK(libcurl_->curl_easy_setopt(curl_, CURLOPT_HEADERDATA, |
| 419 | reinterpret_cast<void*>(this))); |
| 420 | CHECK_CURL_OK(libcurl_->curl_easy_setopt(curl_, CURLOPT_HEADERFUNCTION, |
| 421 | &CurlHttpRequest::HeaderCallback)); |
| 422 | |
| 423 | CHECK_CURL_OK(libcurl_->curl_easy_setopt(curl_, CURLOPT_TIMEOUT, |
| 424 | request_timeout_secs_)); |
| 425 | CHECK_CURL_OK(libcurl_->curl_easy_setopt(curl_, CURLOPT_CONNECTTIMEOUT, |
| 426 | connect_timeout_secs_)); |
| 427 | |
| 428 | char error_buffer[CURL_ERROR_SIZE] = {0}; |
| 429 | CHECK_CURL_OK( |
| 430 | libcurl_->curl_easy_setopt(curl_, CURLOPT_ERRORBUFFER, error_buffer)); |
| 431 | |
| 432 | if (stats_ != nullptr) { |
| 433 | stats_->RecordRequest(this, uri_, method_); |
| 434 | } |
| 435 | |
| 436 | const CURLcode curl_result = libcurl_->curl_easy_perform(curl_); |
| 437 | TF_RETURN_IF_ERROR(CURLcodeToStatus(curl_result, error_buffer)); |
| 438 | |
| 439 | double written_size = 0; |
| 440 | CHECK_CURL_OK(libcurl_->curl_easy_getinfo(curl_, CURLINFO_SIZE_DOWNLOAD, |
| 441 | &written_size)); |
| 442 | |
| 443 | CHECK_CURL_OK(libcurl_->curl_easy_getinfo(curl_, CURLINFO_RESPONSE_CODE, |
| 444 | &response_code_)); |
| 445 | |
| 446 | auto get_error_message = [this]() -> string { |
| 447 | string error_message = strings::StrCat( |
| 448 | "Error executing an HTTP request: HTTP response code ", response_code_); |
| 449 | StringPiece body = GetResponse(); |
| 450 | if (!body.empty()) { |
| 451 | return strings::StrCat( |
| 452 | error_message, " with body '", |
| 453 | body.substr(0, std::min(body.size(), response_to_error_limit_)), "'"); |
| 454 | } |
| 455 | return error_message; |
| 456 | }; |
| 457 | |
| 458 | Status result; |
| 459 | switch (response_code_) { |
| 460 | // The group of response codes indicating that the request achieved |
| 461 | // the expected goal. |