|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the QtNetwork module of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #include "qnetworkaccessdatabackend_p.h" |
|
43 #include "qnetworkrequest.h" |
|
44 #include "qnetworkreply.h" |
|
45 #include "qurlinfo.h" |
|
46 |
|
47 QT_BEGIN_NAMESPACE |
|
48 |
|
49 QNetworkAccessBackend * |
|
50 QNetworkAccessDataBackendFactory::create(QNetworkAccessManager::Operation, |
|
51 const QNetworkRequest &request) const |
|
52 { |
|
53 if (request.url().scheme() == QLatin1String("data")) |
|
54 return new QNetworkAccessDataBackend; |
|
55 |
|
56 return 0; |
|
57 } |
|
58 |
|
59 QNetworkAccessDataBackend::QNetworkAccessDataBackend() |
|
60 { |
|
61 } |
|
62 |
|
63 QNetworkAccessDataBackend::~QNetworkAccessDataBackend() |
|
64 { |
|
65 } |
|
66 |
|
67 void QNetworkAccessDataBackend::open() |
|
68 { |
|
69 QUrl uri = request().url(); |
|
70 |
|
71 if (operation() != QNetworkAccessManager::GetOperation && |
|
72 operation() != QNetworkAccessManager::HeadOperation) { |
|
73 // data: doesn't support anything but GET |
|
74 QString msg = QObject::tr("Operation not supported on %1") |
|
75 .arg(uri.toString()); |
|
76 error(QNetworkReply::ContentOperationNotPermittedError, msg); |
|
77 finished(); |
|
78 return; |
|
79 } |
|
80 |
|
81 if (uri.host().isEmpty()) { |
|
82 setHeader(QNetworkRequest::ContentTypeHeader, QLatin1String("text/plain;charset=US-ASCII")); |
|
83 |
|
84 // the following would have been the correct thing, but |
|
85 // reality often differs from the specification. People have |
|
86 // data: URIs with ? and # |
|
87 //QByteArray data = QByteArray::fromPercentEncoding(uri.encodedPath()); |
|
88 QByteArray data = QByteArray::fromPercentEncoding(uri.toEncoded()); |
|
89 |
|
90 // remove the data: scheme |
|
91 data.remove(0, 5); |
|
92 |
|
93 // parse it: |
|
94 int pos = data.indexOf(','); |
|
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 } |
|
129 |
|
130 // something wrong with this URI |
|
131 QString msg = QObject::tr("Invalid URI: %1").arg(uri.toString()); |
|
132 error(QNetworkReply::ProtocolFailure, msg); |
|
133 finished(); |
|
134 } |
|
135 |
|
136 void QNetworkAccessDataBackend::closeDownstreamChannel() |
|
137 { |
|
138 } |
|
139 |
|
140 void QNetworkAccessDataBackend::closeUpstreamChannel() |
|
141 { |
|
142 } |
|
143 |
|
144 bool QNetworkAccessDataBackend::waitForDownstreamReadyRead(int) |
|
145 { |
|
146 return false; |
|
147 } |
|
148 |
|
149 bool QNetworkAccessDataBackend::waitForUpstreamBytesWritten(int) |
|
150 { |
|
151 return false; |
|
152 } |
|
153 |
|
154 QT_END_NAMESPACE |