Fix chunk streaming (#22730)

* Fix chunk streaming

* Cleanup a print

* Better error handling

* Fix import order
This commit is contained in:
Pascal Vizeli 2019-04-05 08:41:13 +02:00 committed by Paulus Schoutsen
parent 82a1c0d0e8
commit 71e120ce97
2 changed files with 12 additions and 11 deletions

View File

@ -85,7 +85,6 @@ class HassIOView(HomeAssistantView):
"http://{}/{}".format(self._host, path), data=data, "http://{}/{}".format(self._host, path), data=data,
headers=headers, timeout=read_timeout headers=headers, timeout=read_timeout
) )
print(client.headers)
# Simple request # Simple request
if int(client.headers.get(CONTENT_LENGTH, 0)) < 4194000: if int(client.headers.get(CONTENT_LENGTH, 0)) < 4194000:

View File

@ -1,21 +1,23 @@
"""Hass.io Add-on ingress service.""" """Hass.io Add-on ingress service."""
import asyncio import asyncio
from ipaddress import ip_address import logging
import os import os
from ipaddress import ip_address
from typing import Dict, Union from typing import Dict, Union
import aiohttp import aiohttp
from aiohttp import web from aiohttp import hdrs, web
from aiohttp import hdrs
from aiohttp.web_exceptions import HTTPBadGateway from aiohttp.web_exceptions import HTTPBadGateway
from multidict import CIMultiDict from multidict import CIMultiDict
from homeassistant.core import callback
from homeassistant.components.http import HomeAssistantView from homeassistant.components.http import HomeAssistantView
from homeassistant.core import callback
from homeassistant.helpers.typing import HomeAssistantType from homeassistant.helpers.typing import HomeAssistantType
from .const import X_HASSIO, X_INGRESS_PATH from .const import X_HASSIO, X_INGRESS_PATH
_LOGGER = logging.getLogger(__name__)
@callback @callback
def async_setup_ingress(hass: HomeAssistantType, host: str): def async_setup_ingress(hass: HomeAssistantType, host: str):
@ -54,8 +56,8 @@ class HassIOIngress(HomeAssistantView):
# Request # Request
return await self._handle_request(request, token, path) return await self._handle_request(request, token, path)
except aiohttp.ClientError: except aiohttp.ClientError as err:
pass _LOGGER.debug("Ingress error with %s / %s: %s", token, path, err)
raise HTTPBadGateway() from None raise HTTPBadGateway() from None
@ -126,11 +128,11 @@ class HassIOIngress(HomeAssistantView):
try: try:
await response.prepare(request) await response.prepare(request)
async for data in result.content: async for data in result.content.iter_chunked(4096):
await response.write(data) await response.write(data)
except (aiohttp.ClientError, aiohttp.ClientPayloadError): except (aiohttp.ClientError, aiohttp.ClientPayloadError) as err:
pass _LOGGER.debug("Stream error %s / %s: %s", token, path, err)
return response return response
@ -183,7 +185,7 @@ def _response_header(response: aiohttp.ClientResponse) -> Dict[str, str]:
for name, value in response.headers.items(): for name, value in response.headers.items():
if name in (hdrs.TRANSFER_ENCODING, hdrs.CONTENT_LENGTH, if name in (hdrs.TRANSFER_ENCODING, hdrs.CONTENT_LENGTH,
hdrs.CONTENT_TYPE): hdrs.CONTENT_TYPE, hdrs.CONTENT_ENCODING):
continue continue
headers[name] = value headers[name] = value