From ff2b7f9682daa39e14c1bc5ebab4f99058c2c49c Mon Sep 17 00:00:00 2001 From: Alex Root Junior Date: Mon, 27 Nov 2017 17:29:20 +0200 Subject: [PATCH] Small changes in webhook handler. --- aiogram/dispatcher/webhook.py | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/aiogram/dispatcher/webhook.py b/aiogram/dispatcher/webhook.py index 2240a9d4..80163818 100644 --- a/aiogram/dispatcher/webhook.py +++ b/aiogram/dispatcher/webhook.py @@ -109,11 +109,7 @@ class WebhookRequestHandler(web.View): :return: :class:`aiohttp.web.Response` """ - if self.request.app.get('_check_ip', False): - ip_address, accept = self.check_ip() - if not accept: - raise web.HTTPUnauthorized() - context.set_value('TELEGRAM_IP', ip_address) + self.validate_ip() context.update_state({'CALLER': WEBHOOK, WEBHOOK_CONNECTION: True, @@ -129,6 +125,14 @@ class WebhookRequestHandler(web.View): return response.get_web_response() return web.Response(text='ok') + async def get(self): + self.validate_ip() + return web.Response(text='') + + async def head(self): + self.validate_ip() + return web.Response(text='') + async def process_update(self, update): """ Need respond in less than 60 seconds in to webhook. @@ -220,6 +224,16 @@ class WebhookRequestHandler(web.View): # Not allowed and can't get client IP return None, False + def validate_ip(self): + """ + Check ip if that is needed. Raise web.HTTPUnauthorized for not allowed hosts. + """ + if self.request.app.get('_check_ip', False): + ip_address, accept = self.check_ip() + if not accept: + raise web.HTTPUnauthorized() + context.set_value('TELEGRAM_IP', ip_address) + def configure_app(dispatcher, app: web.Application, path=DEFAULT_WEB_PATH): """