Internet Protocol
Header Only Library

HTTP Server Openssl

How to use HTTP server class with openssl

References

#include "IP/InternetProtocol.hpp"

Syntax

namespace InternetProtocol
class HttpServerSsl
#include <iostream>
#define ASIO_USE_OPENSSL
#include <IP/HTTP/HTTPServer.hpp>
 
using namespace InternetProtocol;
 
int main()
{
    HttpServerSsl server;
    server.set_socket(EProtocolType::V4, 3000);
    server.on_request_received = [&](const Server::FRequest request, Server::FResponse &response, const socket_ptr &socket) {
        response.body = "Hello World!";
        server.send_response(response, socket);
    };
    server.get_ssl_context().set_options(asio::ssl::verify_none);
    server.load_private_key_file("client-key.pem"); //Relative path to your PEM file
    server.load_certificate_file("client-cert.pem"); //Relative path to your PEM file
    server.open();
 
    std::string input;
    std::cout << "Digite algo: ";
    while (std::getline(std::cin, input)) {
        if (input == "quit")
        {
            break;
        }
    }
 
    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)

On this page