LLVM 20.0.0git
HTTPServer.h
Go to the documentation of this file.
1//===-- llvm/Debuginfod/HTTPServer.h - HTTP server library ------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10/// This file contains the declarations of the HTTPServer and HTTPServerRequest
11/// classes, the HTTPResponse, and StreamingHTTPResponse structs, and the
12/// streamFile function.
13///
14//===----------------------------------------------------------------------===//
15
16#ifndef LLVM_DEBUGINFOD_HTTPSERVER_H
17#define LLVM_DEBUGINFOD_HTTPSERVER_H
18
19#include "llvm/ADT/StringRef.h"
20#include "llvm/Support/Error.h"
21
22#ifdef LLVM_ENABLE_HTTPLIB
23// forward declarations
24namespace httplib {
25class Request;
26class Response;
27class Server;
28} // namespace httplib
29#endif
30
31namespace llvm {
32
33struct HTTPResponse;
34struct StreamingHTTPResponse;
35class HTTPServer;
36
37class HTTPServerError : public ErrorInfo<HTTPServerError, ECError> {
38public:
39 static char ID;
40 HTTPServerError(const Twine &Msg);
41 void log(raw_ostream &OS) const override;
42
43private:
44 std::string Msg;
45};
46
48 friend HTTPServer;
49
50#ifdef LLVM_ENABLE_HTTPLIB
51private:
52 HTTPServerRequest(const httplib::Request &HTTPLibRequest,
53 httplib::Response &HTTPLibResponse);
54 httplib::Response &HTTPLibResponse;
55#endif
56
57public:
58 std::string UrlPath;
59 /// The elements correspond to match groups in the url path matching regex.
61
62 // TODO bring in HTTP headers
63
65 void setResponse(HTTPResponse Response);
66};
67
69 unsigned Code;
70 const char *ContentType;
72};
73
74typedef std::function<void(HTTPServerRequest &)> HTTPRequestHandler;
75
76/// An HTTPContentProvider is called by the HTTPServer to obtain chunks of the
77/// streaming response body. The returned chunk should be located at Offset
78/// bytes and have Length bytes.
79typedef std::function<StringRef(size_t /*Offset*/, size_t /*Length*/)>
81
82/// Wraps the content provider with HTTP Status code and headers.
84 unsigned Code;
85 const char *ContentType;
88 /// Called after the response transfer is complete with the success value of
89 /// the transfer.
90 std::function<void(bool)> CompletionHandler = [](bool Success) {};
91};
92
93/// Sets the response to stream the file at FilePath, if available, and
94/// otherwise an HTTP 404 error response.
95bool streamFile(HTTPServerRequest &Request, StringRef FilePath);
96
97/// An HTTP server which can listen on a single TCP/IP port for HTTP
98/// requests and delgate them to the appropriate registered handler.
100#ifdef LLVM_ENABLE_HTTPLIB
101 std::unique_ptr<httplib::Server> Server;
102 unsigned Port = 0;
103#endif
104public:
107
108 /// Returns true only if LLVM has been compiled with a working HTTPServer.
109 static bool isAvailable();
110
111 /// Registers a URL pattern routing rule. When the server is listening, each
112 /// request is dispatched to the first registered handler whose UrlPathPattern
113 /// matches the UrlPath.
114 Error get(StringRef UrlPathPattern, HTTPRequestHandler Handler);
115
116 /// Attempts to assign the requested port and interface, returning an Error
117 /// upon failure.
118 Error bind(unsigned Port, const char *HostInterface = "0.0.0.0");
119
120 /// Attempts to assign any available port and interface, returning either the
121 /// port number or an Error upon failure.
122 Expected<unsigned> bind(const char *HostInterface = "0.0.0.0");
123
124 /// Attempts to listen for requests on the bound port. Returns an Error if
125 /// called before binding a port.
126 Error listen();
127
128 /// If the server is listening, stop and unbind the socket.
129 void stop();
130};
131} // end namespace llvm
132
133#endif // LLVM_DEBUGINFOD_HTTPSERVER_H
#define Success
raw_pwrite_stream & OS
Base class for user error types.
Definition: Error.h:355
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
Tagged union holding either a T or a Error.
Definition: Error.h:481
void log(raw_ostream &OS) const override
Print an error message to an output stream.
Definition: HTTPServer.cpp:35
void setResponse(StreamingHTTPResponse Response)
Definition: HTTPServer.cpp:171
SmallVector< std::string, 1 > UrlPathMatches
The elements correspond to match groups in the url path matching regex.
Definition: HTTPServer.h:60
An HTTP server which can listen on a single TCP/IP port for HTTP requests and delgate them to the app...
Definition: HTTPServer.h:99
Error get(StringRef UrlPathPattern, HTTPRequestHandler Handler)
Registers a URL pattern routing rule.
Definition: HTTPServer.cpp:175
Error bind(unsigned Port, const char *HostInterface="0.0.0.0")
Attempts to assign the requested port and interface, returning an Error upon failure.
Definition: HTTPServer.cpp:182
Error listen()
Attempts to listen for requests on the bound port.
Definition: HTTPServer.cpp:190
static bool isAvailable()
Returns true only if LLVM has been compiled with a working HTTPServer.
Definition: HTTPServer.cpp:161
void stop()
If the server is listening, stop and unbind the socket.
Definition: HTTPServer.cpp:194
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
std::function< void(HTTPServerRequest &)> HTTPRequestHandler
Definition: HTTPServer.h:74
bool streamFile(HTTPServerRequest &Request, StringRef FilePath)
Sets the response to stream the file at FilePath, if available, and otherwise an HTTP 404 error respo...
Definition: HTTPServer.cpp:37
std::function< StringRef(size_t, size_t)> HTTPContentProvider
An HTTPContentProvider is called by the HTTPServer to obtain chunks of the streaming response body.
Definition: HTTPServer.h:80
const char * ContentType
Definition: HTTPServer.h:70
StringRef Body
Definition: HTTPServer.h:71
Wraps the content provider with HTTP Status code and headers.
Definition: HTTPServer.h:83
HTTPContentProvider Provider
Definition: HTTPServer.h:87
std::function< void(bool)> CompletionHandler
Called after the response transfer is complete with the success value of the transfer.
Definition: HTTPServer.h:90