Coverage for /home/martinb/.local/share/virtualenvs/camcops/lib/python3.6/site-packages/cardinal_pythonlib/wsgi/constants.py : 100%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1#!/usr/bin/env python
2# cardinal_pythonlib/wsgi/constants.py
4"""
5===============================================================================
7 Original code copyright (C) 2009-2021 Rudolf Cardinal (rudolf@pobox.com).
9 This file is part of cardinal_pythonlib.
11 Licensed under the Apache License, Version 2.0 (the "License");
12 you may not use this file except in compliance with the License.
13 You may obtain a copy of the License at
15 https://www.apache.org/licenses/LICENSE-2.0
17 Unless required by applicable law or agreed to in writing, software
18 distributed under the License is distributed on an "AS IS" BASIS,
19 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 See the License for the specific language governing permissions and
21 limitations under the License.
23===============================================================================
25**Miscellany to help with WSGI work.**
27"""
29from types import TracebackType
30from typing import Callable, Dict, Iterable, List, Optional, Tuple, Type
32# =============================================================================
33# Type hints for WSGI
34# =============================================================================
36# https://www.python.org/dev/peps/pep-0333/
37TYPE_WSGI_ENVIRON = Dict[str, str]
38TYPE_WSGI_STATUS = str
39TYPE_WSGI_RESPONSE_HEADERS = List[Tuple[str, str]]
40TYPE_WSGI_START_RESP_RESULT = Callable[[str], None] # call with e.g. write(body_data) # noqa
41TYPE_WSGI_EXC_INFO = Tuple[
42 # https://docs.python.org/3/library/sys.html#sys.exc_info
43 Optional[Type[BaseException]], # type
44 Optional[BaseException], # value
45 Optional[TracebackType] # traceback
46]
47TYPE_WSGI_START_RESPONSE = Callable[
48 # There is an optional third parameter to start_response():
49 [TYPE_WSGI_STATUS, # status
50 TYPE_WSGI_RESPONSE_HEADERS, # headers
51 Optional[TYPE_WSGI_EXC_INFO]], # exc_info
52 TYPE_WSGI_START_RESP_RESULT
53]
54TYPE_WSGI_APP_RESULT = Iterable[bytes]
55# ... must be BYTE STRINGS, not str; see
56# https://www.python.org/dev/peps/pep-0333/#unicode-issues
57# and also
58# https://github.com/fgallaire/wsgiserver/issues/2
59TYPE_WSGI_APP = Callable[[TYPE_WSGI_ENVIRON, TYPE_WSGI_START_RESPONSE],
60 TYPE_WSGI_APP_RESULT]
63# =============================================================================
64# Constants
65# =============================================================================
67class WsgiEnvVar(object):
68 """
69 WSGI environment variable names.
71 For core ones, see https://wsgi.readthedocs.io/en/latest/definitions.html
72 """
73 CONTENT_LENGTH = "CONTENT_LENGTH" # [1]
74 CONTENT_TYPE = "CONTENT_TYPE" # [1]
75 HTTP_HOST = "HTTP_HOST" # [2]
76 HTTP_X_FORWARDED_FOR = "HTTP_X_FORWARDED_FOR" # [2]
77 HTTP_X_REAL_IP = "HTTP_X_REAL_IP" # [7]
78 HTTP_X_FORWARDED_HTTPS = "HTTP_X_FORWARDED_HTTPS" # [7]
79 HTTP_X_FORWARDED_HOST = "HTTP_X_FORWARDED_HOST" # [4]
80 HTTP_X_FORWARDED_SCRIPT_NAME = "HTTP_X_FORWARDED_SCRIPT_NAME" # [7]
81 HTTP_X_FORWARDED_SERVER = "HTTP_X_FORWARDED_SERVER" # [4]
82 HTTP_X_FORWARDED_PORT = "HTTP_X_FORWARDED_PORT" # [7]
83 HTTP_X_FORWARDED_PROTO = "HTTP_X_FORWARDED_PROTO" # [6]
84 HTTP_X_FORWARDED_PROTOCOL = "HTTP_X_FORWARDED_PROTOCOL" # [6]
85 HTTP_X_FORWARDED_SCHEME = "HTTP_X_FORWARDED_SCHEME" # [7]
86 HTTP_X_FORWARDED_SSL = "HTTP_X_FORWARDED_SSL" # [7]
87 HTTP_X_HOST = "HTTP_X_HOST" # [7]
88 HTTP_X_HTTPS = "HTTP_X_HTTPS" # [7]
89 HTTP_X_SCHEME = "HTTP_X_SCHEME" # [5]
90 HTTP_X_SCRIPT_NAME = "HTTP_X_SCRIPT_NAME" # [5]
91 PATH_INFO = "PATH_INFO" # [1]
92 QUERY_STRING = "QUERY_STRING" # [1]
93 REMOTE_ADDR = "REMOTE_ADDR" # [1]
94 REQUEST_METHOD = "REQUEST_METHOD" # [1]
95 SCRIPT_NAME = "SCRIPT_NAME" # [1]
96 SERVER_NAME = "SERVER_NAME" # [1]
97 SERVER_PORT = "SERVER_PORT" # [1]
98 SERVER_PROTOCOL = "SERVER_PROTOCOL" # [1]
99 WSGI_ERRORS = "wsgi.errors" # [3]
100 WSGI_INPUT = "wsgi.input" # [3]
101 WSGI_MULTIPROCESS = "wsgi.multiprocess" # [3]
102 WSGI_MULTITHREAD = "wsgi.multithread" # [3]
103 WSGI_RUN_ONCE = "wsgi.run_once" # [3]
104 WSGI_URL_SCHEME = "wsgi.url_scheme" # [3]
105 WSGI_VERSION = "wsgi.version" # [3]
107 # [1] Standard WSGI and standard CGI; must always be present;
108 # https://wsgi.readthedocs.io/en/latest/definitions.html
109 # https://www.python.org/dev/peps/pep-0333/
110 # https://en.wikipedia.org/wiki/Common_Gateway_Interface
111 # [2] Standard WSGI as copies of standard HTTP request fields (thus,
112 # optional); https://wsgi.readthedocs.io/en/latest/definitions.html
113 # [3] Also standard WSGI, but not CGI; must always be present.
114 # [4] From non-standard but common HTTP request fields;
115 # https://en.wikipedia.org/wiki/List_of_HTTP_header_fields#Common_non-standard_request_fields # noqa
116 # https://github.com/omnigroup/Apache/blob/master/httpd/modules/proxy/mod_proxy_http.c # noqa
117 # [5] Non-standard; Nginx-specific? Nonetheless, all "HTTP_" variables in
118 # WSGI should follow the HTTP request headers.
119 # [6] Protocols (i.e. http versus https):
120 # https://stackoverflow.com/questions/16042647/whats-the-de-facto-standard-for-a-reverse-proxy-to-tell-the-backend-ssl-is-used # noqa
121 # [7] https://modwsgi.readthedocs.io/en/develop/release-notes/version-4.4.9.html # noqa