Internet Protocol
Header Only Library

HTTP Server Openssl

How to use HTTP server class with openssl

References

#include "ip.hpp"
#include "ip/http/httpserver.hpp"
#define ENABLE_SSL

Syntax

namespace internetprotocol
class http_server_ssl_c
#include <iostream>
#define ENABLE_SSL
#include "ip.hpp"

using namespace internetprotocol;

std::string loadfile(const std::string& file) {
    std::ifstream arquivo(file);

    if (!arquivo.is_open()) {
        throw std::runtime_error("Erro trying to open file: " + file);
    }

    std::stringstream buffer;
    buffer << arquivo.rdbuf();

    return buffer.str();
}
// Relative path to the files
const std::string cert = loadfile("cert.pem");
const std::string key = loadfile("key.pem");
const std::string csr = loadfile("csr.pem");
const std::string ca_cert = loadfile("ca-cert.pem");

int main()
{
    http_server_ssl_c net({ key, cert, "", "", file_format_e::pem, none, "" });
    http_request_t req;
    net.get("/", [&](const http_request_t &request, const std::shared_ptr<http_remote_c> &response) {
        http_response_t &res = response->headers();
        res.body = "hello, world!";
        response->write([](const asio::error_code &ec, const size_t bytes_sent) {
            // Check if response has been sent
        });
    });

    std::string input;
    while (std::getline(std::cin, input)) {
        if (input == "quit") {
            net.close();
            break;
        }
    }
    join_threads();

    return 0;
}

Functions

This class has the same functions from HttpServer, except for the ones shown below

Get Ssl Context

  • Set Ssl extra configuration.
  asio::ssl::context &get_ssl_context()

Get Ssl Sockets

  • Get all ssl sockets connected
  const std::set<ssl_socket_ptr> &get_ssl_sockets() const

Update Ssl Socket

  • Update Ssl socket configuration
  • Must be called afer change options using get_ssl_context() function
  void update_ssl_socket()

Load Private Key Data

  • Load the private key from a string.
  • If any error occurs, it will trigger the OnError event.
 bool load_private_key_data(const std::string &key_data)

Load Private Key File

  • Load the private key from file.
  • If any error occurs, it will trigger the OnError event.
  • Relative path.
 bool load_private_key_file(const std::string &filename)

Load Certificate Data

  • Load the certificate from a string.
  • If any error occurs, it will trigger the OnError event.
 bool load_certificate_data(const std::string &cert_data)

Load Certificate File

  • Load the certificate from file.
  • If any error occurs, it will trigger the OnError event.
  • Relative path.
 bool load_certificate_file(const std::string &filename)

Load Certificate Chain Data

  • Load the certificate chain from a string.
  • If any error occurs, it will trigger the OnError event.
 bool load_certificate_chain_data(const std::string &cert_chain_data)

Load Certificate Chain File

  • Load the certificate chain from file.
  • If any error occurs, it will trigger the OnError event.
  • Relative path.
 bool load_certificate_chain_file(const std::string &filename)

Load Verify File

  • Load the certificate from file and verify if it is valid.
  • If any error occurs, it will trigger the OnError event.
  • Relative path.
 bool load_verify_file(const std::string &filename)