[Qt-interest] QUrl issue while encoding an already encoded string
Mandeep Sandhu
mandeepsandhu.chd at gmail.com
Thu May 13 07:16:47 CEST 2010
Hi All,
I've been bitten by some strange behavior while using QUrl!
I'm not sure if it's a bug with QUrl or it's a case of misuse...
Anyways, I have a simple prog that illustrates the problem:
#include <QtCore/QCoreApplication>
#include <QUrl>
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QString base = QString("http://localhost");
QUrl myurl = QUrl(base);
QUrl myurl2 = QUrl(base, QUrl::TolerantMode); // Same result with
using StrictMode as well
QString param("param");
QString value("50-2010/05/11 22:45:19 +0000"); // This value needs
to have the "/" replaced by %2F
QString encVal = QString(QUrl::toPercentEncoding(value)); //
convert "/" to "%2F"
qDebug() << "Enc Val:" << encVal;
myurl.addQueryItem(param, value);
myurl2.addQueryItem(param, encVal);
qDebug() << "Plain" << myurl.toEncoded(); // This does not encode
the "/" in value
qDebug() << "Enc" << myurl2.toEncoded(); // This encodes the %
in %2F to %25 :(
return a.exec();
}
Now the problem comes when QUrl encodes the percent encoded "value"
string. It's replacing the "%" char with "%25".
However, the QUrl doc says:
<snip>
Single "%" characters: Any occurrences of a percent character "%" not
followed by exactly two hexadecimal characters (e.g., "13%
coverage.html") will be replaced by "%25"
</snip>
Shouldn't it leave the already encoded %2F (representing "/" char)
alone, since the % IS followed by 2 hexadecimal char's "2F"?
Note that I have manually encode the param's value since the normal
encoding done by QUrl leaves out encoding the "/" char in the "value"
string, which is NOT INCORRECT, but just that the server I'm talking
to requires this value to be percent encoded.
My requirement was that I want the complete URL to be encoded as it
should be, except that one of the params is already encoded and should
not be RE-encoded.
Regards,
-mandeep
More information about the Qt-interest-old
mailing list