HTTPServer.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===-- llvm/Debuginfod/HTTPServer.h - HTTP server 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 HTTPServer and HTTPServerRequest
  16. /// classes, the HTTPResponse, and StreamingHTTPResponse structs, and the
  17. /// streamFile function.
  18. ///
  19. //===----------------------------------------------------------------------===//
  20. #ifndef LLVM_DEBUGINFOD_HTTPSERVER_H
  21. #define LLVM_DEBUGINFOD_HTTPSERVER_H
  22. #include "llvm/ADT/StringRef.h"
  23. #include "llvm/Support/Error.h"
  24. #ifdef LLVM_ENABLE_HTTPLIB
  25. // forward declarations
  26. namespace httplib {
  27. class Request;
  28. class Response;
  29. class Server;
  30. } // namespace httplib
  31. #endif
  32. namespace llvm {
  33. struct HTTPResponse;
  34. struct StreamingHTTPResponse;
  35. class HTTPServer;
  36. class HTTPServerRequest {
  37. friend HTTPServer;
  38. #ifdef LLVM_ENABLE_HTTPLIB
  39. private:
  40. HTTPServerRequest(const httplib::Request &HTTPLibRequest,
  41. httplib::Response &HTTPLibResponse);
  42. httplib::Response &HTTPLibResponse;
  43. #endif
  44. public:
  45. std::string UrlPath;
  46. /// The elements correspond to match groups in the url path matching regex.
  47. SmallVector<std::string, 1> UrlPathMatches;
  48. // TODO bring in HTTP headers
  49. void setResponse(StreamingHTTPResponse Response);
  50. void setResponse(HTTPResponse Response);
  51. };
  52. struct HTTPResponse {
  53. unsigned Code;
  54. const char *ContentType;
  55. StringRef Body;
  56. };
  57. typedef std::function<void(HTTPServerRequest &)> HTTPRequestHandler;
  58. /// An HTTPContentProvider is called by the HTTPServer to obtain chunks of the
  59. /// streaming response body. The returned chunk should be located at Offset
  60. /// bytes and have Length bytes.
  61. typedef std::function<StringRef(size_t /*Offset*/, size_t /*Length*/)>
  62. HTTPContentProvider;
  63. /// Wraps the content provider with HTTP Status code and headers.
  64. struct StreamingHTTPResponse {
  65. unsigned Code;
  66. const char *ContentType;
  67. size_t ContentLength;
  68. HTTPContentProvider Provider;
  69. /// Called after the response transfer is complete with the success value of
  70. /// the transfer.
  71. std::function<void(bool)> CompletionHandler = [](bool Success) {};
  72. };
  73. /// Sets the response to stream the file at FilePath, if available, and
  74. /// otherwise an HTTP 404 error response.
  75. bool streamFile(HTTPServerRequest &Request, StringRef FilePath);
  76. /// An HTTP server which can listen on a single TCP/IP port for HTTP
  77. /// requests and delgate them to the appropriate registered handler.
  78. class HTTPServer {
  79. #ifdef LLVM_ENABLE_HTTPLIB
  80. std::unique_ptr<httplib::Server> Server;
  81. unsigned Port = 0;
  82. #endif
  83. public:
  84. HTTPServer();
  85. ~HTTPServer();
  86. /// Returns true only if LLVM has been compiled with a working HTTPServer.
  87. static bool isAvailable();
  88. /// Registers a URL pattern routing rule. When the server is listening, each
  89. /// request is dispatched to the first registered handler whose UrlPathPattern
  90. /// matches the UrlPath.
  91. Error get(StringRef UrlPathPattern, HTTPRequestHandler Handler);
  92. /// Attempts to assign the requested port and interface, returning an Error
  93. /// upon failure.
  94. Error bind(unsigned Port, const char *HostInterface = "0.0.0.0");
  95. /// Attempts to assign any available port and interface, returning either the
  96. /// port number or an Error upon failure.
  97. Expected<unsigned> bind(const char *HostInterface = "0.0.0.0");
  98. /// Attempts to listen for requests on the bound port. Returns an Error if
  99. /// called before binding a port.
  100. Error listen();
  101. /// If the server is listening, stop and unbind the socket.
  102. void stop();
  103. };
  104. } // end namespace llvm
  105. #endif // LLVM_DEBUGINFOD_HTTPSERVER_H
  106. #ifdef __GNUC__
  107. #pragma GCC diagnostic pop
  108. #endif