Skip to content

Commit 33e2f9c

Browse files
committed
[curl] Add SetCurlOption helper to reduce repetition of curl_easy_setopt calls
1 parent d2ef70b commit 33e2f9c

1 file changed

Lines changed: 36 additions & 58 deletions

File tree

net/curl/src/RCurlConnection.cxx

Lines changed: 36 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,15 @@ void ReverseDisplacements(std::vector<std::size_t> &displacements, ROOT::Interna
552552
}
553553
}
554554

555+
/// Wrapper around curl_easy_setopt that asserts on failure. Most option-setting calls in this
556+
/// file use valid options and values by construction, so failure indicates a programming error.
557+
template <typename T>
558+
void SetCurlOption(void *handle, CURLoption option, T value)
559+
{
560+
auto rc = curl_easy_setopt(handle, option, value);
561+
R__ASSERT(rc == CURLE_OK);
562+
}
563+
555564
std::string GetCurlErrorString(CURLcode code)
556565
{
557566
return std::string(curl_easy_strerror(code)) + " (" + std::to_string(code) + ")";
@@ -631,63 +640,41 @@ void ROOT::Internal::RCurlConnection::SetupErrorBuffer()
631640
{
632641
if (!fErrorBuffer)
633642
fErrorBuffer = std::make_unique<char[]>(CURL_ERROR_SIZE);
634-
auto rc = curl_easy_setopt(fHandle, CURLOPT_ERRORBUFFER, fErrorBuffer.get());
635-
R__ASSERT(rc == CURLE_OK);
643+
SetCurlOption(fHandle, CURLOPT_ERRORBUFFER, fErrorBuffer.get());
636644
}
637645

638646
void ROOT::Internal::RCurlConnection::SetOptions()
639647
{
640-
int rc;
641-
642648
if (gDebug) {
643-
rc = curl_easy_setopt(fHandle, CURLOPT_VERBOSE, 1);
644-
R__ASSERT(rc == CURLE_OK);
645-
rc = curl_easy_setopt(fHandle, CURLOPT_DEBUGFUNCTION, CallbackDebug);
646-
R__ASSERT(rc == CURLE_OK);
649+
SetCurlOption(fHandle, CURLOPT_VERBOSE, 1);
650+
SetCurlOption(fHandle, CURLOPT_DEBUGFUNCTION, CallbackDebug);
647651
} else {
648-
rc = curl_easy_setopt(fHandle, CURLOPT_VERBOSE, 0);
649-
R__ASSERT(rc == CURLE_OK);
652+
SetCurlOption(fHandle, CURLOPT_VERBOSE, 0);
650653
}
651654

652655
static const std::string kUserAgent = GetUserAgentString();
653-
rc = curl_easy_setopt(fHandle, CURLOPT_USERAGENT, kUserAgent.c_str());
654-
R__ASSERT(rc == CURLE_OK);
655-
656-
rc = curl_easy_setopt(fHandle, CURLOPT_FOLLOWLOCATION, 1);
657-
R__ASSERT(rc == CURLE_OK);
658-
659-
rc = curl_easy_setopt(fHandle, CURLOPT_WRITEFUNCTION, CallbackData);
660-
R__ASSERT(rc == CURLE_OK);
656+
SetCurlOption(fHandle, CURLOPT_USERAGENT, kUserAgent.c_str());
657+
SetCurlOption(fHandle, CURLOPT_FOLLOWLOCATION, 1);
658+
SetCurlOption(fHandle, CURLOPT_WRITEFUNCTION, CallbackData);
661659
}
662660

663661
/// Reset method-specific sticky curl options so that the easy handle is in a clean state
664662
/// before configuring it for the next request (HEAD, GET, or PUT).
665663
void ROOT::Internal::RCurlConnection::ResetHandle()
666664
{
667-
auto rc = curl_easy_setopt(fHandle, CURLOPT_NOBODY, 0L);
668-
R__ASSERT(rc == CURLE_OK);
669-
rc = curl_easy_setopt(fHandle, CURLOPT_HTTPGET, 0L);
670-
R__ASSERT(rc == CURLE_OK);
671-
rc = curl_easy_setopt(fHandle, CURLOPT_UPLOAD, 0L);
672-
R__ASSERT(rc == CURLE_OK);
673-
rc = curl_easy_setopt(fHandle, CURLOPT_RANGE, NULL);
674-
R__ASSERT(rc == CURLE_OK);
675-
rc = curl_easy_setopt(fHandle, CURLOPT_READFUNCTION, NULL);
676-
R__ASSERT(rc == CURLE_OK);
677-
rc = curl_easy_setopt(fHandle, CURLOPT_READDATA, NULL);
678-
R__ASSERT(rc == CURLE_OK);
679-
rc = curl_easy_setopt(fHandle, CURLOPT_SEEKFUNCTION, NULL);
680-
R__ASSERT(rc == CURLE_OK);
681-
rc = curl_easy_setopt(fHandle, CURLOPT_SEEKDATA, NULL);
682-
R__ASSERT(rc == CURLE_OK);
683-
rc = curl_easy_setopt(fHandle, CURLOPT_INFILESIZE_LARGE, static_cast<curl_off_t>(-1));
684-
R__ASSERT(rc == CURLE_OK);
665+
SetCurlOption(fHandle, CURLOPT_NOBODY, 0L);
666+
SetCurlOption(fHandle, CURLOPT_HTTPGET, 0L);
667+
SetCurlOption(fHandle, CURLOPT_UPLOAD, 0L);
668+
SetCurlOption(fHandle, CURLOPT_RANGE, static_cast<const char *>(nullptr));
669+
SetCurlOption(fHandle, CURLOPT_READFUNCTION, static_cast<curl_read_callback>(nullptr));
670+
SetCurlOption(fHandle, CURLOPT_READDATA, static_cast<void *>(nullptr));
671+
SetCurlOption(fHandle, CURLOPT_SEEKFUNCTION, static_cast<curl_seek_callback>(nullptr));
672+
SetCurlOption(fHandle, CURLOPT_SEEKDATA, static_cast<void *>(nullptr));
673+
SetCurlOption(fHandle, CURLOPT_INFILESIZE_LARGE, static_cast<curl_off_t>(-1));
685674

686675
#ifndef HAS_CURL_EASY_HEADER
687-
rc = curl_easy_setopt(fHandle, CURLOPT_HEADERFUNCTION, NULL);
688-
R__ASSERT(rc == CURLE_OK);
689-
rc = curl_easy_setopt(fHandle, CURLOPT_HEADERDATA, NULL);
690-
R__ASSERT(rc == CURLE_OK);
676+
SetCurlOption(fHandle, CURLOPT_HEADERFUNCTION, static_cast<curl_write_callback>(nullptr));
677+
SetCurlOption(fHandle, CURLOPT_HEADERDATA, static_cast<void *>(nullptr));
691678
#endif
692679
}
693680

@@ -763,14 +750,13 @@ ROOT::Internal::RCurlConnection::RStatus ROOT::Internal::RCurlConnection::SendHe
763750
remoteSize = kUnknownSize;
764751

765752
ResetHandle();
766-
auto rc = curl_easy_setopt(fHandle, CURLOPT_NOBODY, 1);
767-
R__ASSERT(rc == CURLE_OK);
753+
SetCurlOption(fHandle, CURLOPT_NOBODY, 1);
768754

769755
RStatus status;
770756
Perform(status);
771757
if (status) {
772758
curl_off_t length = -1;
773-
rc = curl_easy_getinfo(fHandle, CURLINFO_CONTENT_LENGTH_DOWNLOAD_T, &length);
759+
auto rc = curl_easy_getinfo(fHandle, CURLINFO_CONTENT_LENGTH_DOWNLOAD_T, &length);
774760
if (rc == CURLE_OK && length >= 0)
775761
remoteSize = length;
776762
}
@@ -802,18 +788,14 @@ ROOT::Internal::RCurlConnection::SendRangesReq(std::size_t N, RUserRange *ranges
802788
}
803789

804790
ResetHandle();
805-
auto rc = curl_easy_setopt(fHandle, CURLOPT_HTTPGET, 1);
806-
R__ASSERT(rc == CURLE_OK);
791+
SetCurlOption(fHandle, CURLOPT_HTTPGET, 1);
807792

808793
RTransferState transfer(ranges, order, fHandle);
809-
rc = curl_easy_setopt(fHandle, CURLOPT_WRITEDATA, &transfer);
810-
R__ASSERT(rc == CURLE_OK);
794+
SetCurlOption(fHandle, CURLOPT_WRITEDATA, &transfer);
811795

812796
#ifndef HAS_CURL_EASY_HEADER
813-
rc = curl_easy_setopt(fHandle, CURLOPT_HEADERFUNCTION, CallbackHeader);
814-
R__ASSERT(rc == CURLE_OK);
815-
rc = curl_easy_setopt(fHandle, CURLOPT_HEADERDATA, &transfer);
816-
R__ASSERT(rc == CURLE_OK);
797+
SetCurlOption(fHandle, CURLOPT_HEADERFUNCTION, CallbackHeader);
798+
SetCurlOption(fHandle, CURLOPT_HEADERDATA, &transfer);
817799
#endif
818800

819801
RStatus status;
@@ -835,8 +817,7 @@ ROOT::Internal::RCurlConnection::SendRangesReq(std::size_t N, RUserRange *ranges
835817
for (std::size_t i = 1; i < nRanges; ++i) {
836818
rangeHeader += "," + requestRanges[b + i].ToString();
837819
}
838-
rc = curl_easy_setopt(fHandle, CURLOPT_RANGE, rangeHeader.c_str());
839-
R__ASSERT(rc == CURLE_OK);
820+
SetCurlOption(fHandle, CURLOPT_RANGE, rangeHeader.c_str());
840821

841822
if (b > 0) {
842823
const std::uint64_t lastByteRequested = requestRanges[b - 1].fLastByte;
@@ -900,13 +881,10 @@ void ROOT::Internal::RCurlConnection::ClearCredentials()
900881
if (!fCredentials)
901882
return;
902883

903-
CURLcode rc;
904884
switch (fCredentials->fType) {
905885
case EHTTPCredentialsType::kS3:
906-
rc = curl_easy_setopt(fHandle, CURLOPT_AWS_SIGV4, NULL);
907-
R__ASSERT(rc == CURLE_OK);
908-
rc = curl_easy_setopt(fHandle, CURLOPT_USERPWD, NULL);
909-
R__ASSERT(rc == CURLE_OK);
886+
SetCurlOption(fHandle, CURLOPT_AWS_SIGV4, static_cast<const char *>(nullptr));
887+
SetCurlOption(fHandle, CURLOPT_USERPWD, static_cast<const char *>(nullptr));
910888
break;
911889
default: R__ASSERT(false && "internal error: unknown credentials type");
912890
}

0 commit comments

Comments
 (0)