Extract HTTP request info from uWSGI vars. Header Mapping (CGI/WSGI to HTTP): The uWSGI protocol passes HTTP headers using CGI-style environment variable naming. This method converts them back to HTTP header format: - HTTP_* vars: Strip 'HTTP_' prefix, replace '_'
(self)
| 157 | var_count += 1 |
| 158 | |
| 159 | def _extract_request_info(self): |
| 160 | """Extract HTTP request info from uWSGI vars. |
| 161 | |
| 162 | Header Mapping (CGI/WSGI to HTTP): |
| 163 | |
| 164 | The uWSGI protocol passes HTTP headers using CGI-style environment |
| 165 | variable naming. This method converts them back to HTTP header format: |
| 166 | |
| 167 | - HTTP_* vars: Strip 'HTTP_' prefix, replace '_' with '-' |
| 168 | Example: HTTP_X_FORWARDED_FOR -> X-FORWARDED-FOR |
| 169 | Example: HTTP_ACCEPT_ENCODING -> ACCEPT-ENCODING |
| 170 | |
| 171 | - CONTENT_TYPE: Mapped directly to CONTENT-TYPE header |
| 172 | (CGI spec excludes HTTP_ prefix for this header) |
| 173 | |
| 174 | - CONTENT_LENGTH: Mapped directly to CONTENT-LENGTH header |
| 175 | (CGI spec excludes HTTP_ prefix for this header) |
| 176 | |
| 177 | Note: The underscore-to-hyphen conversion is lossy. Headers that |
| 178 | originally contained underscores (e.g., X_Custom_Header) cannot be |
| 179 | distinguished from hyphenated headers (X-Custom-Header) after |
| 180 | passing through nginx/uWSGI. This is a CGI/WSGI specification |
| 181 | limitation, not specific to this implementation. |
| 182 | """ |
| 183 | # Method |
| 184 | self.method = self.uwsgi_vars.get('REQUEST_METHOD', 'GET') |
| 185 | |
| 186 | # URI and path |
| 187 | self.path = self.uwsgi_vars.get('PATH_INFO', '/') |
| 188 | self.query = self.uwsgi_vars.get('QUERY_STRING', '') |
| 189 | |
| 190 | # Build URI |
| 191 | if self.query: |
| 192 | self.uri = "%s?%s" % (self.path, self.query) |
| 193 | else: |
| 194 | self.uri = self.path |
| 195 | |
| 196 | # Scheme |
| 197 | if self.uwsgi_vars.get('HTTPS', '').lower() in ('on', '1', 'true'): |
| 198 | self.scheme = 'https' |
| 199 | elif 'wsgi.url_scheme' in self.uwsgi_vars: |
| 200 | self.scheme = self.uwsgi_vars['wsgi.url_scheme'] |
| 201 | |
| 202 | # Extract HTTP headers from CGI-style vars |
| 203 | # See docstring above for mapping details |
| 204 | for key, value in self.uwsgi_vars.items(): |
| 205 | if key.startswith('HTTP_'): |
| 206 | # Convert HTTP_HEADER_NAME to HEADER-NAME |
| 207 | header_name = key[5:].replace('_', '-') |
| 208 | self.headers.append((header_name, value)) |
| 209 | elif key == 'CONTENT_TYPE': |
| 210 | self.headers.append(('CONTENT-TYPE', value)) |
| 211 | elif key == 'CONTENT_LENGTH': |
| 212 | self.headers.append(('CONTENT-LENGTH', value)) |
| 213 | |
| 214 | def set_body_reader(self): |
| 215 | """Set up the body reader based on CONTENT_LENGTH.""" |
no test coverage detected