Skip to content

Commit 4ef72f1

Browse files
committed
Refactor of Redirect response class to allow 301 and 302 codes
1 parent 503522c commit 4ef72f1

File tree

3 files changed

+35
-6
lines changed

3 files changed

+35
-6
lines changed

adafruit_httpserver/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@
7777
ACCEPTED_202,
7878
NO_CONTENT_204,
7979
PARTIAL_CONTENT_206,
80+
MOVED_PERMANENTLY_301,
81+
FOUND_302,
8082
TEMPORARY_REDIRECT_307,
8183
PERMANENT_REDIRECT_308,
8284
BAD_REQUEST_400,

adafruit_httpserver/response.py

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
Status,
3232
SWITCHING_PROTOCOLS_101,
3333
OK_200,
34+
MOVED_PERMANENTLY_301,
35+
FOUND_302,
3436
TEMPORARY_REDIRECT_307,
3537
PERMANENT_REDIRECT_308,
3638
)
@@ -393,19 +395,40 @@ def __init__(
393395
url: str,
394396
*,
395397
permanent: bool = False,
398+
preserve_method: bool = False,
399+
status: Union[Status, Tuple[int, str]] = None,
396400
headers: Union[Headers, Dict[str, str]] = None,
397401
) -> None:
398402
"""
403+
By default uses ``permament`` and ``preserve_method`` to determine the ``status`` code to
404+
use, but if you prefer you can specify it directly.
405+
406+
Note that ``301 Moved Permanently`` and ``302 Found`` can change the method to ``GET``
407+
while ``307 Temporary Redirect`` and ``308 Permanent Redirect`` preserve the method.
408+
409+
More information:
410+
https://developer.mozilla.org/en-US/docs/Web/HTTP/Status#redirection_messages
411+
399412
:param Request request: Request that this is a response to.
400413
:param str url: URL to redirect to.
401-
:param bool permanent: Whether to use a permanent redirect (308) or a temporary one (307).
414+
:param bool permanent: Whether to use a permanent redirect or a temporary one.
415+
:param bool preserve_method: Whether to preserve the method of the request.
416+
:param Status status: Status object or tuple with code and message.
402417
:param Headers headers: Headers to include in response.
403418
"""
404-
super().__init__(
405-
request,
406-
status=PERMANENT_REDIRECT_308 if permanent else TEMPORARY_REDIRECT_307,
407-
headers=headers,
408-
)
419+
420+
if status is not None and (permanent or preserve_method):
421+
raise ValueError(
422+
"Cannot specify both status and permanent/preserve_method argument"
423+
)
424+
425+
if status is None:
426+
if preserve_method:
427+
status = PERMANENT_REDIRECT_308 if permanent else TEMPORARY_REDIRECT_307
428+
else:
429+
status = MOVED_PERMANENTLY_301 if permanent else FOUND_302
430+
431+
super().__init__(request, status=status, headers=headers)
409432
self._headers.update({"Location": url})
410433

411434
def _send(self) -> None:

adafruit_httpserver/status.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ def __eq__(self, other: "Status"):
4343

4444
PARTIAL_CONTENT_206 = Status(206, "Partial Content")
4545

46+
MOVED_PERMANENTLY_301 = Status(301, "Moved Permanently")
47+
48+
FOUND_302 = Status(302, "Found")
49+
4650
TEMPORARY_REDIRECT_307 = Status(307, "Temporary Redirect")
4751

4852
PERMANENT_REDIRECT_308 = Status(308, "Permanent Redirect")

0 commit comments

Comments
 (0)