Skip to content

Commit

Permalink
Use only explicit curl tupes for getinfo
Browse files Browse the repository at this point in the history
  • Loading branch information
Hind-M committed Mar 21, 2023
1 parent 8ff6ce0 commit 1bf1bff
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 28 deletions.
44 changes: 32 additions & 12 deletions libmamba/src/core/curl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
//
// The full license is in the file LICENSE, distributed with this software.

#include <iostream> // TODO remove

#include <spdlog/spdlog.h>

#include "curl.hpp"
Expand Down Expand Up @@ -246,27 +244,49 @@ namespace mamba
template <class T>
tl::expected<T, CURLcode> CURLHandle::getinfo(CURLINFO option)
{
// std::cout << "We are in getinfo: " << option << std::endl;
T val;
CURLcode result = curl_easy_getinfo(m_handle, option, &val);
// std::cout << "result is: " << result << " and val is: " << val << std::endl;
if (result != CURLE_OK)
{
// std::cout << "The result is not ok, returning unexpected" << std::endl;
return tl::unexpected(result);
}
return val;
}

// WARNING curl_easy_getinfo MUST have its third argument pointing to long, char*, curl_slist*
// or double
template tl::expected<long, CURLcode> CURLHandle::getinfo(CURLINFO option);
template tl::expected<char*, CURLcode> CURLHandle::getinfo(CURLINFO option);
// template tl::expected<double, CURLcode> CURLHandle::getinfo(CURLINFO option);
// template tl::expected<curl_slist*, CURLcode> CURLHandle::getinfo(CURLINFO option);
template tl::expected<long long, CURLcode> CURLHandle::getinfo(CURLINFO option);
// template tl::expected<unsigned long, CURLcode> CURLHandle::getinfo(CURLINFO option);
// template tl::expected<curl_off_t, CURLcode> CURLHandle::getinfo(CURLINFO option);
template tl::expected<int, CURLcode> CURLHandle::getinfo(CURLINFO option);
template tl::expected<std::size_t, CURLcode> CURLHandle::getinfo(CURLINFO option);
template tl::expected<double, CURLcode> CURLHandle::getinfo(CURLINFO option);
template tl::expected<curl_slist*, CURLcode> CURLHandle::getinfo(CURLINFO option);

template <>
tl::expected<std::size_t, CURLcode> CURLHandle::getinfo(CURLINFO option)
{
auto res = getinfo<long>(option);
if (res)
{
return static_cast<std::size_t>(res.value());
}
else
{
return tl::unexpected(res.error());
}
}

template <>
tl::expected<int, CURLcode> CURLHandle::getinfo(CURLINFO option)
{
auto res = getinfo<long>(option);
if (res)
{
return static_cast<int>(res.value());
}
else
{
return tl::unexpected(res.error());
}
}

template <>
tl::expected<std::string, CURLcode> CURLHandle::getinfo(CURLINFO option)
Expand Down
21 changes: 5 additions & 16 deletions libmamba/src/core/fetch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
//
// The full license is in the file LICENSE, distributed with this software.

#include <iostream> // TODO remove
#include <string_view>

#include <spdlog/spdlog.h>
Expand Down Expand Up @@ -624,7 +623,7 @@ namespace mamba
result = r;
if (r != CURLE_OK)
{
auto leffective_url = m_curl_handle->getinfo<char*>(CURLINFO_EFFECTIVE_URL).value(); // value_or("")
auto leffective_url = m_curl_handle->getinfo<char*>(CURLINFO_EFFECTIVE_URL).value();

std::stringstream err;
err << "Download error (" << result << ") " << curl_easy_strerror(result) << " ["
Expand Down Expand Up @@ -654,16 +653,9 @@ namespace mamba
bool DownloadTarget::finalize()
{
avg_speed = get_speed();
// std::cout << "GETTING http_status..." << std::endl;
http_status = m_curl_handle->getinfo<long>(CURLINFO_RESPONSE_CODE).value_or(10000);
// effective_url =
// m_curl_handle->getinfo<decltype(effective_url)>(CURLINFO_EFFECTIVE_URL).value();
// // value_or("") downloaded_size =
// m_curl_handle->getinfo<decltype(downloaded_size)>(CURLINFO_SIZE_DOWNLOAD_T)
// .value(); // value_or(0)
// curl_easy_getinfo(m_curl_handle->handle(), CURLINFO_RESPONSE_CODE, &http_status);
curl_easy_getinfo(m_curl_handle->handle(), CURLINFO_EFFECTIVE_URL, &effective_url);
curl_easy_getinfo(m_curl_handle->handle(), CURLINFO_SIZE_DOWNLOAD_T, &downloaded_size);
http_status = m_curl_handle->getinfo<int>(CURLINFO_RESPONSE_CODE).value_or(10000);
effective_url = m_curl_handle->getinfo<char*>(CURLINFO_EFFECTIVE_URL).value();
downloaded_size = m_curl_handle->getinfo<long>(CURLINFO_SIZE_DOWNLOAD_T).value_or(0);

LOG_INFO << get_transfer_msg();

Expand All @@ -672,10 +664,7 @@ namespace mamba
// this request didn't work!

// respect Retry-After header if present, otherwise use default timeout
// m_retry_wait_seconds = m_curl_handle
// ->getinfo<decltype(m_retry_wait_seconds)>(CURLINFO_RETRY_AFTER)
// .value(); // value_or(0)
curl_easy_getinfo(m_curl_handle->handle(), CURLINFO_RETRY_AFTER, &m_retry_wait_seconds);
m_retry_wait_seconds = m_curl_handle->getinfo<std::size_t>(CURLINFO_RETRY_AFTER).value_or(0);
if (!m_retry_wait_seconds)
{
m_retry_wait_seconds = get_default_retry_timeout();
Expand Down

0 comments on commit 1bf1bff

Please sign in to comment.