Giri's C++ Support Library
C++ library providing everything you need to quickly create awesome applications.
giri::HTTPServer Class Reference

Class representing a HTTP Server. More...

#include <HTTPServer.h>

Inheritance diagram for giri::HTTPServer:
Collaboration diagram for giri::HTTPServer:

Public Types

using SPtr = std::shared_ptr< HTTPServer >
 
using UPtr = std::unique_ptr< HTTPServer >
 
using WPtr = std::weak_ptr< HTTPServer >
 
- Public Types inherited from giri::Object< T >
using SPtr = std::shared_ptr< T >
 
using UPtr = std::unique_ptr< T >
 
using WPtr = std::weak_ptr< T >
 

Public Member Functions

 HTTPServer (const std::string &address="0.0.0.0", const std::string &port="80", const std::filesystem::path &docRoot="./", const size_t numThreads=1, bool ssl=false, const std::filesystem::path &cert="", const std::filesystem::path &key="", const std::map< std::string, std::string > &mimeTypes={}, const std::string &indexFile="index.html", const std::string &serverString="giris_supportlib_http_server")
 
void run ()
 
HTTPSession::SPtr getSession () const
 
bool getSSL () const
 
std::filesystem::path getCert () const
 
std::filesystem::path getKey () const
 
std::filesystem::path getDocRoot () const
 
std::string getIndexFile () const
 
std::string getServerString () const
 
std::map< std::string, std::string > getMimeTypes () const
 
void setDocRoot (const std::filesystem::path &path)
 
void setIndexFile (const std::string &indx)
 
void setServerString (const std::string &servstr)
 
void addMimeTypes (const std::map< std::string, std::string > &mimeTypes)
 
- Public Member Functions inherited from giri::Observable< HTTPServer >
void subscribe (const std::weak_ptr< Observer< HTTPServer > > &obs)
 
void unsubscribe (const std::weak_ptr< Observer< HTTPServer > > &obs)
 
void unsubscribeAll ()
 
void notify ()
 

Detailed Description

Class representing a HTTP Server.

Example Usage:

#include <HTTPServer.h>
#include <Blob.h>
#include <iostream>
#include <string>
using namespace giri;
// observer to receive async answers.
// use std::lock_guard + std::mutex if resources of this class are
// accessed by multiple threads
class HTTPServerObserver :
public Observer<HTTPServer>,
public Observer<HTTPSession>,
public std::enable_shared_from_this<HTTPServerObserver>
{
public:
void update(HTTPServer::SPtr serv){
std::cout << "Connected... " << serv->getSession()->getClientIP() << ":" << serv->getSession()->getClientPort() << std::endl;
// subscribe to session to receive notifications on message
serv->getSession()->subscribe(this->shared_from_this());
}
void update(HTTPSession::SPtr sess){
std::cout << "Server session requested: " << sess->getRequest().target() << std::endl;
// manipulate result here if needed, set custom result as shown here:
// (Body data is stored as std::vector<char>, can be set using a Blob object)
if(sess->getRequest().target() == "/sayhi") {
http::response<http::vector_body<char>> myCustomResult;
Blob data;
data.loadString("Hello World :)");
myCustomResult = {http::status::ok, sess->getRequest().version()};
myCustomResult.set(http::field::server, sess->getServerString());
myCustomResult.set(http::field::content_type, "text/html");
myCustomResult.keep_alive(sess->getRequest().keep_alive());
myCustomResult.body().assign(data.begin(), data.end());
myCustomResult.prepare_payload();
sess->setResult(myCustomResult);
}
}
using SPtr = std::shared_ptr<HTTPServerObserver>;
using UPtr = std::unique_ptr<HTTPServerObserver>;
using WPtr = std::weak_ptr<HTTPServerObserver>;
};
int main()
{
HTTPServer::SPtr sptr = std::make_shared<HTTPServer>("0.0.0.0", "8808", "/home/giri");
HTTPServerObserver::SPtr obs = std::make_shared<HTTPServerObserver>();
sptr->subscribe(obs);
sptr->run();
while(true){}; // block until ctrl + c is pressed.
return EXIT_SUCCESS;
}
Binary large object implementation using std::vector<char>.
Simple HTTP Server implementation.
Namespace for giri's C++ support library.
Definition: Base64.h:47

Constructor & Destructor Documentation

◆ HTTPServer()

giri::HTTPServer::HTTPServer ( const std::string &  address = "0.0.0.0",
const std::string &  port = "80",
const std::filesystem::path &  docRoot = "./",
const size_t  numThreads = 1,
bool  ssl = false,
const std::filesystem::path &  cert = "",
const std::filesystem::path &  key = "",
const std::map< std::string, std::string > &  mimeTypes = {},
const std::string &  indexFile = "index.html",
const std::string &  serverString = "giris_supportlib_http_server" 
)
inline

Constructor for HTTP server.

Parameters
addressAdress to bind this server to (defaults to 0.0.0.0 for any).
portPort to listen on (defaults to 80).
docRootFolder to serve via http (defaults to "./").
numThreadsNumber of worker threads (defaults to 1).
sslEnable or disable ssl encryption (defaults to false).
certIf ssl is true path to certificate *.pem file needs to be passed.
keyIf ssl is true path to private key *.pem file needs to be passed.
mimeTypesMapping containing additional mimetypes. (will be appended to a set of default mimetypes, overrides existing defaults)
indexFileIndex file to use if no file was provided by request. (defaults to "index.html")
serverStringServer string to be added to the http answers. (defaults to "giris_supportlib_http_server")

Member Function Documentation

◆ addMimeTypes()

void giri::HTTPServer::addMimeTypes ( const std::map< std::string, std::string > &  mimeTypes)
inline

Add additional mimetypes. Only affects new HTTPSessions.

Parameters
mimeTypesAdditional mimetypes to add.

◆ getCert()

std::filesystem::path giri::HTTPServer::getCert ( ) const
inline
Returns
Path to the used certificate *.pem file.

◆ getDocRoot()

std::filesystem::path giri::HTTPServer::getDocRoot ( ) const
inline
Returns
Folder which is served via http.

◆ getIndexFile()

std::string giri::HTTPServer::getIndexFile ( ) const
inline
Returns
Default index file to be used.

◆ getKey()

std::filesystem::path giri::HTTPServer::getKey ( ) const
inline
Returns
Path to the used certificate privatekey *.pem file.

◆ getMimeTypes()

std::map<std::string, std::string> giri::HTTPServer::getMimeTypes ( ) const
inline
Returns
Mapping containing all supported mimetypes.

◆ getServerString()

std::string giri::HTTPServer::getServerString ( ) const
inline
Returns
Server string to be added to the http answers.

◆ getSession()

HTTPSession::SPtr giri::HTTPServer::getSession ( ) const
inline
Returns
last created session.

◆ getSSL()

bool giri::HTTPServer::getSSL ( ) const
inline
Returns
true if ssl is enabled, false otherwise.

◆ run()

void giri::HTTPServer::run ( )
inline

Starts receiving messages asynchrolously. Automatically creates sessions and notifies observers when accepting new connections.

◆ setDocRoot()

void giri::HTTPServer::setDocRoot ( const std::filesystem::path &  path)
inline

Set doc root path. Only affects new HTTPSessions.

Parameters
pathPath to serve html files from.

◆ setIndexFile()

void giri::HTTPServer::setIndexFile ( const std::string &  indx)
inline

Sets default index file to be used. Only affects new HTTPSessions.

Parameters
indxdefault index file to be used.

◆ setServerString()

void giri::HTTPServer::setServerString ( const std::string &  servstr)
inline

Set server string to be added to the http answers. Only affects new HTTPSessions.

Parameters
servstrserver string to be added to the http answers.

The documentation for this class was generated from the following file: