def _handle_request(self, method):  # pylint: disable=too-many-branches
        """Perform some common checks and call appropriate method."""
        url = urlparse(self.path)

        # 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()}

        # Did we get post input ?
        content_length = int(self.headers.get(HTTP_HEADER_CONTENT_LENGTH, 0))

        if content_length:
            body_content = self.rfile.read(content_length).decode("UTF-8")

            try:
                data.update(json.loads(body_content))
            except (TypeError, ValueError):
                # TypeError if JSON object is not a dict
                # ValueError if we could not parse JSON
                _LOGGER.exception(
                    "Exception parsing JSON: %s", body_content)
                self.write_json_message(
                    "Error parsing JSON", HTTP_UNPROCESSABLE_ENTITY)
                return

        if self.verify_session():
            # The user has a valid session already
            self.authenticated = True
        elif self.server.api_password is None:
            # No password is set, so everyone is authenticated
            self.authenticated = True
        elif hmac.compare_digest(self.headers.get(HTTP_HEADER_HA_AUTH, ''),
                                 self.server.api_password):
            # A valid auth header has been set
            self.authenticated = True
        elif hmac.compare_digest(data.get(DATA_API_PASSWORD, ''),
                                 self.server.api_password):
            # A valid password has been specified
            self.authenticated = True
        else:
            self.authenticated = False

        # we really shouldn't need to forward the password from here
        if url.path not in [URL_ROOT, URL_API_EVENT_FORWARD]:
            data.pop(DATA_API_PASSWORD, None)

        if '_METHOD' in data:
            method = data.pop('_METHOD')

        # Var to keep track if we found a path that matched a handler but
        # the method was different
        path_matched_but_not_method = False

        # Var to hold the handler for this path and method if found
        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:
            # we either do string-comparison or regular expression matching
            # pylint: disable=maybe-no-member
            if isinstance(t_path, str):
                path_match = url.path == t_path
            else:
                path_match = t_path.match(url.path)

            if path_match and method == t_method:
                # Call the method
                handle_request_method = t_handler
                require_auth = t_auth
                break

            elif path_match:
                path_matched_but_not_method = True

        # Did we find a handler for the incoming request?
        if handle_request_method:
            # For some calls we need a valid password
            msg = "API password missing or incorrect."
            if require_auth and not self.authenticated:
                self.write_json_message(msg, HTTP_UNAUTHORIZED)
                _LOGGER.warning('%s Source IP: %s',
                                msg,
                                self.client_address[0])
                return

            handle_request_method(self, path_match, data)

        elif path_matched_but_not_method:
            self.send_response(HTTP_METHOD_NOT_ALLOWED)
            self.end_headers()

        else:
            self.send_response(HTTP_NOT_FOUND)
            self.end_headers()
dataset/ETHPy150Open home-assistant/home-assistant/homeassistant/components/http.py/RequestHandler._handle_request/original