Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:46

0001 //===-- llvm/Support/HTTPClient.h - HTTP client library ---------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 ///
0009 /// \file
0010 /// This file contains the declarations of the HTTPClient library for issuing
0011 /// HTTP requests and handling the responses.
0012 ///
0013 //===----------------------------------------------------------------------===//
0014 
0015 #ifndef LLVM_DEBUGINFOD_HTTPCLIENT_H
0016 #define LLVM_DEBUGINFOD_HTTPCLIENT_H
0017 
0018 #include "llvm/ADT/SmallString.h"
0019 #include "llvm/ADT/SmallVector.h"
0020 #include "llvm/Support/Error.h"
0021 #include "llvm/Support/MemoryBuffer.h"
0022 
0023 #include <chrono>
0024 
0025 namespace llvm {
0026 
0027 enum class HTTPMethod { GET };
0028 
0029 /// A stateless description of an outbound HTTP request.
0030 struct HTTPRequest {
0031   SmallString<128> Url;
0032   SmallVector<std::string, 0> Headers;
0033   HTTPMethod Method = HTTPMethod::GET;
0034   bool FollowRedirects = true;
0035   HTTPRequest(StringRef Url);
0036 };
0037 
0038 bool operator==(const HTTPRequest &A, const HTTPRequest &B);
0039 
0040 /// A handler for state updates occurring while an HTTPRequest is performed.
0041 /// Can trigger the client to abort the request by returning an Error from any
0042 /// of its methods.
0043 class HTTPResponseHandler {
0044 public:
0045   /// Processes an additional chunk of bytes of the HTTP response body.
0046   virtual Error handleBodyChunk(StringRef BodyChunk) = 0;
0047 
0048 protected:
0049   ~HTTPResponseHandler();
0050 };
0051 
0052 /// A reusable client that can perform HTTPRequests through a network socket.
0053 class HTTPClient {
0054 #ifdef LLVM_ENABLE_CURL
0055   void *Curl = nullptr;
0056 #endif
0057 
0058 public:
0059   HTTPClient();
0060   ~HTTPClient();
0061 
0062   static bool IsInitialized;
0063 
0064   /// Returns true only if LLVM has been compiled with a working HTTPClient.
0065   static bool isAvailable();
0066 
0067   /// Must be called at the beginning of a program, while it is a single thread.
0068   static void initialize();
0069 
0070   /// Must be called at the end of a program, while it is a single thread.
0071   static void cleanup();
0072 
0073   /// Sets the timeout for the entire request, in milliseconds. A zero or
0074   /// negative value means the request never times out.
0075   void setTimeout(std::chrono::milliseconds Timeout);
0076 
0077   /// Performs the Request, passing response data to the Handler. Returns all
0078   /// errors which occur during the request. Aborts if an error is returned by a
0079   /// Handler method.
0080   Error perform(const HTTPRequest &Request, HTTPResponseHandler &Handler);
0081 
0082   /// Returns the last received response code or zero if none.
0083   unsigned responseCode();
0084 };
0085 
0086 } // end namespace llvm
0087 
0088 #endif // LLVM_DEBUGINFOD_HTTPCLIENT_H