Attica

postfiledata.cpp
1 /*
2  This file is part of KDE.
3 
4  SPDX-FileCopyrightText: 1999 Matthias Kalle Dalheimer <[email protected]>
5  SPDX-FileCopyrightText: 2000 Charles Samuels <[email protected]>
6  SPDX-FileCopyrightText: 2005 Joseph Wenninger <[email protected]>
7  SPDX-FileCopyrightText: 2009 Frederik Gladhorn <[email protected]>
8 
9  SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
10 */
11 
12 #include "postfiledata.h"
13 
14 #include <QRandomGenerator>
15 #include <QDebug>
16 #include <QDateTime>
17 
18 namespace Attica
19 {
20 class PostFileDataPrivate
21 {
22 public:
23  QByteArray buffer;
24  QByteArray boundary;
25  QUrl url;
26  bool finished;
27 
28  PostFileDataPrivate()
29  : finished(false)
30  {
31  }
32 };
33 
34 PostFileData::PostFileData(const QUrl &url)
35  : d(new PostFileDataPrivate)
36 {
37  d->url = url;
38  d->boundary = "----------" + randomString(42 + 13).toLatin1();
39 }
40 
41 PostFileData::~PostFileData()
42 {
43  delete d;
44 }
45 
46 QString PostFileData::randomString(int length)
47 {
48  if (length <= 0) {
49  return QString();
50  }
51 
52  QString str; str.resize(length);
53  int i = 0;
54  auto *generator = QRandomGenerator::global();
55  while (length--) {
56  int r = generator->bounded(62);
57  r += 48;
58  if (r > 57) {
59  r += 7;
60  }
61  if (r > 90) {
62  r += 6;
63  }
64  str[i++] = QLatin1Char(char(r));
65  }
66  return str;
67 }
68 
69 void PostFileData::addArgument(const QString &key, const QString &value)
70 {
71  if (d->finished) {
72  qWarning() << "PostFileData::addFile: should not add data after calling request() or data()";
73  }
74  QByteArray data(
75  "--" + d->boundary + "\r\n"
76  "Content-Disposition: form-data; name=\"" + key.toLatin1() +
77  "\"\r\n\r\n" + value.toUtf8() + "\r\n");
78 
79  d->buffer.append(data);
80 }
81 
82 /*
83 void PostFileData::addFile(const QString& fileName, QIODevice* file, const QString& mimeType)
84 {
85  if (d->finished) {
86  qCDebug(ATTICA) << "PostFileData::addFile: should not add data after calling request() or data()";
87  }
88  QByteArray data = file->readAll();
89  addFile(fileName, data, mimeType);
90 }
91 */
92 
93 void PostFileData::addFile(const QString &fileName, const QByteArray &file, const QString &mimeType, const QString &fieldName)
94 {
95  if (d->finished) {
96  qWarning() << "PostFileData::addFile: should not add data after calling request() or data()";
97  }
98 
99  QByteArray data(
100  "--" + d->boundary + "\r\n"
101  "Content-Disposition: form-data; name=\"");
102  data.append(fieldName.toLatin1());
103  data.append("\"; filename=\"" + fileName.toUtf8()
104  + "\"\r\nContent-Type: " + mimeType.toLatin1() + "\r\n\r\n");
105 
106  d->buffer.append(data);
107  d->buffer.append(file + QByteArray("\r\n"));
108 }
109 
110 QNetworkRequest PostFileData::request()
111 {
112  if (!d->finished) {
113  finish();
114  }
115  QNetworkRequest request;
116  request.setUrl(d->url);
117  request.setHeader(QNetworkRequest::ContentTypeHeader, QByteArray("multipart/form-data; boundary=" + d->boundary));
118  request.setHeader(QNetworkRequest::ContentLengthHeader, d->buffer.length());
119  return request;
120 }
121 
122 QByteArray PostFileData::data()
123 {
124  if (!d->finished) {
125  finish();
126  }
127  return d->buffer;
128 }
129 
130 void PostFileData::finish()
131 {
132  Q_ASSERT(!d->finished);
133  d->finished = true;
134  d->buffer.append("--" + d->boundary + "--");
135 }
136 
137 }
void setUrl(const QUrl &url)
void resize(int size)
void setHeader(QNetworkRequest::KnownHeaders header, const QVariant &value)
QByteArray toLatin1() const const
QRandomGenerator * global()
KCOREADDONS_EXPORT QString randomString(int length)
QByteArray toUtf8() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Mon Jan 25 2021 23:06:24 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.