Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:46

0001 //===-- llvm/Debuginfod/HTTPServer.h - HTTP server library ------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 ///
0009 /// \file
0010 /// This file contains the declarations of the HTTPServer and HTTPServerRequest
0011 /// classes, the HTTPResponse, and StreamingHTTPResponse structs, and the
0012 /// streamFile function.
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 // forward declarations
0024 namespace httplib {
0025 class Request;
0026 class Response;
0027 class Server;
0028 } // namespace httplib
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   /// The elements correspond to match groups in the url path matching regex.
0060   SmallVector<std::string, 1> UrlPathMatches;
0061 
0062   // TODO bring in HTTP headers
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 /// An HTTPContentProvider is called by the HTTPServer to obtain chunks of the
0077 /// streaming response body. The returned chunk should be located at Offset
0078 /// bytes and have Length bytes.
0079 typedef std::function<StringRef(size_t /*Offset*/, size_t /*Length*/)>
0080     HTTPContentProvider;
0081 
0082 /// Wraps the content provider with HTTP Status code and headers.
0083 struct StreamingHTTPResponse {
0084   unsigned Code;
0085   const char *ContentType;
0086   size_t ContentLength;
0087   HTTPContentProvider Provider;
0088   /// Called after the response transfer is complete with the success value of
0089   /// the transfer.
0090   std::function<void(bool)> CompletionHandler = [](bool Success) {};
0091 };
0092 
0093 /// Sets the response to stream the file at FilePath, if available, and
0094 /// otherwise an HTTP 404 error response.
0095 bool streamFile(HTTPServerRequest &Request, StringRef FilePath);
0096 
0097 /// An HTTP server which can listen on a single TCP/IP port for HTTP
0098 /// requests and delgate them to the appropriate registered handler.
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   /// Returns true only if LLVM has been compiled with a working HTTPServer.
0109   static bool isAvailable();
0110 
0111   /// Registers a URL pattern routing rule. When the server is listening, each
0112   /// request is dispatched to the first registered handler whose UrlPathPattern
0113   /// matches the UrlPath.
0114   Error get(StringRef UrlPathPattern, HTTPRequestHandler Handler);
0115 
0116   /// Attempts to assign the requested port and interface, returning an Error
0117   /// upon failure.
0118   Error bind(unsigned Port, const char *HostInterface = "0.0.0.0");
0119 
0120   /// Attempts to assign any available port and interface, returning either the
0121   /// port number or an Error upon failure.
0122   Expected<unsigned> bind(const char *HostInterface = "0.0.0.0");
0123 
0124   /// Attempts to listen for requests on the bound port. Returns an Error if
0125   /// called before binding a port.
0126   Error listen();
0127 
0128   /// If the server is listening, stop and unbind the socket.
0129   void stop();
0130 };
0131 } // end namespace llvm
0132 
0133 #endif // LLVM_DEBUGINFOD_HTTPSERVER_H