Results 1 to 6 of 6

Thread: please explain QUrl::isValid() to me

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    258
    Thanks
    22
    Thanked 19 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default please explain QUrl::isValid() to me

    Hi,
    Before using regular expressions to validate a link I tried QUrl::isValid() and expected it to tell me when an url is not valid, just as a browser like konqi would throw an error message like "malformed url" with a big red error icon at me leaving no doubt that I shouldn't try this again. QUrl::isValid() however kindly waves through anything I pass to it except an empty string.

    The documentation says this:
    The URL is run through a conformance test. Every part of the URL must conform to the standard encoding rules of the URI standard for the URL to be reported as valid.
    The conformance encoding rules seem to be pretty lax.
    Why is that?

    Here is an example:

    Qt Code:
    1. #include <QtGui>
    2.  
    3. int main()
    4. {
    5. QList<QUrl> urls;
    6. urls << QUrl()
    7. << QUrl(" ")
    8. << QUrl("http://google.de")
    9. << QUrl("++ !! 3w27 WHY AM I VALID? 64a.sdk).d.d.d:/77(8*euiafds");
    10.  
    11. foreach (const QUrl url, urls) {
    12. qDebug() << "is " << url << "valid? Answer: " << url.isValid();
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 

    Thanx in advance

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: please explain QUrl::isValid() to me

    Apart from the first two the other urls should be valid... Maybe the second one as well, I'm not sure.

    IMHO QUrl should really be called QUri...

  3. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    258
    Thanks
    22
    Thanked 19 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: please explain QUrl::isValid() to me

    the second one also successfully validates. Only the first one returns false. I Just read the german wikipedia article regardin URI's. Seems like almost anything is valid ...

    According to the article, this regexp can be used to validate an Uri:
    Qt Code:
    1. ^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?
    To copy to clipboard, switch view to plain text mode 
    The Scheme -- if present at all -- may not start with : / ? or #. QUrl::isValid() catches the colon and #, but misses to report an invalid url upon encountering the other two charachters. Then, if // folows, it may not be immediately followed by / ? or #. This time QUrl::isValid() catches one of them, namely the slash but misses the other ones. Maybe I misunderstood the regexp though. My experience in this regard is pretty limited. The string ?http://#qtcenter.org passes the test though it should be invalid as far as I understood the above regular expression.

    Thanx

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: please explain QUrl::isValid() to me

    Quote Originally Posted by momesana View Post
    QUrl::isValid() catches the colon and #, but misses to report an invalid url upon encountering the other two charachters.
    I think it prepends "file://" in case a protocol is not explicitely given.

    he string ?http://#qtcenter.org passes the test though it should be invalid as far as I understood the above regular expression.
    What is you interpret it as file://?http://#qtcenter.org ? According to me it is valid then - it consists of an empty path and a fragment containing "http://#qtcenter.org" or even just a file with a strange path.

  5. #5
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    258
    Thanks
    22
    Thanked 19 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: please explain QUrl::isValid() to me

    Ok,
    the url is valid. Since the other parts are optional, something like this:
    ?http://#qtcenter.org
    would actually be a standalone fragment, thus being valid.

    Thanx

  6. #6
    Join Date
    Apr 2010
    Posts
    9
    Thanked 3 Times in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: please explain QUrl::isValid() to me

    There is another option to check if a url is valid. But the problem is that wroks only for windows systems, but I'll show it here anyway.

    Here is the code:

    Qt Code:
    1. #include <windows.h>
    2.  
    3. HMODULE hUrlMon = LoadLibrary( L"urlmon.dll" );
    4. wchar_t url[] = TEXT("http://www.google.com"); //the url example to test if is valid or not.
    5.  
    6. if( hUrlMon )
    7. {
    8. typedef HRESULT (__stdcall *isValidURL_ptr)( LPBC pBC, LPCWSTR szURL, DWORD dwReserved );
    9. isValidURL_ptr isValidURL_fn = ( isValidURL_ptr )GetProcAddress( hUrlMon, "IsValidURL" );
    10. HRESULT hr = isValidURL_fn( NULL, url, 0 );
    11. if( S_OK == hr )
    12. {
    13. qDebug() << "URL Valid: " << hr;
    14. }else{
    15. qDebug() << "URL Invalid: " << hr;
    16. }
    17. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. could somebody please explain const to me?
    By mikro in forum General Programming
    Replies: 4
    Last Post: 29th September 2006, 14:34

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.