Streaming API will keep session alive
This commit is contained in:
parent
e67732b4f8
commit
733de6b357
2 changed files with 28 additions and 18 deletions
|
@ -106,6 +106,7 @@ def _handle_get_api_stream(handler, path_match, data):
|
||||||
wfile = handler.wfile
|
wfile = handler.wfile
|
||||||
write_lock = threading.Lock()
|
write_lock = threading.Lock()
|
||||||
block = threading.Event()
|
block = threading.Event()
|
||||||
|
session_id = None
|
||||||
|
|
||||||
restrict = data.get('restrict')
|
restrict = data.get('restrict')
|
||||||
if restrict:
|
if restrict:
|
||||||
|
@ -119,6 +120,7 @@ def _handle_get_api_stream(handler, path_match, data):
|
||||||
try:
|
try:
|
||||||
wfile.write(msg.encode("UTF-8"))
|
wfile.write(msg.encode("UTF-8"))
|
||||||
wfile.flush()
|
wfile.flush()
|
||||||
|
handler.server.sessions.extend_validation(session_id)
|
||||||
except IOError:
|
except IOError:
|
||||||
block.set()
|
block.set()
|
||||||
|
|
||||||
|
@ -138,6 +140,7 @@ def _handle_get_api_stream(handler, path_match, data):
|
||||||
|
|
||||||
handler.send_response(HTTP_OK)
|
handler.send_response(HTTP_OK)
|
||||||
handler.send_header('Content-type', 'text/event-stream')
|
handler.send_header('Content-type', 'text/event-stream')
|
||||||
|
session_id = handler.set_session_cookie_header()
|
||||||
handler.end_headers()
|
handler.end_headers()
|
||||||
|
|
||||||
hass.bus.listen(MATCH_ALL, forward_events)
|
hass.bus.listen(MATCH_ALL, forward_events)
|
||||||
|
|
|
@ -337,7 +337,9 @@ class RequestHandler(SimpleHTTPRequestHandler):
|
||||||
|
|
||||||
def set_cache_header(self):
|
def set_cache_header(self):
|
||||||
""" Add cache headers if not in development """
|
""" Add cache headers if not in development """
|
||||||
if not self.server.development:
|
if self.server.development:
|
||||||
|
return
|
||||||
|
|
||||||
# 1 year in seconds
|
# 1 year in seconds
|
||||||
cache_time = 365 * 86400
|
cache_time = 365 * 86400
|
||||||
|
|
||||||
|
@ -349,14 +351,14 @@ class RequestHandler(SimpleHTTPRequestHandler):
|
||||||
self.date_time_string(time.time()+cache_time))
|
self.date_time_string(time.time()+cache_time))
|
||||||
|
|
||||||
def set_session_cookie_header(self):
|
def set_session_cookie_header(self):
|
||||||
""" Add the header for the session cookie. """
|
""" Add the header for the session cookie and return session id. """
|
||||||
if not self.authenticated:
|
if not self.authenticated:
|
||||||
return
|
return
|
||||||
|
|
||||||
current = self.get_cookie_session_id()
|
session_id = self.get_cookie_session_id()
|
||||||
|
|
||||||
if current is not None:
|
if session_id is not None:
|
||||||
self.server.sessions.extend_validation(current)
|
self.server.sessions.extend_validation(session_id)
|
||||||
return
|
return
|
||||||
|
|
||||||
self.send_header(
|
self.send_header(
|
||||||
|
@ -364,6 +366,8 @@ class RequestHandler(SimpleHTTPRequestHandler):
|
||||||
'{}={}'.format(SESSION_KEY, self.server.sessions.create())
|
'{}={}'.format(SESSION_KEY, self.server.sessions.create())
|
||||||
)
|
)
|
||||||
|
|
||||||
|
return session_id
|
||||||
|
|
||||||
def verify_session(self):
|
def verify_session(self):
|
||||||
""" Verify that we are in a valid session. """
|
""" Verify that we are in a valid session. """
|
||||||
return self.get_cookie_session_id() is not None
|
return self.get_cookie_session_id() is not None
|
||||||
|
@ -387,19 +391,22 @@ class RequestHandler(SimpleHTTPRequestHandler):
|
||||||
if morsel is None:
|
if morsel is None:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
current = cookie[SESSION_KEY].value
|
session_id = cookie[SESSION_KEY].value
|
||||||
|
|
||||||
return current if self.server.sessions.is_valid(current) else None
|
if self.server.sessions.is_valid(session_id):
|
||||||
|
return session_id
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
def destroy_session(self):
|
def destroy_session(self):
|
||||||
""" Destroys session. """
|
""" Destroys session. """
|
||||||
current = self.get_cookie_session_id()
|
session_id = self.get_cookie_session_id()
|
||||||
|
|
||||||
if current is None:
|
if session_id is None:
|
||||||
return
|
return
|
||||||
|
|
||||||
self.send_header('Set-Cookie', '')
|
self.send_header('Set-Cookie', '')
|
||||||
self.server.sessions.destroy(current)
|
self.server.sessions.destroy(session_id)
|
||||||
|
|
||||||
|
|
||||||
def session_valid_time():
|
def session_valid_time():
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue