41 |
41 |
42 #include "qnetworkaccessdatabackend_p.h" |
42 #include "qnetworkaccessdatabackend_p.h" |
43 #include "qnetworkrequest.h" |
43 #include "qnetworkrequest.h" |
44 #include "qnetworkreply.h" |
44 #include "qnetworkreply.h" |
45 #include "qurlinfo.h" |
45 #include "qurlinfo.h" |
|
46 #include "private/qdataurl_p.h" |
|
47 #include <qcoreapplication.h> |
46 |
48 |
47 QT_BEGIN_NAMESPACE |
49 QT_BEGIN_NAMESPACE |
48 |
50 |
49 QNetworkAccessBackend * |
51 QNetworkAccessBackend * |
50 QNetworkAccessDataBackendFactory::create(QNetworkAccessManager::Operation, |
52 QNetworkAccessDataBackendFactory::create(QNetworkAccessManager::Operation, |
69 QUrl uri = request().url(); |
71 QUrl uri = request().url(); |
70 |
72 |
71 if (operation() != QNetworkAccessManager::GetOperation && |
73 if (operation() != QNetworkAccessManager::GetOperation && |
72 operation() != QNetworkAccessManager::HeadOperation) { |
74 operation() != QNetworkAccessManager::HeadOperation) { |
73 // data: doesn't support anything but GET |
75 // data: doesn't support anything but GET |
74 QString msg = QObject::tr("Operation not supported on %1") |
76 const QString msg = QCoreApplication::translate("QNetworkAccessDataBackend", |
|
77 "Operation not supported on %1") |
75 .arg(uri.toString()); |
78 .arg(uri.toString()); |
76 error(QNetworkReply::ContentOperationNotPermittedError, msg); |
79 error(QNetworkReply::ContentOperationNotPermittedError, msg); |
77 finished(); |
80 finished(); |
78 return; |
81 return; |
79 } |
82 } |
80 |
83 |
81 if (uri.host().isEmpty()) { |
84 QPair<QString, QByteArray> decoded = qDecodeDataUrl(uri); |
82 setHeader(QNetworkRequest::ContentTypeHeader, QLatin1String("text/plain;charset=US-ASCII")); |
|
83 |
85 |
84 // the following would have been the correct thing, but |
86 if (! decoded.first.isNull()) { |
85 // reality often differs from the specification. People have |
87 setHeader(QNetworkRequest::ContentTypeHeader, decoded.first); |
86 // data: URIs with ? and # |
88 setHeader(QNetworkRequest::ContentLengthHeader, decoded.second.size()); |
87 //QByteArray data = QByteArray::fromPercentEncoding(uri.encodedPath()); |
89 emit metaDataChanged(); |
88 QByteArray data = QByteArray::fromPercentEncoding(uri.toEncoded()); |
|
89 |
90 |
90 // remove the data: scheme |
91 QByteDataBuffer list; |
91 data.remove(0, 5); |
92 list.append(decoded.second); |
|
93 decoded.second.clear(); // important because of implicit sharing! |
|
94 writeDownstreamData(list); |
92 |
95 |
93 // parse it: |
96 finished(); |
94 int pos = data.indexOf(','); |
97 return; |
95 if (pos != -1) { |
|
96 QByteArray payload = data.mid(pos + 1); |
|
97 data.truncate(pos); |
|
98 data = data.trimmed(); |
|
99 |
|
100 // find out if the payload is encoded in Base64 |
|
101 if (data.endsWith(";base64")) { |
|
102 payload = QByteArray::fromBase64(payload); |
|
103 data.chop(7); |
|
104 } |
|
105 |
|
106 if (data.toLower().startsWith("charset")) { |
|
107 int i = 7; // strlen("charset") |
|
108 while (data.at(i) == ' ') |
|
109 ++i; |
|
110 if (data.at(i) == '=') |
|
111 data.prepend("text/plain;"); |
|
112 } |
|
113 |
|
114 if (!data.isEmpty()) |
|
115 setHeader(QNetworkRequest::ContentTypeHeader, data.trimmed()); |
|
116 |
|
117 setHeader(QNetworkRequest::ContentLengthHeader, payload.size()); |
|
118 emit metaDataChanged(); |
|
119 |
|
120 QByteDataBuffer list; |
|
121 list.append(payload); |
|
122 payload.clear(); // important because of implicit sharing! |
|
123 writeDownstreamData(list); |
|
124 |
|
125 finished(); |
|
126 return; |
|
127 } |
|
128 } |
98 } |
129 |
99 |
130 // something wrong with this URI |
100 // something wrong with this URI |
131 QString msg = QObject::tr("Invalid URI: %1").arg(uri.toString()); |
101 const QString msg = QCoreApplication::translate("QNetworkAccessDataBackend", |
|
102 "Invalid URI: %1").arg(uri.toString()); |
132 error(QNetworkReply::ProtocolFailure, msg); |
103 error(QNetworkReply::ProtocolFailure, msg); |
133 finished(); |
104 finished(); |
134 } |
105 } |
135 |
106 |
136 void QNetworkAccessDataBackend::closeDownstreamChannel() |
107 void QNetworkAccessDataBackend::closeDownstreamChannel() |