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 "QtCore/qshareddata.h" |
20 | #include "qnetworkrequest_p.h" // for deriving QHttpPartPrivate from QNetworkHeadersPrivate |
21 | #include "private/qobject_p.h" |
22 | |
23 | #ifndef Q_OS_WASM |
24 | QT_REQUIRE_CONFIG(http); |
25 | #endif |
26 | |
27 | QT_BEGIN_NAMESPACE |
28 | |
29 | |
30 | class QHttpPartPrivate: public QSharedData, public QNetworkHeadersPrivate |
31 | { |
32 | public: |
33 | inline QHttpPartPrivate() : bodyDevice(nullptr), headerCreated(false), readPointer(0) |
34 | { |
35 | } |
36 | ~QHttpPartPrivate() |
37 | { |
38 | } |
39 | |
40 | |
41 | QHttpPartPrivate(const QHttpPartPrivate &other) |
42 | : QSharedData(other), QNetworkHeadersPrivate(other), body(other.body), |
43 | header(other.header), headerCreated(other.headerCreated), readPointer(other.readPointer) |
44 | { |
45 | bodyDevice = other.bodyDevice; |
46 | } |
47 | |
48 | inline bool operator==(const QHttpPartPrivate &other) const |
49 | { |
50 | return rawHeaders == other.rawHeaders && body == other.body && |
51 | bodyDevice == other.bodyDevice && readPointer == other.readPointer; |
52 | } |
53 | |
54 | void setBodyDevice(QIODevice *device) { |
55 | bodyDevice = device; |
56 | readPointer = 0; |
57 | } |
58 | void setBody(const QByteArray &newBody) { |
59 | body = newBody; |
60 | readPointer = 0; |
61 | } |
62 | |
63 | // QIODevice-style methods called by QHttpMultiPartIODevice (but this class is |
64 | // not a QIODevice): |
65 | qint64 bytesAvailable() const; |
66 | qint64 readData(char *data, qint64 maxSize); |
67 | qint64 size() const; |
68 | bool reset(); |
69 | |
70 | QByteArray body; |
71 | QIODevice *bodyDevice; |
72 | |
73 | private: |
74 | void () const; |
75 | |
76 | mutable QByteArray ; |
77 | mutable bool ; |
78 | qint64 readPointer; |
79 | }; |
80 | |
81 | |
82 | |
83 | class QHttpMultiPartPrivate; |
84 | |
85 | class Q_AUTOTEST_EXPORT QHttpMultiPartIODevice : public QIODevice |
86 | { |
87 | public: |
88 | QHttpMultiPartIODevice(QHttpMultiPartPrivate *parentMultiPart) : |
89 | QIODevice(), multiPart(parentMultiPart), readPointer(0), deviceSize(-1) { |
90 | } |
91 | |
92 | ~QHttpMultiPartIODevice() { |
93 | } |
94 | |
95 | virtual bool atEnd() const override { |
96 | return readPointer == size(); |
97 | } |
98 | |
99 | virtual qint64 bytesAvailable() const override { |
100 | return size() - readPointer; |
101 | } |
102 | |
103 | virtual void close() override { |
104 | readPointer = 0; |
105 | partOffsets.clear(); |
106 | deviceSize = -1; |
107 | QIODevice::close(); |
108 | } |
109 | |
110 | virtual qint64 bytesToWrite() const override { |
111 | return 0; |
112 | } |
113 | |
114 | virtual qint64 size() const override; |
115 | virtual bool isSequential() const override; |
116 | virtual bool reset() override; |
117 | virtual qint64 readData(char *data, qint64 maxSize) override; |
118 | virtual qint64 writeData(const char *data, qint64 maxSize) override; |
119 | |
120 | QHttpMultiPartPrivate *multiPart; |
121 | qint64 readPointer; |
122 | mutable QList<qint64> partOffsets; |
123 | mutable qint64 deviceSize; |
124 | }; |
125 | |
126 | |
127 | |
128 | class QHttpMultiPartPrivate: public QObjectPrivate |
129 | { |
130 | public: |
131 | |
132 | QHttpMultiPartPrivate(); |
133 | |
134 | ~QHttpMultiPartPrivate() |
135 | { |
136 | delete device; |
137 | } |
138 | |
139 | QList<QHttpPart> parts; |
140 | QByteArray boundary; |
141 | QHttpMultiPart::ContentType contentType; |
142 | QHttpMultiPartIODevice *device; |
143 | |
144 | }; |
145 | |
146 | QT_END_NAMESPACE |
147 | |
148 | |
149 | #endif // QHTTPMULTIPART_P_H |
150 | |