[Interest] Difference between QUrl::fromEncoded and QUrl::fromPercentEncoding ?

Thiago Macieira thiago.macieira at intel.com
Wed Aug 14 07:53:30 CEST 2013


On quarta-feira, 14 de agosto de 2013 10:54:53, Mandeep Sandhu wrote:
> The docs of QUrl::fromEncoded() (Qt 4.8) state : "input is assumed to be in
> encoded form, containing only ASCII characters".

Right.

> Why does it assumes the input to be only the path? Is the caller expected
> to explicitly 'set' the scheme on a url returned by it? If so, the docs
> statement looks incomplete.

It doesn't.

> However, in the above url if we decode all percent-encoded chars upto the
> port, then the scheme starts getting detected. This behaviour seems
> confusing at best.

It seems completely understandable to me. A URL is:

	URL = [scheme ":"] [authority] [path] ["?" query] ["#" fragment]
with
	authority = "//" [userinfo "@"] [host] [":" port]

(technically, a URL cannot have an empty scheme, but a "URL reference" can, 
and QUrl parses like that)

Since the input had no ":", there was no scheme. Since the input had no "//", 
it also had no authority. That means everything from the beginning until "?" 
or "#" was the path.

Since there was no "?" or "#", everything in the input was parsed as path.

> Also, if we are getting a percent encoded 'string' from external source,
> whats the correct way of making it into a QUrl?

The URL was parsed correctly as it came. The question is why your external 
source gave you that URL which is not what you wanted it to be.

Most likely, you need to decode it, like you've done:

> QUrl url;
> url.setUrl(QUrl::fromPercentEncoding(encodedUrlString));

QUrl url = QUrl::fromEncoded(QUrl::fromPercentEncoding(doubleEncodedUrl));

But this is not by any means a requirement for the general case. It's *your* 
data that is doubly encoded. You need to know this.

-- 
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/a535e23f/attachment.sig>


More information about the Interest mailing list