LLVM 17.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
38 friend HTTPServer;
39
40#ifdef LLVM_ENABLE_HTTPLIB
41private:
42 HTTPServerRequest(const httplib::Request &HTTPLibRequest,
43 httplib::Response &HTTPLibResponse);
44 httplib::Response &HTTPLibResponse;
45#endif
46
47public:
48 std::string UrlPath;
49 /// The elements correspond to match groups in the url path matching regex.
51
52 // TODO bring in HTTP headers
53
55 void setResponse(HTTPResponse Response);
56};
57
59 unsigned Code;
60 const char *ContentType;
62};
63
64typedef std::function<void(HTTPServerRequest &)> HTTPRequestHandler;
65
66/// An HTTPContentProvider is called by the HTTPServer to obtain chunks of the
67/// streaming response body. The returned chunk should be located at Offset
68/// bytes and have Length bytes.
69typedef std::function<StringRef(size_t /*Offset*/, size_t /*Length*/)>
71
72/// Wraps the content provider with HTTP Status code and headers.
74 unsigned Code;
75 const char *ContentType;
78 /// Called after the response transfer is complete with the success value of
79 /// the transfer.
80 std::function<void(bool)> CompletionHandler = [](bool Success) {};
81};
82
83/// Sets the response to stream the file at FilePath, if available, and
84/// otherwise an HTTP 404 error response.
85bool streamFile(HTTPServerRequest &Request, StringRef FilePath);
86
87/// An HTTP server which can listen on a single TCP/IP port for HTTP
88/// requests and delgate them to the appropriate registered handler.
90#ifdef LLVM_ENABLE_HTTPLIB
91 std::unique_ptr<httplib::Server> Server;
92 unsigned Port = 0;
93#endif
94public:
97
98 /// Returns true only if LLVM has been compiled with a working HTTPServer.
99 static bool isAvailable();
100
101 /// Registers a URL pattern routing rule. When the server is listening, each
102 /// request is dispatched to the first registered handler whose UrlPathPattern
103 /// matches the UrlPath.
104 Error get(StringRef UrlPathPattern, HTTPRequestHandler Handler);
105
106 /// Attempts to assign the requested port and interface, returning an Error
107 /// upon failure.
108 Error bind(unsigned Port, const char *HostInterface = "0.0.0.0");
109
110 /// Attempts to assign any available port and interface, returning either the
111 /// port number or an Error upon failure.
112 Expected<unsigned> bind(const char *HostInterface = "0.0.0.0");
113
114 /// Attempts to listen for requests on the bound port. Returns an Error if
115 /// called before binding a port.
116 Error listen();
117
118 /// If the server is listening, stop and unbind the socket.
119 void stop();
120};
121} // end namespace llvm
122
123#endif // LLVM_DEBUGINFOD_HTTPSERVER_H
#define Success
Lightweight error class with error context and mandatory checking.
Definition: Error.h:156
Tagged union holding either a T or a Error.
Definition: Error.h:470
void setResponse(StreamingHTTPResponse Response)
Definition: HTTPServer.cpp:165
SmallVector< std::string, 1 > UrlPathMatches
The elements correspond to match groups in the url path matching regex.
Definition: HTTPServer.h:50
An HTTP server which can listen on a single TCP/IP port for HTTP requests and delgate them to the app...
Definition: HTTPServer.h:89
Error get(StringRef UrlPathPattern, HTTPRequestHandler Handler)
Registers a URL pattern routing rule.
Definition: HTTPServer.cpp:169
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:173
Error listen()
Attempts to listen for requests on the bound port.
Definition: HTTPServer.cpp:181
static bool isAvailable()
Returns true only if LLVM has been compiled with a working HTTPServer.
Definition: HTTPServer.cpp:155
void stop()
If the server is listening, stop and unbind the socket.
Definition: HTTPServer.cpp:185
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1200
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
std::function< void(HTTPServerRequest &)> HTTPRequestHandler
Definition: HTTPServer.h:64
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:31
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:70
const char * ContentType
Definition: HTTPServer.h:60
StringRef Body
Definition: HTTPServer.h:61
Wraps the content provider with HTTP Status code and headers.
Definition: HTTPServer.h:73
HTTPContentProvider Provider
Definition: HTTPServer.h:77
std::function< void(bool)> CompletionHandler
Called after the response transfer is complete with the success value of the transfer.
Definition: HTTPServer.h:80