Skip to content

pgit/anyhttp

Repository files navigation

Overview

Build and run tests

Low-level C++ HTTP client and server library, based on ASIO and its asynchronous model.

** THIS REPOSITORY IS PURELY EXPERIMENTAL **

It supports HTTP/1.x, HTTP/2 and HTTP/3 behind a common, type-erasing interface, hence the any int the name.

None of those protocols are implemented from scratch. Instead, it is a wrapper around the following well-established libraries:

  • Boost Beast
  • nghttp2
  • nghttp3 - not done yet.

Synopsis

Server

awaitable<void> echo(server::Request request, server::Response response)
{
   if (request.content_length())
      response.content_length(request.content_length().value());

   co_await response.async_submit(200, {});

   std::array<uint8_t, 64 * 1024> buffer;
   for (;;)
   {
      size_t n = co_await request.async_read_some(asio::buffer(buffer));
      co_await response.async_write(asio::buffer(buffer, n));
      if (n == 0)
         break;
   }
}

Client

awaitable<void> do_session(Client& client, boost::urls::url url)
{
   auto session = co_await client.async_connect();
   auto request = co_await session.async_submit(url, {});
   auto response = co_await request.async_get_response();   
}

Implementation

The asynchronous operations exposed by server and client are ASIO asynchronous operations. As such, they support a range of completion tokens like use_awaitable or plain callbacks.

The implementation is hidden behind any_completion_handler so that it can be compiled separately.

This work is partly inspired by asio-grpc, which takes the idea even one step further and also supports the upcoming sender/receiver model of execution.

classDiagram

Response --|> Reader
Request_Impl --|> Writer

namespace client {
   class Response {
      async_read_some(buffer)
   }
   class Request {
      async_get_response()
      async_write(buffer)
   }
   class Client {
      async_connect()
   }

   class Request_Impl {

   }
}

namespace impl {
   class Reader {
      get_executor()
      content_length()
      async_read_some(buffer)
      detach()
      destroy()
   }
   class Writer {
      get_executor()
      content_length(optional<size_t>)
      async_write(buffer)
      detach()
      destroy()
   }
   class Client {
      get_executor()
   }
}
Loading

Links

For now, this section contains just a set of random links collected during development.

About

Low-level C++ HTTP client/server library based on ASIO

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •