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/ADT/SmallString.h" |
19 | #include "llvm/ADT/SmallVector.h" |
20 | #include "llvm/Support/Error.h" |
21 | #include "llvm/Support/MemoryBuffer.h" |
22 | |
23 | #include <chrono> |
24 | |
25 | namespace llvm { |
26 | |
27 | enum class HTTPMethod { GET }; |
28 | |
29 | /// A stateless description of an outbound HTTP request. |
30 | struct HTTPRequest { |
31 | SmallString<128> Url; |
32 | SmallVector<std::string, 0> ; |
33 | HTTPMethod Method = HTTPMethod::GET; |
34 | bool FollowRedirects = true; |
35 | HTTPRequest(StringRef Url); |
36 | }; |
37 | |
38 | bool operator==(const HTTPRequest &A, const HTTPRequest &B); |
39 | |
40 | /// A handler for state updates occurring while an HTTPRequest is performed. |
41 | /// Can trigger the client to abort the request by returning an Error from any |
42 | /// of its methods. |
43 | class HTTPResponseHandler { |
44 | public: |
45 | /// Processes an additional chunk of bytes of the HTTP response body. |
46 | virtual Error handleBodyChunk(StringRef BodyChunk) = 0; |
47 | |
48 | protected: |
49 | ~HTTPResponseHandler(); |
50 | }; |
51 | |
52 | /// A reusable client that can perform HTTPRequests through a network socket. |
53 | class HTTPClient { |
54 | #ifdef LLVM_ENABLE_CURL |
55 | void *Curl = nullptr; |
56 | #endif |
57 | |
58 | public: |
59 | HTTPClient(); |
60 | ~HTTPClient(); |
61 | |
62 | static bool IsInitialized; |
63 | |
64 | /// Returns true only if LLVM has been compiled with a working HTTPClient. |
65 | static bool isAvailable(); |
66 | |
67 | /// Must be called at the beginning of a program, while it is a single thread. |
68 | static void initialize(); |
69 | |
70 | /// Must be called at the end of a program, while it is a single thread. |
71 | static void cleanup(); |
72 | |
73 | /// Sets the timeout for the entire request, in milliseconds. A zero or |
74 | /// negative value means the request never times out. |
75 | void setTimeout(std::chrono::milliseconds Timeout); |
76 | |
77 | /// Performs the Request, passing response data to the Handler. Returns all |
78 | /// errors which occur during the request. Aborts if an error is returned by a |
79 | /// Handler method. |
80 | Error perform(const HTTPRequest &Request, HTTPResponseHandler &Handler); |
81 | |
82 | /// Returns the last received response code or zero if none. |
83 | unsigned responseCode(); |
84 | }; |
85 | |
86 | } // end namespace llvm |
87 | |
88 | #endif // LLVM_DEBUGINFOD_HTTPCLIENT_H |
89 | |