HTTPClient.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===-- llvm/Support/HTTPClient.h - HTTP client library ---------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. ///
  14. /// \file
  15. /// This file contains the declarations of the HTTPClient, HTTPMethod,
  16. /// HTTPResponseHandler, and BufferedHTTPResponseHandler classes, as well as
  17. /// the HTTPResponseBuffer and HTTPRequest structs.
  18. ///
  19. //===----------------------------------------------------------------------===//
  20. #ifndef LLVM_DEBUGINFOD_HTTPCLIENT_H
  21. #define LLVM_DEBUGINFOD_HTTPCLIENT_H
  22. #include "llvm/Support/Error.h"
  23. #include "llvm/Support/MemoryBuffer.h"
  24. #include <chrono>
  25. namespace llvm {
  26. enum class HTTPMethod { GET };
  27. /// A stateless description of an outbound HTTP request.
  28. struct HTTPRequest {
  29. SmallString<128> Url;
  30. HTTPMethod Method = HTTPMethod::GET;
  31. bool FollowRedirects = true;
  32. HTTPRequest(StringRef Url);
  33. };
  34. bool operator==(const HTTPRequest &A, const HTTPRequest &B);
  35. /// A handler for state updates occurring while an HTTPRequest is performed.
  36. /// Can trigger the client to abort the request by returning an Error from any
  37. /// of its methods.
  38. class HTTPResponseHandler {
  39. public:
  40. /// Processes one line of HTTP response headers.
  41. virtual Error handleHeaderLine(StringRef HeaderLine) = 0;
  42. /// Processes an additional chunk of bytes of the HTTP response body.
  43. virtual Error handleBodyChunk(StringRef BodyChunk) = 0;
  44. /// Processes the HTTP response status code.
  45. virtual Error handleStatusCode(unsigned Code) = 0;
  46. protected:
  47. ~HTTPResponseHandler();
  48. };
  49. /// An HTTP response status code bundled with a buffer to store the body.
  50. struct HTTPResponseBuffer {
  51. unsigned Code = 0;
  52. std::unique_ptr<WritableMemoryBuffer> Body;
  53. };
  54. /// A simple handler which writes returned data to an HTTPResponseBuffer.
  55. /// Ignores all headers except the Content-Length, which it uses to
  56. /// allocate an appropriately-sized Body buffer.
  57. class BufferedHTTPResponseHandler final : public HTTPResponseHandler {
  58. size_t Offset = 0;
  59. public:
  60. /// Stores the data received from the HTTP server.
  61. HTTPResponseBuffer ResponseBuffer;
  62. /// These callbacks store the body and status code in an HTTPResponseBuffer
  63. /// allocated based on Content-Length. The Content-Length header must be
  64. /// handled by handleHeaderLine before any calls to handleBodyChunk.
  65. Error handleHeaderLine(StringRef HeaderLine) override;
  66. Error handleBodyChunk(StringRef BodyChunk) override;
  67. Error handleStatusCode(unsigned Code) override;
  68. };
  69. /// A reusable client that can perform HTTPRequests through a network socket.
  70. class HTTPClient {
  71. #ifdef LLVM_ENABLE_CURL
  72. void *Curl = nullptr;
  73. #endif
  74. public:
  75. HTTPClient();
  76. ~HTTPClient();
  77. static bool IsInitialized;
  78. /// Returns true only if LLVM has been compiled with a working HTTPClient.
  79. static bool isAvailable();
  80. /// Must be called at the beginning of a program, while it is a single thread.
  81. static void initialize();
  82. /// Must be called at the end of a program, while it is a single thread.
  83. static void cleanup();
  84. /// Sets the timeout for the entire request, in milliseconds. A zero or
  85. /// negative value means the request never times out.
  86. void setTimeout(std::chrono::milliseconds Timeout);
  87. /// Performs the Request, passing response data to the Handler. Returns all
  88. /// errors which occur during the request. Aborts if an error is returned by a
  89. /// Handler method.
  90. Error perform(const HTTPRequest &Request, HTTPResponseHandler &Handler);
  91. /// Performs the Request with the default BufferedHTTPResponseHandler, and
  92. /// returns its HTTPResponseBuffer or an Error.
  93. Expected<HTTPResponseBuffer> perform(const HTTPRequest &Request);
  94. /// Performs an HTTPRequest with the default configuration to make a GET
  95. /// request to the given Url. Returns an HTTPResponseBuffer or an Error.
  96. Expected<HTTPResponseBuffer> get(StringRef Url);
  97. };
  98. } // end namespace llvm
  99. #endif // LLVM_DEBUGINFOD_HTTPCLIENT_H
  100. #ifdef __GNUC__
  101. #pragma GCC diagnostic pop
  102. #endif