qt - Convert QUrl with percent encoding into string -
i utilize url entered user text initialize qurl object. later want convert qurl string displaying , check using regular expression. works fine long user not come in percent encoded urls.
why doesn't next illustration code work?
qdebug() << qurl("http://test.com/query?q=%2b%2be%3axyz%2fen").todisplaystring(qurl::fullydecoded); it doesn't decode of percent-encoded characters. should print "http://test.com/query?q=++e:xyz/en" prints "http://test.com/query?q=%2b%2be%3axyz%2fen".
i tried lot of other methods fromuserinput() not create code work correctly in qt5.3.
can explain me how , why above code doesn't work (i.e. showing decoded url) when using qurl::fullydecoded?
update
after getting frompercentencoding() hint, tried next code:
qurl urlfromuserinput(const qstring& input) { qbytearray latin = input.tolatin1(); qbytearray utf8 = input.toutf8(); if (latin != utf8) { // url string containing unicode characters (no percent encoding expected) homecoming qurl::fromuserinput(input); } else { // url string containing ascii characters (assume possible %-encoding) homecoming qurl::fromuserinput(qurl::frompercentencoding(input.tolatin1())); } } this allows user input unicode urls , percent-encoded urls , possible decode both kinds of urls displaying/matching. percent-encoded urls did not work in qwebview... web-server responded differently (it returned different page). qurl::frompercentencoding() not clean solution since changes url. create 2 qurl objects in above function... 1 constructed directly, 1 constructed using frompercentencoding(), using first qwebview , latter displaying/matching only... seems absurd.
conclusion
i've done research, conclusion far is: absurd.
qurl::frompercentencoding() way go , op has done in update section should've been accepted reply question in title.
i think qt's document of qurl::todisplaystring little bit misleading :
"returns human-displayable string representation of url. output can customized passing flags options. alternative removepassword enabled, since passwords should never shown users."
actually doesn't claim decoding ability, document here unclear it's behavior. @ to the lowest degree password part true. i've found clues on gitorious:
"add qurl::todisplaystring(), tostring() without password. , prepare documentation of tostring() said method utilize displaying humans, while has never been true."
test codein order discern decoding ability of different functions. next code has been tested on qt 5.2.1 (not tested on qt 5.3 yet!)
qstring target(/*path*/); qurl url_path(target); qdebug() << "[original string]:" << target; qdebug() << "--------------------------------------------------------------------"; qdebug() << "(qurl::toencoded) :" << url_path.toencoded(qurl::fullyencoded); qdebug() << "(qurl::url) :" << url_path.url(); qdebug() << "(qurl::tostring) :" << url_path.tostring(); qdebug() << "(qurl::todisplaystring) :" << url_path.todisplaystring(qurl::fullydecoded); qdebug() << "(qurl::frompercentencoding):" << url_path.frompercentencoding(target.toutf8()); return qbytearray: qurl::toencoded return qstring: qurl::url, qurl::tostring, qurl::todisplaystring, qurl::frompercentencoding p.s. qurl::url synonym qurl::tostring.
[case 1]: when target path = "%_%" (test functionality of encoding):
[original string]: "%_%" -------------------------------------------------------------------- (qurl::toencoded) : "%25_%25" (qurl::url) : "%25_%25" (qurl::tostring) : "%25_%25" (qurl::todisplaystring) : "%25_%25" (qurl::frompercentencoding): "%_%" [case 2]: when target path = "meow !" (test functionality of encoding):
[original string]: "meow !" -------------------------------------------------------------------- (qurl::toencoded) : "meow%20!" (qurl::url) : "meow !" (qurl::tostring) : "meow !" (qurl::todisplaystring) : "meow%20!" // "meow !" when using qurl::prettydecoded mode (qurl::frompercentencoding): "meow !" [case 3]: when target path = "meow|!" (test functionality of encoding):
[original string]: "meow|!" -------------------------------------------------------------------- (qurl::toencoded) : "meow%7c!" (qurl::url) : "meow%7c!" (qurl::tostring) : "meow%7c!" (qurl::todisplaystring) : "meow|!" // "meow%7c!" when using qurl::prettydecoded mode (qurl::frompercentencoding): "meow|!" [case 4]: when target path = "http://test.com/query?q=++e:xyz/en" (none % encoded):
[original string]: "http://test.com/query?q=++e:xyz/en" -------------------------------------------------------------------- (qurl::toencoded) : "http://test.com/query?q=++e:xyz/en" (qurl::url) : "http://test.com/query?q=++e:xyz/en" (qurl::tostring) : "http://test.com/query?q=++e:xyz/en" (qurl::todisplaystring) : "http://test.com/query?q=++e:xyz/en" (qurl::frompercentencoding): "http://test.com/query?q=++e:xyz/en" [case 5]: when target path = "http://test.com/query?q=%2b%2be%3axyz%2fen" (% encoded):
[original string]: "http://test.com/query?q=%2b%2be%3axyz%2fen" -------------------------------------------------------------------- (qurl::toencoded) : "http://test.com/query?q=%2b%2be%3axyz%2fen" (qurl::url) : "http://test.com/query?q=%2b%2be%3axyz%2fen" (qurl::tostring) : "http://test.com/query?q=%2b%2be%3axyz%2fen" (qurl::todisplaystring) : "http://test.com/query?q=%2b%2be%3axyz%2fen" (qurl::frompercentencoding): "http://test.com/query?q=++e:xyz/en" p.s. encounter bug ilya mentioned in comments: percent encoding doesn't seem working '+' in qurl
summarythe result of qurl::todisplaystring ambiguous. document says, qurl::fullydecoded mode must used care. no matter type of url got, encode them qurl::toencode , display them qurl::frompercentencoding when necessary.
as malfunction of percent-encoded urls in qwebview mentioned in op, more details needed debug it. different function , different mode used reason.
qt urlencode qstring qurl
No comments:
Post a Comment