add OPTIONS method + CORS

This commit is contained in:
Simon Stürz 2015-08-13 23:52:34 +02:00 committed by Michael Zanetti
parent f3b46361ab
commit 653043f7b1
5 changed files with 19 additions and 7 deletions

View File

@ -135,7 +135,9 @@ HttpReply::HttpReply(QObject *parent) :
// set known headers // set known headers
setHeader(HttpHeaderType::ServerHeader, "guh/" + QByteArray(GUH_VERSION_STRING)); setHeader(HttpHeaderType::ServerHeader, "guh/" + QByteArray(GUH_VERSION_STRING));
setHeader(HttpHeaderType::DateHeader, QDateTime::currentDateTime().toString("ddd, dd MMM yyyy hh:mm:ss").toUtf8() + " GMT"); setHeader(HttpHeaderType::DateHeader, QDateTime::currentDateTime().toString("ddd, dd MMM yyyy hh:mm:ss").toUtf8() + " GMT");
setRawHeader("Access-Control-Allow-Origin","*");
setHeader(HttpHeaderType::CacheControlHeader, "no-cache"); setHeader(HttpHeaderType::CacheControlHeader, "no-cache");
setHeader(HttpHeaderType::ConnectionHeader, "Keep-Alive");
packReply(); packReply();
} }
@ -155,7 +157,9 @@ HttpReply::HttpReply(const HttpReply::HttpStatusCode &statusCode, const HttpRepl
// set known headers // set known headers
setHeader(HttpHeaderType::ServerHeader, "guh/" + QByteArray(GUH_VERSION_STRING)); setHeader(HttpHeaderType::ServerHeader, "guh/" + QByteArray(GUH_VERSION_STRING));
setHeader(HttpHeaderType::DateHeader, QDateTime::currentDateTime().toString("ddd, dd MMM yyyy hh:mm:ss").toUtf8() + " GMT"); setHeader(HttpHeaderType::DateHeader, QDateTime::currentDateTime().toString("ddd, dd MMM yyyy hh:mm:ss").toUtf8() + " GMT");
setRawHeader("Access-Control-Allow-Origin","*");
setHeader(HttpHeaderType::CacheControlHeader, "no-cache"); setHeader(HttpHeaderType::CacheControlHeader, "no-cache");
setHeader(HttpHeaderType::ConnectionHeader, "Keep-Alive");
packReply(); packReply();
} }

View File

@ -43,6 +43,8 @@
Represents the HTTP/1.1 PUT method. Represents the HTTP/1.1 PUT method.
\value Delete \value Delete
Represents the HTTP/1.1 DELETE method. Represents the HTTP/1.1 DELETE method.
\value Options
Represents the HTTP/1.1 OPTIONS method.
\value Unhandled \value Unhandled
Represents every other method which is not handled. Represents every other method which is not handled.
*/ */
@ -256,6 +258,8 @@ HttpRequest::RequestMethod HttpRequest::getRequestMethodType(const QString &meth
return RequestMethod::Put; return RequestMethod::Put;
} else if (methodString == "DELETE") { } else if (methodString == "DELETE") {
return RequestMethod::Delete; return RequestMethod::Delete;
} else if (methodString == "OPTIONS") {
return RequestMethod::Options;
} }
qCWarning(dcWebServer) << "Method" << methodString << "will not be handled."; qCWarning(dcWebServer) << "Method" << methodString << "will not be handled.";
return RequestMethod::Unhandled; return RequestMethod::Unhandled;

View File

@ -36,6 +36,7 @@ public:
Post, Post,
Put, Put,
Delete, Delete,
Options,
Unhandled Unhandled
}; };

View File

@ -113,7 +113,6 @@ WebServer::WebServer(const QSslConfiguration &sslConfiguration, QObject *parent)
if (!m_webinterfaceDir.exists()) if (!m_webinterfaceDir.exists())
qCWarning(dcWebServer) << "Web interface public folder" << m_webinterfaceDir.path() << "does not exist."; qCWarning(dcWebServer) << "Web interface public folder" << m_webinterfaceDir.path() << "does not exist.";
// check SSL // check SSL
if (m_useSsl && m_sslConfiguration.isNull()) if (m_useSsl && m_sslConfiguration.isNull())
m_useSsl = false; m_useSsl = false;
@ -161,7 +160,6 @@ bool WebServer::verifyFile(QSslSocket *socket, const QString &fileName)
reply.setPayload("403 Forbidden."); reply.setPayload("403 Forbidden.");
reply.packReply(); reply.packReply();
writeData(socket, reply.data()); writeData(socket, reply.data());
socket->close();
return false; return false;
} }
@ -172,7 +170,6 @@ bool WebServer::verifyFile(QSslSocket *socket, const QString &fileName)
reply.setPayload("403 Forbidden. Page not readable."); reply.setPayload("403 Forbidden. Page not readable.");
reply.packReply(); reply.packReply();
writeData(socket, reply.data()); writeData(socket, reply.data());
socket->close();
return false; return false;
} }
return true; return true;
@ -190,11 +187,10 @@ QString WebServer::fileName(const QString &query)
return m_webinterfaceDir.path() + fileName; return m_webinterfaceDir.path() + fileName;
} }
void WebServer::writeData(QSslSocket *socket, const QByteArray &data) void WebServer::writeData(QSslSocket *socket, const QByteArray &data)
{ {
socket->write(data); socket->write(data);
socket->close(); //socket->close();
} }
void WebServer::incomingConnection(qintptr socketDescriptor) void WebServer::incomingConnection(qintptr socketDescriptor)
@ -289,12 +285,19 @@ void WebServer::readClient()
// verify method // verify method
if (request.method() == HttpRequest::Unhandled) { if (request.method() == HttpRequest::Unhandled) {
HttpReply reply(HttpReply::MethodNotAllowed); HttpReply reply(HttpReply::MethodNotAllowed);
reply.setHeader(HttpReply::AllowHeader, "GET, PUT, POST, DELETE"); reply.setHeader(HttpReply::AllowHeader, "GET, PUT, POST, DELETE, OPTIONS");
reply.setPayload("405 Method not allowed."); reply.setPayload("405 Method not allowed.");
writeData(socket, reply.data()); writeData(socket, reply.data());
return; return;
} }
// check CORS call
if (request.method() == HttpRequest::Options) {
HttpReply reply(HttpReply::Ok);
reply.setRawHeader("Access-Control-Allow-Methods","PUT, POST, GET, DELETE, OPTIONS");
writeData(socket, reply.data());
}
// verify API query // verify API query
if (request.url().path().startsWith("/api/v1")) { if (request.url().path().startsWith("/api/v1")) {
emit httpRequestReady(clientId, request); emit httpRequestReady(clientId, request);

View File

@ -158,9 +158,9 @@ void TestWebserver::checkAllowedMethodCall_data()
QTest::newRow("PUT") << "PUT" << 200; QTest::newRow("PUT") << "PUT" << 200;
QTest::newRow("POST") << "POST" << 200; QTest::newRow("POST") << "POST" << 200;
QTest::newRow("DELETE") << "DELETE" << 200; QTest::newRow("DELETE") << "DELETE" << 200;
QTest::newRow("OPTIONS") << "OPTIONS" << 200;
QTest::newRow("HEAD") << "HEAD" << 405; QTest::newRow("HEAD") << "HEAD" << 405;
QTest::newRow("CONNECT") << "CONNECT" << 405; QTest::newRow("CONNECT") << "CONNECT" << 405;
QTest::newRow("OPTIONS") << "OPTIONS" << 405;
QTest::newRow("TRACE") << "TRACE" << 405; QTest::newRow("TRACE") << "TRACE" << 405;
} }