|
myTinySQL v0.2.0-2-g5000098
A tiny SQL interpreter over fixed-width text files
|
The HTTP server: the page's files, a small JSON API, and one thread per connection. More...
#include "Server.hpp"#include <arpa/inet.h>#include <netinet/in.h>#include <sys/socket.h>#include <unistd.h>#include <algorithm>#include <cerrno>#include <cctype>#include <format>#include <fstream>#include <iostream>#include <mutex>#include <sstream>#include <system_error>#include <thread>#include <utility>#include "Catalog.hpp"#include "Output.hpp"#include "Session.hpp"#include "Util.hpp"Go to the source code of this file.
Classes | |
| class | tinysql::anonymous_namespace{Server.cpp}::Socket |
| Owns a socket descriptor and closes it when it goes out of scope (RAII). More... | |
| struct | tinysql::anonymous_namespace{Server.cpp}::Request |
| One HTTP request, split into the parts the server uses. More... | |
Functions | |
| void | tinysql::anonymous_namespace{Server.cpp}::throwSystemError (std::string_view what) |
| Throws the error of the last failed system call. | |
| std::string_view | tinysql::anonymous_namespace{Server.cpp}::headerValue (std::string_view head, std::string_view name) |
| Finds a header's value in the raw request, ignoring the case of its name. | |
| Request | tinysql::anonymous_namespace{Server.cpp}::readRequest (int fd) |
| Reads one HTTP request: the request line, the headers and, if there is one, the body. | |
| void | tinysql::anonymous_namespace{Server.cpp}::sendAll (int fd, std::string_view data) |
Sends all of data, retrying until it is written or the client goes away. | |
| std::string | tinysql::anonymous_namespace{Server.cpp}::httpResponse (std::string_view status, std::string_view contentType, std::string_view body) |
| Builds a complete HTTP response. | |
| std::string | tinysql::anonymous_namespace{Server.cpp}::errorResponse (std::string_view status, std::string_view message) |
Builds a JSON error answer, e.g. {"error":"Not found"}. | |
| std::string_view | tinysql::anonymous_namespace{Server.cpp}::contentTypeFor (std::string_view name) |
| The content type of a file of the page, from its extension. | |
| bool | tinysql::anonymous_namespace{Server.cpp}::isSafeFileName (std::string_view name) |
Tells whether a requested file name is safe to serve from the web/ directory. | |
| std::string | tinysql::anonymous_namespace{Server.cpp}::serveFile (const ServerConfig &config, std::string_view name) |
Serves one file of the page from the web/ directory. | |
| std::string | tinysql::anonymous_namespace{Server.cpp}::runSql (const ServerConfig &config, std::string_view sql) |
Runs the SQL of a POST /api/sql request, logs it, and builds the JSON answer. | |
| std::string | tinysql::anonymous_namespace{Server.cpp}::listTables (const ServerConfig &config) |
Builds the JSON answer of GET /api/tables. Call it only while holding workMutex. | |
| std::string | tinysql::anonymous_namespace{Server.cpp}::clientAddress (const sockaddr_in &address) |
| Formats a client's IPv4 address. | |
| void | tinysql::anonymous_namespace{Server.cpp}::handleClient (const ServerConfig &config, int fd, const std::string &client) |
| Serves one connection: reads the request, answers it, and closes. | |
| void | tinysql::runServer (const ServerConfig &config) |
| Serves connections until the process is stopped. | |
Variables | |
| constexpr std::size_t | tinysql::anonymous_namespace{Server.cpp}::kMaxRequestSize = 64 * 1024 |
| The largest request read (headers and body), in bytes; the rest is ignored. | |
| constexpr int | tinysql::anonymous_namespace{Server.cpp}::kIdleSeconds = 30 |
| How long a connection may stay silent before it is closed, in seconds. | |
| std::mutex | tinysql::anonymous_namespace{Server.cpp}::workMutex |
| Lets only one request at a time run SQL and write to the console. | |
The HTTP server: the page's files, a small JSON API, and one thread per connection.
Definition in file Server.cpp.
| void tinysql::runServer | ( | const ServerConfig & | config | ) |
Serves connections until the process is stopped.
It serves the page's files from ServerConfig::webDir and a small JSON API: GET /api/tables lists the tables, POST /api/sql runs the SQL in the request body. Each connection is served in its own thread; the SQL itself runs one request at a time.
| config | The server settings. |
| std::system_error | If the port cannot be opened (for example, it is already in use). |
Definition at line 312 of file Server.cpp.