Port to the new QUrl API

The use of any broken-down components of the query now needs
QUrlQuery.

The QUrl constructor and toString() are now rehabilitated and the
preferred forms. Use toEncoded() and fromEncoded() now only when we
need to store data in a QByteArray or the data comes from a QByteArray
anyway. Change to toString() or the constructor if the data was in a
QString.

Change-Id: I9d761a628bef9c70185a48e927a61779a1642342
Reviewed-by: Lars Knoll <lars.knoll@nokia.com>
bb10
Thiago Macieira 2011-10-13 19:56:28 +02:00 committed by Qt by Nokia
parent 8cf66c3bc4
commit 74d2dba460
13 changed files with 28 additions and 50 deletions

View File

@ -337,7 +337,7 @@ QUrlQuery::QUrlQuery(const QUrl &url)
// use internals to avoid unnecessary recoding
// ### FIXME: actually do it
if (url.hasQuery())
d = new QUrlQueryPrivate(QString::fromUtf8(url.encodedQuery()));
d = new QUrlQueryPrivate(url.query());
}
/*!

View File

@ -59,7 +59,7 @@ static QPixmap getPixmap(QTextDocument *doc, const QTextImageFormat &format)
QString name = format.name();
if (name.startsWith(QLatin1String(":/"))) // auto-detect resources
name.prepend(QLatin1String("qrc"));
QUrl url = QUrl::fromEncoded(name.toUtf8());
QUrl url = QUrl(name);
const QVariant data = doc->resource(QTextDocument::ImageResource, url);
if (data.type() == QVariant::Pixmap || data.type() == QVariant::Image) {
pm = qvariant_cast<QPixmap>(data);
@ -134,7 +134,7 @@ static QImage getImage(QTextDocument *doc, const QTextImageFormat &format)
QString name = format.name();
if (name.startsWith(QLatin1String(":/"))) // auto-detect resources
name.prepend(QLatin1String("qrc"));
QUrl url = QUrl::fromEncoded(name.toUtf8());
QUrl url = QUrl(name);
const QVariant data = doc->resource(QTextDocument::ImageResource, url);
if (data.type() == QVariant::Image) {
image = qvariant_cast<QImage>(data);

View File

@ -366,7 +366,7 @@ void QTextOdfWriter::writeInlineCharacter(QXmlStreamWriter &writer, const QTextF
QString name = imageFormat.name();
if (name.startsWith(QLatin1String(":/"))) // auto-detect resources
name.prepend(QLatin1String("qrc"));
QUrl url = QUrl::fromEncoded(name.toUtf8());
QUrl url = QUrl(name);
const QVariant data = m_document->resource(QTextDocument::ImageResource, url);
if (data.type() == QVariant::Image) {
image = qvariant_cast<QImage>(data);

View File

@ -116,19 +116,18 @@ QByteArray QHttpNetworkRequestPrivate::methodName() const
QByteArray QHttpNetworkRequestPrivate::uri(bool throughProxy) const
{
QUrl::FormattingOptions format(QUrl::RemoveFragment);
QUrl::FormattingOptions format(QUrl::RemoveFragment | QUrl::RemoveUserInfo | QUrl::FullyEncoded);
// for POST, query data is send as content
if (operation == QHttpNetworkRequest::Post && !uploadByteDevice)
format |= QUrl::RemoveQuery;
// for requests through proxy, the Request-URI contains full url
if (throughProxy)
format |= QUrl::RemoveUserInfo;
else
if (!throughProxy)
format |= QUrl::RemoveScheme | QUrl::RemoveAuthority;
QByteArray uri = url.toEncoded(format);
if (uri.isEmpty() || (throughProxy && url.path().isEmpty()))
uri += '/';
QUrl copy = url;
if (copy.path().isEmpty())
copy.setPath(QStringLiteral("/"));
QByteArray uri = copy.toEncoded(format);
return uri;
}
@ -163,7 +162,7 @@ QByteArray QHttpNetworkRequestPrivate::header(const QHttpNetworkRequest &request
ba += "Content-Type: application/octet-stream\r\n";
}
if (!request.d->uploadByteDevice && request.d->url.hasQuery()) {
QByteArray query = request.d->url.encodedQuery();
QByteArray query = request.d->url.query(QUrl::FullyEncoded).toLatin1();
ba += "Content-Length: ";
ba += QByteArray::number(query.size());
ba += "\r\n\r\n";

View File

@ -105,12 +105,12 @@ static QNetworkReply::NetworkError statusCodeFromHttp(int httpStatusCode, const
static QByteArray makeCacheKey(QUrl &url, QNetworkProxy *proxy)
{
QByteArray result;
QString result;
QUrl copy = url;
bool isEncrypted = copy.scheme().toLower() == QLatin1String("https");
copy.setPort(copy.port(isEncrypted ? 443 : 80));
result = copy.toEncoded(QUrl::RemoveUserInfo | QUrl::RemovePath |
QUrl::RemoveQuery | QUrl::RemoveFragment);
result = copy.toString(QUrl::RemoveUserInfo | QUrl::RemovePath |
QUrl::RemoveQuery | QUrl::RemoveFragment | QUrl::FullyEncoded);
#ifndef QT_NO_NETWORKPROXY
if (proxy && proxy->type() != QNetworkProxy::NoProxy) {
@ -134,15 +134,15 @@ static QByteArray makeCacheKey(QUrl &url, QNetworkProxy *proxy)
key.setUserName(proxy->user());
key.setHost(proxy->hostName());
key.setPort(proxy->port());
key.setEncodedQuery(result);
result = key.toEncoded();
key.setQuery(result);
result = key.toString(QUrl::FullyEncoded);
}
}
#else
Q_UNUSED(proxy)
#endif
return "http-connection:" + result;
return "http-connection:" + result.toLatin1();
}
class QNetworkAccessCachedHttpConnection: public QHttpNetworkConnection,
@ -386,7 +386,7 @@ void QHttpThreadDelegate::finishedSlot()
// it's an error reply
QString msg = QLatin1String(QT_TRANSLATE_NOOP("QNetworkReply",
"Error downloading %1 - server replied: %2"));
msg = msg.arg(QString::fromAscii(httpRequest.url().toEncoded()), httpReply->reasonPhrase());
msg = msg.arg(httpRequest.url().toString(), httpReply->reasonPhrase());
emit error(statusCodeFromHttp(httpReply->statusCode(), httpRequest.url()), msg);
}
@ -406,7 +406,7 @@ void QHttpThreadDelegate::synchronousFinishedSlot()
// it's an error reply
QString msg = QLatin1String(QT_TRANSLATE_NOOP("QNetworkReply",
"Error downloading %1 - server replied: %2"));
incomingErrorDetail = msg.arg(QString::fromAscii(httpRequest.url().toEncoded()), httpReply->reasonPhrase());
incomingErrorDetail = msg.arg(httpRequest.url().toString(), httpReply->reasonPhrase());
incomingErrorCode = statusCodeFromHttp(httpReply->statusCode(), httpRequest.url());
}

View File

@ -42,6 +42,7 @@
#include "qnetworkaccessdebugpipebackend_p.h"
#include "QtCore/qdatastream.h"
#include <QCoreApplication>
#include <QUrlQuery>
#include "private/qnoncontiguousbytedevice_p.h"
QT_BEGIN_NAMESPACE
@ -99,7 +100,7 @@ void QNetworkAccessDebugPipeBackend::open()
// socket bytes written -> we can push more from upstream to socket
connect(&socket, SIGNAL(bytesWritten(qint64)), SLOT(socketBytesWritten(qint64)));
bareProtocol = url().queryItemValue(QLatin1String("bare")) == QLatin1String("1");
bareProtocol = QUrlQuery(url()).queryItemValue(QLatin1String("bare")) == QLatin1String("1");
if (operation() == QNetworkAccessManager::PutOperation) {
uploadByteDevice = createUploadByteDevice();

View File

@ -88,7 +88,7 @@ QNetworkReplyDataImpl::QNetworkReplyDataImpl(QObject *parent, const QNetworkRequ
} else {
// something wrong with this URI
const QString msg = QCoreApplication::translate("QNetworkAccessDataBackend",
"Invalid URI: %1").arg(QString::fromLatin1(url.toEncoded()));
"Invalid URI: %1").arg(url.toString());
setError(QNetworkReply::ProtocolFailure, msg);
QMetaObject::invokeMethod(this, "error", Qt::QueuedConnection,
Q_ARG(QNetworkReply::NetworkError, QNetworkReply::ProtocolFailure));

View File

@ -1039,7 +1039,7 @@ void QNetworkReplyHttpImplPrivate::checkForRedirect(const int statusCode)
// The response to a 303 MUST NOT be cached, while the response to
// all of the others is cacheable if the headers indicate it to be
QByteArray header = q->rawHeader("location");
QUrl url = QUrl::fromEncoded(header);
QUrl url = QUrl(QString::fromUtf8(header));
if (!url.isValid())
url = QUrl(QLatin1String(header));
// FIXME?

View File

@ -142,7 +142,7 @@ template<> inline char *toString(const QRectF &s)
template<> inline char *toString(const QUrl &uri)
{
return qstrdup(uri.toEncoded().constData());
return qstrdup(uri.toEncoded(QUrl::DecodeUnambiguousDelimiters).constData());
}
template<> inline char *toString(const QVariant &v)

View File

@ -139,7 +139,7 @@ public:
// re-imlemented from QTextEditPrivate
virtual QUrl resolveUrl(const QUrl &url) const;
inline QUrl resolveUrl(const QString &url) const
{ return resolveUrl(QUrl::fromEncoded(url.toUtf8())); }
{ return resolveUrl(QUrl(url)); }
#ifdef QT_KEYPAD_NAVIGATION
void keypadMove(bool next);

View File

@ -6742,7 +6742,7 @@ void tst_QNetworkReply::pipeliningHelperSlot() {
pipeliningWasUsed = true;
// check that the contents match (the response to echo.cgi?3 should return 3 etc.)
QString urlQueryString = reply->url().queryItems().at(0).first;
QString urlQueryString = reply->url().query();
QString content = reply->readAll();
QVERIFY2(urlQueryString == content, "data corruption with pipelining detected");

View File

@ -659,9 +659,10 @@ void tst_NetworkSelfTest::httpServerFiles()
{
QFETCH(QString, uri);
QFETCH(int, size);
QUrl url(uri);
QList<Chat> chat;
chat << Chat::send("HEAD " + QUrl::toPercentEncoding(uri, "/") + " HTTP/1.0\r\n"
chat << Chat::send("HEAD " + url.toEncoded() + " HTTP/1.0\r\n"
"Host: " + QtNetworkSettings::serverName().toLatin1() + "\r\n"
"Connection: close\r\n"
"Authorization: Basic cXNvY2tzdGVzdDpwYXNzd29yZA==\r\n"

View File

@ -56,8 +56,6 @@ private slots:
void toLocalFile();
void toString_data();
void toString();
void toEncoded_data();
void toEncoded();
void resolved_data();
void resolved();
void equality_data();
@ -160,27 +158,6 @@ void tst_qurl::toString()
}
}
void tst_qurl::toEncoded_data()
{
generateFirstRunData();
}
void tst_qurl::toEncoded()
{
QFETCH(bool, firstRun);
if(firstRun) {
QBENCHMARK {
QUrl url("pics/avatar.png");
url.toEncoded(QUrl::FormattingOption(0x100));
}
} else {
QUrl url("pics/avatar.png");
QBENCHMARK {
url.toEncoded(QUrl::FormattingOption(0x100));
}
}
}
void tst_qurl::resolved_data()
{
generateFirstRunData();