|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | | -from typing import TYPE_CHECKING |
| 3 | +from typing import TYPE_CHECKING, Any, cast |
4 | 4 | from urllib.parse import urlparse |
5 | 5 |
|
6 | | -from werkzeug.wrappers import Request |
| 6 | +try: |
| 7 | + from botocore.httpchecksum import AwsChunkedWrapper |
| 8 | +except ModuleNotFoundError: |
| 9 | + |
| 10 | + class AwsChunkedWrapper: # type: ignore[no-redef] |
| 11 | + def read(self) -> bytes: |
| 12 | + raise NotImplementedError() |
| 13 | + |
| 14 | + |
| 15 | +from botocore.awsrequest import AWSPreparedRequest |
| 16 | +from requests import PreparedRequest |
| 17 | +from werkzeug.local import LocalProxy |
| 18 | +from werkzeug.wrappers import Request as WerkzeugRequest |
7 | 19 |
|
8 | | -from moto.core.utils import gzip_decompress |
9 | 20 | from moto.settings import MAX_FORM_MEMORY_SIZE |
10 | 21 | from moto.utilities.constants import APPLICATION_JSON, JSON_TYPES |
11 | 22 |
|
12 | 23 | if TYPE_CHECKING: |
13 | | - from botocore.awsrequest import AWSPreparedRequest |
14 | | - |
15 | 24 | from moto.core.model import ServiceModel |
16 | 25 |
|
17 | 26 |
|
18 | | -def normalize_request(request: AWSPreparedRequest | Request) -> Request: |
| 27 | +class Request(WerkzeugRequest): |
| 28 | + #: True when this request was received by a real WSGI server (Moto Server). |
| 29 | + from_wsgi_server = False |
| 30 | + |
| 31 | + def __init__(self, *args: Any, **kwargs: Any) -> None: |
| 32 | + super().__init__(*args, **kwargs) |
| 33 | + self.max_form_memory_size = MAX_FORM_MEMORY_SIZE |
| 34 | + |
| 35 | + @classmethod |
| 36 | + def from_primitives( |
| 37 | + cls, method: str, url: str, headers: Any, body: Any = None |
| 38 | + ) -> Request: |
| 39 | + """Build a request from the basic components of an HTTP message.""" |
| 40 | + if isinstance(body, AwsChunkedWrapper): |
| 41 | + body = body.read() |
| 42 | + parsed_url = urlparse(url) |
| 43 | + request = cast( |
| 44 | + Request, |
| 45 | + cls.from_values( |
| 46 | + method=method, |
| 47 | + base_url=f"{parsed_url.scheme}://{parsed_url.netloc}", |
| 48 | + path=parsed_url.path, |
| 49 | + query_string=parsed_url.query, |
| 50 | + data=body if body is not None else b"", |
| 51 | + # The proxy de-chunks before we ever see the request, and we (above) read |
| 52 | + # an AwsChunkedWrapper out in full, so Transfer-Encoding no longer applies. |
| 53 | + headers=[ |
| 54 | + (key, value.decode("utf-8") if isinstance(value, bytes) else value) |
| 55 | + for key, value in headers.items() |
| 56 | + if key.lower() not in ["transfer-encoding"] |
| 57 | + ], |
| 58 | + ), |
| 59 | + ) |
| 60 | + # Ensure a Content-Length header is present, even for bodiless requests. |
| 61 | + # (Some S3 endpoints return a 411 if this header is missing.) |
| 62 | + request.environ.setdefault("CONTENT_LENGTH", "0") |
| 63 | + return request |
| 64 | + |
| 65 | + @property |
| 66 | + def raw_path(self) -> str: |
| 67 | + """The path as it arrived, before werkzeug percent-decoded it. |
| 68 | +
|
| 69 | + S3 keys routinely contain characters - an encoded slash, most awkwardly - |
| 70 | + that `path` decodes away, and Moto matches backend URLs against the |
| 71 | + encoded form. |
| 72 | + """ |
| 73 | + # RAW_URI holds either a path or a full URL, depending on the server. |
| 74 | + # A path may begin with a double slash, which urlparse would read as the |
| 75 | + # start of a netloc, so only parse when there is really a scheme to strip. |
| 76 | + raw_uri: str = self.environ.get("RAW_URI", "") or self.path |
| 77 | + parsed = urlparse(raw_uri) |
| 78 | + raw_path = parsed.path if parsed.scheme else raw_uri.split("?", 1)[0] |
| 79 | + if not raw_path: |
| 80 | + return "/" |
| 81 | + return raw_path if raw_path.startswith("/") else f"/{raw_path}" |
| 82 | + |
| 83 | + @property |
| 84 | + def raw_url(self) -> str: |
| 85 | + """The full URL as it arrived. See `raw_path`.""" |
| 86 | + raw_url = f"{self.url_root.rstrip('/')}{self.raw_path}" |
| 87 | + if self.query_string: |
| 88 | + raw_url += f"?{self.query_string.decode()}" |
| 89 | + return raw_url |
| 90 | + |
| 91 | + |
| 92 | +def normalize_request( |
| 93 | + request: AWSPreparedRequest | LocalProxy[Request] | PreparedRequest | Request, |
| 94 | +) -> Request: |
| 95 | + """Turn however this request reached us into the one type the core acts on.""" |
| 96 | + if isinstance(request, LocalProxy): |
| 97 | + request = request._get_current_object() |
19 | 98 | if isinstance(request, Request): |
20 | 99 | return request |
21 | | - body = request.body |
22 | | - # Request.from_values() does not automatically handle gzip-encoded bodies, |
23 | | - # like the full WSGI server would, so we need to do it manually. |
24 | | - if request.headers.get("Content-Encoding") == "gzip": |
25 | | - body = gzip_decompress(body) # type: ignore[arg-type] |
26 | | - parsed_url = urlparse(request.url) |
27 | | - Request.max_form_memory_size = MAX_FORM_MEMORY_SIZE |
28 | | - normalized_request = Request.from_values( |
29 | | - method=request.method, |
30 | | - base_url=f"{parsed_url.scheme}://{parsed_url.netloc}", |
31 | | - path=parsed_url.path, |
32 | | - query_string=parsed_url.query, |
33 | | - data=body, |
34 | | - headers=[(k, v) for k, v in request.headers.items()], |
35 | | - ) |
36 | | - return normalized_request |
| 100 | + if isinstance(request, (AWSPreparedRequest, PreparedRequest)): |
| 101 | + assert request.method and request.url |
| 102 | + return Request.from_primitives( |
| 103 | + request.method, request.url, request.headers, request.body |
| 104 | + ) |
| 105 | + raise TypeError(f"Cannot normalize request of type {type(request).__name__}") |
37 | 106 |
|
38 | 107 |
|
39 | 108 | def determine_request_protocol( |
|
0 commit comments