Merge pull request #1530 from balloob/content-length
Add content-length header to http resonses
This commit is contained in:
commit
6bc3e0bb58
2 changed files with 9 additions and 4 deletions
|
@ -278,8 +278,11 @@ class RequestHandler(SimpleHTTPRequestHandler):
|
||||||
|
|
||||||
def write_json(self, data=None, status_code=HTTP_OK, location=None):
|
def write_json(self, data=None, status_code=HTTP_OK, location=None):
|
||||||
"""Helper method to return JSON to the caller."""
|
"""Helper method to return JSON to the caller."""
|
||||||
|
json_data = json.dumps(data, indent=4, sort_keys=True,
|
||||||
|
cls=rem.JSONEncoder).encode('UTF-8')
|
||||||
self.send_response(status_code)
|
self.send_response(status_code)
|
||||||
self.send_header(HTTP_HEADER_CONTENT_TYPE, CONTENT_TYPE_JSON)
|
self.send_header(HTTP_HEADER_CONTENT_TYPE, CONTENT_TYPE_JSON)
|
||||||
|
self.send_header(HTTP_HEADER_CONTENT_LENGTH, str(len(json_data)))
|
||||||
|
|
||||||
if location:
|
if location:
|
||||||
self.send_header('Location', location)
|
self.send_header('Location', location)
|
||||||
|
@ -289,20 +292,20 @@ class RequestHandler(SimpleHTTPRequestHandler):
|
||||||
self.end_headers()
|
self.end_headers()
|
||||||
|
|
||||||
if data is not None:
|
if data is not None:
|
||||||
self.wfile.write(
|
self.wfile.write(json_data)
|
||||||
json.dumps(data, indent=4, sort_keys=True,
|
|
||||||
cls=rem.JSONEncoder).encode("UTF-8"))
|
|
||||||
|
|
||||||
def write_text(self, message, status_code=HTTP_OK):
|
def write_text(self, message, status_code=HTTP_OK):
|
||||||
"""Helper method to return a text message to the caller."""
|
"""Helper method to return a text message to the caller."""
|
||||||
|
msg_data = message.encode('UTF-8')
|
||||||
self.send_response(status_code)
|
self.send_response(status_code)
|
||||||
self.send_header(HTTP_HEADER_CONTENT_TYPE, CONTENT_TYPE_TEXT_PLAIN)
|
self.send_header(HTTP_HEADER_CONTENT_TYPE, CONTENT_TYPE_TEXT_PLAIN)
|
||||||
|
self.send_header(HTTP_HEADER_CONTENT_LENGTH, str(len(msg_data)))
|
||||||
|
|
||||||
self.set_session_cookie_header()
|
self.set_session_cookie_header()
|
||||||
|
|
||||||
self.end_headers()
|
self.end_headers()
|
||||||
|
|
||||||
self.wfile.write(message.encode("UTF-8"))
|
self.wfile.write(msg_data)
|
||||||
|
|
||||||
def write_file(self, path, cache_headers=True):
|
def write_file(self, path, cache_headers=True):
|
||||||
"""Return a file to the user."""
|
"""Return a file to the user."""
|
||||||
|
|
|
@ -104,6 +104,8 @@ class TestAPI(unittest.TestCase):
|
||||||
_url(const.URL_API_STATES_ENTITY.format("test.test")),
|
_url(const.URL_API_STATES_ENTITY.format("test.test")),
|
||||||
headers=HA_HEADERS)
|
headers=HA_HEADERS)
|
||||||
|
|
||||||
|
self.assertEqual(req.headers['content-length'], str(len(req.content)))
|
||||||
|
|
||||||
data = ha.State.from_dict(req.json())
|
data = ha.State.from_dict(req.json())
|
||||||
|
|
||||||
state = hass.states.get("test.test")
|
state = hass.states.get("test.test")
|
||||||
|
|
Loading…
Add table
Reference in a new issue