@@ -65,7 +65,7 @@ class Request:
6565 content_params : dict [str , str ] | None
6666 query_params : QueryDict
6767 cookies : dict [str , str ]
68- meta : dict [str , Any ]
68+ environ : dict [str , Any ]
6969 path : str
7070 path_info : str
7171 unique_id : str
@@ -103,7 +103,7 @@ def __deepcopy__(self, memo: dict[int, Any]) -> Request:
103103
104104 @cached_property
105105 def headers (self ) -> RequestHeaders :
106- return RequestHeaders (self .meta )
106+ return RequestHeaders (self .environ )
107107
108108 @cached_property
109109 def csp_nonce (self ) -> str :
@@ -157,13 +157,15 @@ def host(self) -> str:
157157 property can safely return the host without any validation.
158158 """
159159 # We try three options, in order of decreasing preference.
160- if settings .HTTP_X_FORWARDED_HOST and ("HTTP_X_FORWARDED_HOST" in self .meta ):
161- host = self .meta ["HTTP_X_FORWARDED_HOST" ]
162- elif "HTTP_HOST" in self .meta :
163- host = self .meta ["HTTP_HOST" ]
160+ if settings .HTTP_X_FORWARDED_HOST and (
161+ xff_host := self .headers .get ("X-Forwarded-Host" )
162+ ):
163+ host = xff_host
164+ elif http_host := self .headers .get ("Host" ):
165+ host = http_host
164166 else :
165167 # Reconstruct the host using the algorithm from PEP 333.
166- host = self .meta ["SERVER_NAME" ]
168+ host = self .environ ["SERVER_NAME" ]
167169 server_port = self .port
168170 if server_port != ("443" if self .is_https () else "80" ):
169171 host = f"{ host } :{ server_port } "
@@ -172,10 +174,12 @@ def host(self) -> str:
172174 @cached_property
173175 def port (self ) -> str :
174176 """Return the port number for the request as a string."""
175- if settings .HTTP_X_FORWARDED_PORT and "HTTP_X_FORWARDED_PORT" in self .meta :
176- port = self .meta ["HTTP_X_FORWARDED_PORT" ]
177+ if settings .HTTP_X_FORWARDED_PORT and (
178+ xff_port := self .headers .get ("X-Forwarded-Port" )
179+ ):
180+ port = xff_port
177181 else :
178- port = self .meta ["SERVER_PORT" ]
182+ port = self .environ ["SERVER_PORT" ]
179183 return str (port )
180184
181185 @cached_property
@@ -191,7 +195,12 @@ def client_ip(self) -> str:
191195 if settings .HTTP_X_FORWARDED_FOR :
192196 if xff := self .headers .get ("X-Forwarded-For" ):
193197 return xff .split ("," )[0 ].strip ()
194- return self .meta ["REMOTE_ADDR" ]
198+ return self .environ ["REMOTE_ADDR" ]
199+
200+ @property
201+ def query_string (self ) -> str :
202+ """Return the raw query string from the request URL."""
203+ return self .environ .get ("QUERY_STRING" , "" )
195204
196205 def get_full_path (self , force_append_slash : bool = False ) -> str :
197206 """
@@ -219,11 +228,10 @@ def escape_uri_path(path: str) -> str:
219228 # the entire path, not a path segment.
220229 return quote (path , safe = "/:@&+$,-_.!~*'()" )
221230
222- query_string = self .meta .get ("QUERY_STRING" , "" )
223231 return "{}{}{}" .format (
224232 escape_uri_path (self .path ),
225233 "/" if force_append_slash and not self .path .endswith ("/" ) else "" ,
226- ("?" + (iri_to_uri (query_string ) or "" )) if query_string else "" ,
234+ ("?" + (iri_to_uri (self . query_string ) or "" )) if self . query_string else "" ,
227235 )
228236
229237 def build_absolute_uri (self , location : str | None = None ) -> str :
@@ -306,7 +314,7 @@ def body(self) -> bytes:
306314 # Limit the maximum request data size that will be handled in-memory.
307315 if (
308316 settings .DATA_UPLOAD_MAX_MEMORY_SIZE is not None
309- and int (self .meta .get ("CONTENT_LENGTH" ) or 0 )
317+ and int (self .environ .get ("CONTENT_LENGTH" ) or 0 )
310318 > settings .DATA_UPLOAD_MAX_MEMORY_SIZE
311319 ):
312320 raise RequestDataTooBig (
@@ -323,10 +331,12 @@ def body(self) -> bytes:
323331 return self ._body
324332
325333 def _parse_file_upload (
326- self , meta : dict [str , Any ], post_data : IO [bytes ]
334+ self , environ : dict [str , Any ], post_data : IO [bytes ]
327335 ) -> tuple [Any , MultiValueDict ]:
328336 """Return a tuple of (data QueryDict, files MultiValueDict)."""
329- parser = MultiPartParser (meta , post_data , self .upload_handlers , self .encoding )
337+ parser = MultiPartParser (
338+ environ , post_data , self .upload_handlers , self .encoding
339+ )
330340 return parser .parse ()
331341
332342 @cached_property
@@ -341,7 +351,7 @@ def _multipart_data(self) -> tuple[QueryDict, MultiValueDict]:
341351 data = BytesIO (self ._body )
342352 else :
343353 data = self
344- return self ._parse_file_upload (self .meta , data )
354+ return self ._parse_file_upload (self .environ , data )
345355
346356 @cached_property
347357 def json_data (self ) -> dict [str , Any ]:
0 commit comments