Asynchronous, Header-only C++ HTTP-over-(TCP|UNIX Socket|STDIO) Library

Overview

CXXHTTP

A C++ library implementing an asynchronous HTTP server and client.

To clone this library, make sure you also clone the submodules. The --recursive flag should do this for you:

git clone --recursive https://github.com/ef-gy/cxxhttp.git

Features

This library implements HTTP/1.1 for clients as well as servers. It's fairly minimalistic, yet there's a few nifty parts that wouldn't strictly have been necessary.

Some of these are:

  • HTTP Content Negotation
  • HTTP over TCP, UNIX sockets and STDIO
  • CLI help screen with servlet summaries
  • Optional OPTIONS implementation, with a markdown body that shows the supported methods and the relevant location regex
  • Optional TRACE implementation
  • Basic 100-continue flow
  • Basic request validation
  • Fallback HEAD handler

I believe the STDIO feature is quite unique, as is the excellent test coverage of the library, for both the client and the server code.

Non-Features

Some features didn't make it into the library for various reasons - mostly to keep it small. Some of these are:

  • HTTP 'chunked' Transfer Encoding - or any non-identity encoding, really
  • HTTP Date headers, or any other timekeeping-related code
  • Query string parsing - use REST API style location strings instead
  • Logging - though there are internal flags and counters, which e.g. the Prometheus client library based on this makes use of
  • Any form of SSL/TLS support - use a frontend server to provide this for you

Some of the omissions technically make this non-conforming to the HTTP/1.1 standard, but seeing as how the primary purpose of this library is to sit behind an nginx (or similar) HTTP web server, this shouldn't be an issue in practice.

Compiler Requirements

You must have a GNU make and a C++ compiler that supports C++14. On most modern-ish Linux distributions that's pretty easy: just update your PMS and install clang++ or g++. Preferrably clang++. No reason.

OSX

On OSX, you'll be fine if you just install a current version of XCode.

Ubuntu (LTS)

On Ubuntu, you're in for a bit of a rough ride. The only reliable way I've found of getting things to compile, at least on 14.04, is to install this PPA and use g++ 4.9:

sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt-get update
sudo apt-get install g++-4.9

To compile things, add CXX=g++-4.9 to your make commands, like this:

make test CXX=g++-4.9

This may not be necessary if you haven't installed clang++ and if there's no other versions of g++ installed. clang++ doesn't work on Ubuntu in c++14 mode, because libc++ hasn't been updated past an ancient SVN snapshot.

Debian/Raspbian

On Debian and Raspbian it should be sufficient to just update your package sources and install clang++ and libc++:

apt-get update
apt-get install clang++ libc++1 libc++-dev

Test suite

The library has a test suite, which you can run like this:

make test

If this fails, something is wrong. Please open an issue at GitHub with the log: https://github.com/ef-gy/cxxhttp/issues

Getting Started

This library uses the ASIO asynchronous I/O headers as well as libefgy. If you are starting a new project, it's probably easiest to use the build headers from libefgy and git submodules to pull in the dependencies.

The following set of commands ought to get you started:

PROJECT=funtastic-example
git init ${PROJECT}
cd ${PROJECT}
git submodule add https://github.com/ef-gy/libefgy.git dependencies/libefgy
git submodule add https://github.com/chriskohlhoff/asio.git dependencies/asio
git submodule add https://github.com/ef-gy/cxxhttp.git dependencies/cxxhttp
mkdir include
mkdir include/${PROJECT}
mkdir src
ln -s ../dependencies/asio/asio/include/asio include/asio
ln -s ../dependencies/asio/asio/include/asio.hpp include/asio.hpp
ln -s ../dependencies/cxxhttp/include/cxxhttp include/cxxhttp
ln -s ../dependencies/libefgy/include/ef.gy include/ef.gy

A trivial makefile to get you started would look like this:

-include ef.gy/base.mk include/ef.gy/base.mk
NAME:=funtastic-example

A very trivial sample server would be this (see src/server.cpp):

#define ASIO_DISABLE_THREADS
#include <cxxhttp/httpd.h>
using namespace cxxhttp;

static void hello(http::sessionData &session, std::smatch &) {
  session.reply(200, "Hello World!");
}

static http::servlet servlet("/", ::hello);

int main(int argc, char *argv[]) { return cxxhttp::main(argc, argv); }

If you saved this as src/example.cpp, then you could run it like this:

make example
./example http:localhost:8080

This will run the server and to see what it does, use:

curl -i http://localhost:8080/

Which should print the "Hello World!" from above.

You can run the ./example programme without arguments to see all the available command line options. In particular, there is "http:unix:...", which lets you run your server on a UNIX socket.

See src/server.cpp for additional commentary, and the include/cxxhttp/httpd-....h headers, which implement additional common features that web servers tend to have.

I'm lazy, can I clone this example from somewhere?

Sure, try this:

git clone --recursive https://github.com/ef-gy/cxxhttp-example.git

Note the --recursive - that's because this is using submodules.

Usage

You really should be running this behind a real web servers, for security and such. Look into nginx for that, which is really good at forward-proxying.

Updating to a newer version of the libraries

The command you're looking for is built into git:

git submodule update --recursive --remote

Don't forget to commit the new state of the dependencies/ directory, if any of the libraries had updates.

You might also like...
Warp speed Data Transfer (WDT)  is an embeddedable library (and command line tool) aiming to transfer data between 2 systems as fast as possible over multiple TCP paths.
Warp speed Data Transfer (WDT) is an embeddedable library (and command line tool) aiming to transfer data between 2 systems as fast as possible over multiple TCP paths.

WDT Warp speed Data Transfer Design philosophy/Overview Goal: Lowest possible total transfer time - to be only hardware limited (disc or network bandw

Brynet - Header Only Cross platform high performance TCP network library using C++ 11.
Brynet - Header Only Cross platform high performance TCP network library using C++ 11.

Brynet Header Only Cross platform high performance TCP network library using C++ 11. Build status Windows : Linux/MacOS : Features Header only Cross p

Mongoose Embedded Web Server Library - a multi-protocol embedded networking library with TCP/UDP, HTTP, WebSocket,  MQTT built-in protocols, async DNS resolver, and non-blocking API.
Mongoose Embedded Web Server Library - a multi-protocol embedded networking library with TCP/UDP, HTTP, WebSocket, MQTT built-in protocols, async DNS resolver, and non-blocking API.

Mongoose - Embedded Web Server / Embedded Networking Library Mongoose is a networking library for C/C++. It implements event-driven non-blocking APIs

an easy implementation of a multi-process tcp server and a multi-thread tcp client
an easy implementation of a multi-process tcp server and a multi-thread tcp client

一个TCP多进程服务器-多线程客户端的简单实现。 客户端类似Apache ab的测试功能,能够通过向某一个ip端口发送指定并发量和总数量的tcp短连接;服务端处理tcp短连接,每来一条消息就打印一条log。 使用cmake编译,建议在vscode里编译,或者命令行 # 终端进入目录 mkdir bu

Cross-platform, efficient, customizable, and robust asynchronous HTTP/WebSocket server C++14 library with the right balance between performance and ease of use

What Is RESTinio? RESTinio is a header-only C++14 library that gives you an embedded HTTP/Websocket server. It is based on standalone version of ASIO

A C++ async HTTP client library to use in asynchronous applications while communicating with REST services.

libashttp An asynchronous HTTP library using Boost.ASIO as the backend. This project is licensed under: Usage Here is a example usage which is taken f

A DLL that serves OutputDebugString content over a TCP connection
A DLL that serves OutputDebugString content over a TCP connection

RemoteDebugView A DLL that serves OutputDebugString content over a TCP connection Usage You will need to compile the DLL and then call the exported fu

C and C++ client for QuestDB Input Line Protocol over TCP

c-questdb-client QuestDB - InfluxDB Line Protocol - Ingestion Client Library for C and C++ This library makes it easy to insert data into QuestDB. Thi

A modern C++ network library for developing high performance network services in TCP/UDP/HTTP protocols.
A modern C++ network library for developing high performance network services in TCP/UDP/HTTP protocols.

evpp Introduction 中文说明 evpp is a modern C++ network library for developing high performance network services using TCP/UDP/HTTP protocols. evpp provid

Comments
Releases(v2)
  • v2(Jun 25, 2017)

    This release fixes a few minor bugs (e.g. being unable to have a query string as part of the local part of a URL) and adds the ability to talk HTTP over STDIO for both the server and client.

    For the client, simply pass "stdio" as a URL's "port", like http://somewhere:stdio/ - in this case, the host part of the location is ignored for anything but the Host header, and the programme will start talking HTTP on STDIO. This is mostly useful for testing, and for running an HTTP server through xinetd.

    Speaking of testing, the tests now cover 100% of the code lines, which means it's pretty likely that this server does exactly what it says on the tin. If you find bugs, please do open an issue - and I do consider lack of documentation a bug :).

    Source code(tar.gz)
    Source code(zip)
  • v1(Jun 3, 2017)

    This is the first release of this library. The server side provides a convenient servlet-based way to create HTTP servers, and the client side has an API that vaguely resembles some JS HTTP clients.

    See http://github.com/ef-gy/cxxhttp-example for an example of how to use it. The readme also has a recap of that. It's pretty straightforward. Seriously.

    Current library docs are available at: http://documentation.ef.gy/cxxhttp

    If you find bugs, please do post them in the Issues section here at GitHub.

    Source code(tar.gz)
    Source code(zip)
Wrapper for linux TCP/UDP/unix/USB socket connections

Socket Connection wrapper shared library Shared library that realize sockets connections and could transfer data-packages. Navigation Navigation Insta

Dmitry Golgovsky 7 Dec 21, 2021
A Tcp/Ip stack implementation on top of Solarflare ef_vi, and a C++ headers only framework for tcp multiplexing client/server.

Efvitcp Efvitcp is a tcp library using Solarflare ef_vi interface on linux, and also a tcp multiplexing framework for both C++ client and server progr

Meng Rao 23 Nov 26, 2022
Small utility that leverages eBPF to dump the traffic of a unix domain socket

UnixDump UnixDump is a small eBPF powered utility that can be used to dump unix socket traffic. System requirements This project was developed on a Ub

Guillaume Fournier 8 Nov 19, 2022
Faster termux-am implementation that connects to a receiver in termux-app using a unix socket

termux-am-socket This is a small program for sending commands to the Termux:API app, thereby allowing terminal programs to use the Android API. The pr

Termux 26 Dec 21, 2022
Lightweight, header-only, Boost-based socket pool library

Stream-client This is a lightweight, header-only, Boost-based library providing client-side network primitives to easily organize and implement data t

Tinkoff.ru 12 Aug 5, 2022
H2O - the optimized HTTP/1, HTTP/2, HTTP/3 server

H2O - an optimized HTTP server with support for HTTP/1.x, HTTP/2 and HTTP/3 (experimental) Copyright (c) 2014-2019 DeNA Co., Ltd., Kazuho Oku, Tatsuhi

H2O 10.2k Dec 30, 2022
Provide translation, currency conversion, and voting services. First using telnet you create a connection to a TCP socket, then the server connects to 3 UDP sockets hosted on other servers to do tasks.

to run micro servers g++ translator.cpp -o translator ./translator <port 1> g++ voting.cpp -o voting ./voting <port 2> g++ currency_converter.cpp -o c

Jacob Artuso 1 Oct 29, 2021
Portable, single-file, protocol-agnostic TCP and UDP socket wrapper, primarily for game networking

Documentation This is a header-only library, as such most of its functional documentation is contained within the "header section" of the source code

null 64 Dec 3, 2022
Asynchronous TCP Library for STM32H7-based Portenta_H7 using mbed_portenta core.

Asynchronous TCP Library for STM32H7-based Portenta_H7 using mbed_portenta core. This library is the base for future and more advanced Async libraries, such as AsyncWebServer, AsyncHTTPRequest, AsyncHTTPSRequest

Khoi Hoang 3 Dec 29, 2022
Asynchronous SSL TCP Library for ESP32.

Asynchronous SSL TCP Library for ESP32. This library is the base for future and more advanced Async SSL libraries, such as AsyncSSLWebServer, AsyncHTTPSRequest

Khoi Hoang 12 Dec 29, 2022