| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #ifndef QHTTPMULTIPART_P_H |
| 5 | #define QHTTPMULTIPART_P_H |
| 6 | |
| 7 | // |
| 8 | // W A R N I N G |
| 9 | // ------------- |
| 10 | // |
| 11 | // This file is not part of the Qt API. It exists for the convenience |
| 12 | // of the Network Access API. This header file may change from |
| 13 | // version to version without notice, or even be removed. |
| 14 | // |
| 15 | // We mean it. |
| 16 | // |
| 17 | |
| 18 | #include <QtNetwork/private/qtnetworkglobal_p.h> |
| 19 | #include <QtNetwork/qhttpmultipart.h> |
| 20 | |
| 21 | #include "QtCore/qshareddata.h" |
| 22 | #include "qnetworkrequest_p.h" // for deriving QHttpPartPrivate from QNetworkHeadersPrivate |
| 23 | #include "qhttpheadershelper_p.h" |
| 24 | |
| 25 | #include "private/qobject_p.h" |
| 26 | #include <QtCore/qiodevice.h> |
| 27 | |
| 28 | #ifndef Q_OS_WASM |
| 29 | QT_REQUIRE_CONFIG(http); |
| 30 | #endif |
| 31 | |
| 32 | QT_BEGIN_NAMESPACE |
| 33 | |
| 34 | |
| 35 | class QHttpPartPrivate: public QSharedData, public QNetworkHeadersPrivate |
| 36 | { |
| 37 | public: |
| 38 | inline QHttpPartPrivate() : bodyDevice(nullptr), headerCreated(false), readPointer(0) |
| 39 | { |
| 40 | } |
| 41 | ~QHttpPartPrivate() |
| 42 | { |
| 43 | } |
| 44 | |
| 45 | |
| 46 | QHttpPartPrivate(const QHttpPartPrivate &other) |
| 47 | : QSharedData(other), QNetworkHeadersPrivate(other), body(other.body), |
| 48 | header(other.header), headerCreated(other.headerCreated), readPointer(other.readPointer) |
| 49 | { |
| 50 | bodyDevice = other.bodyDevice; |
| 51 | } |
| 52 | |
| 53 | inline bool operator==(const QHttpPartPrivate &other) const |
| 54 | { |
| 55 | return QHttpHeadersHelper::compareStrict(left: httpHeaders, right: other.httpHeaders) |
| 56 | && body == other.body |
| 57 | && bodyDevice == other.bodyDevice |
| 58 | && readPointer == other.readPointer; |
| 59 | } |
| 60 | |
| 61 | void setBodyDevice(QIODevice *device) { |
| 62 | bodyDevice = device; |
| 63 | readPointer = 0; |
| 64 | } |
| 65 | void setBody(const QByteArray &newBody) { |
| 66 | body = newBody; |
| 67 | readPointer = 0; |
| 68 | } |
| 69 | |
| 70 | // QIODevice-style methods called by QHttpMultiPartIODevice (but this class is |
| 71 | // not a QIODevice): |
| 72 | qint64 bytesAvailable() const; |
| 73 | qint64 readData(char *data, qint64 maxSize); |
| 74 | qint64 size() const; |
| 75 | bool reset(); |
| 76 | |
| 77 | QByteArray body; |
| 78 | QIODevice *bodyDevice; |
| 79 | |
| 80 | private: |
| 81 | void () const; |
| 82 | |
| 83 | mutable QByteArray ; |
| 84 | mutable bool ; |
| 85 | qint64 readPointer; |
| 86 | }; |
| 87 | |
| 88 | |
| 89 | |
| 90 | class QHttpMultiPartPrivate; |
| 91 | |
| 92 | class Q_AUTOTEST_EXPORT QHttpMultiPartIODevice : public QIODevice |
| 93 | { |
| 94 | public: |
| 95 | QHttpMultiPartIODevice(QHttpMultiPartPrivate *parentMultiPart) : |
| 96 | QIODevice(), multiPart(parentMultiPart), readPointer(0), deviceSize(-1) { |
| 97 | } |
| 98 | |
| 99 | ~QHttpMultiPartIODevice() override; |
| 100 | |
| 101 | virtual bool atEnd() const override { |
| 102 | return readPointer == size(); |
| 103 | } |
| 104 | |
| 105 | virtual qint64 bytesAvailable() const override { |
| 106 | return size() - readPointer; |
| 107 | } |
| 108 | |
| 109 | virtual void close() override { |
| 110 | readPointer = 0; |
| 111 | partOffsets.clear(); |
| 112 | deviceSize = -1; |
| 113 | QIODevice::close(); |
| 114 | } |
| 115 | |
| 116 | virtual qint64 bytesToWrite() const override { |
| 117 | return 0; |
| 118 | } |
| 119 | |
| 120 | virtual qint64 size() const override; |
| 121 | virtual bool isSequential() const override; |
| 122 | virtual bool reset() override; |
| 123 | virtual qint64 readData(char *data, qint64 maxSize) override; |
| 124 | virtual qint64 writeData(const char *data, qint64 maxSize) override; |
| 125 | |
| 126 | QHttpMultiPartPrivate *multiPart; |
| 127 | qint64 readPointer; |
| 128 | mutable QList<qint64> partOffsets; |
| 129 | mutable qint64 deviceSize; |
| 130 | }; |
| 131 | |
| 132 | |
| 133 | |
| 134 | class Q_AUTOTEST_EXPORT QHttpMultiPartPrivate: public QObjectPrivate |
| 135 | { |
| 136 | public: |
| 137 | |
| 138 | QHttpMultiPartPrivate(); |
| 139 | ~QHttpMultiPartPrivate() override; |
| 140 | |
| 141 | static QHttpMultiPartPrivate *get(QHttpMultiPart *message) { return message->d_func(); } |
| 142 | static const QHttpMultiPartPrivate *get(const QHttpMultiPart *message) |
| 143 | { |
| 144 | return message->d_func(); |
| 145 | } |
| 146 | |
| 147 | QList<QHttpPart> parts; |
| 148 | QByteArray boundary; |
| 149 | QHttpMultiPart::ContentType contentType; |
| 150 | QHttpMultiPartIODevice *device; |
| 151 | |
| 152 | }; |
| 153 | |
| 154 | QT_END_NAMESPACE |
| 155 | |
| 156 | |
| 157 | #endif // QHTTPMULTIPART_P_H |
| 158 | |