From 287de5808a6f98b493988914f62ba737fcbbcb22 Mon Sep 17 00:00:00 2001 From: Fabiana Romagnoli Date: Fri, 26 Apr 2024 12:44:20 +0200 Subject: [PATCH] Make body size limit configurable --- .env.dist | 2 ++ services.yml | 2 ++ services_test.yml | 1 + src/Service/HttpProducersServer.php | 15 +++++++++++++-- 4 files changed, 18 insertions(+), 2 deletions(-) diff --git a/.env.dist b/.env.dist index 097da86..f52c69d 100644 --- a/.env.dist +++ b/.env.dist @@ -4,6 +4,8 @@ ESB_BEANSTALKD_URL=tcp://beanstalkd:11300 ESB_CONSOLE_PORT=8080 # ESB_HTTP_SERVER is the port for the ESB's HTTP request producers ESB_HTTP_SERVER_PORT=34981 +#ESB_HTTP_SERVER_MAX_BODY_SIZE is an optional parameter. You shoud set it if you need a value larger then default, that is 10485760 +ESB_HTTP_SERVER_MAX_BODY_SIZE=10485760 # MAILHOG_WEB_PORT_HOST is the host's port binding of the MailHog 8025 web UI server port MAILHOG_WEB_PORT_HOST=8025 diff --git a/services.yml b/services.yml index 3ecd9df..39d8b97 100644 --- a/services.yml +++ b/services.yml @@ -15,6 +15,8 @@ services: Webgriffe\Esb\Service\HttpProducersServer: arguments: $port: '%http_server_port%' + $logger: '@Psr\Log\LoggerInterface' + $maxBodySize: '%http_server_max_body_size%' Monolog\Handler\StreamHandler: class: \Monolog\Handler\StreamHandler diff --git a/services_test.yml b/services_test.yml index 5df9c8f..adbc1a9 100644 --- a/services_test.yml +++ b/services_test.yml @@ -1,6 +1,7 @@ parameters: beanstalkd: '%env(string:ESB_BEANSTALKD_URL)%' http_server_port: '%env(int:ESB_HTTP_SERVER_PORT)%' + http_server_max_body_size: '%env(ESB_HTTP_SERVER_MAX_BODY_SIZE)%' logger_mail_to: "toemail@address.com" logger_mail_from: "From Name " console_port: '%env(int:ESB_CONSOLE_PORT)%' diff --git a/src/Service/HttpProducersServer.php b/src/Service/HttpProducersServer.php index a18f7a1..e506868 100644 --- a/src/Service/HttpProducersServer.php +++ b/src/Service/HttpProducersServer.php @@ -7,6 +7,7 @@ use function Amp\call; use Amp\CallableMaker; use Amp\Http\Server\HttpServer; +use Amp\Http\Server\Options; use Amp\Http\Server\Request; use Amp\Http\Server\RequestHandler\CallableRequestHandler; use Amp\Http\Server\Response; @@ -42,10 +43,16 @@ class HttpProducersServer */ private $httpServer; - public function __construct(int $port, LoggerInterface $logger) + /** + * @var int|null + */ + private $maxBodySize; + + public function __construct(int $port, LoggerInterface $logger, int $maxBodySize) { $this->port = $port; $this->logger = $logger; + $this->maxBodySize = $maxBodySize; } /** @@ -59,10 +66,14 @@ public function start(): Promise Socket\listen("[::]:{$this->port}"), ]; + $options = new Options(); + $options->withBodySizeLimit($this->maxBodySize); + $this->httpServer = new HttpServer( $sockets, new CallableRequestHandler($this->callableFromInstanceMethod('requestHandler')), - new NullLogger() + new NullLogger(), + $options ); yield $this->httpServer->start();