MythTV  master
Public Slots | Signals | Protected Slots | Protected Member Functions | Private Member Functions | Static Private Member Functions | Private Attributes | Friends | List of all members
MythHTTPServer Class Reference

#include <mythhttpserver.h>

Inheritance diagram for MythHTTPServer:
Inheritance graph
[legend]
Collaboration diagram for MythHTTPServer:
Collaboration graph
[legend]

Public Slots

void ThreadFinished ()
 
- Public Slots inherited from MythHTTPThreadPool
void ThreadFinished ()
 
void ThreadUpgraded (QThread *Thread)
 

Signals

void EnableHTTP (bool Enable)
 
void AddPaths (const QStringList &Paths)
 
void RemovePaths (const QStringList &Paths)
 
void AddHandlers (const HTTPHandlers &Handlers)
 
void RemoveHandlers (const HTTPHandlers &Handlers)
 
void AddServices (const HTTPServices &Services)
 
void RemoveServices (const HTTPServices &Services)
 
void AddErrorPageHandler (const HTTPHandler &Handler)
 
void PathsChanged (const QStringList &Paths)
 
void HandlersChanged (const HTTPHandlers &Handlers)
 
void ServicesChanged (const HTTPServices &Services)
 
void OriginsChanged (const QStringList &Origins)
 
void HostsChanged (const QStringList &Hosts)
 
void ErrorHandlerChanged (const HTTPHandler &Handler)
 
void MasterResolved (QHostInfo Info)
 
void HostResolved (QHostInfo Info)
 
void ProcessTCPQueue ()
 
- Signals inherited from ServerPool
void newConnection (QTcpSocket *)
 
void newDatagram (QByteArray, QHostAddress, quint16)
 

Protected Slots

void newTcpConnection (qintptr socket) override
 
void EnableDisable (bool Enable)
 
void NewPaths (const QStringList &Paths)
 Add new paths that will serve simple files. More...
 
void StalePaths (const QStringList &Paths)
 
void NewHandlers (const HTTPHandlers &Handlers)
 Add new handlers. More...
 
void StaleHandlers (const HTTPHandlers &Handlers)
 
void NewServices (const HTTPServices &Services)
 
void StaleServices (const HTTPServices &Services)
 
void ResolveMaster (QHostInfo Info)
 Add master backend addresses to the allowed Origins list. More...
 
void ResolveHost (QHostInfo Info)
 Add the results of a reverse lookup to our allowed list. More...
 
void NewErrorPageHandler (const HTTPHandler &Handler)
 Add new error page handler. More...
 
void ProcessTCPQueueHandler ()
 
- Protected Slots inherited from ServerPool
virtual void newUdpDatagram (void)
 
virtual void newTcpConnection (qintptr socket)
 

Protected Member Functions

 MythHTTPServer ()
 
 ~MythHTTPServer () override
 

Private Member Functions

void Init ()
 Initialise server configuration. More...
 
void Started (bool Tcp, bool Ssl)
 
void Stopped ()
 
void BuildHosts ()
 
void BuildOrigins ()
 Generate a list of allowed 'Origins' for validating CORS requests. More...
 
void DebugHosts ()
 
void DebugOrigins ()
 

Static Private Member Functions

static bool ReservedPath (const QString &Path)
 
static QStringList BuildAddressList (QHostInfo &Info)
 

Private Attributes

int m_originLookups { 0 }
 
int m_hostLookups { 0 }
 
MythHTTPConfig m_config
 
int m_masterStatusPort { 0 }
 
int m_masterSSLPort { 0 }
 
QString m_masterIPAddress
 
QQueue< qintptr > m_connectionQueue
 
int m_threadNum { 0 }
 

Friends

class MythHTTPInstance
 

Additional Inherited Members

- Public Member Functions inherited from MythHTTPThreadPool
 MythHTTPThreadPool ()
 
 ~MythHTTPThreadPool () override
 
size_t AvailableThreads () const
 
size_t MaxThreads () const
 
size_t ThreadCount () const
 
void AddThread (MythHTTPThread *Thread)
 
- Public Member Functions inherited from ServerPool
 ServerPool (QObject *parent=nullptr)
 
 ~ServerPool (void) override
 
bool listen (QList< QHostAddress > addrs, quint16 port, bool requireall=true, PoolServerType type=kTCPServer)
 
bool listen (QStringList addrs, quint16 port, bool requireall=true, PoolServerType type=kTCPServer)
 
bool listen (quint16 port, bool requireall=true, PoolServerType type=kTCPServer)
 
bool bind (QList< QHostAddress > addrs, quint16 port, bool requireall=true)
 
bool bind (QStringList addrs, quint16 port, bool requireall=true)
 
bool bind (quint16 port, bool requireall=true)
 
qint64 writeDatagram (const char *data, qint64 size, const QHostAddress &addr, quint16 port)
 
qint64 writeDatagram (const QByteArray &datagram, const QHostAddress &addr, quint16 port)
 
bool isListening (void) const
 
int maxPendingConnections (void) const
 
void setMaxPendingConnections (int n)
 
quint16 serverPort (void) const
 
QNetworkProxy proxy (void)
 
void setProxy (const QNetworkProxy &proxy)
 
void close (void)
 
int tryListeningPort (int baseport, int range=1)
 tryListeningPort More...
 
int tryBindingPort (int baseport, int range=1)
 tryBindingPort More...
 
- Static Public Member Functions inherited from ServerPool
static void RefreshDefaultListen (void)
 
static QList< QHostAddress > DefaultListen (void)
 
static QList< QHostAddress > DefaultListenIPv4 (void)
 
static QList< QHostAddress > DefaultListenIPv6 (void)
 
static QList< QHostAddress > DefaultBroadcast (void)
 
static QList< QHostAddress > DefaultBroadcastIPv4 (void)
 
static QList< QHostAddress > DefaultBroadcastIPv6 (void)
 
static int tryListeningPort (QTcpServer *server, int baseport, int range=1, bool *isipv6=nullptr)
 tryListeningPort More...
 
static int tryBindingPort (QUdpSocket *socket, int baseport, int range=1, bool *isipv6=nullptr)
 tryBindingPort More...
 

Detailed Description

Definition at line 12 of file mythhttpserver.h.

Constructor & Destructor Documentation

◆ MythHTTPServer()

MythHTTPServer::MythHTTPServer ( )
protected

Definition at line 31 of file mythhttpserver.cpp.

◆ ~MythHTTPServer()

MythHTTPServer::~MythHTTPServer ( )
overrideprotected

Definition at line 71 of file mythhttpserver.cpp.

Member Function Documentation

◆ EnableHTTP

void MythHTTPServer::EnableHTTP ( bool  Enable)
signal

◆ AddPaths

void MythHTTPServer::AddPaths ( const QStringList &  Paths)
signal

◆ RemovePaths

void MythHTTPServer::RemovePaths ( const QStringList &  Paths)
signal

◆ AddHandlers

void MythHTTPServer::AddHandlers ( const HTTPHandlers Handlers)
signal

◆ RemoveHandlers

void MythHTTPServer::RemoveHandlers ( const HTTPHandlers Handlers)
signal

◆ AddServices

void MythHTTPServer::AddServices ( const HTTPServices Services)
signal

◆ RemoveServices

void MythHTTPServer::RemoveServices ( const HTTPServices Services)
signal

◆ AddErrorPageHandler

void MythHTTPServer::AddErrorPageHandler ( const HTTPHandler Handler)
signal

◆ PathsChanged

void MythHTTPServer::PathsChanged ( const QStringList &  Paths)
signal

◆ HandlersChanged

void MythHTTPServer::HandlersChanged ( const HTTPHandlers Handlers)
signal

◆ ServicesChanged

void MythHTTPServer::ServicesChanged ( const HTTPServices Services)
signal

◆ OriginsChanged

void MythHTTPServer::OriginsChanged ( const QStringList &  Origins)
signal

◆ HostsChanged

void MythHTTPServer::HostsChanged ( const QStringList &  Hosts)
signal

Referenced by ResolveHost(), and MythHTTPThread::run().

◆ ErrorHandlerChanged

void MythHTTPServer::ErrorHandlerChanged ( const HTTPHandler Handler)
signal

Referenced by NewErrorPageHandler().

◆ MasterResolved

void MythHTTPServer::MasterResolved ( QHostInfo  Info)
signal

Referenced by BuildOrigins(), and MythHTTPServer().

◆ HostResolved

void MythHTTPServer::HostResolved ( QHostInfo  Info)
signal

Referenced by BuildHosts(), and MythHTTPServer().

◆ ProcessTCPQueue

void MythHTTPServer::ProcessTCPQueue ( )
signal

◆ ThreadFinished

void MythHTTPServer::ThreadFinished ( )
slot

Definition at line 241 of file mythhttpserver.cpp.

Referenced by ProcessTCPQueueHandler().

◆ newTcpConnection

void MythHTTPServer::newTcpConnection ( qintptr  socket)
overrideprotectedslot

Definition at line 267 of file mythhttpserver.cpp.

◆ EnableDisable

void MythHTTPServer::EnableDisable ( bool  Enable)
protectedslot

Definition at line 76 of file mythhttpserver.cpp.

Referenced by MythHTTPServer().

◆ NewPaths

void MythHTTPServer::NewPaths ( const QStringList &  Paths)
protectedslot

Add new paths that will serve simple files.

File paths should not be duplicated (as it will reduce performance) but this function will only warn when an attempt is made to add a path for a second time. This ensures the server's behaviour does not break if a duplicated path is removed dynamically (i.e. before the server quits).

File paths are handled after all other handlers and are only called if another handler for the given path does not provide a response. This allows a dynamic handler to process specific requests for the given path, falling back to a regular file handler for anything it does not recognise.

Paths are not recursive and each directory must be added explicitly (e.g. "/images/" will only serve files from the /images/ directory and not /images/icons/). This improves performance and adds an additional modicum of security.

It is assumed file paths are largely used for static server content (e.g. css, images etc). As a result, caching defaults to 'long life', authorisation is never requested and there is no filtering of files (i.e. any request for a readable file in the given directory will result in the file being sent).

Definition at line 308 of file mythhttpserver.cpp.

Referenced by MythHTTPServer().

◆ StalePaths

void MythHTTPServer::StalePaths ( const QStringList &  Paths)
protectedslot

Definition at line 325 of file mythhttpserver.cpp.

Referenced by MythHTTPServer().

◆ NewHandlers

void MythHTTPServer::NewHandlers ( const HTTPHandlers Handlers)
protectedslot

Add new handlers.

Handlers allow dynamic content to be served for the given path.

The underlying function reference is a std::function. This allows the handler to be a lambda, a functor, a static member function or a full blown member function

  • all with parameter re-ordering, defaults etc.

It should be noted that the supplied function will be called from multiple threads, so the code must ensure thread safety. The webserver code attempts to maximise concurrency (and hence responsiveness) by avoiding blocking the socket thread. It is expected that individual handlers will attempt to do the same.

As for regular file paths, handlers paths are not recursive.

Note
We do not allow duplication of paths with handlers as there is no way to compare std::function for equality - hence we cannot differentiate between std::pair<"/",FuncXXX> and std::pair<"/",FuncYYY> and hence cannot remove a handler unambiguously.

Definition at line 361 of file mythhttpserver.cpp.

Referenced by MythHTTPServer().

◆ StaleHandlers

void MythHTTPServer::StaleHandlers ( const HTTPHandlers Handlers)
protectedslot

Definition at line 385 of file mythhttpserver.cpp.

Referenced by MythHTTPServer().

◆ NewServices

void MythHTTPServer::NewServices ( const HTTPServices Services)
protectedslot

Definition at line 402 of file mythhttpserver.cpp.

Referenced by MythHTTPServer().

◆ StaleServices

void MythHTTPServer::StaleServices ( const HTTPServices Services)
protectedslot

Definition at line 426 of file mythhttpserver.cpp.

Referenced by MythHTTPServer().

◆ ResolveMaster

void MythHTTPServer::ResolveMaster ( QHostInfo  Info)
protectedslot

Add master backend addresses to the allowed Origins list.

The master backend may be using both http and https so we need to account for both here.

Definition at line 558 of file mythhttpserver.cpp.

Referenced by MythHTTPServer().

◆ ResolveHost

void MythHTTPServer::ResolveHost ( QHostInfo  Info)
protectedslot

Add the results of a reverse lookup to our allowed list.

Definition at line 592 of file mythhttpserver.cpp.

Referenced by MythHTTPServer().

◆ NewErrorPageHandler

void MythHTTPServer::NewErrorPageHandler ( const HTTPHandler Handler)
protectedslot

Add new error page handler.

Handler to return error page when no other content is found.

Used for single page web apps (eg. backend web app)

Definition at line 450 of file mythhttpserver.cpp.

Referenced by MythHTTPServer().

◆ ProcessTCPQueueHandler

void MythHTTPServer::ProcessTCPQueueHandler ( )
protectedslot

Definition at line 249 of file mythhttpserver.cpp.

Referenced by MythHTTPServer().

◆ Init()

void MythHTTPServer::Init ( )
private

Initialise server configuration.

This is called before we ask the server to start listening, which allows us to stop and restart the server with different settings (new port etc)

Definition at line 125 of file mythhttpserver.cpp.

Referenced by EnableDisable().

◆ Started()

void MythHTTPServer::Started ( bool  Tcp,
bool  Ssl 
)
private

Definition at line 190 of file mythhttpserver.cpp.

Referenced by EnableDisable().

◆ Stopped()

void MythHTTPServer::Stopped ( )
private

Definition at line 226 of file mythhttpserver.cpp.

Referenced by EnableDisable(), Init(), and ~MythHTTPServer().

◆ BuildHosts()

void MythHTTPServer::BuildHosts ( )
private

Definition at line 457 of file mythhttpserver.cpp.

Referenced by Started().

◆ BuildOrigins()

void MythHTTPServer::BuildOrigins ( )
private

Generate a list of allowed 'Origins' for validating CORS requests.

This is not as straightforward as it might first appear. We need the IP addresses AND host names for all origins that are deemed safe. This is limited to the current machine (for addresses/interfaces that ServerPool is listening on), the master backend and any additional overrides from the 'AllowedOriginsList' setting. Furthermore the port is optional and there will be the usual additional complications for IPv6 addresses...

Note
DO NOT USE the 'Access-Control-Allow-Origin' wildcard.

Definition at line 513 of file mythhttpserver.cpp.

Referenced by Started().

◆ DebugHosts()

void MythHTTPServer::DebugHosts ( )
private

Definition at line 611 of file mythhttpserver.cpp.

Referenced by ResolveHost().

◆ DebugOrigins()

void MythHTTPServer::DebugOrigins ( )
private

Definition at line 579 of file mythhttpserver.cpp.

Referenced by ResolveMaster().

◆ ReservedPath()

bool MythHTTPServer::ReservedPath ( const QString &  Path)
staticprivate

Definition at line 276 of file mythhttpserver.cpp.

Referenced by NewHandlers(), NewPaths(), and NewServices().

◆ BuildAddressList()

QStringList MythHTTPServer::BuildAddressList ( QHostInfo &  Info)
staticprivate

Definition at line 534 of file mythhttpserver.cpp.

Referenced by ResolveHost(), and ResolveMaster().

Friends And Related Function Documentation

◆ MythHTTPInstance

friend class MythHTTPInstance
friend

Definition at line 16 of file mythhttpserver.h.

Member Data Documentation

◆ m_originLookups

int MythHTTPServer::m_originLookups { 0 }
private

Definition at line 77 of file mythhttpserver.h.

Referenced by BuildOrigins(), and ResolveMaster().

◆ m_hostLookups

int MythHTTPServer::m_hostLookups { 0 }
private

Definition at line 78 of file mythhttpserver.h.

Referenced by BuildHosts(), and ResolveHost().

◆ m_config

MythHTTPConfig MythHTTPServer::m_config
private

◆ m_masterStatusPort

int MythHTTPServer::m_masterStatusPort { 0 }
private

Definition at line 80 of file mythhttpserver.h.

Referenced by Init(), and ResolveMaster().

◆ m_masterSSLPort

int MythHTTPServer::m_masterSSLPort { 0 }
private

Definition at line 81 of file mythhttpserver.h.

Referenced by Init(), and ResolveMaster().

◆ m_masterIPAddress

QString MythHTTPServer::m_masterIPAddress
private

Definition at line 82 of file mythhttpserver.h.

Referenced by BuildOrigins(), and Init().

◆ m_connectionQueue

QQueue<qintptr> MythHTTPServer::m_connectionQueue
private

Definition at line 83 of file mythhttpserver.h.

Referenced by newTcpConnection(), ProcessTCPQueueHandler(), and ThreadFinished().

◆ m_threadNum

int MythHTTPServer::m_threadNum { 0 }
private

Definition at line 84 of file mythhttpserver.h.

Referenced by ProcessTCPQueueHandler().


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