From 02089e2bb801d560e2bf13c6e13d3499b6c950f2 Mon Sep 17 00:00:00 2001 From: Jake Ichikawa Date: Fri, 19 May 2023 09:24:12 -0700 Subject: [PATCH] Add request_body_size_within_limit method. --- tabpy/tabpy_server/handlers/base_handler.py | 25 +++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/tabpy/tabpy_server/handlers/base_handler.py b/tabpy/tabpy_server/handlers/base_handler.py index 3d522b03..0a418a0c 100644 --- a/tabpy/tabpy_server/handlers/base_handler.py +++ b/tabpy/tabpy_server/handlers/base_handler.py @@ -127,6 +127,7 @@ def initialize(self, app): self.username = None self.password = None self.eval_timeout = self.settings[SettingsParameters.EvaluateTimeout] + self.max_request_size = app.max_request_size self.logger = ContextLoggerWrapper(self.request) self.logger.enable_context_logging( @@ -442,3 +443,27 @@ def fail_with_auth_error(self): info="Not Acceptable", log_message="Username or password provided when authentication not available.", ) + + def request_body_size_within_limit(self): + """ + Determines if the request body size is within the specified limit. + + Returns + ------- + bool + True if the request body size is within the limit, False otherwise. + """ + headers = self.request.headers + + if "Content-Length" in headers: + content_length = int(headers["Content-Length"]) + + if content_length > self.max_request_size: + self.error_out( + 413, + info="Request Entity Too Large", + log_message="Request body size exceeds the specified limit.", + ) + return False + + return True