core/homeassistant/components/http.py

472 lines
16 KiB
Python
Raw Normal View History

"""
2013-09-28 18:09:36 +00:00
This module provides an API and a HTTP interface for debug purposes.
2015-10-25 14:40:35 +00:00
For more details about the RESTful API, please refer to the documentation at
2015-11-09 12:12:18 +00:00
https://home-assistant.io/developers/api/
"""
2015-12-06 22:05:58 +00:00
import gzip
2013-09-28 18:09:36 +00:00
import json
2013-09-22 00:59:31 +00:00
import logging
2015-12-06 22:05:58 +00:00
import ssl
import threading
import time
2016-02-19 05:27:50 +00:00
from datetime import timedelta
from http import cookies
from http.server import HTTPServer, SimpleHTTPRequestHandler
from socketserver import ThreadingMixIn
from urllib.parse import parse_qs, urlparse
2013-09-20 06:59:49 +00:00
2016-02-19 05:27:50 +00:00
import homeassistant.bootstrap as bootstrap
2015-08-17 03:44:46 +00:00
import homeassistant.core as ha
import homeassistant.remote as rem
import homeassistant.util as util
import homeassistant.util.dt as date_util
2016-02-19 05:27:50 +00:00
from homeassistant.const import (
CONTENT_TYPE_JSON, CONTENT_TYPE_TEXT_PLAIN, HTTP_HEADER_ACCEPT_ENCODING,
HTTP_HEADER_CACHE_CONTROL, HTTP_HEADER_CONTENT_ENCODING,
HTTP_HEADER_CONTENT_LENGTH, HTTP_HEADER_CONTENT_TYPE, HTTP_HEADER_EXPIRES,
HTTP_HEADER_HA_AUTH, HTTP_HEADER_VARY, HTTP_METHOD_NOT_ALLOWED,
HTTP_NOT_FOUND, HTTP_OK, HTTP_UNAUTHORIZED, HTTP_UNPROCESSABLE_ENTITY,
SERVER_PORT)
2014-10-27 01:10:01 +00:00
DOMAIN = "http"
2013-09-20 06:59:49 +00:00
CONF_API_PASSWORD = "api_password"
CONF_SERVER_HOST = "server_host"
CONF_SERVER_PORT = "server_port"
CONF_DEVELOPMENT = "development"
2015-12-06 22:19:25 +00:00
CONF_SSL_CERTIFICATE = 'ssl_certificate'
CONF_SSL_KEY = 'ssl_key'
DATA_API_PASSWORD = 'api_password'
# Throttling time in seconds for expired sessions check
SESSION_CLEAR_INTERVAL = timedelta(seconds=20)
SESSION_TIMEOUT_SECONDS = 1800
SESSION_KEY = 'sessionId'
2014-11-08 21:57:08 +00:00
_LOGGER = logging.getLogger(__name__)
2014-11-23 20:57:29 +00:00
def setup(hass, config):
2016-03-08 16:55:57 +00:00
"""Set up the HTTP API and debug interface."""
2015-12-05 21:44:50 +00:00
conf = config.get(DOMAIN, {})
api_password = util.convert(conf.get(CONF_API_PASSWORD), str)
# If no server host is given, accept all incoming requests
server_host = conf.get(CONF_SERVER_HOST, '0.0.0.0')
server_port = conf.get(CONF_SERVER_PORT, SERVER_PORT)
development = str(conf.get(CONF_DEVELOPMENT, "")) == "1"
2015-12-06 22:19:25 +00:00
ssl_certificate = conf.get(CONF_SSL_CERTIFICATE)
ssl_key = conf.get(CONF_SSL_KEY)
2015-07-26 07:14:55 +00:00
try:
server = HomeAssistantHTTPServer(
(server_host, server_port), RequestHandler, hass, api_password,
2015-12-06 22:19:25 +00:00
development, ssl_certificate, ssl_key)
2015-07-26 07:14:55 +00:00
except OSError:
# If address already in use
2015-07-26 07:14:55 +00:00
_LOGGER.exception("Error setting up HTTP server")
return False
2013-09-22 00:59:31 +00:00
2014-11-29 07:19:59 +00:00
hass.bus.listen_once(
ha.EVENT_HOMEASSISTANT_START,
lambda event:
2016-03-08 00:43:33 +00:00
threading.Thread(target=server.start, daemon=True,
name='HTTP-server').start())
hass.http = server
hass.config.api = rem.API(server_host if server_host != '0.0.0.0'
else util.get_local_ip(),
api_password, server_port,
2015-12-06 22:19:25 +00:00
ssl_certificate is not None)
return True
2015-05-18 14:08:02 +00:00
# pylint: disable=too-many-instance-attributes
class HomeAssistantHTTPServer(ThreadingMixIn, HTTPServer):
2016-03-07 17:49:31 +00:00
"""Handle HTTP requests in a threaded fashion."""
2016-03-08 16:55:57 +00:00
2014-11-23 20:57:29 +00:00
# pylint: disable=too-few-public-methods
2014-11-23 17:51:16 +00:00
allow_reuse_address = True
2014-11-23 20:57:29 +00:00
daemon_threads = True
2014-11-23 17:51:16 +00:00
2014-10-25 06:44:00 +00:00
# pylint: disable=too-many-arguments
2014-12-07 09:28:52 +00:00
def __init__(self, server_address, request_handler_class,
2015-12-06 22:19:25 +00:00
hass, api_password, development, ssl_certificate, ssl_key):
2016-03-08 16:55:57 +00:00
"""Initialize the server."""
2014-12-07 09:28:52 +00:00
super().__init__(server_address, request_handler_class)
2013-09-22 00:59:31 +00:00
self.server_address = server_address
self.hass = hass
self.api_password = api_password
self.development = development
self.paths = []
self.sessions = SessionStore()
2015-12-06 23:13:41 +00:00
self.use_ssl = ssl_certificate is not None
2013-09-22 00:59:31 +00:00
# We will lazy init this one if needed
self.event_forwarder = None
2013-09-22 00:59:31 +00:00
if development:
_LOGGER.info("running http in development mode")
2015-12-06 22:19:25 +00:00
if ssl_certificate is not None:
2016-01-15 20:39:54 +00:00
context = ssl.create_default_context(
purpose=ssl.Purpose.CLIENT_AUTH)
context.load_cert_chain(ssl_certificate, keyfile=ssl_key)
self.socket = context.wrap_socket(self.socket, server_side=True)
2015-12-06 22:05:58 +00:00
def start(self):
2016-03-08 16:55:57 +00:00
"""Start the HTTP server."""
2015-08-03 15:05:33 +00:00
def stop_http(event):
2016-03-08 16:55:57 +00:00
"""Stop the HTTP server."""
2015-08-03 15:05:33 +00:00
self.shutdown()
self.hass.bus.listen_once(ha.EVENT_HOMEASSISTANT_STOP, stop_http)
2014-11-29 04:22:08 +00:00
2015-12-06 23:13:41 +00:00
protocol = 'https' if self.use_ssl else 'http'
2014-11-08 21:57:08 +00:00
_LOGGER.info(
2015-12-06 23:13:41 +00:00
"Starting web interface at %s://%s:%d",
protocol, self.server_address[0], self.server_address[1])
# 31-1-2015: Refactored frontend/api components out of this component
# To prevent stuff from breaking, load the two extracted components
bootstrap.setup_component(self.hass, 'api')
bootstrap.setup_component(self.hass, 'frontend')
self.serve_forever()
2013-09-22 00:59:31 +00:00
def register_path(self, method, url, callback, require_auth=True):
2016-03-08 16:55:57 +00:00
"""Register a path with the server."""
self.paths.append((method, url, callback, require_auth))
def log_message(self, fmt, *args):
2016-03-07 17:49:31 +00:00
"""Redirect built-in log to HA logging."""
# pylint: disable=no-self-use
_LOGGER.info(fmt, *args)
2013-11-11 00:46:48 +00:00
# pylint: disable=too-many-public-methods,too-many-locals
2014-10-27 01:10:01 +00:00
class RequestHandler(SimpleHTTPRequestHandler):
2016-03-08 16:55:57 +00:00
"""Handle incoming HTTP requests.
2014-10-27 01:10:01 +00:00
We extend from SimpleHTTPRequestHandler instead of Base so we
can use the guess content type methods.
"""
2016-03-08 16:55:57 +00:00
2014-10-27 01:10:01 +00:00
server_version = "HomeAssistant/1.0"
2013-09-20 06:59:49 +00:00
def __init__(self, req, client_addr, server):
2016-03-08 16:55:57 +00:00
"""Constructor, call the base constructor and set up session."""
# Track if this was an authenticated request
self.authenticated = False
SimpleHTTPRequestHandler.__init__(self, req, client_addr, server)
2016-03-27 02:03:16 +00:00
self.protocol_version = 'HTTP/1.1'
def log_message(self, fmt, *arguments):
2016-03-07 17:49:31 +00:00
"""Redirect built-in log to HA logging."""
2015-11-29 02:32:15 +00:00
if self.server.api_password is None:
2015-09-24 03:56:34 +00:00
_LOGGER.info(fmt, *arguments)
else:
_LOGGER.info(
fmt, *(arg.replace(self.server.api_password, '*******')
if isinstance(arg, str) else arg for arg in arguments))
2013-11-11 00:46:48 +00:00
def _handle_request(self, method): # pylint: disable=too-many-branches
2016-03-08 16:55:57 +00:00
"""Perform some common checks and call appropriate method."""
2013-10-29 07:22:38 +00:00
url = urlparse(self.path)
2013-09-20 06:59:49 +00:00
2015-12-13 06:18:38 +00:00
# Read query input. parse_qs gives a list for each value, we want last
data = {key: data[-1] for key, data in parse_qs(url.query).items()}
2014-10-17 07:17:02 +00:00
2013-10-29 07:22:38 +00:00
# Did we get post input ?
content_length = int(self.headers.get(HTTP_HEADER_CONTENT_LENGTH, 0))
2013-09-28 18:09:36 +00:00
2013-10-29 07:22:38 +00:00
if content_length:
2014-10-17 07:17:02 +00:00
body_content = self.rfile.read(content_length).decode("UTF-8")
2013-09-28 18:09:36 +00:00
try:
data.update(json.loads(body_content))
except (TypeError, ValueError):
2015-01-30 16:26:06 +00:00
# TypeError if JSON object is not a dict
# ValueError if we could not parse JSON
2015-01-30 16:26:06 +00:00
_LOGGER.exception(
"Exception parsing JSON: %s", body_content)
self.write_json_message(
"Error parsing JSON", HTTP_UNPROCESSABLE_ENTITY)
return
2014-10-17 07:17:02 +00:00
self.authenticated = (self.server.api_password is None or
self.headers.get(HTTP_HEADER_HA_AUTH) ==
self.server.api_password or
data.get(DATA_API_PASSWORD) ==
self.server.api_password or
self.verify_session())
if '_METHOD' in data:
2014-10-20 01:41:06 +00:00
method = data.pop('_METHOD')
# Var to keep track if we found a path that matched a handler but
# the method was different
2013-10-29 07:22:38 +00:00
path_matched_but_not_method = False
# Var to hold the handler for this path and method if found
2013-10-29 07:22:38 +00:00
handle_request_method = False
require_auth = True
# Check every handler to find matching result
for t_method, t_path, t_handler, t_auth in self.server.paths:
2013-10-29 07:22:38 +00:00
# we either do string-comparison or regular expression matching
2014-03-12 05:35:51 +00:00
# pylint: disable=maybe-no-member
2013-10-29 07:22:38 +00:00
if isinstance(t_path, str):
path_match = url.path == t_path
2013-10-29 07:22:38 +00:00
else:
path_match = t_path.match(url.path)
2013-09-20 06:59:49 +00:00
2013-10-29 07:22:38 +00:00
if path_match and method == t_method:
# Call the method
handle_request_method = t_handler
require_auth = t_auth
2013-10-29 07:22:38 +00:00
break
2013-10-29 07:22:38 +00:00
elif path_match:
path_matched_but_not_method = True
2013-09-20 06:59:49 +00:00
# Did we find a handler for the incoming request?
2013-10-29 07:22:38 +00:00
if handle_request_method:
# For some calls we need a valid password
2016-03-15 22:38:46 +00:00
msg = "API password missing or incorrect."
if require_auth and not self.authenticated:
2016-03-15 22:38:46 +00:00
self.write_json_message(msg, HTTP_UNAUTHORIZED)
2016-04-03 07:46:05 +00:00
_LOGGER.warning('%s Source IP: %s',
msg,
self.client_address[0])
return
handle_request_method(self, path_match, data)
2013-09-20 06:59:49 +00:00
2013-10-29 07:22:38 +00:00
elif path_matched_but_not_method:
self.send_response(HTTP_METHOD_NOT_ALLOWED)
self.end_headers()
2013-10-29 07:22:38 +00:00
else:
self.send_response(HTTP_NOT_FOUND)
self.end_headers()
2013-09-20 06:59:49 +00:00
2014-10-27 01:10:01 +00:00
def do_HEAD(self): # pylint: disable=invalid-name
2016-03-07 17:49:31 +00:00
"""HEAD request handler."""
2014-10-27 01:10:01 +00:00
self._handle_request('HEAD')
2013-11-11 00:46:48 +00:00
def do_GET(self): # pylint: disable=invalid-name
2016-03-07 17:49:31 +00:00
"""GET request handler."""
2013-10-29 07:22:38 +00:00
self._handle_request('GET')
2013-11-11 00:46:48 +00:00
def do_POST(self): # pylint: disable=invalid-name
2016-03-07 17:49:31 +00:00
"""POST request handler."""
2013-10-29 07:22:38 +00:00
self._handle_request('POST')
2013-09-28 18:09:36 +00:00
2014-10-17 07:17:02 +00:00
def do_PUT(self): # pylint: disable=invalid-name
2016-03-07 17:49:31 +00:00
"""PUT request handler."""
2014-10-17 07:17:02 +00:00
self._handle_request('PUT')
def do_DELETE(self): # pylint: disable=invalid-name
2016-03-07 17:49:31 +00:00
"""DELETE request handler."""
2014-10-17 07:17:02 +00:00
self._handle_request('DELETE')
2015-01-30 16:26:06 +00:00
def write_json_message(self, message, status_code=HTTP_OK):
2016-03-07 17:49:31 +00:00
"""Helper method to return a message to the caller."""
2015-01-30 16:26:06 +00:00
self.write_json({'message': message}, status_code=status_code)
2013-09-28 18:09:36 +00:00
2015-01-30 16:26:06 +00:00
def write_json(self, data=None, status_code=HTTP_OK, location=None):
2016-03-07 17:49:31 +00:00
"""Helper method to return JSON to the caller."""
json_data = json.dumps(data, indent=4, sort_keys=True,
cls=rem.JSONEncoder).encode('UTF-8')
2013-10-29 07:22:38 +00:00
self.send_response(status_code)
2013-11-01 18:34:43 +00:00
if location:
self.send_header('Location', location)
self.set_session_cookie_header()
self.write_content(json_data, CONTENT_TYPE_JSON)
2015-01-30 16:26:06 +00:00
def write_text(self, message, status_code=HTTP_OK):
2016-03-07 17:49:31 +00:00
"""Helper method to return a text message to the caller."""
msg_data = message.encode('UTF-8')
self.send_response(status_code)
self.set_session_cookie_header()
self.write_content(msg_data, CONTENT_TYPE_TEXT_PLAIN)
def write_file(self, path, cache_headers=True):
2016-03-08 16:55:57 +00:00
"""Return a file to the user."""
2015-01-30 16:26:06 +00:00
try:
with open(path, 'rb') as inp:
self.write_file_pointer(self.guess_type(path), inp,
cache_headers)
2015-01-30 16:26:06 +00:00
except IOError:
self.send_response(HTTP_NOT_FOUND)
self.end_headers()
_LOGGER.exception("Unable to serve %s", path)
def write_file_pointer(self, content_type, inp, cache_headers=True):
2016-03-08 16:55:57 +00:00
"""Helper function to write a file pointer to the user."""
2015-01-30 16:26:06 +00:00
self.send_response(HTTP_OK)
if cache_headers:
self.set_cache_header()
self.set_session_cookie_header()
2015-01-30 16:26:06 +00:00
self.write_content(inp.read(), content_type)
def write_content(self, content, content_type=None):
"""Helper method to write content bytes to output stream."""
if content_type is not None:
self.send_header(HTTP_HEADER_CONTENT_TYPE, content_type)
if 'gzip' in self.headers.get(HTTP_HEADER_ACCEPT_ENCODING, ''):
content = gzip.compress(content)
2015-01-30 16:26:06 +00:00
self.send_header(HTTP_HEADER_CONTENT_ENCODING, "gzip")
self.send_header(HTTP_HEADER_VARY, HTTP_HEADER_ACCEPT_ENCODING)
2015-01-30 16:26:06 +00:00
self.send_header(HTTP_HEADER_CONTENT_LENGTH, str(len(content)))
2015-01-30 16:26:06 +00:00
self.end_headers()
if self.command == 'HEAD':
return
self.wfile.write(content)
2015-03-04 05:15:15 +00:00
def set_cache_header(self):
2016-03-07 17:49:31 +00:00
"""Add cache headers if not in development."""
2015-11-29 06:14:40 +00:00
if self.server.development:
return
# 1 year in seconds
cache_time = 365 * 86400
2015-03-04 05:15:15 +00:00
2015-11-29 06:14:40 +00:00
self.send_header(
HTTP_HEADER_CACHE_CONTROL,
"public, max-age={}".format(cache_time))
self.send_header(
HTTP_HEADER_EXPIRES,
self.date_time_string(time.time()+cache_time))
def set_session_cookie_header(self):
2016-03-07 17:49:31 +00:00
"""Add the header for the session cookie and return session ID."""
if not self.authenticated:
2015-12-15 07:20:43 +00:00
return None
2015-11-29 06:14:40 +00:00
session_id = self.get_cookie_session_id()
2015-11-29 06:14:40 +00:00
if session_id is not None:
self.server.sessions.extend_validation(session_id)
2015-12-15 07:20:43 +00:00
return session_id
self.send_header(
'Set-Cookie',
'{}={}'.format(SESSION_KEY, self.server.sessions.create())
)
2015-11-29 06:14:40 +00:00
return session_id
def verify_session(self):
2016-03-07 17:49:31 +00:00
"""Verify that we are in a valid session."""
return self.get_cookie_session_id() is not None
def get_cookie_session_id(self):
2016-03-08 16:55:57 +00:00
"""Extract the current session ID from the cookie.
Return None if not set or invalid.
"""
if 'Cookie' not in self.headers:
return None
cookie = cookies.SimpleCookie()
try:
cookie.load(self.headers["Cookie"])
except cookies.CookieError:
return None
morsel = cookie.get(SESSION_KEY)
if morsel is None:
return None
2015-11-29 06:14:40 +00:00
session_id = cookie[SESSION_KEY].value
if self.server.sessions.is_valid(session_id):
return session_id
2015-11-29 06:14:40 +00:00
return None
def destroy_session(self):
2016-03-08 16:55:57 +00:00
"""Destroy the session."""
2015-11-29 06:14:40 +00:00
session_id = self.get_cookie_session_id()
2015-11-29 06:14:40 +00:00
if session_id is None:
return
self.send_header('Set-Cookie', '')
2015-11-29 06:14:40 +00:00
self.server.sessions.destroy(session_id)
def session_valid_time():
2016-03-07 17:49:31 +00:00
"""Time till when a session will be valid."""
return date_util.utcnow() + timedelta(seconds=SESSION_TIMEOUT_SECONDS)
class SessionStore(object):
2016-03-07 17:49:31 +00:00
"""Responsible for storing and retrieving HTTP sessions."""
2016-03-08 16:55:57 +00:00
2015-12-15 07:20:43 +00:00
def __init__(self):
2016-03-07 17:49:31 +00:00
"""Setup the session store."""
self._sessions = {}
2015-12-15 07:20:43 +00:00
self._lock = threading.RLock()
@util.Throttle(SESSION_CLEAR_INTERVAL)
def _remove_expired(self):
2016-03-07 17:49:31 +00:00
"""Remove any expired sessions."""
now = date_util.utcnow()
for key in [key for key, valid_time in self._sessions.items()
if valid_time < now]:
self._sessions.pop(key)
def is_valid(self, key):
2016-03-07 17:49:31 +00:00
"""Return True if a valid session is given."""
2015-12-15 07:20:43 +00:00
with self._lock:
self._remove_expired()
return (key in self._sessions and
self._sessions[key] > date_util.utcnow())
def extend_validation(self, key):
2016-03-07 17:49:31 +00:00
"""Extend a session validation time."""
2015-12-15 07:20:43 +00:00
with self._lock:
if key not in self._sessions:
return
self._sessions[key] = session_valid_time()
def destroy(self, key):
2016-03-07 17:49:31 +00:00
"""Destroy a session by key."""
2015-12-15 07:20:43 +00:00
with self._lock:
self._sessions.pop(key, None)
def create(self):
2016-03-08 16:55:57 +00:00
"""Create a new session."""
2015-12-15 07:20:43 +00:00
with self._lock:
session_id = util.get_random_string(20)
while session_id in self._sessions:
session_id = util.get_random_string(20)
self._sessions[session_id] = session_valid_time()
return session_id