Hobrasoft httpd server
Embedded HTTP server for Qt and C++
httpserver.cpp
Go to the documentation of this file.
1 
7 #include "httpserver.h"
8 #include "httpsettings.h"
9 #include "httpconnection.h"
10 #include "httprequesthandler.h"
11 #include "httptcpserver.h"
12 #include <QSslSocket>
13 #include <QPointer>
14 #include <QThread>
15 
16 using namespace HobrasoftHttpd;
17 
18 
19 HttpServer::HttpServer(QObject *parent) : QObject(parent) {
20  m_server = NULL;
21  m_settings = new HttpSettings(this);
22  start();
23 }
24 
25 
26 HttpServer::HttpServer(const HttpSettings* settings, QObject *parent) : QObject(parent) {
27  m_server = NULL;
28  m_settings = settings;
29  start();
30 }
31 
32 
34  if (m_server != NULL) {
35  m_server->close();
36  }
37 }
38 
39 
41  QHostAddress address = m_settings->address();
42  int port = m_settings->port();
43 
44  if (m_server != NULL) {
45  m_server->close();
46  delete m_server;
47  }
48  m_server = new HttpTcpServer(this);
49  connect(m_server, SIGNAL( newConnection()),
50  this, SLOT(slotNewConnection()));
51 
52  m_server->listen(address, port);
53 
54  if (!m_server->isListening()) {
55  qWarning("HttpServer cannot bind on %s:%i : %s",
56  qPrintable(address.toString()),
57  port,
58  qPrintable(m_server->errorString())
59  );
60  } else {
61  /*
62  qDebug("HttpServer listening on %s port %i",
63  qPrintable(address.toString()),
64  port
65  );
66  */
67  }
68 }
69 
70 
72  return new HttpRequestHandler(parent);
73 }
74 
75 
77  bool threads = m_settings->threads();
78  while (m_server->hasPendingConnections()) {
79  QTcpSocket *socket = m_server->nextPendingConnection();
80  QPointer<HttpConnection> connection = new HttpConnection(this, socket);
81  socket->setParent(connection);
82  connection->setPeerCertificate(m_server->peerCertificate(socket));
83  connection->setVerified(m_server->verified(socket));
84 
85  if (threads) {
86  QThread *thread = new QThread(this);
87  thread->setObjectName("Http connection");
88  thread->start();
89  m_connections << connection;
90  connection->setParent(0);
91  connection->moveToThread(thread);
92  connect(connection, SIGNAL(destroyed(QObject *)),
93  this, SLOT(slotConnectionClosed(QObject *)));
94  connect(connection, SIGNAL(destroyed(QObject *)),
95  thread, SLOT(quit()));
96  connect(thread, SIGNAL(finished()),
97  thread, SLOT(deleteLater()));
98  }
99  }
100 }
101 
102 
103 void HttpServer::slotConnectionClosed(QObject *object) {
104  Q_UNUSED(object);
105  // QPointer<HttpConnection> connection = qobject_cast<HobrasoftHttpd::HttpConnection*>(object);
106  // HttpConnection *connection = qobject_cast<HobrasoftHttpd::HttpConnection*>(object);
107  // m_connections.removeAll(connection);
108  m_connections.removeAll(NULL);
109 }
110 
111 
112 QVariant HttpServer::webStatus() const {
113  QVariantList objectlist;
114  QObjectList list = children();
115  for (int i=0; i<list.size(); i++) {
116  const QObject *qobject = list[i];
117  QString classname = qobject->metaObject()->className();
118  if (classname != "HobrasoftHttpd::HttpConnection") {
119  continue;
120  }
121  const HttpConnection *connection = qobject_cast<const HttpConnection *>(qobject);
122  objectlist += connection->webStatus().toList();
123  }
124 
125  return objectlist;
126 }
127 
const HttpSettings * settings() const
Returs pointer to HttpSettings used in the HttpServer.
Definition: httpserver.h:120
void slotNewConnection()
Slot is invoked when QTcpServer::newConnection() signal arrived.
Definition: httpserver.cpp:76
Listens for incoming TCP connections, supports plain and ssl connections.
Definition: httptcpserver.h:32
HttpServer(QObject *parent)
Constructor using default HttpSettings object.
Definition: httpserver.cpp:19
Configuration of the http server instance.
Definition: httpsettings.h:39
void close()
Closes the QTcpServer bind with your HttpServer.
Definition: httpserver.cpp:33
Processes incoming requests.
virtual HttpRequestHandler * requestHandler(HttpConnection *)
Creates new request handler and returs pointer to it.
Definition: httpserver.cpp:71
void start()
Starts of restart HttpServer with new parameters.
Definition: httpserver.cpp:40
Namespace of HTTP server.
One single connection to http server.