Skip to content

Commit

Permalink
remove guzzlehttp/psr7 package
Browse files Browse the repository at this point in the history
  • Loading branch information
recca0120 committed Aug 21, 2022
1 parent a5ab1b1 commit 21fc348
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 5 deletions.
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
"type": "library",
"require": {
"ext-json": "*",
"guzzlehttp/promises": "^1.3.1|^1.4",
"guzzlehttp/psr7": "^1.8|^2.4"
"guzzlehttp/promises": "^1.3.1|^1.4"
},
"require-dev": {
"mockery/mockery": "^0.9|^1.3",
Expand Down
84 changes: 81 additions & 3 deletions src/ResponseIdentifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

namespace Recca0120\LaravelParallel;

use GuzzleHttp\Psr7\Message;
use Illuminate\Http\Response;
use InvalidArgumentException;
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;

class ResponseIdentifier
{
private const HEADER_REGEX = "(^([^()<>@,;:\\\"/[\]?={}\x01-\x20\x7F]++):[ \t]*+((?:[ \t]*+[\x21-\x7E\x80-\xFF]++)*+)[ \t]*+\r?\n)m";
private const HEADER_FOLD_REGEX = "(\r?\n[ \t]++)";

/**
* @var string
*/
Expand Down Expand Up @@ -40,9 +43,84 @@ public static function fromSymfonyResponse(SymfonyResponse $response): self

public static function fromMessage(string $message): self
{
$response = Message::parseResponse(PreventEcho::prevent($message));
$data = self::parseMessage(PreventEcho::prevent($message));
// According to https://tools.ietf.org/html/rfc7230#section-3.1.2 the space
// between status-code and reason-phrase is required. But browsers accept
// responses without space and reason as well.
if (! preg_match('/^HTTP\/.* [0-9]{3}( .*|$)/', $data['start-line'])) {
throw new InvalidArgumentException('Invalid response string: '.$data['start-line']);
}
$parts = explode(' ', $data['start-line'], 3);

return new self($data['body'], (int) $parts[1], $data['headers']);
}

/**
* Parses an HTTP message into an associative array.
*
* The array contains the "start-line" key containing the start line of
* the message, "headers" key containing an associative array of header
* array values, and a "body" key containing the body of the message.
*
* @link https://github.com/guzzle/psr7/blob/2.4.0/src/Message.php#L114-L167
*
* @license https://github.com/guzzle/psr7/blob/2.4.0/LICENSE
*
* @param string $message HTTP request or response to parse.
*/
public static function parseMessage(string $message): array
{
if (! $message) {
throw new InvalidArgumentException('Invalid message');
}

$message = ltrim($message, "\r\n");

$messageParts = preg_split("/\r?\n\r?\n/", $message, 2);

if ($messageParts === false || count($messageParts) !== 2) {
throw new InvalidArgumentException('Invalid message: Missing header delimiter');
}

[$rawHeaders, $body] = $messageParts;
$rawHeaders .= "\r\n"; // Put back the delimiter we split previously
$headerParts = preg_split("/\r?\n/", $rawHeaders, 2);

if ($headerParts === false || count($headerParts) !== 2) {
throw new InvalidArgumentException('Invalid message: Missing status line');
}

[$startLine, $rawHeaders] = $headerParts;

if (preg_match("/(?:^HTTP\/|^[A-Z]+ \S+ HTTP\/)(\d+(?:\.\d+)?)/i", $startLine, $matches) && $matches[1] === '1.0') {
// Header folding is deprecated for HTTP/1.1, but allowed in HTTP/1.0
$rawHeaders = preg_replace(self::HEADER_FOLD_REGEX, ' ', $rawHeaders);
}

/** @var array[] $headerLines */
$count = preg_match_all(self::HEADER_REGEX, $rawHeaders, $headerLines, PREG_SET_ORDER);

// If these aren't the same, then one line didn't match and there's an invalid header.
if ($count !== substr_count($rawHeaders, "\n")) {
// Folding is deprecated, see https://tools.ietf.org/html/rfc7230#section-3.2.4
if (preg_match(self::HEADER_FOLD_REGEX, $rawHeaders)) {
throw new InvalidArgumentException('Invalid header syntax: Obsolete line folding');
}

throw new InvalidArgumentException('Invalid header syntax');
}

$headers = [];

foreach ($headerLines as $headerLine) {
$headers[$headerLine[1]][] = $headerLine[2];
}

return new self((string) $response->getBody(), $response->getStatusCode(), $response->getHeaders());
return [
'start-line' => $startLine,
'headers' => $headers,
'body' => $body,
];
}

public function toMessage(): string
Expand Down

0 comments on commit 21fc348

Please sign in to comment.