Skip to content

nayandas69/swiftserve

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SwiftServe

SwiftServe made for fun is a minimalist, high-performance HTTP server written in C++. It is designed to be simple to use while providing powerful features for building web applications and APIs. SwiftServe focuses on non-blocking I/O and efficient request handling to achieve low latency and high throughput.

SwiftServe Made for Fun

I have

  • Non-blocking I/O powered by poll(2)
  • Full HTTP/1.1 protocol support (keep-alive by default)
  • Regex-based route matching with capture groups
  • Supports GET, HEAD, POST, PUT, PATCH, DELETE, and OPTIONS methods
  • Chunked transfer encoding (both request and response)
  • Built-in static file server with:
    • MIME type detection
    • Byte-range requests (Range header)
    • Conditional caching (If-Modified-Since)
    • Automatic directory listing
  • Streaming file responses for large files
  • Chunked request trailer headers
  • Cookie and query string parsing
  • Safe path resolution (blocks directory traversal attacks)

Requirements

  • A C++20 compatible compiler (GCC 11+, Clang 14+)
  • Linux or macOS (uses POSIX poll)
  • make or cmake

Build

Using Make:

make

Using CMake:

mkdir build && cd build
cmake ..
make

Usage

./swiftserve -p 8080 -d ./public
Flag Description Default
-p, --port Port to listen on 8080
-d, --directory Directory to serve ./
-h, --help Show help message

Open your browser and visit http://localhost:8080/.

API Reference

SwiftServe can also be used as a library to build custom HTTP applications.

Registering Routes

#include "http/server.h"

int main() {
    swiftserve::http::Server server;

    server.get("/hello", [](swiftserve::http::Request* req,
                            swiftserve::http::Response* res) {
        res->status(200)->end("Hello, World!");
    });

    server.start(8080, [] { return 3500; });
    return 0;
}

Route Parameters with Regex

server.get("/user/(\\d+)", [](swiftserve::http::Request* req,
                               swiftserve::http::Response* res) {
    std::string user_id = req->params[1];
    res->status(200)->end("User ID: " + user_id);
});

Handling POST Body

server.post("/submit",
    swiftserve::http::Server::with_body(
        [](swiftserve::http::Request* req,
           swiftserve::http::Response* res,
           const char* data, int length) {
            std::string body(data, length);
            res->status(200)->end("Received: " + body);
        }
    )
);

Streaming a Response

server.get("/download", [](swiftserve::http::Request* req,
                            swiftserve::http::Response* res) {
    auto reader = std::make_shared<swiftserve::http::FileBodyReader>(
        std::make_unique<std::ifstream>("largefile.bin", std::ios::binary),
        file_size
    );
    res->status(200)->stream(reader, {{"Content-Type", "application/octet-stream"}});
});

Serving Static Files

server.get(".*", [](swiftserve::http::Request* req,
                     swiftserve::http::Response* res) {
    swiftserve::http::serve_static("./public", req, res);
});

Request Object

Property Type Description
method std::string HTTP method (GET, POST, etc.)
raw_uri std::string Original URI from the request line
path std::string Decoded, normalised path
raw_query std::string Raw query string after ?
headers HeaderMap Request headers
trailers HeaderMap Trailer headers (chunked requests only)
params std::smatch Regex capture groups from route matching
query() const HeaderMap& Lazy-parsed query parameters
cookies() const HeaderMap& Lazy-parsed cookies

Response Object

Method Description
status(int code) Set the HTTP status code
content_length(long len) Set Content-Length (omit for chunked)
end(body, headers) Send complete response
write_head(code, headers) Send headers only
write(data) Write a chunk of body data
stream(reader, headers) Stream a file or generated content
close() Close the connection

License

MIT @nayandas69

About

HTTP server written in C++

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages