Overview
The Zeal::Core::HttpServer class provides a local HTTP server for serving documentation files. It allows mounting filesystem directories to URL prefixes, enabling the browser to access local docset files via HTTP URLs. This is essential for proper rendering of documentation that uses relative paths and JavaScript.
Header: <core/httpserver.h>
Namespace: Zeal::Core
Backend: Uses the cpp-httplib library
Constructor & Destructor
HttpServer()
explicit HttpServer(QObject *parent = nullptr)
Constructs the HttpServer instance and starts the server on a local port.
parent
QObject*
default:"nullptr"
Parent QObject for memory management
Source: src/libs/core/httpserver.h:25
~HttpServer()
Destructor that stops the HTTP server and cleans up resources.
Source: src/libs/core/httpserver.h:26
Public Methods
baseUrl()
Returns the base URL of the HTTP server.
Returns: QUrl object with the server’s base URL (e.g., http://127.0.0.1:8080)
Example:
auto httpServer = Core::Application::instance()->httpServer();
const QUrl &baseUrl = httpServer->baseUrl();
qDebug() << "Server running at:" << baseUrl.toString();
Source: src/libs/core/httpserver.h:28
mount()
QUrl mount(const QString &prefix, const QString &path)
Mounts a filesystem directory to a URL prefix, making it accessible via HTTP.
URL prefix (e.g., “python”, “javascript”) where the directory will be accessible
Absolute filesystem path to the directory to serve
Returns: Complete QUrl where the mounted directory is accessible (baseUrl + prefix)
Example:
auto httpServer = Core::Application::instance()->httpServer();
QUrl url = httpServer->mount("python", "/home/user/.local/share/Zeal/docsets/Python.docset/Contents/Resources/Documents");
// url might be: http://127.0.0.1:8080/python
Usage in codebase:
// From docsetregistry.cpp:133
QUrl url = Core::Application::instance()->httpServer()->mount(name, docset->documentPath());
Source: src/libs/core/httpserver.h:30
unmount()
bool unmount(const QString &prefix)
Unmounts a previously mounted directory from the specified URL prefix.
Returns: true if the prefix was successfully unmounted, false if the prefix was not found
Example:
auto httpServer = Core::Application::instance()->httpServer();
if (httpServer->unmount("python")) {
qDebug() << "Successfully unmounted python documentation";
}
Usage in codebase:
// From docsetregistry.cpp:151
Core::Application::instance()->httpServer()->unmount(name);
Source: src/libs/core/httpserver.h:31
Usage Example
// Get the HTTP server instance
auto httpServer = Zeal::Core::Application::instance()->httpServer();
// Get the base URL
QUrl baseUrl = httpServer->baseUrl();
qDebug() << "Server listening at:" << baseUrl.toString();
// Mount a docset directory
QString docsetName = "Python";
QString docsetPath = "/path/to/Python.docset/Contents/Resources/Documents";
QUrl docsetUrl = httpServer->mount(docsetName, docsetPath);
// Now you can access files via HTTP:
// http://127.0.0.1:8080/Python/index.html
// http://127.0.0.1:8080/Python/library/functions.html
// Load in a web view
webView->load(docsetUrl.toString() + "/index.html");
// Unmount when the docset is removed
httpServer->unmount(docsetName);
Implementation Details
The HttpServer runs in a separate thread to avoid blocking the main Qt event loop. It:
- Starts automatically when constructed
- Listens on a random available local port (127.0.0.1)
- Serves static files from mounted directories
- Sanitizes URL prefixes to prevent path traversal attacks
- Returns appropriate HTTP status codes (200, 404, etc.)
Thread Safety: The server runs asynchronously. Mount and unmount operations are thread-safe.
- Application - Provides access to the HttpServer instance via
httpServer()
Registry::DocsetRegistry - Uses HttpServer to serve docset files
Core::NetworkAccessManager - Intercepts requests to the base URL for local serving