Skip to content

sub1to/ScatGat

Repository files navigation

ScatGat

A cross-platform toy http server written in C++.

It uses a destructive zero-copy parser which re-uses heap-allocated memory and scatter/gather to send a stack-allocated response.

Output goes to cout and cerr, no log files, just journald.

All (test) routes are currently defined in register_routes.h

Usage

Linux

premake5 gmake
make

Set up systemd service

[Unit]
Description=ScatGat C++ HTTP Server
After=network.target

[Service]
User=scatgat
Group=scatgat

WorkingDirectory=/home/scatgat
ExecStart=/home/scatgat/http/.bin/debug/http

Restart=always
RestartSec=5

StandardOutput=journal
StandardError=journal

[Install]
WantedBy=multi-user.target

Windows

Run build_vs2022.sh (or premake5 vs2022) to generate a VS solution in .projects/vs2022

You might have to add this to your PATH env for the debug build (for ASan):

C:\Program Files\Microsoft Visual Studio\2022\<YOUR EDITION>\VC\Tools\MSVC\<YOUR VERSION>\bin\Hostx64\x64

There is an example script at scripts/run.ps1 that shows the output in the console, and writes it to a file.

Nginx reverse proxy

Set up the reverse proxy (with TLS if you'd like)

server {
    listen          80;
    server_name     localhost;

    client_max_body_size 10M;

    location / {
            # Forward traffic to the C++ server
            proxy_pass http://127.0.0.1:8080;

            # Force connection to close after each request
            proxy_http_version 1.0;
            proxy_set_header Connection "close";

            proxy_pass_header Server;

            # Pass real IP headers so you know who is connecting
            set $real_ip $remote_addr;
            if ($http_cf_connecting_ip) {
                    set $real_ip $http_cf_connecting_ip;
            }

            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $real_ip;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

    # When the C++ service is down, show a custom bad gateway page
    error_page 502 504 /502.html;
    location /502.html {
            root    /var/www/html;
            internal;
    }
}

About

C++ cross-platform toy http server

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages