Skip to content

Commit

Permalink
Refactor request parser to simply emitting request headers
Browse files Browse the repository at this point in the history
  • Loading branch information
clue committed Jun 16, 2019
1 parent 29741b0 commit 2fe19c4
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/Io/RequestHeaderParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace React\Http\Io;

use Evenement\EventEmitter;
use Psr\Http\Message\ServerRequestInterface;
use RingCentral\Psr7 as g7;
use Exception;

Expand Down Expand Up @@ -44,21 +45,24 @@ public function feed($data)

if (false !== $endOfHeader) {
try {
$this->parseAndEmitRequest($endOfHeader);
$request = $this->parseRequest((string)\substr($this->buffer, 0, $endOfHeader));
} catch (Exception $exception) {
$this->emit('error', array($exception));
$this->removeAllListeners();
return;
}

$bodyBuffer = isset($this->buffer[$endOfHeader + 4]) ? \substr($this->buffer, $endOfHeader + 4) : '';
$this->emit('headers', array($request, $bodyBuffer));
$this->removeAllListeners();
}
}

private function parseAndEmitRequest($endOfHeader)
{
$request = $this->parseRequest((string)\substr($this->buffer, 0, $endOfHeader));
$bodyBuffer = isset($this->buffer[$endOfHeader + 4]) ? \substr($this->buffer, $endOfHeader + 4) : '';
$this->emit('headers', array($request, $bodyBuffer));
}

/**
* @param string $headers buffer string containing request headers only
* @throws \InvalidArgumentException
* @return ServerRequestInterface
*/
private function parseRequest($headers)
{
// additional, stricter safe-guard for request line
Expand Down

0 comments on commit 2fe19c4

Please sign in to comment.