SDRAngel  4.11.5
Developer docs for <a href="https://github.com/f4exb/sdrangel">SDRangel<\a>, an Open Source Qt5 / OpenGL 3.0+ SDR and signal analyzer frontend to various hardware.
Signals | Public Member Functions | Protected Member Functions | Private Attributes | List of all members
qtwebapp::HttpListener Class Reference

#include <httplistener.h>

Inherits QTcpServer.

+ Collaboration diagram for qtwebapp::HttpListener:

Signals

void handleConnection (tSocketDescriptor socketDescriptor)
 

Public Member Functions

 HttpListener (QSettings *settings, HttpRequestHandler *requestHandler, QObject *parent=NULL)
 
 HttpListener (const HttpListenerSettings &settings, HttpRequestHandler *requestHandler, QObject *parent=NULL)
 
virtual ~HttpListener ()
 
void listen ()
 
void close ()
 
HttpListenerSettings getListenerSettings () const
 
void setListenerSettings (const HttpListenerSettings &settings)
 

Protected Member Functions

void incomingConnection (tSocketDescriptor socketDescriptor)
 

Private Attributes

QSettings * settings
 
HttpListenerSettings listenerSettings
 
HttpRequestHandlerrequestHandler
 
HttpConnectionHandlerPoolpool
 
bool useQtSettings
 

Detailed Description

Listens for incoming TCP connections and and passes all incoming HTTP requests to your implementation of HttpRequestHandler, which processes the request and generates the response (usually a HTML document).

Example for the required settings in the config file:

;host=192.168.0.100
port=8080
minThreads=1
maxThreads=10
cleanupInterval=1000
readTimeout=60000
;sslKeyFile=ssl/my.key
;sslCertFile=ssl/my.cert
maxRequestSize=16000
maxMultiPartSize=1000000

The optional host parameter binds the listener to one network interface. The listener handles all network interfaces if no host is configured. The port number specifies the incoming TCP port that this listener listens to.

See also
HttpConnectionHandlerPool for description of config settings minThreads, maxThreads, cleanupInterval and ssl settings
HttpConnectionHandler for description of the readTimeout
HttpRequest for description of config settings maxRequestSize and maxMultiPartSize

Definition at line 47 of file httplistener.h.

Constructor & Destructor Documentation

◆ HttpListener() [1/2]

HttpListener::HttpListener ( QSettings *  settings,
HttpRequestHandler requestHandler,
QObject *  parent = NULL 
)

Constructor. Creates a connection pool and starts listening on the configured host and port.

Parameters
settingsConfiguration settings for the HTTP server. Must not be 0.
requestHandlerProcesses each received HTTP request, usually by dispatching to controller classes.
parentParent object.
Warning
Ensure to close or delete the listener before deleting the request handler.

Definition at line 14 of file httplistener.cpp.

References listen(), pool, requestHandler, and settings.

15  : QTcpServer(parent), useQtSettings(true)
16 {
17  Q_ASSERT(settings != 0);
18  Q_ASSERT(requestHandler != 0);
19  pool = 0;
20  this->settings = settings;
21  this->requestHandler = requestHandler;
22  // Reqister type of socketDescriptor for signal/slot handling
23  qRegisterMetaType<tSocketDescriptor>("tSocketDescriptor");
24  // Start listening
25  listen();
26 }
HttpConnectionHandlerPool * pool
Definition: httplistener.h:115
HttpRequestHandler * requestHandler
Definition: httplistener.h:112
+ Here is the call graph for this function:

◆ HttpListener() [2/2]

HttpListener::HttpListener ( const HttpListenerSettings settings,
HttpRequestHandler requestHandler,
QObject *  parent = NULL 
)

Constructor. Creates a connection pool and starts listening on the configured host and port.

Parameters
settingsConfiguration settings for the HTTP server as a structure.
requestHandlerProcesses each received HTTP request, usually by dispatching to controller classes.
parentParent object.
Warning
Ensure to close or delete the listener before deleting the request handler.

Definition at line 28 of file httplistener.cpp.

References listen(), listenerSettings, pool, requestHandler, and settings.

29  : QTcpServer(parent), useQtSettings(false)
30 {
31  Q_ASSERT(requestHandler != 0);
32  pool = 0;
33  this->settings = 0;
35  this->requestHandler = requestHandler;
36  // Reqister type of socketDescriptor for signal/slot handling
37  qRegisterMetaType<tSocketDescriptor>("tSocketDescriptor");
38  // Start listening
39  listen();
40 }
HttpConnectionHandlerPool * pool
Definition: httplistener.h:115
HttpRequestHandler * requestHandler
Definition: httplistener.h:112
HttpListenerSettings listenerSettings
Definition: httplistener.h:109
+ Here is the call graph for this function:

◆ ~HttpListener()

HttpListener::~HttpListener ( )
virtual

Destructor

Definition at line 43 of file httplistener.cpp.

References close().

44 {
45  close();
46  qDebug("HttpListener: destroyed");
47 }
+ Here is the call graph for this function:

Member Function Documentation

◆ close()

void HttpListener::close ( )

Closes the listener, waits until all pending requests are processed, then closes the connection pool.

Definition at line 73 of file httplistener.cpp.

References pool.

Referenced by ~HttpListener().

73  {
74  QTcpServer::close();
75  qDebug("HttpListener: closed");
76  if (pool) {
77  delete pool;
78  pool=NULL;
79  }
80 }
HttpConnectionHandlerPool * pool
Definition: httplistener.h:115
+ Here is the caller graph for this function:

◆ getListenerSettings()

HttpListenerSettings qtwebapp::HttpListener::getListenerSettings ( ) const
inline

Get a listener settings copy

Returns
The current listener settings

Definition at line 90 of file httplistener.h.

90 { return listenerSettings; }
HttpListenerSettings listenerSettings
Definition: httplistener.h:109

◆ handleConnection

void qtwebapp::HttpListener::handleConnection ( tSocketDescriptor  socketDescriptor)
signal

Sent to the connection handler to process a new incoming connection.

Parameters
socketDescriptorreferences the accepted connection.

◆ incomingConnection()

void HttpListener::incomingConnection ( tSocketDescriptor  socketDescriptor)
protected

Serves new incoming connection requests

Definition at line 82 of file httplistener.cpp.

References qtwebapp::HttpConnectionHandlerPool::getConnectionHandler(), and pool.

82  {
83 #ifdef SUPERVERBOSE
84  qDebug("HttpListener: New connection");
85 #endif
86 
87  HttpConnectionHandler* freeHandler=NULL;
88  if (pool)
89  {
90  freeHandler=pool->getConnectionHandler();
91  }
92 
93  // Let the handler process the new connection.
94  if (freeHandler)
95  {
96  // The descriptor is passed via event queue because the handler lives in another thread
97  QMetaObject::invokeMethod(freeHandler, "handleConnection", Qt::QueuedConnection, Q_ARG(tSocketDescriptor, socketDescriptor));
98  }
99  else
100  {
101  // Reject the connection
102  qDebug("HttpListener: Too many incoming connections");
103  QTcpSocket* socket=new QTcpSocket(this);
104  socket->setSocketDescriptor(socketDescriptor);
105  connect(socket, SIGNAL(disconnected()), socket, SLOT(deleteLater()));
106  socket->write("HTTP/1.1 503 too many connections\r\nConnection: close\r\n\r\nToo many connections\r\n");
107  socket->disconnectFromHost();
108  }
109 }
HttpConnectionHandlerPool * pool
Definition: httplistener.h:115
+ Here is the call graph for this function:

◆ listen()

void HttpListener::listen ( )

Restart listeing after close().

Definition at line 50 of file httplistener.cpp.

References qtwebapp::HttpListenerSettings::host, listenerSettings, pool, qtwebapp::HttpListenerSettings::port, requestHandler, settings, and useQtSettings.

Referenced by HttpListener().

51 {
52  if (!pool)
53  {
54  if (useQtSettings) {
56  } else {
58  }
59  }
60  QString host = useQtSettings ? settings->value("host").toString() : listenerSettings.host;
61  int port = useQtSettings ? settings->value("port").toInt() : listenerSettings.port;
62  QTcpServer::listen(host.isEmpty() ? QHostAddress::Any : QHostAddress(host), port);
63  if (!isListening())
64  {
65  qCritical("HttpListener: Cannot bind on port %i: %s",port,qPrintable(errorString()));
66  }
67  else {
68  qDebug("HttpListener: Listening on port %i",port);
69  }
70 }
HttpConnectionHandlerPool * pool
Definition: httplistener.h:115
HttpRequestHandler * requestHandler
Definition: httplistener.h:112
HttpListenerSettings listenerSettings
Definition: httplistener.h:109
+ Here is the caller graph for this function:

◆ setListenerSettings()

void qtwebapp::HttpListener::setListenerSettings ( const HttpListenerSettings settings)
inline

Set new listener settings data

Parameters
Listenersettings to replace current data

Definition at line 96 of file httplistener.h.

HttpListenerSettings listenerSettings
Definition: httplistener.h:109

Member Data Documentation

◆ listenerSettings

HttpListenerSettings qtwebapp::HttpListener::listenerSettings
private

Configuration settings for the HTTP server as a structure

Definition at line 109 of file httplistener.h.

Referenced by HttpListener(), and listen().

◆ pool

HttpConnectionHandlerPool* qtwebapp::HttpListener::pool
private

Pool of connection handlers

Definition at line 115 of file httplistener.h.

Referenced by close(), HttpListener(), incomingConnection(), and listen().

◆ requestHandler

HttpRequestHandler* qtwebapp::HttpListener::requestHandler
private

Point to the reuqest handler which processes all HTTP requests

Definition at line 112 of file httplistener.h.

Referenced by HttpListener(), and listen().

◆ settings

QSettings* qtwebapp::HttpListener::settings
private

Configuration settings for the HTTP server

Definition at line 106 of file httplistener.h.

Referenced by HttpListener(), and listen().

◆ useQtSettings

bool qtwebapp::HttpListener::useQtSettings
private

Settings flag

Definition at line 118 of file httplistener.h.

Referenced by listen().


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