Convert percent-encoded QUrl to string

I am using the URL entered by the user as text to initialize the QUrl object. Later, I want to convert QUrl back to a string to display it and test it with a regular expression. This works fine until the user enters percent encoded URLs.

Why doesn't the following code sample work?

qDebug() << QUrl("http://test.com/query?q=%2B%2Be%3Axyz%2Fen").toDisplayString(QUrl::FullyDecoded); 

It simply does not decrypt any of the characters encoded in percent. It should print "http://test.com/query?q=++e:xyz/en" , but actually prints "http://test.com/query?q=%2B%2Be%3Axyz%2Fen" . "http://test.com/query?q=%2B%2Be%3Axyz%2Fen"

I also tried many other methods, such as fromUserInput (), but I could not work correctly with the code in Qt5.3.

Can someone explain to me how to do this and why the code above does not work (i.e. showing a decoded URL) even when using QUrl :: FullyDecoded?

UPDATE

After getting the help fromPercentEncoding (), I tried the following 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) return QUrl::fromUserInput(input); } else { // URL string containing ASCII characters only (assume possible %-encoding) return QUrl::fromUserInput(QUrl::fromPercentEncoding(input.toLatin1())); } } 

This allows the user to enter Unicode URLs and percentage URLs, and both types of URLs can be decoded for display / mapping. However, the URLs encoded as a percentage did not work in QWebView ... the web server responded differently (it returned a different page). Obviously, QUrl :: fromPercentEncoding () is not a clean solution, as it changes URLs efficiently. I could create two QUrl objects in the above function ... one directly constructed, one of which was created using the PercentEncoding () method, using the first for QWebView, and the second for displaying / matching only ... but this seems absurd.

+6
source share
3 answers

Conclusion

I did some research, the conclusion so far: absurdity .

QUrl::fromPercentEncoding() is the way to go and what the OP did in the UPDATE section should have been the accepted answer to the question in the header.

I think the Qt QUrl::toDisplayString bit misleading:

" Returns the user-displayed string representation of the URL . The output can be customized by passing flags with parameters. RemovePassword is always on, since passwords should never be shown to users."

In fact, it does not pretend to be decoding, the document here is unclear regarding its behavior. But at least part of the password is correct. I found some Gitorious hints :

" Add QUrl :: toDisplayString (), which is toString () without a password. And correct the documentation toString (), which indicated that this method should be used to display to people, while this was never true."


Security Code

To recognize the decoding ability of various functions. The following code has been tested on Qt 5.2.1 (not yet tested on Qt 5.3!)

 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()); 

PS QUrl::url is synonymous with QUrl::toString .


Output

[Case 1]: When the target path = "%_%" ( check coding functionality ):

 [Original String]: "%_%" -------------------------------------------------------------------- (QUrl::toEncoded) : "%25_%25" (QUrl::url) : "%25_%25" (QUrl::toString) : "%25_%25" (QUrl::toDisplayString) : "%25_%25" (QUrl::fromPercentEncoding): "%_%" 

[Case 2]: When the target path = "Meow !" ( check coding functionality ):

 [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 the target path = "Meow|!" ( check coding functionality ):

 [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 the 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 the 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" 

PS I also encounter the error that Ilya mentioned in the comments: Percentage of coding does not seem to work for "+" in QUrl


Summary

The result of QUrl::toDisplayString mixed. As the document says, the QUrl::FullyDecoded should be used with caution. No matter what type of URL you get, QUrl::toEncode them QUrl::toEncode and show them with QUrl::fromPercentEncoding , if necessary.

Regarding the failure of the percentage URLs in the QWebView mentioned in the OP, debugging requires more detailed information. The reason may be different functions and the other mode used.


Useful resources

+10
source

You can use QUrlQuery::toString(QUrl::FullyEncoded) or QUrl::fromPercentEncoding() for this conversion.

+3
source

I am not sure why toDisplayString(QUrl::FullyDecoded) does not work.

After several versions, I found that copy.query(QUrl::FullyDecoded) decodes part of the query. The documentation provides an example with the code below that returns a decoded URL:

 QUrl url("http://test.com/query?q=%2B%2Be%3Axyz%2Fen"); url.setQuery(url.query(QUrl::FullyDecoded), QUrl::DecodedMode); qDebug() << url.toString(); 

To solve the problem, this method is not optimal, because part of the request is copied unnecessarily.

+2
source

Source: https://habr.com/ru/post/971146/


All Articles