From 71e120ce971373afd466c9027b11e55b2c3b6f73 Mon Sep 17 00:00:00 2001 From: Pascal Vizeli Date: Fri, 5 Apr 2019 08:41:13 +0200 Subject: [PATCH] Fix chunk streaming (#22730) * Fix chunk streaming * Cleanup a print * Better error handling * Fix import order --- homeassistant/components/hassio/http.py | 1 - homeassistant/components/hassio/ingress.py | 22 ++++++++++++---------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/homeassistant/components/hassio/http.py b/homeassistant/components/hassio/http.py index 7284004d72f..a798d312c25 100644 --- a/homeassistant/components/hassio/http.py +++ b/homeassistant/components/hassio/http.py @@ -85,7 +85,6 @@ class HassIOView(HomeAssistantView): "http://{}/{}".format(self._host, path), data=data, headers=headers, timeout=read_timeout ) - print(client.headers) # Simple request if int(client.headers.get(CONTENT_LENGTH, 0)) < 4194000: diff --git a/homeassistant/components/hassio/ingress.py b/homeassistant/components/hassio/ingress.py index 49e949c5789..27a7f05ec14 100644 --- a/homeassistant/components/hassio/ingress.py +++ b/homeassistant/components/hassio/ingress.py @@ -1,21 +1,23 @@ """Hass.io Add-on ingress service.""" import asyncio -from ipaddress import ip_address +import logging import os +from ipaddress import ip_address from typing import Dict, Union import aiohttp -from aiohttp import web -from aiohttp import hdrs +from aiohttp import hdrs, web from aiohttp.web_exceptions import HTTPBadGateway from multidict import CIMultiDict -from homeassistant.core import callback from homeassistant.components.http import HomeAssistantView +from homeassistant.core import callback from homeassistant.helpers.typing import HomeAssistantType from .const import X_HASSIO, X_INGRESS_PATH +_LOGGER = logging.getLogger(__name__) + @callback def async_setup_ingress(hass: HomeAssistantType, host: str): @@ -54,8 +56,8 @@ class HassIOIngress(HomeAssistantView): # Request return await self._handle_request(request, token, path) - except aiohttp.ClientError: - pass + except aiohttp.ClientError as err: + _LOGGER.debug("Ingress error with %s / %s: %s", token, path, err) raise HTTPBadGateway() from None @@ -126,11 +128,11 @@ class HassIOIngress(HomeAssistantView): try: await response.prepare(request) - async for data in result.content: + async for data in result.content.iter_chunked(4096): await response.write(data) - except (aiohttp.ClientError, aiohttp.ClientPayloadError): - pass + except (aiohttp.ClientError, aiohttp.ClientPayloadError) as err: + _LOGGER.debug("Stream error %s / %s: %s", token, path, err) return response @@ -183,7 +185,7 @@ def _response_header(response: aiohttp.ClientResponse) -> Dict[str, str]: for name, value in response.headers.items(): if name in (hdrs.TRANSFER_ENCODING, hdrs.CONTENT_LENGTH, - hdrs.CONTENT_TYPE): + hdrs.CONTENT_TYPE, hdrs.CONTENT_ENCODING): continue headers[name] = value