Skip to main content

Validating URI References per RFC 3986

Project description

A Python implementation of RFC 3986 including validation and authority parsing. Coming soon: Reference Resolution.

Installation

Simply use pip to install rfc3986 like so:

pip install rfc3986

License

Apache License Version 2.0

Example Usage

To parse a URI into a convenient named tuple, you can simply:

from rfc3986 import uri_reference

example = uri_reference('http://example.com')
email = uri_reference('mailto:user@domain.com')
ssh = uri_reference('ssh://user@git.openstack.org:29418/openstack/keystone.git')

With a parsed URI you can access data about the components:

print(example.scheme)  # => http
print(email.path)  # => user@domain.com
print(ssh.userinfo)  # => user
print(ssh.host)  # => git.openstack.org
print(ssh.port)  # => 29418

It can also parse URIs with unicode present:

uni = uri_reference(b'http://httpbin.org/get?utf8=\xe2\x98\x83')  # ☃
print(uni.query)  # utf8=%E2%98%83

With a parsed URI you can also validate it:

if ssh.is_valid():
    subprocess.call(['git', 'clone', ssh.unsplit()])

You can also take a parsed URI and normalize it:

mangled = uri_reference('hTTp://exAMPLe.COM')
print(mangled.scheme)  # => hTTp
print(mangled.authority)  # => exAMPLe.COM

normal = mangled.normalize()
print(normal.scheme)  # => http
print(mangled.authority)  # => example.com

But these two URIs are (functionally) equivalent:

if normal == mangled:
    webbrowser.open(normal.unsplit())

Your paths, queries, and fragments are safe with us though:

mangled = uri_reference('hTTp://exAMPLe.COM/Some/reallY/biZZare/pAth')
normal = mangled.normalize()
assert normal == 'hTTp://exAMPLe.COM/Some/reallY/biZZare/pAth'
assert normal == 'http://example.com/Some/reallY/biZZare/pAth'
assert normal != 'http://example.com/some/really/bizzare/path'

If you do not actually need a real reference object and just want to normalize your URI:

from rfc3986 import normalize_uri

assert (normalize_uri('hTTp://exAMPLe.COM/Some/reallY/biZZare/pAth') ==
        'http://example.com/Some/reallY/biZZare/pAth')

You can also very simply validate a URI:

from rfc3986 import is_valid_uri

assert is_valid_uri('hTTp://exAMPLe.COM/Some/reallY/biZZare/pAth')

Requiring Components

You can validate that a particular string is a valid URI and require independent components:

from rfc3986 import is_valid_uri

assert is_valid_uri('http://localhost:8774/v2/resource',
                    require_scheme=True,
                    require_authority=True,
                    require_path=True)

# Assert that a mailto URI is invalid if you require an authority
# component
assert is_valid_uri('mailto:user@example.com', require_authority=True) is False

If you have an instance of a URIReference, you can pass the same arguments to URIReference#is_valid, e.g.,

from rfc3986 import uri_reference

http = uri_reference('http://localhost:8774/v2/resource')
assert uri.is_valid(require_scheme=True,
                    require_authority=True,
                    require_path=True)

# Assert that a mailto URI is invalid if you require an authority
# component
mailto = uri_reference('mailto:user@example.com')
assert uri.is_valid(require_authority=True) is False

Alternatives

  • rfc3987

    This is a direct competitor to this library, with extra features, licensed under the GPL.

  • uritools

    This can parse URIs in the manner of RFC 3986 but provides no validation and only recently added Python 3 support.

  • Standard library’s urlparse/urllib.parse

    The functions in these libraries can only split a URI (valid or not) and provide no validation.

Contributing

This project follows and enforces the Python Software Foundation’s Code of Conduct.

If you would like to contribute but do not have a bug or feature in mind, feel free to email Ian and find out how you can help.

The git repository for this project is maintained at https://github.com/sigmavirus24/rfc3986

0.2.2 – 2015-05-27

  • Update the regular name regular expression to accept all of the characters allowed in the RFC. Closes bug #11 (Thanks Viktor Haag). Previously URIs similar to “http://http-bin.org” would be considered invalid.

0.2.1 – 2015-03-20

  • Check that the bytes of an IPv4 Host Address are within the valid range. Otherwise, URIs like “http://256.255.255.0/v1/resource” are considered valid.

  • Add 6 to the list of unreserved characters. It was previously missing. Closes bug #9

0.2.0 – 2014-06-30

  • Add support for requiring components during validation. This includes adding parameters require_scheme, require_authority, require_path, require_path, require_query, and require_fragment to rfc3986.is_valid_uri and URIReference#is_valid.

0.1.0 – 2014-06-27

  • Initial Release includes validation and normalization of URIs

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rfc3986-0.2.2.tar.gz (17.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

rfc3986-0.2.2-py2-none-any.whl (17.6 kB view details)

Uploaded Python 2

File details

Details for the file rfc3986-0.2.2.tar.gz.

File metadata

  • Download URL: rfc3986-0.2.2.tar.gz
  • Upload date:
  • Size: 17.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for rfc3986-0.2.2.tar.gz
Algorithm Hash digest
SHA256 b66238ee937485509326e0280eab4d8193df0e640936b99a6a181374dff97f7b
MD5 818c7851fd49ecb83c89856a9962959e
BLAKE2b-256 01bdafbc17f2b5c4f1bdf94b13b23ea257bff4bab3dc35260c703c7a1887ec79

See more details on using hashes here.

File details

Details for the file rfc3986-0.2.2-py2-none-any.whl.

File metadata

File hashes

Hashes for rfc3986-0.2.2-py2-none-any.whl
Algorithm Hash digest
SHA256 c22348d548b4f9c02c24afe46eb83eb4b97af1798480e642367d3c0a4770d48a
MD5 6a6d89ec1d3c88b70fe7f01788809e4b
BLAKE2b-256 2396bd4bd05eb6bc684dd92647765c1965a5f93c21bf9f60808017c62e4b7a23

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page