summaryrefslogtreecommitdiffstats
path: root/src/httpserver/qhttpserverresponder.cpp
blob: 1a0d8d3cf0ed4953be10ae6521535deffe60416a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/****************************************************************************
**
** Copyright (C) 2018 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtHttpServer module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** 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 Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or 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.GPL2 and 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-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include <QtHttpServer/qhttpserverresponder.h>
#include <QtHttpServer/qhttpserverrequest.h>
#include <private/qhttpserverresponder_p.h>
#include <private/qhttpserverrequest_p.h>
#include <QtCore/qjsondocument.h>
#include <QtCore/qloggingcategory.h>
#include <QtCore/qtimer.h>
#include <QtNetwork/qtcpsocket.h>
#if defined(QT_WEBSOCKETS_LIB)
#include <QtWebSockets/qwebsocket.h>
#include <private/qwebsocket_p.h>
#include <private/qwebsockethandshakeresponse_p.h>
#include <private/qwebsockethandshakerequest_p.h>
#endif

#include <map>
#include <memory>

QT_BEGIN_NAMESPACE

static const QLoggingCategory &lc()
{
    static const QLoggingCategory category("qt.httpserver.response");
    return category;
}

// https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
static const std::map<QHttpServerResponder::StatusCode, QByteArray> statusString {
#define STATUS_CODE(CODE, TEXT) { QHttpServerResponder::StatusCode::CODE, QByteArrayLiteral(TEXT) }
    STATUS_CODE(Continue,                      "Continue"),
    STATUS_CODE(SwitchingProtocols,            "Switching Protocols"),
    STATUS_CODE(Processing,                    "Processing"),
    STATUS_CODE(Ok,                            "OK"),
    STATUS_CODE(Created,                       "Created"),
    STATUS_CODE(Accepted,                      "Accepted"),
    STATUS_CODE(NonAuthoritativeInformation,   "Non-Authoritative Information"),
    STATUS_CODE(NoContent,                     "No Content"),
    STATUS_CODE(ResetContent,                  "Reset Content"),
    STATUS_CODE(PartialContent,                "Partial Content"),
    STATUS_CODE(MultiStatus,                   "Multi Status"),
    STATUS_CODE(AlreadyReported,               "Already Reported"),
    STATUS_CODE(IMUsed,                        "IM Used"),
    STATUS_CODE(MultipleChoices,               "Multiple Choices"),
    STATUS_CODE(MovedPermanently,              "Moved Permanently"),
    STATUS_CODE(Found,                         "Found"),
    STATUS_CODE(SeeOther,                      "See Other"),
    STATUS_CODE(NotModified,                   "Not Modified"),
    STATUS_CODE(UseProxy,                      "Use Proxy"),
    STATUS_CODE(TemporaryRedirect,             "Temporary Redirect"),
    STATUS_CODE(PermanentRedirect,             "Permanent Redirect"),
    STATUS_CODE(BadRequest,                    "Bad Request"),
    STATUS_CODE(Unauthorized,                  "Unauthorized"),
    STATUS_CODE(PaymentRequired,               "Payment Required"),
    STATUS_CODE(Forbidden,                     "Forbidden"),
    STATUS_CODE(NotFound,                      "Not Found"),
    STATUS_CODE(MethodNotAllowed,              "Method Not Allowed"),
    STATUS_CODE(NotAcceptable,                 "Not Acceptable"),
    STATUS_CODE(ProxyAuthenticationRequired,   "Proxy Authentication Required"),
    STATUS_CODE(RequestTimeout,                "Request Time-out"),
    STATUS_CODE(Conflict,                      "Conflict"),
    STATUS_CODE(Gone,                          "Gone"),
    STATUS_CODE(LengthRequired,                "Length Required"),
    STATUS_CODE(PreconditionFailed,            "Precondition Failed"),
    STATUS_CODE(PayloadTooLarge,               "Payload Too Large"),
    STATUS_CODE(UriTooLong,                    "URI Too Long"),
    STATUS_CODE(UnsupportedMediaType,          "Unsupported Media Type"),
    STATUS_CODE(RequestRangeNotSatisfiable,    "Request Range Not Satisfiable"),
    STATUS_CODE(ExpectationFailed,             "Expectation Failed"),
    STATUS_CODE(ImATeapot,                     "I'm A Teapot"),
    STATUS_CODE(MisdirectedRequest,            "Misdirected Request"),
    STATUS_CODE(UnprocessableEntity,           "Unprocessable Entity"),
    STATUS_CODE(Locked,                        "Locked"),
    STATUS_CODE(FailedDependency,              "Failed Dependency"),
    STATUS_CODE(UpgradeRequired,               "Upgrade Required"),
    STATUS_CODE(PreconditionRequired,          "Precondition Required"),
    STATUS_CODE(TooManyRequests,               "Too Many Requests"),
    STATUS_CODE(RequestHeaderFieldsTooLarge,   "Request Header Fields Too Large"),
    STATUS_CODE(UnavailableForLegalReasons,    "Unavailable For Legal Reasons"),
    STATUS_CODE(InternalServerError,           "Internal Server Error"),
    STATUS_CODE(NotImplemented,                "Not Implemented"),
    STATUS_CODE(BadGateway,                    "Bad Gateway"),
    STATUS_CODE(ServiceUnavailable,            "Service Unavailable"),
    STATUS_CODE(GatewayTimeout,                "Gateway Time-out"),
    STATUS_CODE(HttpVersionNotSupported,       "HTTP Version not supported"),
    STATUS_CODE(VariantAlsoNegotiates,         "Variant Also Negotiates"),
    STATUS_CODE(InsufficientStorage,           "Insufficient Storage"),
    STATUS_CODE(LoopDetected,                  "Loop Detected"),
    STATUS_CODE(NotExtended,                   "Not Extended"),
    STATUS_CODE(NetworkAuthenticationRequired, "Network Authentication Required"),
    STATUS_CODE(NetworkConnectTimeoutError,    "Network Connect Timeout Error"),
#undef STATUS_CODE
};

static const QByteArray contentTypeString(QByteArrayLiteral("Content-Type"));
static const QByteArray contentLengthString(QByteArrayLiteral("Content-Length"));

template <qint64 BUFFERSIZE = 512>
struct IOChunkedTransfer
{
    // TODO This is not the fastest implementation, as it does read & write
    // in a sequential fashion, but these operation could potentially overlap.
    // TODO Can we implement it without the buffer? Direct write to the target buffer
    // would be great.

    const qint64 bufferSize = BUFFERSIZE;
    char buffer[BUFFERSIZE];
    qint64 beginIndex = -1;
    qint64 endIndex = -1;
    QScopedPointer<QIODevice, QScopedPointerDeleteLater> source;
    const QPointer<QIODevice> sink;
    const QMetaObject::Connection bytesWrittenConnection;
    const QMetaObject::Connection readyReadConnection;
    IOChunkedTransfer(QIODevice *input, QIODevice *output) :
        source(input),
        sink(output),
        bytesWrittenConnection(QObject::connect(sink, &QIODevice::bytesWritten, [this] () {
              writeToOutput();
        })),
        readyReadConnection(QObject::connect(source.get(), &QIODevice::readyRead, [this] () {
            readFromInput();
        }))
    {
        Q_ASSERT(!source->atEnd());  // TODO error out
        readFromInput();
    }

    ~IOChunkedTransfer()
    {
        QObject::disconnect(bytesWrittenConnection);
        QObject::disconnect(readyReadConnection);
    }

    inline bool isBufferEmpty()
    {
        Q_ASSERT(beginIndex <= endIndex);
        return beginIndex == endIndex;
    }

    void readFromInput()
    {
        if (!isBufferEmpty()) // We haven't consumed all the data yet.
            return;
        beginIndex = 0;
        endIndex = source->read(buffer, bufferSize);
        if (endIndex < 0) {
            endIndex = beginIndex; // Mark the buffer as empty
            qCWarning(lc, "Error reading chunk: %s", qPrintable(source->errorString()));
            return;
        } else if (endIndex) {
            memset(buffer + endIndex, 0, sizeof(buffer) - std::size_t(endIndex));
            writeToOutput();
        }
    }

    void writeToOutput()
    {
        if (isBufferEmpty())
            return;

        const auto writtenBytes = sink->write(buffer + beginIndex, endIndex);
        if (writtenBytes < 0) {
            qCWarning(lc, "Error writing chunk: %s", qPrintable(sink->errorString()));
            return;
        }
        beginIndex += writtenBytes;
        if (isBufferEmpty()) {
            if (source->bytesAvailable())
                QTimer::singleShot(0, source.get(), [this]() { readFromInput(); });
            else if (source->atEnd()) // Finishing
                source.reset();
        }
    }
};

/*!
    Constructs a QHttpServerResponder using the request \a request
    and the socket \a socket.
*/
QHttpServerResponder::QHttpServerResponder(const QHttpServerRequest &request,
                                           QTcpSocket *socket) :
    d_ptr(new QHttpServerResponderPrivate(request, socket))
{
    Q_ASSERT(socket);
}

/*!
    Move-constructs a QHttpServerResponder instance, making it point
    at the same object that \a other was pointing to.
*/
QHttpServerResponder::QHttpServerResponder(QHttpServerResponder &&other) :
    d_ptr(other.d_ptr.take())
{}

/*!
    Destroys a QHttpServerResponder.
*/
QHttpServerResponder::~QHttpServerResponder()
{}

/*!
    Answers a request with an HTTP status code \a status and a
    MIME type \a mimeType. The I/O device \a data provides the body
    of the response. If \a data is sequential, the body of the
    message is sent in chunks: otherwise, the function assumes all
    the content is available and sends it all at once but the read
    is done in chunks.

    \note This function takes the ownership of \a data.
*/
void QHttpServerResponder::write(QIODevice *data,
                                 const QByteArray &mimeType,
                                 StatusCode status)
{
    Q_D(QHttpServerResponder);
    Q_ASSERT(d->socket);
    QScopedPointer<QIODevice, QScopedPointerDeleteLater> input(data);
    auto socket = d->socket;
    QObject::connect(input.get(), &QIODevice::aboutToClose, [&input](){ input.reset(); });
    // TODO protect keep alive sockets
    QObject::connect(input.get(), &QObject::destroyed, socket, &QObject::deleteLater);
    QObject::connect(socket, &QObject::destroyed, [&input](){ input.reset(); });

    input->setParent(nullptr);
    auto openMode = input->openMode();
    if (!(openMode & QIODevice::ReadOnly)) {
        if (openMode == QIODevice::NotOpen) {
            if (!input->open(QIODevice::ReadOnly)) {
                // TODO Add developer error handling
                // TODO Send 500
                qCDebug(lc, "500: Could not open device %s", qPrintable(input->errorString()));
                return;
            }
        } else {
            // TODO Handle that and send 500, the device is opened but not for reading.
            // That doesn't make sense
            qCDebug(lc) << "500: Device is opened in a wrong mode" << openMode
                        << qPrintable(input->errorString());
            return;
        }
    }
    if (!socket->isOpen()) {
        qCWarning(lc, "Cannot write to socket. It's disconnected");
        delete socket;
        return;
    }

    d->writeStatusLine(status);

    if (!input->isSequential()) // Non-sequential QIODevice should know its data size
        d->addHeader(contentLengthString, QByteArray::number(input->size()));

    d->addHeader(contentTypeString, mimeType);

    d->writeHeaders();
    socket->write("\r\n");

    if (input->atEnd()) {
        qCDebug(lc, "No more data available.");
        return;
    }

    auto transfer = new IOChunkedTransfer<>(input.take(), socket);
    QObject::connect(transfer->source.get(), &QObject::destroyed, [transfer]() {
        delete transfer;
    });
}

/*!
    Answers a request with an HTTP status code \a status, a
    MIME type \a mimeType and a body \a data.
*/
void QHttpServerResponder::write(const QByteArray &data,
                                 const QByteArray &mimeType,
                                 StatusCode status)
{
    Q_D(QHttpServerResponder);
    d->writeStatusLine(status);
    addHeaders(contentTypeString, mimeType,
               contentLengthString, QByteArray::number(data.size()));
    d->writeHeaders();
    d->writeBody(data);
}

/*!
    Answers a request with an HTTP status code \a status, and JSON
    document \a document.
*/
void QHttpServerResponder::write(const QJsonDocument &document, StatusCode status)
{
    write(document.toJson(), QByteArrayLiteral("text/json"), status);
}

/*!
    Answers a request with an HTTP status code \a status.
*/
void QHttpServerResponder::write(StatusCode status)
{
    write(QByteArray(), QByteArrayLiteral("application/x-empty"), status);
}

/*!
    Returns the socket used.
*/
QTcpSocket *QHttpServerResponder::socket() const
{
    Q_D(const QHttpServerResponder);
    return d->socket;
}

bool QHttpServerResponder::addHeader(const QByteArray &key, const QByteArray &value)
{
    Q_D(QHttpServerResponder);
    return d->addHeader(key, value);
}

void QHttpServerResponderPrivate::writeStatusLine(StatusCode status,
                                                  const QPair<quint8, quint8> &version) const
{
    Q_ASSERT(socket->isOpen());
    socket->write("HTTP/");
    socket->write(QByteArray::number(version.first));
    socket->write(".");
    socket->write(QByteArray::number(version.second));
    socket->write(" ");
    socket->write(QByteArray::number(quint32(status)));
    socket->write(" ");
    socket->write(statusString.at(status));
    socket->write("\r\n");
}

void QHttpServerResponderPrivate::writeHeader(const QByteArray &header,
                                              const QByteArray &value) const
{
    socket->write(header);
    socket->write(": ");
    socket->write(value);
    socket->write("\r\n");
}

void QHttpServerResponderPrivate::writeHeaders() const
{
    for (const auto &pair : qAsConst(headers()))
        writeHeader(pair.first, pair.second);
}

void QHttpServerResponderPrivate::writeBody(const QByteArray &body) const
{
    Q_ASSERT(socket->isOpen());
    socket->write("\r\n");
    socket->write(body);
}

QT_END_NAMESPACE