File indexing completed on 2026-05-10 08:43:46
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016 #ifndef LLVM_DEBUGINFOD_HTTPSERVER_H
0017 #define LLVM_DEBUGINFOD_HTTPSERVER_H
0018
0019 #include "llvm/ADT/StringRef.h"
0020 #include "llvm/Support/Error.h"
0021
0022 #ifdef LLVM_ENABLE_HTTPLIB
0023
0024 namespace httplib {
0025 class Request;
0026 class Response;
0027 class Server;
0028 }
0029 #endif
0030
0031 namespace llvm {
0032
0033 struct HTTPResponse;
0034 struct StreamingHTTPResponse;
0035 class HTTPServer;
0036
0037 class HTTPServerError : public ErrorInfo<HTTPServerError, ECError> {
0038 public:
0039 static char ID;
0040 HTTPServerError(const Twine &Msg);
0041 void log(raw_ostream &OS) const override;
0042
0043 private:
0044 std::string Msg;
0045 };
0046
0047 class HTTPServerRequest {
0048 friend HTTPServer;
0049
0050 #ifdef LLVM_ENABLE_HTTPLIB
0051 private:
0052 HTTPServerRequest(const httplib::Request &HTTPLibRequest,
0053 httplib::Response &HTTPLibResponse);
0054 httplib::Response &HTTPLibResponse;
0055 #endif
0056
0057 public:
0058 std::string UrlPath;
0059
0060 SmallVector<std::string, 1> UrlPathMatches;
0061
0062
0063
0064 void setResponse(StreamingHTTPResponse Response);
0065 void setResponse(HTTPResponse Response);
0066 };
0067
0068 struct HTTPResponse {
0069 unsigned Code;
0070 const char *ContentType;
0071 StringRef Body;
0072 };
0073
0074 typedef std::function<void(HTTPServerRequest &)> HTTPRequestHandler;
0075
0076
0077
0078
0079 typedef std::function<StringRef(size_t , size_t )>
0080 HTTPContentProvider;
0081
0082
0083 struct StreamingHTTPResponse {
0084 unsigned Code;
0085 const char *ContentType;
0086 size_t ContentLength;
0087 HTTPContentProvider Provider;
0088
0089
0090 std::function<void(bool)> CompletionHandler = [](bool Success) {};
0091 };
0092
0093
0094
0095 bool streamFile(HTTPServerRequest &Request, StringRef FilePath);
0096
0097
0098
0099 class HTTPServer {
0100 #ifdef LLVM_ENABLE_HTTPLIB
0101 std::unique_ptr<httplib::Server> Server;
0102 unsigned Port = 0;
0103 #endif
0104 public:
0105 HTTPServer();
0106 ~HTTPServer();
0107
0108
0109 static bool isAvailable();
0110
0111
0112
0113
0114 Error get(StringRef UrlPathPattern, HTTPRequestHandler Handler);
0115
0116
0117
0118 Error bind(unsigned Port, const char *HostInterface = "0.0.0.0");
0119
0120
0121
0122 Expected<unsigned> bind(const char *HostInterface = "0.0.0.0");
0123
0124
0125
0126 Error listen();
0127
0128
0129 void stop();
0130 };
0131 }
0132
0133 #endif