| 1 | """Miscellaneous WSGI-related Utilities"""
|
|---|
| 2 |
|
|---|
| 3 | import posixpath
|
|---|
| 4 |
|
|---|
| 5 | __all__ = [
|
|---|
| 6 | 'FileWrapper', 'guess_scheme', 'application_uri', 'request_uri',
|
|---|
| 7 | 'shift_path_info', 'setup_testing_defaults',
|
|---|
| 8 | ]
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 | class FileWrapper:
|
|---|
| 12 | """Wrapper to convert file-like objects to iterables"""
|
|---|
| 13 |
|
|---|
| 14 | def __init__(self, filelike, blksize=8192):
|
|---|
| 15 | self.filelike = filelike
|
|---|
| 16 | self.blksize = blksize
|
|---|
| 17 | if hasattr(filelike,'close'):
|
|---|
| 18 | self.close = filelike.close
|
|---|
| 19 |
|
|---|
| 20 | def __getitem__(self,key):
|
|---|
| 21 | data = self.filelike.read(self.blksize)
|
|---|
| 22 | if data:
|
|---|
| 23 | return data
|
|---|
| 24 | raise IndexError
|
|---|
| 25 |
|
|---|
| 26 | def __iter__(self):
|
|---|
| 27 | return self
|
|---|
| 28 |
|
|---|
| 29 | def next(self):
|
|---|
| 30 | data = self.filelike.read(self.blksize)
|
|---|
| 31 | if data:
|
|---|
| 32 | return data
|
|---|
| 33 | raise StopIteration
|
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 | def guess_scheme(environ):
|
|---|
| 43 | """Return a guess for whether 'wsgi.url_scheme' should be 'http' or 'https'
|
|---|
| 44 | """
|
|---|
| 45 | if environ.get("HTTPS") in ('yes','on','1'):
|
|---|
| 46 | return 'https'
|
|---|
| 47 | else:
|
|---|
| 48 | return 'http'
|
|---|
| 49 |
|
|---|
| 50 | def application_uri(environ):
|
|---|
| 51 | """Return the application's base URI (no PATH_INFO or QUERY_STRING)"""
|
|---|
| 52 | url = environ['wsgi.url_scheme']+'://'
|
|---|
| 53 | from urllib import quote
|
|---|
| 54 |
|
|---|
| 55 | if environ.get('HTTP_HOST'):
|
|---|
| 56 | url += environ['HTTP_HOST']
|
|---|
| 57 | else:
|
|---|
| 58 | url += environ['SERVER_NAME']
|
|---|
|
|---|