9#include "dataprotocol_p.h"
15#include <QStringDecoder>
41 static const char comma =
',';
42 static const char semicolon =
';';
46 const char ch = buf[pos];
47 if (ch == comma || ch == semicolon || (c1 !=
'\0' && ch == c1)) {
63static inline QString extract(
const QByteArray &buf,
int &pos,
const char c1 =
'\0')
66 pos =
find(buf, oldpos, c1);
76static inline void ignoreWS(
const QByteArray &buf,
int &pos)
79 while (pos < size && (buf[pos] ==
' ' || buf[pos] ==
'\t')) {
100 while (parsing && pos < size) {
128static DataHeader parseDataHeader(
const QUrl &url,
const bool mimeOnly)
130 DataHeader header_info;
133 header_info.mime_type = QStringLiteral(
"text/plain");
134 header_info.attributes.
insert(QStringLiteral(
"charset"), QStringLiteral(
"us-ascii"));
135 header_info.is_base64 =
false;
139 const int raw_url_len = raw_url.
length();
141 header_info.data_offset = 0;
144 if (raw_url_len == 0) {
147 const QString mime_type = extract(raw_url, header_info.data_offset).
trimmed();
149 header_info.mime_type = mime_type;
155 if (header_info.data_offset >= raw_url_len) {
159 if (raw_url[header_info.data_offset++] ==
',') {
164 bool data_begin_reached =
false;
165 while (!data_begin_reached && header_info.data_offset < raw_url_len) {
167 const QString attribute = extract(raw_url, header_info.data_offset,
'=').
trimmed();
168 if (header_info.data_offset >= raw_url_len || raw_url[header_info.data_offset] !=
'=') {
171 header_info.is_base64 =
true;
174 header_info.data_offset++;
177 ignoreWS(raw_url, header_info.data_offset);
178 if (header_info.data_offset >= raw_url_len) {
183 if (raw_url[header_info.data_offset] ==
'"') {
184 value = parseQuotedString(raw_url, header_info.data_offset);
185 ignoreWS(raw_url, header_info.data_offset);
187 value = extract(raw_url, header_info.data_offset).
trimmed();
191 header_info.attributes[attribute.
toLower()] = value;
194 if (header_info.data_offset < raw_url_len && raw_url[header_info.data_offset] ==
',') {
195 data_begin_reached =
true;
197 header_info.data_offset++;
203DataProtocol::DataProtocol()
207DataProtocol::~DataProtocol() =
default;
209void DataProtocol::get(
const QUrl &url)
214 const DataHeader hdr = parseDataHeader(url,
false);
216 const int size = hdr.url.
length();
217 const int data_ofs = qMin(hdr.data_offset, size);
227 QStringDecoder codec(hdr.attributes[QStringLiteral(
"charset")].toLatin1().constData());
228 if (codec.isValid()) {
238 Q_EMIT totalSize(outData.
size());
241 setAllMetaData(hdr.attributes);
247 Q_EMIT data(outData);
258void DataProtocol::mimetype(
const QUrl &url)
261 Q_EMIT
mimeType(parseDataHeader(url,
true).mime_type);
269#include "moc_dataprotocol_p.cpp"
KCALUTILS_EXPORT QString mimeType()
A namespace for KIO globals.
const QList< QKeySequence > & begin()
QByteArray fromBase64(const QByteArray &base64, Base64Options options)
QByteArray fromPercentEncoding(const QByteArray &input, char percent)
qsizetype length() const const
QByteArray mid(qsizetype pos, qsizetype len) const const
qsizetype size() const const
iterator insert(const Key &key, const T &value)
QString fromLatin1(QByteArrayView str)
bool isEmpty() const const
void reserve(qsizetype size)
QByteArray toLatin1() const const
QString toLower() const const
QByteArray toUtf8() const const
QString trimmed() const const
QString path(ComponentFormattingOptions options) const const