A header-only library for embedding HTTP or HTTPS, IPV6 and IPV4 servers in C++ applications.
via-httplib
is an asynchronous communications library built upon asio
(either
boost or
standalone) to enable simple, secure and efficient
HTTP/HTTPS servers to be built that comply with
rfc7230 wherever possible.
-
A C++17 compliant compiler.
This version requires a complier that supports string_view. It's been tested withMSVC 2017
,Clang 7.0.1
,GCC 8.2
andMinGw 7.3.0
.
C++11 versions are available in tags1.6.0
and lower. -
The
asio
C++ library.
Either standalone asio or boost asio. -
For: mutithreading, authentication and/or the request router, the relevant boost libraries are required.
However, if they are not required, the library can use standalone asio on its own. -
For HTTPS: the
OpenSSL
library, see openssl. -
For C++ code documentation: Doxygen
Download the latest tagged version of via-httplib
from
Github
and follow the instructions here: Make.
via-http
lib depends on the standalone asio
or boost
libraries.
If boost
is not installed on your machine then download the latest package from
boost and follow the instructions here:
boost getting started.
Otherwise standalone asio
can be downloaded from: asio.
If you require an HTTPS server or client then you must also install the
OpenSSL library.
Please note that a plain HTTP server does not require OpenSLL
.
The Server User Guide explains how to use the library to create HTTP servers and the Client User Guide explains how to use the library to create HTTP clients.
An example https server docker image can be built using the following commands from this directory:
docker build . -t via-http-server:1.8.0
The following code implements an HTTP server that responds to GET requests to the
/hello
and /hello/:name
endpoints:
#include "via/comms/tcp_adaptor.hpp"
#include "via/http_server.hpp"
#include "via/http/request_router.hpp"
#include <iostream>
/// Define an HTTP server using std::string to store message bodies
typedef via::http_server<via::comms::tcp_adaptor, std::string> http_server_type;
typedef http_server_type::http_connection_type http_connection;
using namespace via::http;
namespace
{
/// /hello request handler
tx_response get_hello_handler(rx_request const&, //request,
Parameters const&, //parameters,
std::string const&, // data,
std::string &response_body)
{
response_body += "Hello, whoever you are?!";
return tx_response(response_status::code::OK);
}
/// /hello/:name request handler
tx_response get_hello_name_handler(rx_request const&, //request,
Parameters const& parameters,
std::string const&, // data,
std::string &response_body)
{
response_body += "Hello, ";
auto iter(parameters.find("name"));
if (iter != parameters.end())
response_body += iter->second;
return tx_response(response_status::code::OK);
}
}
int main(int /* argc */, char *argv[])
{
std::string app_name(argv[0]);
unsigned short port_number(via::comms::tcp_adaptor::DEFAULT_HTTP_PORT);
std::cout << app_name << ": " << port_number << std::endl;
try
{
// The asio io_service.
boost::asio::io_service io_service;
// Create the HTTP server
http_server_type http_server(io_service);
// Attach the request method handlers
http_server.request_router().add_method("GET", "/hello", get_hello_handler);
http_server.request_router().add_method(request_method::GET, "/hello/:name",
get_hello_name_handler);
// Accept connections (both IPV4 and IPV6) on the default port (80)
boost::system::error_code error(http_server.accept_connections());
if (error)
{
std::cerr << "Error: " << error.message() << std::endl;
return 1;
}
// Start the server
io_service.run();
}
catch (std::exception& e)
{
std::cerr << "Exception:" << e.what() << std::endl;
return 1;
}
return 0;
}
Document | Description |
---|---|
Build Guide | How to build the library. |
Configuration | How to configure the library. |
Server User Guide | How to use the library to create HTTP servers. |
Client User Guide | How to use the library to create HTTP clients. |
Security Guide | HTTP Server secruity considerations. |
Design | The library design. |
examples/server | Example HTTP & HTTPS servers. |
examples/client | Example HTTP & HTTPS clients. |
Directory | Contents |
---|---|
via | The via-httplib API classes: http_server, http_connection and http_client. |
examples/server | Example HTTP & HTTPS servers. |
examples/client | Example HTTP & HTTPS clients. |
tests | Unit tests for the HTTP parsers and encoders. |
docs | The User Guides and design documents. |
docs/html |
Doxygen output directory. Created by running doxygen Doxyfile in the docs directory. |
Thanks to:
- Neil Tisdale for encouraging and inspiring me to create the library
- Louis Nayegon for helping to develop it and recommending GitHub
- Adam Leggett for helping to identify and fix security, efficiency and CMake issues
- Christopher Kohlhoff for the
asio
library, without which, this library wouldn't exist.