source: trunk/src/network/access/qnetworkaccessdatabackend.cpp@ 123

Last change on this file since 123 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 4.9 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the QtNetwork module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
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
47QT_BEGIN_NAMESPACE
48
49QNetworkAccessBackend *
50QNetworkAccessDataBackendFactory::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
59QNetworkAccessDataBackend::QNetworkAccessDataBackend()
60{
61}
62
63QNetworkAccessDataBackend::~QNetworkAccessDataBackend()
64{
65}
66
67void 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 writeDownstreamData(payload);
121 finished();
122 return;
123 }
124 }
125
126 // something wrong with this URI
127 QString msg = QObject::tr("Invalid URI: %1").arg(uri.toString());
128 error(QNetworkReply::ProtocolFailure, msg);
129 finished();
130}
131
132void QNetworkAccessDataBackend::closeDownstreamChannel()
133{
134}
135
136void QNetworkAccessDataBackend::closeUpstreamChannel()
137{
138}
139
140bool QNetworkAccessDataBackend::waitForDownstreamReadyRead(int)
141{
142 return false;
143}
144
145bool QNetworkAccessDataBackend::waitForUpstreamBytesWritten(int)
146{
147 return false;
148}
149
150QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.