25#ifdef LLVM_ENABLE_HTTPLIB
35 Request.
setResponse({404u,
"text/plain",
"Could not open file to read.\n"});
45 Request.
setResponse({404u,
"text/plain",
"Could not memory-map file.\n"});
55 [=](
bool Success) {
delete MB; }});
59#ifdef LLVM_ENABLE_HTTPLIB
67static void expandUrlPathMatches(
const std::smatch &Matches,
69 bool UrlPathSet =
false;
70 for (
const auto &it : Matches) {
80HTTPServerRequest::HTTPServerRequest(
const httplib::Request &HTTPLibRequest,
81 httplib::Response &HTTPLibResponse)
82 : HTTPLibResponse(HTTPLibResponse) {
83 expandUrlPathMatches(HTTPLibRequest.matches, *
this);
89 HTTPLibResponse.status = Response.
Code;
93 HTTPLibResponse.set_content_provider(
95 [=](
size_t Offset,
size_t Length, httplib::DataSink &Sink) {
96 if (Offset < Response.ContentLength) {
97 StringRef Chunk = Response.Provider(Offset, Length);
98 Sink.write(Chunk.begin(), Chunk.size());
104 HTTPLibResponse.status = Response.
Code;
108 std::string ErrorMessage;
111 Server->Get(std::string(UrlPathPattern),
112 [Handler](
const httplib::Request &HTTPLibRequest,
113 httplib::Response &HTTPLibResponse) {
121 if (!Server->bind_to_port(HostInterface, ListenPort))
123 "Could not assign requested address.");
129 int ListenPort = Server->bind_to_any_port(HostInterface);
132 "Could not assign any port on requested address.");
133 return Port = ListenPort;
139 "Cannot listen without first binding to a port.");
140 if (!Server->listen_after_bind())
143 "An unknown error occurred when cpp-httplib attempted to listen.");
This file contains the declarations of the HTTPServer and HTTPServerRequest classes,...
static bool isValid(const char C)
Returns true if C is a valid mangled character: <0-9a-zA-Z_>.
static StringRef substr(StringRef Str, uint64_t Len)
Represents either an error or a value T.
std::error_code getError() const
Lightweight error class with error context and mandatory checking.
static ErrorSuccess success()
Create a success value.
Tagged union holding either a T or a Error.
Error takeError()
Take ownership of the stored error.
void setResponse(StreamingHTTPResponse Response)
SmallVector< std::string, 1 > UrlPathMatches
The elements correspond to match groups in the url path matching regex.
Error get(StringRef UrlPathPattern, HTTPRequestHandler Handler)
Registers a URL pattern routing rule.
Error bind(unsigned Port, const char *HostInterface="0.0.0.0")
Attempts to assign the requested port and interface, returning an Error upon failure.
Error listen()
Attempts to listen for requests on the bound port.
static bool isAvailable()
Returns true only if LLVM has been compiled with a working HTTPServer.
void stop()
If the server is listening, stop and unbind the socket.
This interface provides simple read-only access to a block of memory, and provides simple methods for...
static ErrorOr< std::unique_ptr< MemoryBuffer > > getOpenFile(sys::fs::file_t FD, const Twine &Filename, uint64_t FileSize, bool RequiresNullTerminator=true, bool IsVolatile=false, std::optional< Align > Alignment=std::nullopt)
Given an already-open file descriptor, read the file and return a MemoryBuffer.
size_t getBufferSize() const
StringRef getBuffer() const
void push_back(const T &Elt)
StringRef - Represent a constant reference to a string, i.e.
constexpr size_t size() const
size - Get the string size.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
std::error_code closeFile(file_t &F)
Close the file object.
Expected< file_t > openNativeFileForRead(const Twine &Name, OpenFlags Flags=OF_None, SmallVectorImpl< char > *RealPath=nullptr)
Opens the file with the given name in a read-only mode, returning its open file descriptor.
This is an optimization pass for GlobalISel generic memory operations.
std::function< void(HTTPServerRequest &)> HTTPRequestHandler
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
bool streamFile(HTTPServerRequest &Request, StringRef FilePath)
Sets the response to stream the file at FilePath, if available, and otherwise an HTTP 404 error respo...
void consumeError(Error Err)
Consume a Error without doing anything.
Wraps the content provider with HTTP Status code and headers.
std::function< void(bool)> CompletionHandler
Called after the response transfer is complete with the success value of the transfer.