add HTTPS versions of webserver classes

This commit is contained in:
Stephen Birarda 2014-04-24 14:06:24 -07:00
parent 04f3e01a1b
commit 260966915a
10 changed files with 139 additions and 15 deletions

View file

@ -31,11 +31,13 @@
#include "DomainServer.h"
const quint16 DOMAIN_SERVER_HTTP_PORT = 8080;
const quint16 DOMAIN_SERVER_HTTP_PORT = 40100;
const quint16 DOMAIN_SERVER_HTTPS_PORT = 40101;
DomainServer::DomainServer(int argc, char* argv[]) :
QCoreApplication(argc, argv),
_HTTPManager(DOMAIN_SERVER_HTTP_PORT, QString("%1/resources/web/").arg(QCoreApplication::applicationDirPath()), this),
_httpManager(DOMAIN_SERVER_HTTP_PORT, QString("%1/resources/web/").arg(QCoreApplication::applicationDirPath()), this),
_httpsManager(NULL),
_staticAssignmentHash(),
_assignmentQueue(),
_isUsingDTLS(false),

View file

@ -24,6 +24,7 @@
#include <Assignment.h>
#include <HTTPManager.h>
#include <HTTPSManager.h>
#include <LimitedNodeList.h>
#include "DTLSServerSession.h"
@ -79,7 +80,8 @@ private:
QJsonObject jsonForSocket(const HifiSockAddr& socket);
QJsonObject jsonObjectForNode(const SharedNodePointer& node);
HTTPManager _HTTPManager;
HTTPManager _httpManager;
HTTPSManager* _httpsManager;
QHash<QUuid, SharedAssignmentPointer> _staticAssignmentHash;
QQueue<SharedAssignmentPointer> _assignmentQueue;

View file

@ -28,8 +28,8 @@ HTTPConnection::HTTPConnection (QTcpSocket* socket, HTTPManager* parentManager)
_parentManager(parentManager),
_socket(socket),
_stream(socket),
_address(socket->peerAddress()) {
_address(socket->peerAddress())
{
// take over ownership of the socket
_socket->setParent(this);

View file

@ -18,10 +18,10 @@
#include <QDataStream>
#include <QHash>
#include <QHostAddress>
#include <QtNetwork/QHostAddress>
#include <QIODevice>
#include <QList>
#include <QNetworkAccessManager>
#include <QtNetwork/QNetworkAccessManager>
#include <QObject>
#include <QPair>
#include <QUrl>

View file

@ -138,14 +138,14 @@ HTTPManager::HTTPManager(quint16 port, const QString& documentRoot, HTTPRequestH
qDebug() << "Failed to open HTTP server socket:" << errorString();
return;
}
// connect the connection signal
connect(this, SIGNAL(newConnection()), SLOT(acceptConnections()));
}
void HTTPManager::acceptConnections() {
QTcpSocket* socket;
while ((socket = nextPendingConnection()) != 0) {
void HTTPManager::incomingConnection(qintptr socketDescriptor) {
QTcpSocket* socket = new QTcpSocket(this);
if (socket->setSocketDescriptor(socketDescriptor)) {
new HTTPConnection(socket, this);
} else {
delete socket;
}
}

View file

@ -35,9 +35,9 @@ public:
bool handleHTTPRequest(HTTPConnection* connection, const QUrl& url);
protected slots:
protected:
/// Accepts all pending connections
void acceptConnections();
virtual void incomingConnection(qintptr socketDescriptor);
protected:
QString _documentRoot;
HTTPRequestHandler* _requestHandler;

View file

@ -0,0 +1,23 @@
//
// HTTPSConnection.cpp
// libraries/embedded-webserver/src
//
// Created by Stephen Birarda on 2014-04-24.
// Copyright 2014 High Fidelity, Inc.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
#include "HTTPSConnection.h"
HTTPSConnection::HTTPSConnection(QSslSocket* sslSocket, HTTPSManager* parentManager) :
HTTPConnection(sslSocket, parentManager)
{
connect(sslSocket, SIGNAL(sslErrors(const QList<QSslError>&)), this, SLOT(sslErrors(const QList<QSslError>&)));
sslSocket->startServerEncryption();
}
void HTTPSConnection::handleSSLErrors(const QList<QSslError>& errors) {
qDebug() << "SSL errors:" << errors;
}

View file

@ -0,0 +1,26 @@
//
// HTTPSConnection.h
// libraries/embedded-webserver/src
//
// Created by Stephen Birarda on 2014-04-24.
// Copyright 2014 High Fidelity, Inc.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
#ifndef hifi_HTTPSConnection_h
#define hifi_HTTPSConnection_h
#include "HTTPConnection.h"
#include "HTTPSManager.h"
class HTTPSConnection : public HTTPConnection {
Q_OBJECT
public:
HTTPSConnection(QSslSocket* sslSocket, HTTPSManager* parentManager);
protected slots:
void handleSSLErrors(const QList<QSslError>& errors);
};
#endif // hifi_HTTPSConnection_h

View file

@ -0,0 +1,34 @@
//
// HTTPSManager.cpp
// libraries/embedded-webserver/src
//
// Created by Stephen Birarda on 2014-04-24.
// Copyright 2014 High Fidelity, Inc.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
#include <QtNetwork/QSslSocket>
#include "HTTPSConnection.h"
#include "HTTPSManager.h"
HTTPSManager::HTTPSManager(quint16 port, const QString& documentRoot, HTTPRequestHandler* requestHandler, QObject* parent) :
HTTPManager(port, documentRoot, requestHandler, parent),
_certificate(),
_privateKey()
{
}
void HTTPSManager::incomingConnection(qintptr socketDescriptor) {
QSslSocket* sslSocket = new QSslSocket(this);
if (sslSocket->setSocketDescriptor(socketDescriptor)) {
new HTTPSConnection(sslSocket, this);
} else {
delete sslSocket;
}
}

View file

@ -0,0 +1,37 @@
//
// HTTPSManager.h
// libraries/embedded-webserver/src
//
// Created by Stephen Birarda on 2014-04-24.
// Copyright 2014 High Fidelity, Inc.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
#ifndef hifi_HTTPSManager_h
#define hifi_HTTPSManager_h
#include <QtNetwork/QSslKey>
#include <QtNetwork/QSslCertificate>
#include "HTTPManager.h"
class HTTPSManager : public HTTPManager {
Q_OBJECT
public:
HTTPSManager(quint16 port,
const QString& documentRoot,
HTTPRequestHandler* requestHandler = NULL, QObject* parent = 0);
void setCertificate(const QSslCertificate& certificate) { _certificate = certificate; }
void setPrivateKey(const QSslKey& privateKey) { _privateKey = privateKey; }
protected:
virtual void incomingConnection(qintptr socketDescriptor);
private:
QSslCertificate _certificate;
QSslKey _privateKey;
};
#endif // hifi_HTTPSManager_h