[Interest] Difference between QUrl::fromEncoded and QUrl::fromPercentEncoding ?
Thiago Macieira
thiago.macieira at intel.com
Wed Aug 14 01:01:09 CEST 2013
On quarta-feira, 14 de agosto de 2013 05:30:44, Richard Turner wrote:
> Just one more question: could you elaborate on the idea that calling the
> QUrl constructor taking a QString is a big no-no in Qt 4 code, but not
> in Qt 5?
The problem is that the QUrl constructor in Qt 4 takes a fully-decoded URL,
which is a broken concept. Fully-decoded URLs don't exist because you *need*
the percents in some cases to make distinctions.
For example, the two URIs are not equivalent (as you've found out):
http%3A%2F%2Fexample.com
http://example.com
URIs (and URLs, which are a subset of URIs) only truly exist in percent-
encoded forms so that the two above forms can be distinguished from one
another. But the QUrl constructor in Qt 4 assumes that all URLs are in fully
decoded form. Then when you do:
QUrl url("http%3A%2F%2Fexample.com")
qDebug() << url.toEncoded();
because QUrl::toEncoded is the form you'd pass to other applications, you'll
get:
"http%253A%252F%252Fexample.com"
Which is a third URL, different from the other two above!
PS: I've just found a bug with this URL in Qt 5.[01]:
QUrl url("http%3A%2F%2Fexample.com");
qDebug() << url.path(); // prints "http:%2F%2Fexample.com"
qDebug() << url.isValid(); // prints false
qDebug() << url.toEncoded(); // prints ""
There's a special error condition in URLs that QUrl in Qt 5 recognises: path
contains a colon before the first slash. However, your URL does *not* contain a
colon before the first slash, it contains a %3A and that's not the same thing.
This is fixed in Qt 5.2 and I've just submitted a new unit test to prove it:
https://codereview.qt-project.org/62946
--
Thiago Macieira - thiago.macieira (AT) intel.com
Software Architect - Intel Open Source Technology Center
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 190 bytes
Desc: This is a digitally signed message part.
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20130813/bb7965fd/attachment.sig>
More information about the Interest
mailing list