Skip to content

Commit

Permalink
Added capability for a URI to be constructed from_wsgi; work on #13.
Browse files Browse the repository at this point in the history
  • Loading branch information
amcgregor committed Mar 11, 2021
1 parent 3b7d4ff commit 62730ba
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
'pytest-cov', # coverage reporting
'pytest-flakes', # syntax validation
'pytest-isort', # import ordering
'webob', # Request WSGI environment mocking.
]

trove_map = {
Expand Down
23 changes: 23 additions & 0 deletions uri/uri.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,29 @@ class URI(object):
hostname = host
credentials = authentication = auth

# Factories

@classmethod
def from_wsgi(URI, environ):
if hasattr(environ, 'environ'): # Incidentally support passing of a variety of Request object wrappers.
environ = environ.environ

scheme = environ['wsgi.url_scheme']

uri = URI(
scheme = environ['wsgi.url_scheme'],
host = environ['SERVER_NAME'],
path = environ['SCRIPT_NAME'] + environ['PATH_INFO'],
query = environ['QUERY_STRING']
)

# Handled this way to automatically elide default port numbers.
if scheme == 'http' and environ['SERVER_PORT'] != 80 or\
scheme == 'https' and environ['SERVER_PORT'] != 443:
uri.port = environ['SERVER_PORT']

return uri

# Shortcuts

@property
Expand Down

0 comments on commit 62730ba

Please sign in to comment.