An open API service indexing awesome lists of open source software.

https://github.com/fantasy-peak/simple_http

A header-only HTTP library that supports both HTTP/2 and HTTP/1, based on Beast, nghttp2, and Asio.
https://github.com/fantasy-peak/simple_http

cpp20-coroutine http-server http1-1 http2

Last synced: 7 months ago
JSON representation

A header-only HTTP library that supports both HTTP/2 and HTTP/1, based on Beast, nghttp2, and Asio.

Awesome Lists containing this project

README

          

# simple_http

## Require
* C++20
* nghttp2
* boost

## Server Example
```
asio::awaitable start()
{
simple_http::Config cfg{.ip = "0.0.0.0",
.port = 6666,
.worker_num = 8,
.concurrent_streams = 200};
simple_http::HttpServer hs(cfg);
simple_http::LOG_CB =
[](simple_http::LogLevel level, auto file, auto line, std::string msg) {
std::out << to_string(level) << " " << file << ":" << line << " " << msg
<< std::endl;
};
hs.setBefore([](const auto &req,
const auto &writer) -> asio::awaitable {
std::cout << "setBefore:" << req.target() << std::endl;
if (req.target() != "/hello")
{
auto res = simple_http::makeHttpResponse(http::status::bad_request);
res->prepare_payload();
writer->writeHttpResponse(res);
co_return false;
}
co_return true;
});
hs.setHttpHandler(
"/hello", [](auto req, auto writer) -> asio::awaitable {
if (writer->version() == simple_http::Version::Http2)
{
#if 0
auto res = simple_http::makeHttpResponse(http::status::ok);
res->body() = "hello h2";
writer->writeHttpResponse(res);
#else
writer->writeHeader("content-type", "text/plain");
writer->writeHeader(http::field::server, "test");
writer->writeHeaderEnd();
writer->writeBody("123");
writer->writeBody("456");
writer->writeBodyEnd("789");
#endif
}
co_return;
});
co_await hs.start();
}

int main()
{
simple_http::IoCtxPool pool{1};
pool.start();
asio::co_spawn(pool.getIoContext(), start(), asio::detached);
while (true)
sleep(1000);
return 0;
}
```

## Test Cmd
```
curl -v --http2-prior-knowledge http://localhost:6666/hello\?key1\=value1\&key2\=value2
curl -v --http2-prior-knowledge http://localhost:6666/hello -d "abcd"
curl -v --http2 http://localhost:6666/hello -d "abcd"

nghttp --upgrade -v http://127.0.0.1:6666/hello
nghttp --upgrade -v http://nghttp2.org
h2load -n 60000 -c 1000 -m 200 -H 'Content-Type: application/json' --data=b.txt http://localhost:6666/hello
```