summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJenkins <jenkins@review.openstack.org>2014-08-27 20:55:38 +0000
committerGerrit Code Review <review@openstack.org>2014-08-27 20:55:38 +0000
commit5d1f3915e878d2293fdf88f9325e6350a6a3cbef (patch)
tree12ac443ded2a2ee2b86d76938c5ba3aed45574a3
parent078a0dd0937b7e59b62d7eae60d35af77cea1283 (diff)
parent489afb78197a0adbb5e768ac00ff71bf38ec2a18 (diff)
downloadpecan-5d1f3915e878d2293fdf88f9325e6350a6a3cbef.tar.gz
Merge "Refactor 204 handling."
-rw-r--r--pecan/core.py34
1 files changed, 20 insertions, 14 deletions
diff --git a/pecan/core.py b/pecan/core.py
index 925e7ab..a52dae2 100644
--- a/pecan/core.py
+++ b/pecan/core.py
@@ -571,33 +571,37 @@ class PecanBase(object):
resp.text = result
elif result:
resp.body = result
- elif response.status_int == 200:
+
+ if pecan_state['content_type']:
+ # set the content type
+ resp.content_type = pecan_state['content_type']
+
+ def _handle_empty_response_body(self, state):
+ # Enforce HTTP 204 for responses which contain no body
+ if state.response.status_int == 200:
# If the response is a generator...
- if isinstance(response.app_iter, types.GeneratorType):
+ if isinstance(state.response.app_iter, types.GeneratorType):
# Split the generator into two so we can peek at one of them
# and determine if there is any response body content
- a, b = tee(response.app_iter)
+ a, b = tee(state.response.app_iter)
try:
next(a)
except StopIteration:
# If we hit StopIteration, the body is empty
- resp.status = 204
+ state.response.status = 204
finally:
- resp.app_iter = b
+ state.response.app_iter = b
else:
text = None
- if response.charset:
+ if state.response.charset:
# `response.text` cannot be accessed without a charset
# (because we don't know which encoding to use)
- text = response.text
- if not any((response.body, text)):
- resp.status = 204
+ text = state.response.text
+ if not any((state.response.body, text)):
+ state.response.status = 204
- if resp.status_int in (204, 304):
- resp.content_type = None
- elif pecan_state['content_type']:
- # set the content type
- resp.content_type = pecan_state['content_type']
+ if state.response.status_int in (204, 304):
+ state.response.content_type = None
def __call__(self, environ, start_response):
'''
@@ -668,6 +672,8 @@ class PecanBase(object):
self.determine_hooks(state.controller), 'after', state
)
+ self._handle_empty_response_body(state)
+
# get the response
return state.response(environ, start_response)