Skip to content

Commit

Permalink
Add --http-headers CLI option for specifying custom HTTP response header
Browse files Browse the repository at this point in the history
Regarding gzip example:
- browsers need content-type: gzip and don't need transfer-encoding
- curl does not need content-type: gzip, but  needs transfer-encoding

specifying both should result in maximum compatbility
  • Loading branch information
ostrolucky committed Jan 26, 2019
1 parent f5ca3dd commit 1512083
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 8 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,17 @@ curl 127.0.0.1:1337
</p>
</details>
<details><summary>Share application logs in realtime</summary>
<details><summary>Tail application logs in realtime</summary>
<p>
```bash
# Server
$ tail -f project/var/log/*.log|stdinho 0.0.0.0:1337
# Client
$ curl 127.0.0.1:1337

# Bonus: gzip transfer encoding (server)
$ tail -f project/var/*.log|gzip -c|stdinho 0.0.0.0:1337 --http-headers='["Content-Type: text/plain", "Content-Encoding: gzip", "Transfer-Encoding: gzip"]'
```
</p>
Expand Down
Empty file added bin/composer.json
Empty file.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"require": {
"php": ">=7.1",
"ext-fileinfo": "*",
"ext-json": "*",
"amphp/amp": "^2.0",
"amphp/byte-stream": "^1.2.5",
"amphp/socket": "^0.10",
Expand Down
25 changes: 22 additions & 3 deletions src/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ class Command extends \Symfony\Component\Console\Command\Command
* @var bool
*/
private $hasStdin = false;
/**
* @var string[]
*/
private $customHttpHeaders = [];

protected function configure(): void
{
Expand All @@ -33,14 +37,29 @@ protected function configure(): void
InputOption::VALUE_REQUIRED,
'File path. If no stdin is provided, this is used as source. Otherwise, it is target where stdin is copied to'
)
->setDescription('Open HTTP portal to your standard input stream')
->addOption(
'http-headers',
null,
InputOption::VALUE_REQUIRED,
'Custom HTTP headers to append to response (in JSON format). Eg. --http-headers=\'["Content-encoding: gzip"]\'',
'{}'
)
->setDescription('Turn any STDIN/STDOUT into HTTP server')
;
}

protected function initialize(InputInterface $input, OutputInterface $output): void
{
$this->hasStdin = ftell(STDIN) !== false && !stream_isatty(STDIN);
$filePath = $input->getOption('file');
$json = $input->getOption('http-headers');
$this->hasStdin = ftell(STDIN) !== false && !stream_isatty(STDIN);
$this->customHttpHeaders = @json_decode($json, true);

if (!is_array($this->customHttpHeaders)) {
throw new InvalidOptionException(
sprintf('Invalid JSON "%s" has been used in --http-headers option', $json)
);
}

if ($this->hasStdin) {
return;
Expand Down Expand Up @@ -73,7 +92,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
;

$bufferHandler = asyncCoroutine($bufferer);
$clientHandler = asyncCoroutine(new Responder($logger, $bufferer, $output));
$clientHandler = asyncCoroutine(new Responder($logger, $bufferer, $output, $this->customHttpHeaders));

Loop::run(function () use ($addressPort, $clientHandler, $logger, $firstSection, $bufferHandler) {
$bufferHandler();
Expand Down
18 changes: 15 additions & 3 deletions src/Responder.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,24 @@ class Responder
* @var ConsoleOutput
*/
private $consoleOutput;
/**
* @var string[]
*/
private $customHttpHeaders = [];

public function __construct(LoggerInterface $logger, BuffererInterface $bufferer, ConsoleOutput $consoleOutput)
{
/**
* @param string[] $customHttpHeaders
*/
public function __construct(
LoggerInterface $logger,
BuffererInterface $bufferer,
ConsoleOutput $consoleOutput,
array $customHttpHeaders
) {
$this->logger = $logger;
$this->bufferer = $bufferer;
$this->consoleOutput = $consoleOutput;
$this->customHttpHeaders = $customHttpHeaders;
}

public function __invoke(Socket $socket): \Generator
Expand Down Expand Up @@ -59,7 +71,7 @@ public function __invoke(Socket $socket): \Generator
$handle = new ResourceInputStream(fopen($this->bufferer->getFilePath(), 'rb'));

try {
yield $socket->write(implode("\r\n", $header)."\r\n\r\n");
yield $socket->write(implode("\r\n", array_merge($header, $this->customHttpHeaders))."\r\n\r\n");

while (true) {
$buffererProgress = $this->bufferer->getCurrentProgress();
Expand Down
3 changes: 2 additions & 1 deletion tests/ResponderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ public function testResponderHandlesClientAbruptDisconnect(): void
$responder = new Responder(
$logger = new TestLogger(),
new ResolvedBufferer(__FILE__),
$this->createMock(ConsoleOutput::class)
$this->createMock(ConsoleOutput::class),
[]
);

$socket = $this->getMockBuilder(ClientSocket::class)
Expand Down

0 comments on commit 1512083

Please sign in to comment.