QUrl from a string (porting KUrl constructor or KCmdLineArgs::url)

Kevin Kofler kevin.kofler at chello.at
Sat Oct 18 01:39:27 BST 2014


I wrote:
> So, Lukáš Tinkl answered me on #fedora-kde IRC. For posterity:
> * Qt 5.4 introduces a new overload:
>   http://doc-snapshot.qt-project.org/qt5-5.4/qurl.html#fromUserInput-2
>   with a third parameter that solves this issue.
> * This is how Okular solves the problem without hard-depending on Qt 5.4:
>   http://quickgit.kde.org/?p=okular.git&a=commit&h=d98b4d920037422fe052ffa2633349d41fdbe02e

Sorry for yet another self-reply, but I'll point out that Okular currently
uses the two-argument form, which according to the documentation is enough
when only existing files need to be supported, but I definitely recommend
passing the third QUrl::AssumeLocalFile argument. The reason is that I don't
want Kompare (or most other applications that accept either a file or a URL)
to do a hostname lookup if it's passed a nonexisting file. I think it
doesn't make any sense to assume a URL if no http:// is given. It sends
mistyped file names out as DNS lookups, which can even be argued to be a
security issue.

Thus, the Kompare code will probably look like this:
QUrl urlFromArg(const QString& arg)
#if QT_VERSION >= 0x050400
    return QUrl::fromUserInput(arg, QDir::currentPath(), QUrl::AssumeLocalFile);
    // Logic from QUrl::fromUserInput(QString, QString, UserInputResolutionOptions)
    return (QUrl(arg, QUrl::TolerantMode).isRelative() && !QDir::isAbsolutePath(arg))
           ? QUrl::fromLocalFile(QDir::current().absoluteFilePath(arg))
           : QUrl::fromUserInput(arg);

        Kevin Kofler

More information about the kde-core-devel mailing list