Skip to content

URLs validation should use urlparse #189

Closed

Description

@waynew

Python already parses URLs, and does it correctly:

>>> from urllib.parse import urlparse
>>> urlparse('https://google.com')
ParseResult(scheme='https', netloc='google.com', path='', params='', query='', fragment='')
>>> urlparse('gopher://gopher.waynewerner.com')
ParseResult(scheme='gopher', netloc='gopher.waynewerner.com', path='', params='', query='', fragment='')
>>> urlparse('tel://555-555-5555')
ParseResult(scheme='tel', netloc='555-555-5555', path='', params='', query='', fragment='')
>>> urlparse('file:///path-to-some-file')
ParseResult(scheme='file', netloc='', path='/path-to-some-file', params='', query='', fragment='')
>>> urlparse('missing-scheme.com')
ParseResult(scheme='', netloc='', path='missing-scheme.com', params='', query='', fragment='')

I had to chase down this library because click-params uses validators to validate URLs, but totally valid URLs aren't parsed correctly because the scheme wasn't expected by this library 😞

Activity

  1. rcirca commented on Aug 11, 2021

    @rcirca

    It doesn't validate though, parses sure, but validating with it is not good. http:////.google.com would be considered valid based on urllib.

  2. waynew commented on Aug 11, 2021

    @waynew
    Author

    You're right, it would, and is a valid URL:

    >>> p.urlparse('http:////.google.com')
    ParseResult(scheme='http', netloc='', path='//.google.com', params='', query='', fragment='')
    

    path might not be a valid domain name, but that's an entirely different problem. Interestingly enough, http:////.google.com works fine if you type it into your address bar in Chrome, though it fails if you click that link. http:////google.com works, though.

  3. rcirca commented on Aug 12, 2021

    @rcirca

    well, technically it's wrong to place that for the 'path', google.com should be the 'netloc'?
    Yeah, without the period it works fine in chrome, but not in safari 😅

  4. waynew commented on Aug 13, 2021

    @waynew
    Author

    Well, it is the path, strictly speaking, and should be rejected because there is no netloc.

    Just because it's a real URL doesn't mean you can get there from here 🤣

  5. added a commit that references this issue on Mar 17, 2023
    c43826c
  6. waynew commented on Mar 19, 2023

    @waynew
    Author

    Awesome! Thanks for your efforts 🎉🚀👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementIssue/PR: A new featureoutdatedIssue/PR: Open for more than 3 months

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions