AIOHTTP

Extensions and helpers for building AIOHTTP-based applications.

Middlewares

async kw.platform.aiohttp.middlewares.user_agent_middleware(request, handler)[source]

Validate client’s User-Agent header and modify response based on that.

If the User-Agent header is invalid, there are three possible outcomes:

  1. The current time is less then settings.KIWI_REQUESTS_SLOWDOWN_DATETIME, do nothing in this case.

  2. The current time is less then settings.KIWI_REQUESTS_RESTRICT_DATETIME, slow down the response twice the normal responce time.

  3. The current time is more then settings.KIWI_REQUESTS_RESTRICT_DATETIME, refuse the request, return HTTP 400 to the client.

Usage:

from aiohttp import web

from kw.platform.aiohttp.middlewares import user_agent_middleware

app = web.Application(middlewares=[user_agent_middleware])

Warning

The middleware slows down requests by calling asyncio.sleep() (in the time frame when requests with invalid user-agent are being delayed). This can increase busyness and overload a service.