aboutsummaryrefslogtreecommitdiff
path: root/llvm/include/llvm/Debuginfod/HTTPClient.h
blob: ca3b76ca9f3f416a78e6a7b491c1bf18e0c2456a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
//===-- llvm/Support/HTTPClient.h - HTTP client library ---------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
///
/// \file
/// This file contains the declarations of the HTTPClient, HTTPMethod,
/// HTTPResponseHandler, and BufferedHTTPResponseHandler classes, as well as
/// the HTTPResponseBuffer and HTTPRequest structs.
///
//===----------------------------------------------------------------------===//

#ifndef LLVM_DEBUGINFOD_HTTPCLIENT_H
#define LLVM_DEBUGINFOD_HTTPCLIENT_H

#include "llvm/Support/Error.h"
#include "llvm/Support/MemoryBuffer.h"

#include <chrono>

namespace llvm {

enum class HTTPMethod { GET };

/// A stateless description of an outbound HTTP request.
struct HTTPRequest {
  SmallString<128> Url;
  HTTPMethod Method = HTTPMethod::GET;
  bool FollowRedirects = true;
  HTTPRequest(StringRef Url);
};

bool operator==(const HTTPRequest &A, const HTTPRequest &B);

/// A handler for state updates occurring while an HTTPRequest is performed.
/// Can trigger the client to abort the request by returning an Error from any
/// of its methods.
class HTTPResponseHandler {
public:
  /// Processes one line of HTTP response headers.
  virtual Error handleHeaderLine(StringRef HeaderLine) = 0;

  /// Processes an additional chunk of bytes of the HTTP response body.
  virtual Error handleBodyChunk(StringRef BodyChunk) = 0;

  /// Processes the HTTP response status code.
  virtual Error handleStatusCode(unsigned Code) = 0;

protected:
  ~HTTPResponseHandler();
};

/// An HTTP response status code bundled with a buffer to store the body.
struct HTTPResponseBuffer {
  unsigned Code = 0;
  std::unique_ptr<WritableMemoryBuffer> Body;
};

/// A simple handler which writes returned data to an HTTPResponseBuffer.
/// Ignores all headers except the Content-Length, which it uses to
/// allocate an appropriately-sized Body buffer.
class BufferedHTTPResponseHandler final : public HTTPResponseHandler {
  size_t Offset = 0;

public:
  /// Stores the data received from the HTTP server.
  HTTPResponseBuffer ResponseBuffer;

  /// These callbacks store the body and status code in an HTTPResponseBuffer
  /// allocated based on Content-Length. The Content-Length header must be
  /// handled by handleHeaderLine before any calls to handleBodyChunk.
  Error handleHeaderLine(StringRef HeaderLine) override;
  Error handleBodyChunk(StringRef BodyChunk) override;
  Error handleStatusCode(unsigned Code) override;
};

/// A reusable client that can perform HTTPRequests through a network socket.
class HTTPClient {
#ifdef LLVM_ENABLE_CURL
  void *Curl = nullptr;
#endif

public:
  HTTPClient();
  ~HTTPClient();

  static bool IsInitialized;

  /// Returns true only if LLVM has been compiled with a working HTTPClient.
  static bool isAvailable();

  /// Must be called at the beginning of a program, while it is a single thread.
  static void initialize();

  /// Must be called at the end of a program, while it is a single thread.
  static void cleanup();

  /// Sets the timeout for the entire request, in milliseconds. A zero or
  /// negative value means the request never times out.
  void setTimeout(std::chrono::milliseconds Timeout);

  /// Performs the Request, passing response data to the Handler. Returns all
  /// errors which occur during the request. Aborts if an error is returned by a
  /// Handler method.
  Error perform(const HTTPRequest &Request, HTTPResponseHandler &Handler);

  /// Performs the Request with the default BufferedHTTPResponseHandler, and
  /// returns its HTTPResponseBuffer or an Error.
  Expected<HTTPResponseBuffer> perform(const HTTPRequest &Request);

  /// Performs an HTTPRequest with the default configuration to make a GET
  /// request to the given Url. Returns an HTTPResponseBuffer or an Error.
  Expected<HTTPResponseBuffer> get(StringRef Url);
};

} // end namespace llvm

#endif // LLVM_DEBUGINFOD_HTTPCLIENT_H