Skip to content

Commit

Permalink
Replace repeated regex with constant
Browse files Browse the repository at this point in the history
  • Loading branch information
trowski committed Dec 10, 2024
1 parent 97a40cc commit 955b246
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/Driver/Http1Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ public function handleClient(
throw new ClientException($this->client, "Bad Request: multiple host headers", HttpStatus::BAD_REQUEST);
}

if (!\preg_match("#^([A-Z\d._\-]+|\[[\d:]+])(?::([1-9]\d*))?$#i", $headers["host"][0], $matches)) {
if (!\preg_match(self::HOST_HEADER_REGEX, $headers["host"][0], $matches)) {
throw new ClientException($this->client, "Bad Request: invalid host header", HttpStatus::BAD_REQUEST);
}

Expand Down Expand Up @@ -317,7 +317,7 @@ public function handleClient(
);
}

if (!\preg_match("#^([A-Z\d._\-]+|\[[\d:]+]):([1-9]\d*)$#i", $target, $matches)) {
if (!\preg_match(self::HOST_HEADER_REGEX, $target, $matches)) {
throw new ClientException(
$this->client,
"Bad Request: invalid connect target",
Expand Down
2 changes: 1 addition & 1 deletion src/Driver/Http2Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -967,7 +967,7 @@ public function handleHeaders(int $streamId, array $pseudo, array $headers, bool
[':method' => $method, ':path' => $target, ':scheme' => $scheme, ':authority' => $host] = $pseudo;
$query = null;

if (!\preg_match("#^([A-Z\d._\-]+|\[[\d:]+])(?::([1-9]\d*))?$#i", $host, $matches)) {
if (!\preg_match(self::HOST_HEADER_REGEX, $host, $matches)) {
throw new Http2StreamException(
"Invalid authority (host) name",
$streamId,
Expand Down
2 changes: 2 additions & 0 deletions src/Driver/Internal/AbstractHttpDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
/** @internal */
abstract class AbstractHttpDriver implements HttpDriver
{
protected const HOST_HEADER_REGEX = /** @lang RegExp */ '#^([A-Z\d._\-]+|\[[\d:]+])(?::([1-9]\d*))?$#i';

private static ?TimeoutQueue $timeoutQueue = null;

final protected static function getTimeoutQueue(): TimeoutQueue
Expand Down

0 comments on commit 955b246

Please sign in to comment.