Custom request subclass that decodes from an ASGI-standard request dict and wraps request body handling.
| 37 | |
| 38 | |
| 39 | class ASGIRequest(HttpRequest): |
| 40 | """ |
| 41 | Custom request subclass that decodes from an ASGI-standard request dict |
| 42 | and wraps request body handling. |
| 43 | """ |
| 44 | |
| 45 | # Number of seconds until a Request gives up on trying to read a request |
| 46 | # body and aborts. |
| 47 | body_receive_timeout = 60 |
| 48 | |
| 49 | def __init__(self, scope, body_file): |
| 50 | self.scope = scope |
| 51 | self._post_parse_error = False |
| 52 | self._read_started = False |
| 53 | self.resolver_match = None |
| 54 | self.path = scope["path"] |
| 55 | self.script_name = get_script_prefix(scope) |
| 56 | if self.script_name: |
| 57 | script_name = self.script_name.rstrip("/") |
| 58 | if self.path.startswith(script_name + "/") or self.path == script_name: |
| 59 | self.path_info = self.path[len(script_name) :] |
| 60 | else: |
| 61 | self.path_info = self.path |
| 62 | else: |
| 63 | self.path_info = self.path |
| 64 | # HTTP basics. |
| 65 | self.method = self.scope["method"].upper() |
| 66 | # Ensure query string is encoded correctly. |
| 67 | query_string = self.scope.get("query_string", "") |
| 68 | if isinstance(query_string, bytes): |
| 69 | query_string = query_string.decode() |
| 70 | self.META = { |
| 71 | "REQUEST_METHOD": self.method, |
| 72 | "QUERY_STRING": query_string, |
| 73 | "SCRIPT_NAME": self.script_name, |
| 74 | "PATH_INFO": self.path_info, |
| 75 | # WSGI-expecting code will need these for a while |
| 76 | "wsgi.multithread": True, |
| 77 | "wsgi.multiprocess": True, |
| 78 | } |
| 79 | if self.scope.get("client"): |
| 80 | self.META["REMOTE_ADDR"] = self.scope["client"][0] |
| 81 | self.META["REMOTE_HOST"] = self.META["REMOTE_ADDR"] |
| 82 | self.META["REMOTE_PORT"] = self.scope["client"][1] |
| 83 | if self.scope.get("server"): |
| 84 | self.META["SERVER_NAME"] = self.scope["server"][0] |
| 85 | self.META["SERVER_PORT"] = str(self.scope["server"][1]) |
| 86 | else: |
| 87 | self.META["SERVER_NAME"] = "unknown" |
| 88 | self.META["SERVER_PORT"] = "0" |
| 89 | # Headers go into META. |
| 90 | _headers = defaultdict(list) |
| 91 | for name, value in self.scope.get("headers", []): |
| 92 | name = name.decode("latin1") |
| 93 | # Prevent spoofing via ambiguity between underscores and hyphens. |
| 94 | if "_" in name: |
| 95 | continue |
| 96 | if name == "content-length": |
no outgoing calls