LLVM  15.0.0git
HTTPClient.h
Go to the documentation of this file.
1 //===-- llvm/Support/HTTPClient.h - HTTP client library ---------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 ///
9 /// \file
10 /// This file contains the declarations of the HTTPClient library for issuing
11 /// HTTP requests and handling the responses.
12 ///
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_DEBUGINFOD_HTTPCLIENT_H
16 #define LLVM_DEBUGINFOD_HTTPCLIENT_H
17 
18 #include "llvm/Support/Error.h"
20 
21 #include <chrono>
22 
23 namespace llvm {
24 
25 enum class HTTPMethod { GET };
26 
27 /// A stateless description of an outbound HTTP request.
28 struct HTTPRequest {
31  bool FollowRedirects = true;
33 };
34 
35 bool operator==(const HTTPRequest &A, const HTTPRequest &B);
36 
37 /// A handler for state updates occurring while an HTTPRequest is performed.
38 /// Can trigger the client to abort the request by returning an Error from any
39 /// of its methods.
41 public:
42  /// Processes an additional chunk of bytes of the HTTP response body.
43  virtual Error handleBodyChunk(StringRef BodyChunk) = 0;
44 
45 protected:
47 };
48 
49 /// A reusable client that can perform HTTPRequests through a network socket.
50 class HTTPClient {
51 #ifdef LLVM_ENABLE_CURL
52  void *Curl = nullptr;
53 #endif
54 
55 public:
56  HTTPClient();
57  ~HTTPClient();
58 
59  static bool IsInitialized;
60 
61  /// Returns true only if LLVM has been compiled with a working HTTPClient.
62  static bool isAvailable();
63 
64  /// Must be called at the beginning of a program, while it is a single thread.
65  static void initialize();
66 
67  /// Must be called at the end of a program, while it is a single thread.
68  static void cleanup();
69 
70  /// Sets the timeout for the entire request, in milliseconds. A zero or
71  /// negative value means the request never times out.
72  void setTimeout(std::chrono::milliseconds Timeout);
73 
74  /// Performs the Request, passing response data to the Handler. Returns all
75  /// errors which occur during the request. Aborts if an error is returned by a
76  /// Handler method.
77  Error perform(const HTTPRequest &Request, HTTPResponseHandler &Handler);
78 
79  /// Returns the last received response code or zero if none.
80  unsigned responseCode();
81 };
82 
83 } // end namespace llvm
84 
85 #endif // LLVM_DEBUGINFOD_HTTPCLIENT_H
MemoryBuffer.h
llvm::HTTPMethod
HTTPMethod
Definition: HTTPClient.h:25
llvm::HTTPClient
A reusable client that can perform HTTPRequests through a network socket.
Definition: HTTPClient.h:50
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
llvm::HTTPResponseHandler
A handler for state updates occurring while an HTTPRequest is performed.
Definition: HTTPClient.h:40
llvm::HTTPClient::IsInitialized
static bool IsInitialized
Definition: HTTPClient.h:59
llvm::HTTPClient::~HTTPClient
~HTTPClient()
Error.h
llvm::HTTPClient::initialize
static void initialize()
Must be called at the beginning of a program, while it is a single thread.
Definition: HTTPClient.cpp:139
llvm::HTTPRequest::HTTPRequest
HTTPRequest(StringRef Url)
Definition: HTTPClient.cpp:27
llvm::HTTPClient::cleanup
static void cleanup()
Must be called at the end of a program, while it is a single thread.
Definition: HTTPClient.cpp:141
B
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
llvm::HTTPClient::perform
Error perform(const HTTPRequest &Request, HTTPResponseHandler &Handler)
Performs the Request, passing response data to the Handler.
Definition: HTTPClient.cpp:145
llvm::HTTPResponseHandler::handleBodyChunk
virtual Error handleBodyChunk(StringRef BodyChunk)=0
Processes an additional chunk of bytes of the HTTP response body.
llvm::SmallString< 128 >
llvm::HTTPResponseHandler::~HTTPResponseHandler
~HTTPResponseHandler()
llvm::HTTPRequest::Url
SmallString< 128 > Url
Definition: HTTPClient.h:29
llvm::HTTPClient::responseCode
unsigned responseCode()
Returns the last received response code or zero if none.
Definition: HTTPClient.cpp:150
llvm::HTTPClient::isAvailable
static bool isAvailable()
Returns true only if LLVM has been compiled with a working HTTPClient.
Definition: HTTPClient.cpp:137
llvm::HTTPClient::setTimeout
void setTimeout(std::chrono::milliseconds Timeout)
Sets the timeout for the entire request, in milliseconds.
Definition: HTTPClient.cpp:143
llvm::operator==
bool operator==(uint64_t V1, const APInt &V2)
Definition: APInt.h:1990
llvm::HTTPRequest::FollowRedirects
bool FollowRedirects
Definition: HTTPClient.h:31
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::HTTPRequest::Method
HTTPMethod Method
Definition: HTTPClient.h:30
llvm::HTTPRequest
A stateless description of an outbound HTTP request.
Definition: HTTPClient.h:28
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:155
llvm::HTTPClient::HTTPClient
HTTPClient()
llvm::HTTPMethod::GET
@ GET