Back to index

python3.2  3.2.2
Classes | Functions | Variables
wsgiref.util Namespace Reference

Classes

class  FileWrapper

Functions

def guess_scheme
def application_uri
def request_uri
def shift_path_info
def setup_testing_defaults
def is_hop_by_hop

Variables

list __all__
dictionary _hoppish

Detailed Description

Miscellaneous WSGI-related Utilities

Function Documentation

def wsgiref.util.application_uri (   environ)
Return the application's base URI (no PATH_INFO or QUERY_STRING)

Definition at line 43 of file util.py.

00043 
00044 def application_uri(environ):
00045     """Return the application's base URI (no PATH_INFO or QUERY_STRING)"""
00046     url = environ['wsgi.url_scheme']+'://'
00047     from urllib.parse import quote
00048 
00049     if environ.get('HTTP_HOST'):
00050         url += environ['HTTP_HOST']
00051     else:
00052         url += environ['SERVER_NAME']
00053 
00054         if environ['wsgi.url_scheme'] == 'https':
00055             if environ['SERVER_PORT'] != '443':
00056                 url += ':' + environ['SERVER_PORT']
00057         else:
00058             if environ['SERVER_PORT'] != '80':
00059                 url += ':' + environ['SERVER_PORT']
00060 
00061     url += quote(environ.get('SCRIPT_NAME') or '/')
00062     return url

Here is the call graph for this function:

Here is the caller graph for this function:

def wsgiref.util.guess_scheme (   environ)
Return a guess for whether 'wsgi.url_scheme' should be 'http' or 'https'

Definition at line 35 of file util.py.

00035 
00036 def guess_scheme(environ):
00037     """Return a guess for whether 'wsgi.url_scheme' should be 'http' or 'https'
00038     """
00039     if environ.get("HTTPS") in ('yes','on','1'):
00040         return 'https'
00041     else:
00042         return 'http'

Here is the caller graph for this function:

def wsgiref.util.is_hop_by_hop (   header_name)
Return true if 'header_name' is an HTTP/1.1 "Hop-by-Hop" header

Definition at line 163 of file util.py.

00163 
00164 def is_hop_by_hop(header_name):
00165     """Return true if 'header_name' is an HTTP/1.1 "Hop-by-Hop" header"""
00166     return _hoppish(header_name.lower())

Here is the caller graph for this function:

def wsgiref.util.request_uri (   environ,
  include_query = True 
)
Return the full request URI, optionally including the query string

Definition at line 63 of file util.py.

00063 
00064 def request_uri(environ, include_query=True):
00065     """Return the full request URI, optionally including the query string"""
00066     url = application_uri(environ)
00067     from urllib.parse import quote
00068     path_info = quote(environ.get('PATH_INFO',''),safe='/;=,')
00069     if not environ.get('SCRIPT_NAME'):
00070         url += path_info[1:]
00071     else:
00072         url += path_info
00073     if include_query and environ.get('QUERY_STRING'):
00074         url += '?' + environ['QUERY_STRING']
00075     return url

Here is the call graph for this function:

Update 'environ' with trivial defaults for testing purposes

This adds various parameters required for WSGI, including HTTP_HOST,
SERVER_NAME, SERVER_PORT, REQUEST_METHOD, SCRIPT_NAME, PATH_INFO,
and all of the wsgi.* variables.  It only supplies default values,
and does not replace any existing settings for these variables.

This routine is intended to make it easier for unit tests of WSGI
servers and applications to set up dummy environments.  It should *not*
be used by actual WSGI servers or applications, since the data is fake!

Definition at line 117 of file util.py.

00117 
00118 def setup_testing_defaults(environ):
00119     """Update 'environ' with trivial defaults for testing purposes
00120 
00121     This adds various parameters required for WSGI, including HTTP_HOST,
00122     SERVER_NAME, SERVER_PORT, REQUEST_METHOD, SCRIPT_NAME, PATH_INFO,
00123     and all of the wsgi.* variables.  It only supplies default values,
00124     and does not replace any existing settings for these variables.
00125 
00126     This routine is intended to make it easier for unit tests of WSGI
00127     servers and applications to set up dummy environments.  It should *not*
00128     be used by actual WSGI servers or applications, since the data is fake!
00129     """
00130 
00131     environ.setdefault('SERVER_NAME','127.0.0.1')
00132     environ.setdefault('SERVER_PROTOCOL','HTTP/1.0')
00133 
00134     environ.setdefault('HTTP_HOST',environ['SERVER_NAME'])
00135     environ.setdefault('REQUEST_METHOD','GET')
00136 
00137     if 'SCRIPT_NAME' not in environ and 'PATH_INFO' not in environ:
00138         environ.setdefault('SCRIPT_NAME','')
00139         environ.setdefault('PATH_INFO','/')
00140 
00141     environ.setdefault('wsgi.version', (1,0))
00142     environ.setdefault('wsgi.run_once', 0)
00143     environ.setdefault('wsgi.multithread', 0)
00144     environ.setdefault('wsgi.multiprocess', 0)
00145 
00146     from io import StringIO, BytesIO
00147     environ.setdefault('wsgi.input', BytesIO())
00148     environ.setdefault('wsgi.errors', StringIO())
00149     environ.setdefault('wsgi.url_scheme',guess_scheme(environ))
00150 
00151     if environ['wsgi.url_scheme']=='http':
00152         environ.setdefault('SERVER_PORT', '80')
00153     elif environ['wsgi.url_scheme']=='https':
00154         environ.setdefault('SERVER_PORT', '443')
00155 
00156 

Here is the call graph for this function:

Here is the caller graph for this function:

def wsgiref.util.shift_path_info (   environ)
Shift a name from PATH_INFO to SCRIPT_NAME, returning it

If there are no remaining path segments in PATH_INFO, return None.
Note: 'environ' is modified in-place; use a copy if you need to keep
the original PATH_INFO or SCRIPT_NAME.

Note: when PATH_INFO is just a '/', this returns '' and appends a trailing
'/' to SCRIPT_NAME, even though empty path segments are normally ignored,
and SCRIPT_NAME doesn't normally end in a '/'.  This is intentional
behavior, to ensure that an application can tell the difference between
'/x' and '/x/' when traversing to objects.

Definition at line 76 of file util.py.

00076 
00077 def shift_path_info(environ):
00078     """Shift a name from PATH_INFO to SCRIPT_NAME, returning it
00079 
00080     If there are no remaining path segments in PATH_INFO, return None.
00081     Note: 'environ' is modified in-place; use a copy if you need to keep
00082     the original PATH_INFO or SCRIPT_NAME.
00083 
00084     Note: when PATH_INFO is just a '/', this returns '' and appends a trailing
00085     '/' to SCRIPT_NAME, even though empty path segments are normally ignored,
00086     and SCRIPT_NAME doesn't normally end in a '/'.  This is intentional
00087     behavior, to ensure that an application can tell the difference between
00088     '/x' and '/x/' when traversing to objects.
00089     """
00090     path_info = environ.get('PATH_INFO','')
00091     if not path_info:
00092         return None
00093 
00094     path_parts = path_info.split('/')
00095     path_parts[1:-1] = [p for p in path_parts[1:-1] if p and p != '.']
00096     name = path_parts[1]
00097     del path_parts[1]
00098 
00099     script_name = environ.get('SCRIPT_NAME','')
00100     script_name = posixpath.normpath(script_name+'/'+name)
00101     if script_name.endswith('/'):
00102         script_name = script_name[:-1]
00103     if not name and not script_name.endswith('/'):
00104         script_name += '/'
00105 
00106     environ['SCRIPT_NAME'] = script_name
00107     environ['PATH_INFO']   = '/'.join(path_parts)
00108 
00109     # Special case: '/.' on PATH_INFO doesn't get stripped,
00110     # because we don't strip the last element of PATH_INFO
00111     # if there's only one path part left.  Instead of fixing this
00112     # above, we fix it here so that PATH_INFO gets normalized to
00113     # an empty string in the environ.
00114     if name=='.':
00115         name = None
00116     return name

Here is the call graph for this function:


Variable Documentation

Initial value:
00001 [
00002     'FileWrapper', 'guess_scheme', 'application_uri', 'request_uri',
00003     'shift_path_info', 'setup_testing_defaults',
00004 ]

Definition at line 5 of file util.py.

Initial value:
00001 {
00002     'connection':1, 'keep-alive':1, 'proxy-authenticate':1,
00003     'proxy-authorization':1, 'te':1, 'trailers':1, 'transfer-encoding':1,
00004     'upgrade':1
00005 }

Definition at line 157 of file util.py.