Opened 6 years ago
Closed 6 years ago
#31388 closed Bug (invalid)
HttpResponseBase.setdefault() issue.
| Reported by: | liuwei | Owned by: | nobody |
|---|---|---|---|
| Component: | HTTP handling | Version: | 2.2 |
| Severity: | Normal | Keywords: | |
| Cc: | Triage Stage: | Unreviewed | |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
class HttpResponseBase:
"""
An HTTP response base class with dictionary-accessed headers.
This class doesn't handle content. It should not be used directly.
Use the HttpResponse and StreamingHttpResponse subclasses instead.
"""
status_code = 200
def __setitem__(self, header, value):
header = self._convert_to_charset(header, 'ascii')
value = self._convert_to_charset(value, 'latin-1', mime_encode=True)
self._headers[header.lower()] = (header, value)
def setdefault(self, key, value):
"""Set a header unless it has already been set."""
if key not in self:
self[key] = value
I want to return the response in the middleware modification,my code like this:
class ParamsMiddleWare(MiddlewareMixin):
def process_response(self, request, response):
"""
:param request:
:return:
"""
if request.path.startswith('/api/'):
if response.status_code == 200:
resp = HttpResponse(content=encrypt_data(KEY.encode('utf-8'), response.content.decode('utf-8')),
content_type='text/plain')
resp.setdefault('x-frame-options', ('X-Frame-Options', 'SAMEORIGIN'))
return resp
return response
However, the header of the resp I set is not
'x-frame-options':('X-Frame-Options', 'SAMEORIGIN')
but
'x-frame-options':('x-frame-options','('X-Frame-Options', 'SAMEORIGIN')')
Why?????????
Change History (2)
comment:1 by , 6 years ago
comment:2 by , 6 years ago
| Component: | Uncategorized → HTTP handling |
|---|---|
| Resolution: | → invalid |
| Status: | new → closed |
| Summary: | [HttpResponseBase.setdefault] BUG??? → HttpResponseBase.setdefault() issue. |
It works properly, described behavior is an implementation detail. Please don't shout: "Why?????" and don't use trac as a support channel.
Note:
See TracTickets
for help on using tickets.
I fail to reproduce. There might be something wrong with that encryption, but I highly doubt.
Curl returns
< x-frame-options: ('X-Frame-Options', 'SAMEORIGIN').Try to reduce the amount of your code that might cause the issue and post a full code.
Also, I think you're looking at wrong data.
print(resp._headers)returns{'x-frame-options': ('x-frame-options', "('X-Frame-Options', 'SAMEORIGIN')")}, but curl gets what you want it to get.Paste the output of
curl -v 127.0.0.1:8000/api/