Index: llvm/CMakeLists.txt =================================================================== --- llvm/CMakeLists.txt +++ llvm/CMakeLists.txt @@ -383,6 +383,8 @@ set(LLVM_ENABLE_ZLIB "ON" CACHE STRING "Use zlib for compression/decompression if available. Can be ON, OFF, or FORCE_ON") +set(LLVM_ENABLE_CURL "OFF" CACHE STRING "Use libcurl HTTP client if available. Can be ON, OFF, or FORCE_ON") + set(LLVM_Z3_INSTALL_DIR "" CACHE STRING "Install directory of the Z3 solver.") option(LLVM_ENABLE_Z3_SOLVER Index: llvm/include/llvm/Support/HTTPClient.h =================================================================== --- llvm/include/llvm/Support/HTTPClient.h +++ llvm/include/llvm/Support/HTTPClient.h @@ -80,6 +80,12 @@ /// A reusable client for performing outbound HTTP requests. class HTTPClient { +#ifdef LLVM_ENABLE_CURL + void *Curl = nullptr; + Error curlInit(); + ~HTTPClient(); +#endif + public: /// Returns true only if LLVM has been compiled with a working HTTP client /// implementation Index: llvm/lib/Support/CMakeLists.txt =================================================================== --- llvm/lib/Support/CMakeLists.txt +++ llvm/lib/Support/CMakeLists.txt @@ -74,6 +74,11 @@ set(system_libs ${system_libs} ${Z3_LIBRARIES}) endif() +# Link LibCURL if the user wants it +if (LLVM_ENABLE_CURL) + set(system_libs ${system_libs} ${CURL_LIBRARIES}) +endif() + # Override the C runtime allocator on Windows and embed it into LLVM tools & libraries if(LLVM_INTEGRATED_CRT_ALLOC) if (CMAKE_BUILD_TYPE AND NOT ${LLVM_USE_CRT_${uppercase_CMAKE_BUILD_TYPE}} MATCHES "^(MT|MTd)$") Index: llvm/lib/Support/HTTPClient.cpp =================================================================== --- llvm/lib/Support/HTTPClient.cpp +++ llvm/lib/Support/HTTPClient.cpp @@ -19,6 +19,9 @@ #include "llvm/Support/Errc.h" #include "llvm/Support/Error.h" #include "llvm/Support/MemoryBuffer.h" +#ifdef LLVM_ENABLE_CURL +#include +#endif using namespace llvm; @@ -78,8 +81,93 @@ return Error::success(); } +#ifdef LLVM_ENABLE_CURL + +bool HTTPClient::isAvailable() { return true; } + +/// CurlHTTPRequest and the curl{HeaderLine,BodyChunk}Hook functions are +/// implementation details used to work with curl. (Curl makes callbacks +/// with a single customizable pointer parameter.) +struct CurlHTTPRequest { + CurlHTTPRequest(HTTPResponseHandler &Handler) : Handler(Handler) {} + void storeError(Error &Err) { + ErrorState = joinErrors(std::move(Err), std::move(ErrorState)); + } + HTTPResponseHandler &Handler; + llvm::Error ErrorState = Error::success(); +}; + +static size_t curlHeaderFunction(char *Contents, size_t Size, size_t NMemb, + CurlHTTPRequest *CurlRequest) { + assert(Size == 1 && "The Size passed by libCURL to CURLOPT_HEADERFUNCTION " + "should always be 1."); + if (Error Err = + CurlRequest->Handler.handleHeaderLine(StringRef(Contents, NMemb))) { + CurlRequest->storeError(Err); + return 0; + } + return NMemb; +} + +static size_t curlWriteFunction(char *Contents, size_t Size, size_t NMemb, + CurlHTTPRequest *CurlRequest) { + Size *= NMemb; + if (Error Err = + CurlRequest->Handler.handleBodyChunk(StringRef(Contents, Size))) { + CurlRequest->storeError(Err); + return 0; + } + return Size; +} + +Error HTTPClient::curlInit() { + if (Curl) + return Error::success(); + if (!(Curl = curl_easy_init())) + return createStringError(errc::io_error, "Error initializing curl."); + + // Set the callback hooks on first initialization. + curl_easy_setopt(Curl, CURLOPT_WRITEFUNCTION, curlWriteFunction); + curl_easy_setopt(Curl, CURLOPT_HEADERFUNCTION, curlHeaderFunction); + return Error::success(); +} + +HTTPClient::~HTTPClient() { curl_easy_cleanup(Curl); } + +Error HTTPClient::perform(const HTTPRequest &Request, + HTTPResponseHandler &Handler) { + if (Request.Method != HTTPMethod::GET) + return createStringError(errc::invalid_argument, + "Unsupported CURL request method."); + if (Error Err = curlInit()) + return Err; + + SmallString<128> Url = Request.Url; + curl_easy_setopt(Curl, CURLOPT_URL, Url.c_str()); + curl_easy_setopt(Curl, CURLOPT_FOLLOWLOCATION, Request.FollowRedirects); + + CurlHTTPRequest CurlRequest(Handler); + curl_easy_setopt(Curl, CURLOPT_WRITEDATA, &CurlRequest); + curl_easy_setopt(Curl, CURLOPT_HEADERDATA, &CurlRequest); + CURLcode CurlRes = curl_easy_perform(Curl); + if (CurlRes != CURLE_OK) + return joinErrors(std::move(CurlRequest.ErrorState), + createStringError(errc::io_error, + "curl_easy_perform() failed: %s\n", + curl_easy_strerror(CurlRes))); + if (CurlRequest.ErrorState) + return std::move(CurlRequest.ErrorState); + + unsigned Code; + curl_easy_getinfo(Curl, CURLINFO_RESPONSE_CODE, &Code); + if (Error Err = Handler.handleStatusCode(Code)) + return Err; + return std::move(CurlRequest.ErrorState); +} +#else bool HTTPClient::isAvailable() { return false; } Error HTTPClient::perform(const HTTPRequest &Request, HTTPResponseHandler &Handler) { llvm_unreachable("No HTTP Client implementation available."); } +#endif