@@ -44,6 +44,13 @@ protected function configure(): void
4444 'Custom HTTP headers to append to response (in JSON format). Eg. --http-headers= \'["Content-encoding: gzip"] \'' ,
4545 '[] '
4646 )
47+ ->addOption (
48+ 'connections-limit ' ,
49+ null ,
50+ InputOption::VALUE_REQUIRED ,
51+ 'Determines after how many client connections should program shut down ' ,
52+ INF
53+ )
4754 ->setDescription ('Turn any STDIN/STDOUT into HTTP server ' )
4855 ;
4956 }
@@ -81,26 +88,24 @@ protected function initialize(InputInterface $input, OutputInterface $output): v
8188 */
8289 protected function execute (InputInterface $ input , OutputInterface $ output ): int
8390 {
84- $ addressPort = $ input ->getArgument ('addressPort ' );
85- $ filePath = $ input ->getOption ('file ' );
86-
8791 $ logger = new ConsoleLogger ($ firstSection = $ output ->section ());
8892
8993 $ bufferer = $ this ->hasStdin ?
90- new PipeBufferer ($ logger , STDIN , $ filePath , $ output ->section ()) :
91- new ResolvedBufferer ($ filePath )
94+ new PipeBufferer ($ logger , STDIN , $ input -> getOption ( ' file ' ) , $ output ->section ()) :
95+ new ResolvedBufferer ($ input -> getOption ( ' file ' ) )
9296 ;
9397
9498 $ bufferHandler = asyncCoroutine ($ bufferer );
9599 $ clientHandler = asyncCoroutine (new Responder ($ logger , $ bufferer , $ output , $ this ->customHttpHeaders ));
96100
97- Loop::run (function () use ($ addressPort , $ clientHandler , $ logger , $ firstSection , $ bufferHandler ) {
101+ Loop::run (function () use ($ input , $ clientHandler , $ logger , $ firstSection , $ bufferHandler ) {
98102 $ bufferHandler ();
99- $ server = listen ($ addressPort );
103+ $ server = listen ($ input -> getArgument ( ' addressPort ' ) );
100104 $ firstSection ->writeln (
101105 "<info>Connection opened at http:// {$ server ->getAddress ()}\nPress CTRL+C to exit.</info> \n"
102106 );
103- while ($ socket = yield $ server ->accept ()) {
107+ $ connectionsLimit = $ input ->getOption ('connections-limit ' );
108+ while ($ connectionsLimit -- && ($ socket = yield $ server ->accept ())) {
104109 $ clientHandler ($ socket );
105110 }
106111 });
0 commit comments