Add timeout to integrated sever
This commit is contained in:
		
							
								
								
									
										3
									
								
								config
									
									
									
									
									
								
							
							
						
						
									
										3
									
								
								config
									
									
									
									
									
								
							| @@ -24,6 +24,9 @@ | ||||
| # File storing the PID in daemon mode | ||||
| #pid = | ||||
|  | ||||
| # Socket timeout (seconds) | ||||
| #timeout = 10 | ||||
|  | ||||
| # SSL flag, enable HTTPS protocol | ||||
| #ssl = False | ||||
|  | ||||
|   | ||||
| @@ -54,6 +54,10 @@ WELL_KNOWN_RE = re.compile(r"/\.well-known/(carddav|caldav)/?$") | ||||
|  | ||||
| class HTTPServer(wsgiref.simple_server.WSGIServer): | ||||
|     """HTTP server.""" | ||||
|  | ||||
|     # These class attributes must be set before creating instance | ||||
|     client_timeout = None | ||||
|  | ||||
|     def __init__(self, address, handler, bind_and_activate=True): | ||||
|         """Create server.""" | ||||
|         ipv6 = ":" in address[0] | ||||
| @@ -72,6 +76,13 @@ class HTTPServer(wsgiref.simple_server.WSGIServer): | ||||
|             self.server_bind() | ||||
|             self.server_activate() | ||||
|  | ||||
|     def get_request(self): | ||||
|         # Set timeout for client | ||||
|         _socket, address = super().get_request() | ||||
|         if self.client_timeout: | ||||
|             _socket.settimeout(self.client_timeout) | ||||
|         return _socket, address | ||||
|  | ||||
|  | ||||
| class HTTPSServer(HTTPServer): | ||||
|     """HTTPS server.""" | ||||
| @@ -290,8 +301,11 @@ class Application: | ||||
|         # Get content | ||||
|         content_length = int(environ.get("CONTENT_LENGTH") or 0) | ||||
|         if content_length: | ||||
|             try: | ||||
|                 content = self.decode( | ||||
|                     environ["wsgi.input"].read(content_length), environ) | ||||
|             except socket.timeout: | ||||
|                 return response(client.REQUEST_TIMEOUT) | ||||
|             self.logger.debug("Request content:\n%s" % content) | ||||
|         else: | ||||
|             content = None | ||||
|   | ||||
| @@ -170,6 +170,7 @@ def run(): | ||||
|                         name, filename, exception)) | ||||
|     else: | ||||
|         server_class = ThreadedHTTPServer | ||||
|     server_class.client_timeout = configuration.getint("server", "timeout") | ||||
|  | ||||
|     if not configuration.getboolean("server", "dns_lookup"): | ||||
|         RequestHandler.address_string = lambda self: self.client_address[0] | ||||
|   | ||||
| @@ -34,6 +34,7 @@ INITIAL_CONFIG = { | ||||
|         "hosts": "0.0.0.0:5232", | ||||
|         "daemon": "False", | ||||
|         "pid": "", | ||||
|         "timeout": "10", | ||||
|         "ssl": "False", | ||||
|         "certificate": "/etc/apache2/ssl/server.crt", | ||||
|         "key": "/etc/apache2/ssl/server.key", | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Unrud
					Unrud