Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions localstack/http/request.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from io import BytesIO
from typing import IO, TYPE_CHECKING, Dict, Mapping, Optional, Tuple, Union
from urllib.parse import quote, unquote, urlencode
from urllib.parse import quote, unquote, urlencode, urlparse

if TYPE_CHECKING:
from _typeshed.wsgi import WSGIEnvironment
Expand Down Expand Up @@ -187,15 +187,16 @@ def application(cls, *args):
def get_raw_path(request) -> str:
"""
Returns the raw_path inside the request without the query string. The request can either be a Quart Request
object (that encodes the raw path in request.scope['raw_path']) or a Werkzeug WSGi request (that encodes the raw
path in request.environ['RAW_URI']).
object (that encodes the raw path in request.scope['raw_path']) or a Werkzeug WSGI request (that encodes the raw
URI in request.environ['RAW_URI']).

:param request: the request object
:return: the raw path if any
"""
if hasattr(request, "environ"):
# werkzeug/flask request (already a string, and contains the query part)
return request.environ.get("RAW_URI", request.path).split("?")[0]
# we need to parse it, because the RAW_URI can contain a full URL if it is specified in the HTTP request
return urlparse(request.environ.get("RAW_URI", request.path)).path

if hasattr(request, "scope"):
# quart request raw_path comes as bytes, and without the query part
Expand Down
10 changes: 10 additions & 0 deletions tests/unit/http_/test_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,16 @@ def test_get_raw_path_with_query():
assert get_raw_path(request) == "/foo%2Fbar/ed"


def test_get_raw_path_with_full_uri():
# raw_path is actually raw_uri in the WSGI environment
# it can be a full URL
request = Request("GET", "/foo/bar/ed", raw_path="http://localhost:4566/foo%2Fbar/ed")

assert request.path == "/foo/bar/ed"
assert request.environ["RAW_URI"] == "http://localhost:4566/foo%2Fbar/ed"
assert get_raw_path(request) == "/foo%2Fbar/ed"


def test_headers_retain_dashes():
request = Request("GET", "/foo/bar/ed", {"X-Amz-Meta--foo_bar-ed": "foobar"})
assert "x-amz-meta--foo_bar-ed" in request.headers
Expand Down