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.
httplistener.cpp
Go to the documentation of this file.
1 
6 #include "httplistener.h"
9 
10 #include <QCoreApplication>
11 
12 using namespace qtwebapp;
13 
14 HttpListener::HttpListener(QSettings* settings, HttpRequestHandler* requestHandler, QObject *parent)
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 }
27 
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 }
41 
42 
44 {
45  close();
46  qDebug("HttpListener: destroyed");
47 }
48 
49 
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 }
71 
72 
74  QTcpServer::close();
75  qDebug("HttpListener: closed");
76  if (pool) {
77  delete pool;
78  pool=NULL;
79  }
80 }
81 
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 }
void incomingConnection(tSocketDescriptor socketDescriptor)
HttpConnectionHandlerPool * pool
Definition: httplistener.h:115
HttpRequestHandler * requestHandler
Definition: httplistener.h:112
HttpListener(QSettings *settings, HttpRequestHandler *requestHandler, QObject *parent=NULL)
HttpListenerSettings listenerSettings
Definition: httplistener.h:109