[Interest] Preventing QUrl from encoding query parameters

Thiago Macieira thiago.macieira at intel.com
Mon Aug 4 00:09:13 CEST 2014


On Sunday 03 August 2014 18:52:05 Konstantin Tokarev wrote:
> 03.08.2014, 06:18, "Thiago Macieira" <thiago.macieira at intel.com>:
> > On Saturday 02 August 2014 13:26:15 Lorne Sturtevant wrote:
> >>  On 08/02/2014 11:27 AM, Dmitriy Purgin wrote:
> >>>  Hi,
> >>> 
> >>>  If you pass this raw QString or QByteArray somewhere, why use QUrl
> >>> 
> >>>  then? Anyway, try QUrl::fromPercentEncoding():
> >>>      QUrl url("http://example.com?query=[{\"name\":\"value\"}]");
> >>>      qDebug() << QUrl::fromPercentEncoding(url.toEncoded());
> >>>      // produces: "http://example.com?query=[{"name":"value"}]"
> >> 
> >>  I should clarify that I need the QUrl because I need to pass it to a
> >>  QNetworkRequest.  That class only takes QUrls.  I can't pass a string.
> >> 
> >>  QUrl::fromPercentEncoding() produces a QString.  That's not what I need.
> > 
> > You can't do what you want. QUrl will normalise what it has to.
> > 
> > So just make sure that you are running Qt 5.3, since there were bugs in
> > previous versions. If that doesn't work, fix your server.
> 
> ...or use different HTTP client implementation.

No, fix the server. Really.

You cannot expect to that an HTTP stack will allow you to send non-compliant 
data. And even if you did, please be careful of proxies (including transparent 
ones) that might rewrite your URL.

In any case, the question is about characters ", { and } in the query in the 
URL. The spec says:

    query         = *( pchar / "/" / "?" )
    pchar         = unreserved / pct-encoded / sub-delims / ":" / "@"
    unreserved    = ALPHA / DIGIT / "-" / "." / "_" / "~"
    gen-delims    = ":" / "/" / "?" / "#" / "[" / "]" / "@"
    sub-delims    = "!" / "$" / "&" / "'" / "(" / ")"
                  / "*" / "+" / "," / ";" / "="

As you can see from the ABNF above, none of the three characters are allowed 
to appear in literal form in the query part of the URL. They must be encoded, 
which QUrl does.

The server CANNOT require them to appear in literal form. If you're going to 
violate HTTP, you may as well write your own protocol and stop usnig HTTP.

-- 
Thiago Macieira - thiago.macieira (AT) intel.com
  Software Architect - Intel Open Source Technology Center




More information about the Interest mailing list