HTTPClient.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 library for issuing
  16. /// HTTP requests and handling the responses.
  17. ///
  18. //===----------------------------------------------------------------------===//
  19. #ifndef LLVM_DEBUGINFOD_HTTPCLIENT_H
  20. #define LLVM_DEBUGINFOD_HTTPCLIENT_H
  21. #include "llvm/Support/Error.h"
  22. #include "llvm/Support/MemoryBuffer.h"
  23. #include <chrono>
  24. namespace llvm {
  25. enum class HTTPMethod { GET };
  26. /// A stateless description of an outbound HTTP request.
  27. struct HTTPRequest {
  28. SmallString<128> Url;
  29. SmallVector<std::string, 0> Headers;
  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 an additional chunk of bytes of the HTTP response body.
  41. virtual Error handleBodyChunk(StringRef BodyChunk) = 0;
  42. protected:
  43. ~HTTPResponseHandler();
  44. };
  45. /// A reusable client that can perform HTTPRequests through a network socket.
  46. class HTTPClient {
  47. #ifdef LLVM_ENABLE_CURL
  48. void *Curl = nullptr;
  49. #endif
  50. public:
  51. HTTPClient();
  52. ~HTTPClient();
  53. static bool IsInitialized;
  54. /// Returns true only if LLVM has been compiled with a working HTTPClient.
  55. static bool isAvailable();
  56. /// Must be called at the beginning of a program, while it is a single thread.
  57. static void initialize();
  58. /// Must be called at the end of a program, while it is a single thread.
  59. static void cleanup();
  60. /// Sets the timeout for the entire request, in milliseconds. A zero or
  61. /// negative value means the request never times out.
  62. void setTimeout(std::chrono::milliseconds Timeout);
  63. /// Performs the Request, passing response data to the Handler. Returns all
  64. /// errors which occur during the request. Aborts if an error is returned by a
  65. /// Handler method.
  66. Error perform(const HTTPRequest &Request, HTTPResponseHandler &Handler);
  67. /// Returns the last received response code or zero if none.
  68. unsigned responseCode();
  69. };
  70. } // end namespace llvm
  71. #endif // LLVM_DEBUGINFOD_HTTPCLIENT_H
  72. #ifdef __GNUC__
  73. #pragma GCC diagnostic pop
  74. #endif