LLVM 23.0.0git
HTTPClient.h
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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_HTTP_HTTPCLIENT_H
16#define LLVM_HTTP_HTTPCLIENT_H
17
20#include "llvm/Support/Error.h"
22
23#include <chrono>
24#include <optional>
25
26namespace llvm {
27
28enum class HTTPMethod { GET };
29
30/// A stateless description of an outbound HTTP request.
35 // Follow redirects without security downgrades.
36 bool FollowRedirects = true;
37 // Allow self-signed TLS certificates with this SHA-256 (WinHTTP only).
38 std::optional<std::string> PinnedCertFingerprint;
40};
41
42bool operator==(const HTTPRequest &A, const HTTPRequest &B);
43
44/// A handler for state updates occurring while an HTTPRequest is performed.
45/// Can trigger the client to abort the request by returning an Error from any
46/// of its methods.
48public:
49 /// Processes an additional chunk of bytes of the HTTP response body.
50 virtual Error handleBodyChunk(StringRef BodyChunk) = 0;
51
52protected:
54};
55
56/// A reusable client that can perform HTTPRequests through a network socket.
58#if defined(LLVM_ENABLE_CURL) || defined(_WIN32)
59 void *Handle = nullptr;
60#endif
61
62public:
65
66 static bool IsInitialized;
67
68 /// Returns true only if LLVM has been compiled with a working HTTPClient.
69 static bool isAvailable();
70
71 /// Must be called at the beginning of a program, while it is a single thread.
72 static void initialize();
73
74 /// Must be called at the end of a program, while it is a single thread.
75 static void cleanup();
76
77 /// Sets the timeout for the entire request, in milliseconds. A zero or
78 /// negative value means the request never times out.
79 void setTimeout(std::chrono::milliseconds Timeout);
80
81 /// Performs the Request, passing response data to the Handler. Returns all
82 /// errors which occur during the request. Aborts if an error is returned by a
83 /// Handler method.
84 Error perform(const HTTPRequest &Request, HTTPResponseHandler &Handler);
85
86 /// Returns the last received response code or zero if none.
87 unsigned responseCode();
88};
89
90} // end namespace llvm
91
92#endif // LLVM_HTTP_HTTPCLIENT_H
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
This file defines the SmallString class.
This file defines the SmallVector class.
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
static bool isAvailable()
Returns true only if LLVM has been compiled with a working HTTPClient.
static bool IsInitialized
Definition HTTPClient.h:66
unsigned responseCode()
Returns the last received response code or zero if none.
static void initialize()
Must be called at the beginning of a program, while it is a single thread.
Error perform(const HTTPRequest &Request, HTTPResponseHandler &Handler)
Performs the Request, passing response data to the Handler.
void setTimeout(std::chrono::milliseconds Timeout)
Sets the timeout for the entire request, in milliseconds.
static void cleanup()
Must be called at the end of a program, while it is a single thread.
A handler for state updates occurring while an HTTPRequest is performed.
Definition HTTPClient.h:47
virtual Error handleBodyChunk(StringRef BodyChunk)=0
Processes an additional chunk of bytes of the HTTP response body.
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition SmallString.h:26
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
This is an optimization pass for GlobalISel generic memory operations.
bool operator==(const AddressRangeValuePair &LHS, const AddressRangeValuePair &RHS)
HTTPMethod
Definition HTTPClient.h:28
@ Timeout
Reached timeout while waiting for the owner to release the lock.
A stateless description of an outbound HTTP request.
Definition HTTPClient.h:31
std::optional< std::string > PinnedCertFingerprint
Definition HTTPClient.h:38
SmallVector< std::string, 0 > Headers
Definition HTTPClient.h:33
HTTPRequest(StringRef Url)
SmallString< 128 > Url
Definition HTTPClient.h:32
HTTPMethod Method
Definition HTTPClient.h:34