Skip to content

traysh/httpp

Repository files navigation

Coverage Status

In-development easy to use Modern C++ web backend framework.

Table of Contents

Usage Example

Minimal server example:

#include "server.hpp"

int main() {
    const unsigned short port = 9933;

    Server server;

    auto& router = server.GetRouter();
    using Method = HTTP::MethodType;
    router.Add({
        { "/hi", Method::Get,
            [](auto& response) {
                response << "Hello from " << request.Path;
        }},
        { "/echo", Method::Post,
            [](const auto& request, auto& response) {
                response << request.Body.CStr();
        }},
    });
    router.SetNotFoundHandler([](auto& response) {
        response.Status = HTTPResponseStatus::Type::NotFound;
        response << "sorry, I don't know that URL";
    });

    server.Serve(port);

    return 0;
}

Output from calling these endpoints:

$ curl localhost:9933/hi  
Hello from /hi%
$ curl --data "hello world" localhost:9933/echo
hello world%
$ curl -v localhost:9933/blah
Trying ::1...
* TCP_NODELAY set
* Connection failed
* connect to ::1 port 9933 failed: Connection refused
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 9933 (#0)
> GET /blah HTTP/1.1
> Host: localhost:9933
> User-Agent: curl/7.54.0
> Accept: */*
>
< HTTP/1.1 404 Not Found
< CONNECTION: Close
<
* Connection #0 to host localhost left intact
sorry, I don't know that URL%

Goals

  • Easy to use as most high-level language frameworks, such as the ones available for Node.js and Python.
  • Easy to use the right way
  • Cross platform must support at least Linux and MacOS
  • Cross architecture must support at least x86 and ARMv5 and later
  • TDD to ensure testable and high quality code
  • Good performance though good usability is preferred over it
  • Secure

How to build on Mac or Linux

Prerequisites

  • llvm
  • cmake
  • gcovr (for test coverage reports)

Install prerequisites on Linux

Install prerequisites on Arch Linux: pacman -Sy llvm cmake gcovr

Note: the default compiler of most linux distributions is GCC, which is not currently working for building this project. So LLVM is required for now. Thus, it will be required to export it as the C and C++ compiler prior to running cmake:

export CC=clang
export CXX=clang++

Install prerequisites on Mac

First, install a compiler: Xcode or Command Line Tools

Then, install homebrew package manager: https://brew.sh/#install

Then use it to install the other requirements: brew install cmake gcovr

You can also use homebrew's LLVM compiler instead, but will need to adjust CC and CXX environment variables to point to clang and clang++ binaries installed by homebrew.

Building

  1. check prerequisites for your platform above
  2. clone this repo and cwd to your local copy
  3. clone submodules:
    git submodule update --init
  4. create the build dir and change to it:
    mkdir -p build && cd build
  5. run cmake to generate the Makefile:
    cmake ..
  6. build the project:
    • build using a single job: make
    • optionally, build using multiple cores instead: make -j9
  7. build and run tests:
    • build using a single job: make test
    • optionally, build using multiple cores instead: make -j9 test

Roadmap

Note: this is just a proposal to be discussed between project members

  • version 1.0
    • HTTP/1 support
    • multithreaded requests processor
    • request parser providing access to HTTP request method, headers and body
    • JSON parser for this content-type
    • router for endpoints
    • middleware support

TODO

Better name for the project

About

C++ Backend Framework

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published