diff options
author | Dimitry Andric <dim@FreeBSD.org> | 2022-07-03 14:10:23 +0000 |
---|---|---|
committer | Dimitry Andric <dim@FreeBSD.org> | 2022-07-03 14:10:23 +0000 |
commit | 145449b1e420787bb99721a429341fa6be3adfb6 (patch) | |
tree | 1d56ae694a6de602e348dd80165cf881a36600ed /llvm/lib/Debuginfod/HTTPClient.cpp | |
parent | ecbca9f5fb7d7613d2b94982c4825eb0d33d6842 (diff) |
Diffstat (limited to 'llvm/lib/Debuginfod/HTTPClient.cpp')
-rw-r--r-- | llvm/lib/Debuginfod/HTTPClient.cpp | 88 |
1 files changed, 13 insertions, 75 deletions
diff --git a/llvm/lib/Debuginfod/HTTPClient.cpp b/llvm/lib/Debuginfod/HTTPClient.cpp index 65f457933b92..3376eaa7cd0d 100644 --- a/llvm/lib/Debuginfod/HTTPClient.cpp +++ b/llvm/lib/Debuginfod/HTTPClient.cpp @@ -7,9 +7,8 @@ //===----------------------------------------------------------------------===// /// /// \file -/// -/// This file defines the methods of the HTTPRequest, HTTPClient, and -/// BufferedHTTPResponseHandler classes. +/// This file defines the implementation of the HTTPClient library for issuing +/// HTTP requests and handling the responses. /// //===----------------------------------------------------------------------===// @@ -34,44 +33,6 @@ bool operator==(const HTTPRequest &A, const HTTPRequest &B) { HTTPResponseHandler::~HTTPResponseHandler() = default; -static inline bool parseContentLengthHeader(StringRef LineRef, - size_t &ContentLength) { - // Content-Length is a mandatory header, and the only one we handle. - return LineRef.consume_front("Content-Length: ") && - to_integer(LineRef.trim(), ContentLength, 10); -} - -Error BufferedHTTPResponseHandler::handleHeaderLine(StringRef HeaderLine) { - if (ResponseBuffer.Body) - return Error::success(); - - size_t ContentLength; - if (parseContentLengthHeader(HeaderLine, ContentLength)) - ResponseBuffer.Body = - WritableMemoryBuffer::getNewUninitMemBuffer(ContentLength); - - return Error::success(); -} - -Error BufferedHTTPResponseHandler::handleBodyChunk(StringRef BodyChunk) { - if (!ResponseBuffer.Body) - return createStringError(errc::io_error, - "Unallocated response buffer. HTTP Body data " - "received before Content-Length header."); - if (Offset + BodyChunk.size() > ResponseBuffer.Body->getBufferSize()) - return createStringError(errc::io_error, - "Content size exceeds buffer size."); - memcpy(ResponseBuffer.Body->getBufferStart() + Offset, BodyChunk.data(), - BodyChunk.size()); - Offset += BodyChunk.size(); - return Error::success(); -} - -Error BufferedHTTPResponseHandler::handleStatusCode(unsigned Code) { - ResponseBuffer.Code = Code; - return Error::success(); -} - bool HTTPClient::IsInitialized = false; class HTTPClientCleanup { @@ -80,18 +41,6 @@ public: }; static const HTTPClientCleanup Cleanup; -Expected<HTTPResponseBuffer> HTTPClient::perform(const HTTPRequest &Request) { - BufferedHTTPResponseHandler Handler; - if (Error Err = perform(Request, Handler)) - return std::move(Err); - return std::move(Handler.ResponseBuffer); -} - -Expected<HTTPResponseBuffer> HTTPClient::get(StringRef Url) { - HTTPRequest Request(Url); - return perform(Request); -} - #ifdef LLVM_ENABLE_CURL bool HTTPClient::isAvailable() { return true; } @@ -128,18 +77,6 @@ struct CurlHTTPRequest { 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(std::move(Err)); - return 0; - } - return NMemb; -} - static size_t curlWriteFunction(char *Contents, size_t Size, size_t NMemb, CurlHTTPRequest *CurlRequest) { Size *= NMemb; @@ -156,10 +93,10 @@ HTTPClient::HTTPClient() { "Must call HTTPClient::initialize() at the beginning of main()."); if (Curl) return; - assert((Curl = curl_easy_init()) && "Curl could not be initialized."); + Curl = curl_easy_init(); + assert(Curl && "Curl could not be initialized"); // Set the callback hooks. curl_easy_setopt(Curl, CURLOPT_WRITEFUNCTION, curlWriteFunction); - curl_easy_setopt(Curl, CURLOPT_HEADERFUNCTION, curlHeaderFunction); } HTTPClient::~HTTPClient() { curl_easy_cleanup(Curl); } @@ -176,22 +113,19 @@ Error HTTPClient::perform(const HTTPRequest &Request, 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); + return std::move(CurlRequest.ErrorState); +} - unsigned Code; +unsigned HTTPClient::responseCode() { + long Code = 0; curl_easy_getinfo(Curl, CURLINFO_RESPONSE_CODE, &Code); - if (Error Err = Handler.handleStatusCode(Code)) - return joinErrors(std::move(CurlRequest.ErrorState), std::move(Err)); - - return std::move(CurlRequest.ErrorState); + return Code; } #else @@ -213,4 +147,8 @@ Error HTTPClient::perform(const HTTPRequest &Request, llvm_unreachable("No HTTP Client implementation available."); } +unsigned HTTPClient::responseCode() { + llvm_unreachable("No HTTP Client implementation available."); +} + #endif |