Browse Source

Add configurable request size limit

master
JustAnotherArchivist 2 years ago
parent
commit
3cc32cbc41
1 changed files with 9 additions and 6 deletions
  1. +9
    -6
      http2irc.py

+ 9
- 6
http2irc.py View File

@@ -146,12 +146,14 @@ class Config(dict):
if not is_valid_pem(obj['irc']['certkeyfile'], False):
raise InvalidConfig('Invalid certificate key file: not a valid PEM key')
if 'web' in obj:
if any(x not in ('host', 'port') for x in obj['web']):
if any(x not in ('host', 'port', 'maxrequestsize') for x in obj['web']):
raise InvalidConfig('Unknown key found in web section')
if 'host' in obj['web'] and not isinstance(obj['web']['host'], str): #TODO: Check whether it's a valid hostname (must resolve I guess?)
raise InvalidConfig('Invalid web hostname')
if 'port' in obj['web'] and (not isinstance(obj['web']['port'], int) or not 1 <= obj['web']['port'] <= 65535):
raise InvalidConfig('Invalid web port')
if 'maxrequestsize' in obj['web'] and not isinstance(obj['web']['maxrequestsize'], int) or obj['web']['maxrequestsize'] <= 0:
raise InvalidConfig('Invalid web maxrequestsize')
if 'maps' in obj:
seenWebPaths = {}
for key, map_ in obj['maps'].items():
@@ -223,7 +225,7 @@ class Config(dict):
finalObj = {
'logging': {'level': 'INFO', 'format': '{asctime} {levelname} {name} {message}'},
'irc': {'host': 'irc.hackint.org', 'port': 6697, 'ssl': 'yes', 'family': 0, 'nick': 'h2ibot', 'real': 'I am an http2irc bot.', 'certfile': None, 'certkeyfile': None},
'web': {'host': '127.0.0.1', 'port': 8080},
'web': {'host': '127.0.0.1', 'port': 8080, 'maxrequestsize': 1048576},
'maps': {}
}

@@ -969,12 +971,11 @@ class WebServer:
# '/path' => ('#channel', postauth, getauth, module, moduleargs, overlongmode)
# {post,get}auth are either False (access denied) or the HTTP header value for basic auth

self._app = aiohttp.web.Application()
self._app.add_routes([
self._routes = [
aiohttp.web.get('/status', self.get_status),
aiohttp.web.post('/{path:.+}', functools.partial(self._path_request, func = self.post)),
aiohttp.web.get('/{path:.+}', functools.partial(self._path_request, func = self.get)),
])
]

self.update_config(config)
self._configChanged = asyncio.Event()
@@ -997,7 +998,9 @@ class WebServer:
async def run(self, stopEvent):
self.stopEvent = stopEvent
while True:
runner = aiohttp.web.AppRunner(self._app)
app = aiohttp.web.Application(client_max_size = self.config['web']['maxrequestsize'])
app.add_routes(self._routes)
runner = aiohttp.web.AppRunner(app)
await runner.setup()
site = aiohttp.web.TCPSite(runner, self.config['web']['host'], self.config['web']['port'])
await site.start()


Loading…
Cancel
Save