Opened 10 years ago

Closed 10 years ago

#24139 closed New feature (fixed)

The response reason phrases should evaluate lazily

Reported by: Jon Dufresne Owned by: nobody
Component: HTTP handling Version: dev
Severity: Normal Keywords:
Cc: jon.dufresne@…, cmawebsite@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Motivation for this suggestion comes from a bug I found in my own code. I traced it back to how Django handles HTTP reason phrases.

Suggestion:

If the status_code of an HTTP response is set outside of the constructor, the reason_phrase will be "OK", the default for a 200 response. The calling code is required to take two steps to successfully change the status and reason phrase together.

response = render_to_response(....)
response.status_code = 503  # Or any status code
# As of now, reason_phrase is still "OK"
#
# Need the line below -- or something like it -- to successfully update the reason phrase.
response.reason_phrase = REASON_PHRASES[response.status_code]

I'm suggesting wrap reason_phrase as a property. Internal to the response, reason_phrase remains None unless specified by the calling code. Upon accessing reason_phrase, if the value is None pull from the default REASON_PHRASES based on the current value of status_code.

I'd also suggest that any change to the status_code automatically sets reason_phrase back to None to avoid surprise reason phrases.

Right now the reason_phrase is set once in the constructor and not updated based on the latest status_code.

Pull request to follow.

Change History (9)

comment:1 by Jon Dufresne, 10 years ago

Cc: jon.dufresne@… added
Has patch: set

comment:2 by Collin Anderson, 10 years ago

Cc: cmawebsite@… added
Triage Stage: UnreviewedAccepted

The implementation looks simple enough.

comment:3 by Aymeric Augustin, 10 years ago

Needs documentation: set

Is it documented that you can set HttpResponse.status_code outside of the constructor?

If we're adding code to support setting this attribute, it should be documented. If it's already documented, the change in behavior should be documented.

comment:4 by Jon Dufresne, 10 years ago

Is it documented that you can set HttpResponse.status_code outside of the constructor?

Grepping for "status_code" and "status" in docs shows the following:

./request-response.txt:663:

.. method:: HttpResponse.__init__(content='', content_type=None, status=200, reason=None, charset=None)

    ``status`` is the `HTTP status code`_ for the response.


./ref/request-response.txt:639:

.. attribute:: HttpResponse.status_code

    The `HTTP status code`_ for the response.

Nothing about this indicates to me that I shouldn't change status_code outside the constructor.

This was originally a problem for me as I was using render_to_response in Django 1.6. This version does not accept status as a kwarg. I was trying to change the status_code of the return value. This is when I noticed the reason phrase was incorrect.

If we're adding code to support setting this attribute, it should be documented. If it's already documented, the change in behavior should be documented.

Will do.

comment:5 by Jon Dufresne, 10 years ago

Needs documentation: unset

If we're adding code to support setting this attribute, it should be documented. If it's already documented, the change in behavior should be documented.

Added docs to PR. All feedback welcome.

comment:6 by Collin Anderson, 10 years ago

Triage Stage: AcceptedReady for checkin

comment:7 by Tim Graham, 10 years ago

Patch needs improvement: set
Triage Stage: Ready for checkinAccepted

comment:8 by Jon Dufresne, 10 years ago

Patch needs improvement: unset

Fixed PR based on feedback.

comment:9 by Tim Graham <timograham@…>, 10 years ago

Resolution: fixed
Status: newclosed

In d861f95:

Fixed #24139 -- Changed HttpResponse.reason_phrase to evaluate based on status_code.

Note: See TracTickets for help on using tickets.
Back to Top