From 64594afa78973c8910f3911fec7bbddc390480d4 Mon Sep 17 00:00:00 2001 From: Sam Bull Date: Sun, 14 May 2023 18:15:09 +0100 Subject: [PATCH] Export HTTPMove (#6594) (#7288) Nitpick: If you catch `HTTPRedirection` then mypy complains that it doesn't have a `location` attribute (this is used in aiohttp-debugtoolbar, for example). Proposal to export `HTTPMove`, in order to catch any exceptions with location. (cherry picked from commit a5d641803f7ac77c6276edecfbf7e6ae1c958754) ## What do these changes do? ## Are there changes in behavior for the user? ## Related issue number ## Checklist - [ ] I think the code is well written - [ ] Unit tests for the changes exist - [ ] Documentation reflects the changes - [ ] If you provide code modification, please add yourself to `CONTRIBUTORS.txt` * The format is <Name> <Surname>. * Please keep alphabetical order, the file is sorted by names. - [ ] Add a new news fragment into the `CHANGES` folder * name it `.` for example (588.bugfix) * if you don't have an `issue_id` change it to the pr id after creating the pr * ensure type is one of the following: * `.feature`: Signifying a new feature. * `.bugfix`: Signifying a bug fix. * `.doc`: Signifying a documentation improvement. * `.removal`: Signifying a deprecation or removal of public API. * `.misc`: A ticket has been closed, but it is not of interest to users. * Make sure to use full sentences with correct case and punctuation, for example: "Fix issue with non-ascii contents in doctest text files." --- CHANGES/6594.feature | 2 ++ aiohttp/web.py | 2 ++ aiohttp/web_exceptions.py | 17 +++++++++-------- aiohttp/web_middlewares.py | 4 ++-- 4 files changed, 15 insertions(+), 10 deletions(-) create mode 100644 CHANGES/6594.feature diff --git a/CHANGES/6594.feature b/CHANGES/6594.feature new file mode 100644 index 00000000000..4edadb07b3a --- /dev/null +++ b/CHANGES/6594.feature @@ -0,0 +1,2 @@ +Exported ``HTTPMove`` which can be used to catch any redirection request +that has a location -- :user:`dreamsorcerer`. diff --git a/aiohttp/web.py b/aiohttp/web.py index 4db66cda8bd..7be9d345226 100644 --- a/aiohttp/web.py +++ b/aiohttp/web.py @@ -44,6 +44,7 @@ HTTPLengthRequired as HTTPLengthRequired, HTTPMethodNotAllowed as HTTPMethodNotAllowed, HTTPMisdirectedRequest as HTTPMisdirectedRequest, + HTTPMove as HTTPMove, HTTPMovedPermanently as HTTPMovedPermanently, HTTPMultipleChoices as HTTPMultipleChoices, HTTPNetworkAuthenticationRequired as HTTPNetworkAuthenticationRequired, @@ -177,6 +178,7 @@ "HTTPLengthRequired", "HTTPMethodNotAllowed", "HTTPMisdirectedRequest", + "HTTPMove", "HTTPMovedPermanently", "HTTPMultipleChoices", "HTTPNetworkAuthenticationRequired", diff --git a/aiohttp/web_exceptions.py b/aiohttp/web_exceptions.py index ae706a18062..4c0a10aa75a 100644 --- a/aiohttp/web_exceptions.py +++ b/aiohttp/web_exceptions.py @@ -18,6 +18,7 @@ "HTTPNoContent", "HTTPResetContent", "HTTPPartialContent", + "HTTPMove", "HTTPMultipleChoices", "HTTPMovedPermanently", "HTTPFound", @@ -160,7 +161,7 @@ class HTTPPartialContent(HTTPSuccessful): ############################################################ -class _HTTPMove(HTTPRedirection): +class HTTPMove(HTTPRedirection): def __init__( self, location: StrOrURL, @@ -184,21 +185,21 @@ def __init__( self.location = location -class HTTPMultipleChoices(_HTTPMove): +class HTTPMultipleChoices(HTTPMove): status_code = 300 -class HTTPMovedPermanently(_HTTPMove): +class HTTPMovedPermanently(HTTPMove): status_code = 301 -class HTTPFound(_HTTPMove): +class HTTPFound(HTTPMove): status_code = 302 # This one is safe after a POST (the redirected location will be # retrieved with GET): -class HTTPSeeOther(_HTTPMove): +class HTTPSeeOther(HTTPMove): status_code = 303 @@ -208,16 +209,16 @@ class HTTPNotModified(HTTPRedirection): empty_body = True -class HTTPUseProxy(_HTTPMove): +class HTTPUseProxy(HTTPMove): # Not a move, but looks a little like one status_code = 305 -class HTTPTemporaryRedirect(_HTTPMove): +class HTTPTemporaryRedirect(HTTPMove): status_code = 307 -class HTTPPermanentRedirect(_HTTPMove): +class HTTPPermanentRedirect(HTTPMove): status_code = 308 diff --git a/aiohttp/web_middlewares.py b/aiohttp/web_middlewares.py index 3a9d911c80e..cb24eec9107 100644 --- a/aiohttp/web_middlewares.py +++ b/aiohttp/web_middlewares.py @@ -2,7 +2,7 @@ from typing import TYPE_CHECKING, Tuple, Type, TypeVar from .typedefs import Handler, Middleware -from .web_exceptions import HTTPPermanentRedirect, _HTTPMove +from .web_exceptions import HTTPMove, HTTPPermanentRedirect from .web_request import Request from .web_response import StreamResponse from .web_urldispatcher import SystemRoute @@ -40,7 +40,7 @@ def normalize_path_middleware( append_slash: bool = True, remove_slash: bool = False, merge_slashes: bool = True, - redirect_class: Type[_HTTPMove] = HTTPPermanentRedirect, + redirect_class: Type[HTTPMove] = HTTPPermanentRedirect, ) -> Middleware: """Factory for producing a middleware that normalizes the path of a request.