import logging
import yaml
+import time
logging.basicConfig(level=logging.INFO)
mimetypes.add_type('text/gemini', '.gmi')
self.logdata = logdata
-def recv_req(conn: SSL.Connection, timeout=.1):
+def recv_req(conn: SSL.Connection, timeout=.5):
data = b''
+ start = time.time()
while True:
+ # This prevents "slow loris" types of timeouts
+ if time.time() > start + timeout:
+ raise CBSException(59, 'Timeout while waiting for URL')
ready = select.select([conn], [], [], timeout)
if ready[0]:
- data += conn.recv(4096)
+ data += conn.recv(1024)
if b'\r\n' in data:
lines = data.splitlines()
if len(lines) > 1:
- logging.warning('Discarding data after URL line of request: {}'.format(data))
+ logging.warning(f'Discarding data after URL line of request: {data}')
+ if len(lines[0]) > 1024:
+ raise CBSException(59, 'URL too long', lines[0])
try:
req = lines[0].decode('ascii')
except UnicodeDecodeError:
- raise CBSException(59, 'Non-ascii URL', data)
+ raise CBSException(59, 'Non-ascii URL', lines[0])
return req
else:
raise CBSException(59, 'Timeout while waiting for URL')