join 1.0
lightweight network framework library
Loading...
Searching...
No Matches
join::BasicHttpServer< Protocol > Class Template Reference

basic HTTP server. More...

#include <httpserver.hpp>

Inheritance diagram for join::BasicHttpServer< Protocol >:
Collaboration diagram for join::BasicHttpServer< Protocol >:

Public Types

using Worker = BasicHttpWorker <Protocol>
 
using Content = BasicHttpContent <Protocol>
 
using Handler = typename Content::Handler
 
using Access = typename Content::Access
 
using Endpoint = typename Protocol::Endpoint
 
using Socket = typename Protocol::Socket
 
using Acceptor = typename Protocol::Acceptor
 

Public Member Functions

 BasicHttpServer (size_t workers=std::thread::hardware_concurrency())
 create the HTTP server instance.
 
 BasicHttpServer (const BasicHttpServer &other)=delete
 create instance by copy.
 
BasicHttpServeroperator= (const BasicHttpServer &other)=delete
 assign instance by copy.
 
 BasicHttpServer (BasicHttpServer &&other)=delete
 create instance by move.
 
BasicHttpServeroperator= (BasicHttpServer &&other)=delete
 assign instance by move.
 
virtual ~BasicHttpServer ()
 destroy the HTTP server.
 
int create (const Endpoint &endpoint) noexcept
 create server.
 
void close () noexcept
 close server.
 
virtual Socket accept () const
 accept new connection.
 
void baseLocation (const std::string &path)
 set file base location.
 
const std::string & baseLocation () const
 get file base location.
 
void keepAlive (std::chrono::seconds timeout, int max=1000)
 set HTTP keep alive.
 
std::chrono::seconds keepAliveTimeout () const
 get HTTP keep alive timeout.
 
int keepAliveMax () const
 get HTTP keep alive max.
 
virtual std::string scheme () const
 get scheme.
 
ContentaddDocumentRoot (const std::string &dir, const std::string &name, const Access &access=nullptr)
 map an URL to filesystem adding URL path to the base location.
 
ContentaddAlias (const std::string &dir, const std::string &name, const std::string &alias, const Access &access=nullptr)
 map an URL to filesystem replacing URL path by the specified path.
 
ContentaddExecute (const HttpMethod methods, const std::string &dir, const std::string &name, const Handler &handler, const Access &access=nullptr)
 map an URL to a callback.
 
ContentaddRedirect (const std::string &dir, const std::string &name, const std::string &location, const Access &access=nullptr)
 map an URL to a redirection.
 

Protected Member Functions

ContentfindContent (HttpMethod method, const std::string &path) const
 find content.
 

Protected Attributes

Acceptor _acceptor
 acceptor.
 
int _event = -1
 gracefully stop all workers.
 
size_t _nworkers
 number of workers.
 
std::vector< std::unique_ptr< Worker > > _workers
 workers.
 
Mutex _mutex
 accept protection mutex.
 
std::vector< std::unique_ptr< Content > > _contents
 contents.
 
std::string _baseLocation
 base location.
 
std::chrono::seconds _keepTimeout
 keep alive timeout.
 
int _keepMax = 1000
 keep alive max.
 
Cache _cache
 file cache.
 
friend Worker
 friendship with worker.
 

Detailed Description

template<class Protocol>
class join::BasicHttpServer< Protocol >

basic HTTP server.

Member Typedef Documentation

◆ Acceptor

template<class Protocol >
using join::BasicHttpServer< Protocol >::Acceptor = typename Protocol::Acceptor

◆ Access

template<class Protocol >
using join::BasicHttpServer< Protocol >::Access = typename Content::Access

◆ Content

template<class Protocol >
using join::BasicHttpServer< Protocol >::Content = BasicHttpContent <Protocol>

◆ Endpoint

template<class Protocol >
using join::BasicHttpServer< Protocol >::Endpoint = typename Protocol::Endpoint

◆ Handler

template<class Protocol >
using join::BasicHttpServer< Protocol >::Handler = typename Content::Handler

◆ Socket

template<class Protocol >
using join::BasicHttpServer< Protocol >::Socket = typename Protocol::Socket

◆ Worker

template<class Protocol >
using join::BasicHttpServer< Protocol >::Worker = BasicHttpWorker <Protocol>

Constructor & Destructor Documentation

◆ BasicHttpServer() [1/3]

template<class Protocol >
join::BasicHttpServer< Protocol >::BasicHttpServer ( size_t workers = std::thread::hardware_concurrency ())
inline

create the HTTP server instance.

Parameters
workersnumber of worker threads.

◆ BasicHttpServer() [2/3]

template<class Protocol >
join::BasicHttpServer< Protocol >::BasicHttpServer ( const BasicHttpServer< Protocol > & other)
delete

create instance by copy.

Parameters
otherobject to copy.

◆ BasicHttpServer() [3/3]

template<class Protocol >
join::BasicHttpServer< Protocol >::BasicHttpServer ( BasicHttpServer< Protocol > && other)
delete

create instance by move.

Parameters
otherobject to move.

◆ ~BasicHttpServer()

template<class Protocol >
virtual join::BasicHttpServer< Protocol >::~BasicHttpServer ( )
inlinevirtual

destroy the HTTP server.

Member Function Documentation

◆ accept()

template<class Protocol >
virtual Socket join::BasicHttpServer< Protocol >::accept ( ) const
inlinevirtual

accept new connection.

Returns
the accepted client socket object.

Reimplemented in join::BasicHttpSecureServer< Protocol >.

◆ addAlias()

template<class Protocol >
Content * join::BasicHttpServer< Protocol >::addAlias ( const std::string & dir,
const std::string & name,
const std::string & alias,
const Access & access = nullptr )
inline

map an URL to filesystem replacing URL path by the specified path.

Parameters
dirdirectory.
namefile name.
aliascorresponding file system path.
accessaccess handler.
Returns
a pointer to the content on success, nullptr on failure.

◆ addDocumentRoot()

template<class Protocol >
Content * join::BasicHttpServer< Protocol >::addDocumentRoot ( const std::string & dir,
const std::string & name,
const Access & access = nullptr )
inline

map an URL to filesystem adding URL path to the base location.

Parameters
dirdirectory.
namefile name.
accessaccess handler.
Returns
a pointer to the content on success, nullptr on failure.

◆ addExecute()

template<class Protocol >
Content * join::BasicHttpServer< Protocol >::addExecute ( const HttpMethod methods,
const std::string & dir,
const std::string & name,
const Handler & handler,
const Access & access = nullptr )
inline

map an URL to a callback.

Parameters
methodsallowed methods.
dirdirectory.
namefile name.
handlercontent handler.
accessaccess handler.
Returns
a pointer to the content on success, nullptr on failure.

◆ addRedirect()

template<class Protocol >
Content * join::BasicHttpServer< Protocol >::addRedirect ( const std::string & dir,
const std::string & name,
const std::string & location,
const Access & access = nullptr )
inline

map an URL to a redirection.

Parameters
dirdirectory.
namefile name.
locationlocation where to do the redirection.
accessaccess handler.
Returns
a pointer to the content on success, nullptr on failure.

◆ baseLocation() [1/2]

template<class Protocol >
const std::string & join::BasicHttpServer< Protocol >::baseLocation ( ) const
inline

get file base location.

Returns
file base location path.

◆ baseLocation() [2/2]

template<class Protocol >
void join::BasicHttpServer< Protocol >::baseLocation ( const std::string & path)
inline

set file base location.

Parameters
pathfile base location path.

◆ close()

template<class Protocol >
void join::BasicHttpServer< Protocol >::close ( )
inlinenoexcept

close server.

◆ create()

template<class Protocol >
int join::BasicHttpServer< Protocol >::create ( const Endpoint & endpoint)
inlinenoexcept

create server.

Parameters
endpointendpoint to assign to the server.
Returns
0 on success, -1 on failure.

◆ findContent()

template<class Protocol >
Content * join::BasicHttpServer< Protocol >::findContent ( HttpMethod method,
const std::string & path ) const
inlineprotected

find content.

Parameters
methodmethod.
pathresource path.
Returns
a pointer to the content on success, nullptr on failure.

◆ keepAlive()

template<class Protocol >
void join::BasicHttpServer< Protocol >::keepAlive ( std::chrono::seconds timeout,
int max = 1000 )
inline

set HTTP keep alive.

Parameters
timeoutkeep alive timeout (default 10 secs).
maxkeep alive max (default 1000).

◆ keepAliveMax()

template<class Protocol >
int join::BasicHttpServer< Protocol >::keepAliveMax ( ) const
inline

get HTTP keep alive max.

Returns
HTTP keep alive max.

◆ keepAliveTimeout()

template<class Protocol >
std::chrono::seconds join::BasicHttpServer< Protocol >::keepAliveTimeout ( ) const
inline

get HTTP keep alive timeout.

Returns
HTTP keep alive timeout.

◆ operator=() [1/2]

template<class Protocol >
BasicHttpServer & join::BasicHttpServer< Protocol >::operator= ( BasicHttpServer< Protocol > && other)
delete

assign instance by move.

Parameters
otherobject to move.
Returns
a reference of the current object.

◆ operator=() [2/2]

template<class Protocol >
BasicHttpServer & join::BasicHttpServer< Protocol >::operator= ( const BasicHttpServer< Protocol > & other)
delete

assign instance by copy.

Parameters
otherobject to copy.
Returns
a reference of the current object.

◆ scheme()

template<class Protocol >
virtual std::string join::BasicHttpServer< Protocol >::scheme ( ) const
inlinevirtual

get scheme.

Returns
htpp or https.

Reimplemented in join::BasicHttpSecureServer< Protocol >.

Member Data Documentation

◆ _acceptor

template<class Protocol >
Acceptor join::BasicHttpServer< Protocol >::_acceptor
protected

acceptor.

◆ _baseLocation

template<class Protocol >
std::string join::BasicHttpServer< Protocol >::_baseLocation
protected

base location.

◆ _cache

template<class Protocol >
Cache join::BasicHttpServer< Protocol >::_cache
protected

file cache.

◆ _contents

template<class Protocol >
std::vector<std::unique_ptr <Content> > join::BasicHttpServer< Protocol >::_contents
protected

contents.

◆ _event

template<class Protocol >
int join::BasicHttpServer< Protocol >::_event = -1
protected

gracefully stop all workers.

◆ _keepMax

template<class Protocol >
int join::BasicHttpServer< Protocol >::_keepMax = 1000
protected

keep alive max.

◆ _keepTimeout

template<class Protocol >
std::chrono::seconds join::BasicHttpServer< Protocol >::_keepTimeout
protected

keep alive timeout.

◆ _mutex

template<class Protocol >
Mutex join::BasicHttpServer< Protocol >::_mutex
protected

accept protection mutex.

◆ _nworkers

template<class Protocol >
size_t join::BasicHttpServer< Protocol >::_nworkers
protected

number of workers.

◆ _workers

template<class Protocol >
std::vector<std::unique_ptr <Worker> > join::BasicHttpServer< Protocol >::_workers
protected

workers.

◆ Worker

template<class Protocol >
friend join::BasicHttpServer< Protocol >::Worker
protected

friendship with worker.


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