Skip to content

gh-102247: Update HTTP status codes in http package to match rfc9110 #102570

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 8 commits into from
Closed
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
13 changes: 9 additions & 4 deletions Doc/library/http.rst
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ Code Enum Name Details
``304`` ``NOT_MODIFIED`` HTTP/1.1 :rfc:`7232`, Section 4.1
``305`` ``USE_PROXY`` HTTP/1.1 :rfc:`7231`, Section 6.4.5
``307`` ``TEMPORARY_REDIRECT`` HTTP/1.1 :rfc:`7231`, Section 6.4.7
``308`` ``PERMANENT_REDIRECT`` Permanent Redirect :rfc:`7238`, Section 3 (Experimental)
``308`` ``PERMANENT_REDIRECT`` HTTP Semantics :rfc:`9110`, Section 15.4.9
``400`` ``BAD_REQUEST`` HTTP/1.1 :rfc:`7231`, Section 6.5.1
``401`` ``UNAUTHORIZED`` HTTP/1.1 Authentication :rfc:`7235`, Section 3.1
``402`` ``PAYMENT_REQUIRED`` HTTP/1.1 :rfc:`7231`, Section 6.5.2
Expand All @@ -94,14 +94,14 @@ Code Enum Name Details
``410`` ``GONE`` HTTP/1.1 :rfc:`7231`, Section 6.5.9
``411`` ``LENGTH_REQUIRED`` HTTP/1.1 :rfc:`7231`, Section 6.5.10
``412`` ``PRECONDITION_FAILED`` HTTP/1.1 :rfc:`7232`, Section 4.2
``413`` ``REQUEST_ENTITY_TOO_LARGE`` HTTP/1.1 :rfc:`7231`, Section 6.5.11
``413`` ``CONTENT_TOO_LARGE`` HTTP Semantics :rfc:`9110`, Section 15.5.14
``414`` ``REQUEST_URI_TOO_LONG`` HTTP/1.1 :rfc:`7231`, Section 6.5.12
``415`` ``UNSUPPORTED_MEDIA_TYPE`` HTTP/1.1 :rfc:`7231`, Section 6.5.13
``416`` ``REQUESTED_RANGE_NOT_SATISFIABLE`` HTTP/1.1 Range Requests :rfc:`7233`, Section 4.4
``417`` ``EXPECTATION_FAILED`` HTTP/1.1 :rfc:`7231`, Section 6.5.14
``418`` ``IM_A_TEAPOT`` HTCPCP/1.0 :rfc:`2324`, Section 2.3.2
``421`` ``MISDIRECTED_REQUEST`` HTTP/2 :rfc:`7540`, Section 9.1.2
``422`` ``UNPROCESSABLE_ENTITY`` WebDAV :rfc:`4918`, Section 11.2
``421`` ``MISDIRECTED_REQUEST`` HTTP Semantics :rfc:`9110`, Section 15.5.20
``422`` ``UNPROCESSABLE_CONTENT`` HTTP Semantics :rfc:`9110`, Section 15.5.21
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have to notify the change with the versionchanged tag.

``423`` ``LOCKED`` WebDAV :rfc:`4918`, Section 11.3
``424`` ``FAILED_DEPENDENCY`` WebDAV :rfc:`4918`, Section 11.4
``425`` ``TOO_EARLY`` Using Early Data in HTTP :rfc:`8470`
Expand Down Expand Up @@ -137,6 +137,11 @@ equal to the constant name (i.e. ``http.HTTPStatus.OK`` is also available as
.. versionadded:: 3.9
Added ``103 EARLY_HINTS``, ``418 IM_A_TEAPOT`` and ``425 TOO_EARLY`` status codes.

.. versionadded:: 3.12
Updated ``413 REQUEST_ENTITY_TOO_LARGE`` and ``422 UNPROCESSABLE_ENTITY``
to ``413 CONTENT_TOO_LARGE`` and ``422 UNPROCESSABLE_CONTENT``. Backward compatibility
of previous status codes in :mod:`http` module is preserved.

HTTP status category
--------------------

Expand Down
8 changes: 8 additions & 0 deletions Doc/whatsnew/3.12.rst
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,14 @@ sys
with contributions from Gregory P. Smith [Google] and Mark Shannon
in :gh:`96123`.)

http
----

* Updated ``413 REQUEST_ENTITY_TOO_LARGE`` and ``422 UNPROCESSABLE_ENTITY``
to ``413 CONTENT_TOO_LARGE`` and ``422 UNPROCESSABLE_CONTENT``. This is
for applying :rfc:`9110`. Backward compatibility of previous status codes in
:mod:`http` module is preserved. (Contributed by Yeojin Kim in :gh:`102570`.)


Optimizations
=============
Expand Down
9 changes: 6 additions & 3 deletions Lib/http/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class HTTPStatus:
* RFC 2324: Hyper Text Coffee Pot Control Protocol (HTCPCP/1.0)
* RFC 8297: An HTTP Status Code for Indicating Hints
* RFC 8470: Using Early Data in HTTP
* RFC 9110: HTTP Semantics
"""
def __new__(cls, value, phrase, description=''):
obj = int.__new__(cls, value)
Expand Down Expand Up @@ -115,8 +116,9 @@ def is_server_error(self):
'Client must specify Content-Length')
PRECONDITION_FAILED = (412, 'Precondition Failed',
'Precondition in headers is false')
REQUEST_ENTITY_TOO_LARGE = (413, 'Request Entity Too Large',
'Entity is too large')
CONTENT_TOO_LARGE = (413, 'Content Too Large',
'Content is too large')
REQUEST_ENTITY_TOO_LARGE = CONTENT_TOO_LARGE # for backward compatibility
REQUEST_URI_TOO_LONG = (414, 'Request-URI Too Long',
'URI is too long')
UNSUPPORTED_MEDIA_TYPE = (415, 'Unsupported Media Type',
Expand All @@ -130,7 +132,8 @@ def is_server_error(self):
'Server refuses to brew coffee because it is a teapot.')
MISDIRECTED_REQUEST = (421, 'Misdirected Request',
'Server is not able to produce a response')
UNPROCESSABLE_ENTITY = 422, 'Unprocessable Entity'
UNPROCESSABLE_CONTENT = 422, 'Unprocessable Content'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One of the problems of removing this constant is that it will break end-user code.

This is one example
https://github.com/aiven/karapace/blob/88bee3ab15115fca80b8f74020ebb30a3b562ffe/karapace/karapace.py#L41-L46

Copy link
Member

@corona10 corona10 Mar 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even if we decide to accept this change, we may need to add deprecation period rather than direct removing.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we have to keep the old names as aliases for the new ones. I would continue to support them indefinitely, even -- deprecations and breaking things has a cost, and supporting a few extra constant names indefinitely is essentially free, so it's not worth spending our limited breakage budget on it.

UNPROCESSABLE_ENTITY = UNPROCESSABLE_CONTENT # for backward compatibility
LOCKED = 423, 'Locked'
FAILED_DEPENDENCY = 424, 'Failed Dependency'
TOO_EARLY = 425, 'Too Early'
Expand Down
10 changes: 7 additions & 3 deletions Lib/test/test_httplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -633,8 +633,9 @@ def is_server_error(self):
'Client must specify Content-Length')
PRECONDITION_FAILED = (412, 'Precondition Failed',
'Precondition in headers is false')
REQUEST_ENTITY_TOO_LARGE = (413, 'Request Entity Too Large',
'Entity is too large')
CONTENT_TOO_LARGE = (413, 'Content Too Large',
'Content is too large')
REQUEST_ENTITY_TOO_LARGE = CONTENT_TOO_LARGE # for backward compatibility
REQUEST_URI_TOO_LONG = (414, 'Request-URI Too Long',
'URI is too long')
UNSUPPORTED_MEDIA_TYPE = (415, 'Unsupported Media Type',
Expand All @@ -648,7 +649,8 @@ def is_server_error(self):
'Server refuses to brew coffee because it is a teapot.')
MISDIRECTED_REQUEST = (421, 'Misdirected Request',
'Server is not able to produce a response')
UNPROCESSABLE_ENTITY = 422, 'Unprocessable Entity'
UNPROCESSABLE_CONTENT = 422, 'Unprocessable Content'
UNPROCESSABLE_ENTITY = UNPROCESSABLE_CONTENT # for backward compatibility
LOCKED = 423, 'Locked'
FAILED_DEPENDENCY = 424, 'Failed Dependency'
TOO_EARLY = 425, 'Too Early'
Expand Down Expand Up @@ -1688,13 +1690,15 @@ def test_client_constants(self):
'GONE',
'LENGTH_REQUIRED',
'PRECONDITION_FAILED',
'CONTENT_TOO_LARGE',
'REQUEST_ENTITY_TOO_LARGE',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please maintain old constants

'REQUEST_URI_TOO_LONG',
'UNSUPPORTED_MEDIA_TYPE',
'REQUESTED_RANGE_NOT_SATISFIABLE',
'EXPECTATION_FAILED',
'IM_A_TEAPOT',
'MISDIRECTED_REQUEST',
'UNPROCESSABLE_CONTENT',
'UNPROCESSABLE_ENTITY',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

'LOCKED',
'FAILED_DEPENDENCY',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Update HTTP status codes in http package to match rfc9110. Patch by
Yeojin Kim