summaryrefslogtreecommitdiffstats
path: root/src/httpserver
diff options
context:
space:
mode:
Diffstat (limited to 'src/httpserver')
-rw-r--r--src/httpserver/httpserver.pro2
-rw-r--r--src/httpserver/qhttpserverliterals.cpp59
-rw-r--r--src/httpserver/qhttpserverliterals_p.h62
-rw-r--r--src/httpserver/qhttpserverresponder.cpp34
-rw-r--r--src/httpserver/qhttpserverresponse.cpp19
5 files changed, 150 insertions, 26 deletions
diff --git a/src/httpserver/httpserver.pro b/src/httpserver/httpserver.pro
index d683ee3..1ba716b 100644
--- a/src/httpserver/httpserver.pro
+++ b/src/httpserver/httpserver.pro
@@ -11,6 +11,7 @@ HEADERS += \
qabstracthttpserver_p.h \
qhttpserver.h \
qhttpserver_p.h \
+ qhttpserverliterals_p.h \
qhttpserverrequest.h \
qhttpserverrequest_p.h \
qhttpserverresponder.h \
@@ -26,6 +27,7 @@ HEADERS += \
SOURCES += \
qabstracthttpserver.cpp \
qhttpserver.cpp \
+ qhttpserverliterals.cpp \
qhttpserverrequest.cpp \
qhttpserverresponder.cpp \
qhttpserverresponse.cpp \
diff --git a/src/httpserver/qhttpserverliterals.cpp b/src/httpserver/qhttpserverliterals.cpp
new file mode 100644
index 0000000..3fc7627
--- /dev/null
+++ b/src/httpserver/qhttpserverliterals.cpp
@@ -0,0 +1,59 @@
+/****************************************************************************
+**
+** Copyright (C) 2019 Mikhail Svetkin <mikhail.svetkin@gmail.com>
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the QtHttpServer module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:GPL$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3 or (at your option) any later version
+** approved by the KDE Free Qt Foundation. The licenses are as published by
+** the Free Software Foundation and appearing in the file LICENSE.GPL3
+** included in the packaging of this file. Please review the following
+** information to ensure the GNU General Public License requirements will
+** be met: https://www.gnu.org/licenses/gpl-3.0.html.
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "qhttpserverliterals_p.h"
+
+QT_BEGIN_NAMESPACE
+
+QByteArray QHttpServerLiterals::contentTypeHeader()
+{
+ return QByteArrayLiteral("Content-Type");
+}
+
+QByteArray QHttpServerLiterals::contentTypeXEmpty()
+{
+ return QByteArrayLiteral("application/x-empty");
+}
+
+QByteArray QHttpServerLiterals::contentTypeTextHtml()
+{
+ return QByteArrayLiteral("text/html");
+}
+
+QByteArray QHttpServerLiterals::contentTypeJson()
+{
+ return QByteArrayLiteral("application/json");
+}
+
+QByteArray QHttpServerLiterals::contentLengthHeader()
+{
+ return QByteArrayLiteral("Content-Length");
+}
+
+QT_END_NAMESPACE
diff --git a/src/httpserver/qhttpserverliterals_p.h b/src/httpserver/qhttpserverliterals_p.h
new file mode 100644
index 0000000..a6ac05a
--- /dev/null
+++ b/src/httpserver/qhttpserverliterals_p.h
@@ -0,0 +1,62 @@
+/****************************************************************************
+**
+** Copyright (C) 2019 Mikhail Svetkin <mikhail.svetkin@gmail.com>
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the QtHttpServer module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:GPL$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3 or (at your option) any later version
+** approved by the KDE Free Qt Foundation. The licenses are as published by
+** the Free Software Foundation and appearing in the file LICENSE.GPL3
+** included in the packaging of this file. Please review the following
+** information to ensure the GNU General Public License requirements will
+** be met: https://www.gnu.org/licenses/gpl-3.0.html.
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef QHTTPSERVERLITERALS_P_H
+#define QHTTPSERVERLITERALS_P_H
+
+//
+// W A R N I N G
+// -------------
+//
+// This file is not part of the Qt API. It exists for the convenience
+// of QHttpServer. This header file may change from version to
+// version without notice, or even be removed.
+//
+// We mean it.
+
+#include <QtHttpServer/qthttpserverglobal.h>
+
+#include <QtCore/qbytearray.h>
+
+QT_BEGIN_NAMESPACE
+
+class Q_HTTPSERVER_EXPORT QHttpServerLiterals
+{
+
+public:
+ static QByteArray contentTypeHeader();
+ static QByteArray contentTypeXEmpty();
+ static QByteArray contentTypeTextHtml();
+ static QByteArray contentTypeJson();
+ static QByteArray contentLengthHeader();
+};
+
+QT_END_NAMESPACE
+
+#endif // QHTTPSERVERLITERALS_P_H
diff --git a/src/httpserver/qhttpserverresponder.cpp b/src/httpserver/qhttpserverresponder.cpp
index a25119d..5285233 100644
--- a/src/httpserver/qhttpserverresponder.cpp
+++ b/src/httpserver/qhttpserverresponder.cpp
@@ -30,6 +30,7 @@
#include <QtHttpServer/qhttpserverresponder.h>
#include <QtHttpServer/qhttpserverrequest.h>
#include <private/qhttpserverresponder_p.h>
+#include <private/qhttpserverliterals_p.h>
#include <private/qhttpserverrequest_p.h>
#include <QtCore/qjsondocument.h>
#include <QtCore/qloggingcategory.h>
@@ -55,13 +56,6 @@ static const std::map<QHttpServerResponder::StatusCode, QByteArray> statusString
#undef XX
};
-static const QByteArray contentTypeHeader(QByteArrayLiteral("Content-Type"));
-static const QByteArray contentLengthHeader(QByteArrayLiteral("Content-Length"));
-
-static const QByteArray contentTypeEmpty(QByteArrayLiteral("application/x-empty"));
-static const QByteArray contentTypeJson(QByteArrayLiteral("text/json"));
-
-
template <qint64 BUFFERSIZE = 512>
struct IOChunkedTransfer
{
@@ -208,8 +202,10 @@ void QHttpServerResponder::write(QIODevice *data,
writeStatusLine(status);
- if (!input->isSequential()) // Non-sequential QIODevice should know its data size
- writeHeader(contentLengthHeader, QByteArray::number(input->size()));
+ if (!input->isSequential()) { // Non-sequential QIODevice should know its data size
+ writeHeader(QHttpServerLiterals::contentLengthHeader(),
+ QByteArray::number(input->size()));
+ }
for (auto &&header : headers)
writeHeader(header.first, header.second);
@@ -239,7 +235,9 @@ void QHttpServerResponder::write(QIODevice *data,
const QByteArray &mimeType,
StatusCode status)
{
- write(data, {{ contentTypeHeader, mimeType }}, status);
+ write(data,
+ {{ QHttpServerLiterals::contentTypeHeader(), mimeType }},
+ status);
}
/*!
@@ -253,9 +251,12 @@ void QHttpServerResponder::write(const QJsonDocument &document,
StatusCode status)
{
const QByteArray &json = document.toJson();
+
writeStatusLine(status);
- writeHeader(contentTypeHeader, contentTypeJson);
- writeHeader(contentLengthHeader, QByteArray::number(json.size()));
+ writeHeader(QHttpServerLiterals::contentTypeHeader(),
+ QHttpServerLiterals::contentTypeJson());
+ writeHeader(QHttpServerLiterals::contentLengthHeader(),
+ QByteArray::number(json.size()));
writeHeaders(std::move(headers));
writeBody(document.toJson());
}
@@ -288,7 +289,8 @@ void QHttpServerResponder::write(const QByteArray &data,
for (auto &&header : headers)
writeHeader(header.first, header.second);
- writeHeader(contentLengthHeader, QByteArray::number(data.size()));
+ writeHeader(QHttpServerLiterals::contentLengthHeader(),
+ QByteArray::number(data.size()));
writeBody(data);
}
@@ -300,7 +302,9 @@ void QHttpServerResponder::write(const QByteArray &data,
const QByteArray &mimeType,
StatusCode status)
{
- write(data, {{ contentTypeHeader, mimeType }}, status);
+ write(data,
+ {{ QHttpServerLiterals::contentTypeHeader(), mimeType }},
+ status);
}
/*!
@@ -310,7 +314,7 @@ void QHttpServerResponder::write(const QByteArray &data,
*/
void QHttpServerResponder::write(StatusCode status)
{
- write(QByteArray(), contentTypeEmpty, status);
+ write(QByteArray(), QHttpServerLiterals::contentTypeXEmpty(), status);
}
/*!
diff --git a/src/httpserver/qhttpserverresponse.cpp b/src/httpserver/qhttpserverresponse.cpp
index e997e96..89d6b47 100644
--- a/src/httpserver/qhttpserverresponse.cpp
+++ b/src/httpserver/qhttpserverresponse.cpp
@@ -30,6 +30,7 @@
#include <QtHttpServer/qhttpserverresponse.h>
#include <private/qhttpserverresponse_p.h>
+#include <private/qhttpserverliterals_p.h>
#include <QtCore/qfile.h>
#include <QtCore/qjsondocument.h>
@@ -38,20 +39,16 @@
QT_BEGIN_NAMESPACE
-namespace {
-
-const QByteArray mimeTextHtml("text/html");
-const QByteArray mimeApplicationJson("application/json");
-
-}
-
QHttpServerResponse::QHttpServerResponse(QHttpServerResponse &&other)
: d_ptr(other.d_ptr.take())
{
}
-QHttpServerResponse::QHttpServerResponse(const QHttpServerResponse::StatusCode statusCode)
- : QHttpServerResponse(mimeTextHtml, QByteArray(), statusCode)
+QHttpServerResponse::QHttpServerResponse(
+ const QHttpServerResponse::StatusCode statusCode)
+ : QHttpServerResponse(QHttpServerLiterals::contentTypeTextHtml(),
+ QByteArray(),
+ statusCode)
{
}
@@ -78,13 +75,13 @@ QHttpServerResponse::QHttpServerResponse(QByteArray &&data)
}
QHttpServerResponse::QHttpServerResponse(const QJsonObject &data)
- : QHttpServerResponse(mimeApplicationJson,
+ : QHttpServerResponse(QHttpServerLiterals::contentTypeJson(),
QJsonDocument(data).toJson(QJsonDocument::Compact))
{
}
QHttpServerResponse::QHttpServerResponse(const QJsonArray &data)
- : QHttpServerResponse(mimeApplicationJson,
+ : QHttpServerResponse(QHttpServerLiterals::contentTypeJson(),
QJsonDocument(data).toJson(QJsonDocument::Compact))
{
}