Hobrasoft httpd server
Embedded HTTP server for Qt and C++
httptcpserver.cpp
Go to the documentation of this file.
1 
6 #include "httptcpserver.h"
7 #include "httpserver.h"
8 #include "httpsettings.h"
9 #include <QSslSocket>
10 #include <QSslCertificate>
11 #include <QFile>
12 #include <QDateTime>
13 #include <QDebug>
14 
15 using namespace HobrasoftHttpd;
16 
17 
21 HttpTcpServer::HttpTcpServer(HttpServer *parent) : QTcpServer(parent) {
22  m_settings = parent->settings();
23 }
24 
25 
31 void HttpTcpServer::incomingConnection(QINTPTR socketDescriptor) {
35  if (!m_settings->useSSL()) {
36  QTcpServer::incomingConnection(socketDescriptor);
37  return;
38  }
39 
44  QSslSocket *socket = new QSslSocket;
45  if (!socket->setSocketDescriptor(socketDescriptor)) {
46  qDebug() << "setSocketDescriptor failed";
47  delete socket;
48  return;
49  }
50 
57  m_verified[socket] = true;
58  connect(socket, SIGNAL(encrypted()),
59  this, SLOT(slotEncrypted()));
60  connect(socket, SIGNAL( sslErrors(const QList<QSslError>&)),
61  this, SLOT( slotSslErrors(const QList<QSslError>&)));
62  connect(socket, SIGNAL( peerVerifyError(const QSslError&)),
63  this, SLOT(slotPeerVerifyError(const QSslError&)));
64  connect(socket, SIGNAL( disconnected()),
65  this, SLOT(slotDisconnected()));
66 
71  QList<QSslCertificate> cacerts;
72  QFile cacertf(m_settings->sslCaCrt());
73  if (cacertf.open(QIODevice::ReadOnly)) {
74  cacerts << QSslCertificate(&cacertf);
75  cacertf.close();
76  }
77 
82  socket->setPrivateKey(m_settings->sslKey());
83  socket->setLocalCertificate(m_settings->sslCrt());
84  socket->setCaCertificates(cacerts);
85  socket->startServerEncryption();
86 
87 }
88 
89 
97  QSslSocket *socket = qobject_cast<QSslSocket *>(sender());
98  m_verified.remove(socket);
99  m_peerCert.remove(socket);
100 }
101 
102 
114  QSslSocket *socket = qobject_cast<QSslSocket *>(sender());
115 
116  QSslCertificate crt = socket->peerCertificate();
117  m_peerCert[socket] = crt;
118 
119  #if QT_VERSION > 0x040700
120  addPendingConnection(socket);
121  #endif
122  emit newConnection();
123 }
124 
125 
129 bool HttpTcpServer::verified(QTcpSocket *socket) const {
130  if (!m_verified.contains(socket)) { return false; }
131  return m_verified[socket];
132 }
133 
134 
138 QSslCertificate HttpTcpServer::peerCertificate(QTcpSocket *socket) const {
139  if (!m_peerCert.contains(socket)) { return QSslCertificate(); }
140  return m_peerCert[socket];
141 }
142 
143 
147 void HttpTcpServer::slotPeerVerifyError(const QSslError& error) {
148  Q_UNUSED(error);
149  QSslSocket *socket = qobject_cast<QSslSocket *>(sender());
150  m_verified[socket] = false;
151 }
152 
153 
157 void HttpTcpServer::slotSslErrors(const QList<QSslError>& errors) {
158  QList<QSslError> ignoreList;
159  for (int i=0; i<errors.size(); i++) {
160  if (m_settings->ignoreSslError(errors[i])) {
161  ignoreList << errors[i];
162  continue;
163  }
164  qDebug() << "sslError" << errors[i].errorString();
165  }
166 
167  QSslSocket *socket = qobject_cast<QSslSocket *>(sender());
168  socket->ignoreSslErrors(ignoreList);
169 
170 }
171 
172 
bool ignoreSslError(QSslError error) const
Returns true if the error should be ignored, default true.
const HttpSettings * settings() const
Returs pointer to HttpSettings used in the HttpServer.
Definition: httpserver.h:120
void slotEncrypted()
Slot is invoked when the SSL hanshake is complete and connection is established.
const QString & sslCaCrt() const
Returns SSL CA certificate.
Definition: httpsettings.h:128
General single-threaded, event-driven HTTP server.
Definition: httpserver.h:88
void slotSslErrors(const QList< QSslError > &)
Slot is called when an SSL error occured.
HttpTcpServer(HttpServer *)
Constructor creates the class instance.
QHash< QTcpSocket *, bool > m_verified
Verified status of each socket.
Definition: httptcpserver.h:57
void slotDisconnected()
Slot is invoked when the socket disconnets.
QHash< QTcpSocket *, QSslCertificate > m_peerCert
Peer&#39;s certificate of each socket.
Definition: httptcpserver.h:62
QSslCertificate peerCertificate(QTcpSocket *) const
Returns peer&#39;s certificate.
const QString & sslKey() const
Returns SSL key.
Definition: httpsettings.h:120
bool verified(QTcpSocket *) const
Returns true if the peer&#39;s certificate is valid and signed with server&#39;s CA certificate.
bool useSSL() const
Returns status of SSL connections.
Definition: httpsettings.h:108
void incomingConnection(QINTPTR socketDescriptor)
Method is invoked when incoming connection arrived.
const QString & sslCrt() const
Returns SSL certificate.
Definition: httpsettings.h:124
Namespace of HTTP server.
void slotPeerVerifyError(const QSslError &)
Slot is called when the peer&#39;s certificate is not verified.