Skip to content

Commit

Permalink
sunrise/http-header integration for validation
Browse files Browse the repository at this point in the history
  • Loading branch information
fenric committed Mar 5, 2019
1 parent a8150e9 commit 739fdd4
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 23 deletions.
13 changes: 8 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@
"keywords": ["fenric", "sunrise", "http-message", "rfc-7230", "psr-7", "psr-17"],
"homepage": "https://github.com/sunrise-php/http-message",
"license": "MIT",
"authors": [{
"name": "Anatoly Fenric",
"email": "[email protected]",
"homepage": "https://anatoly.fenric.ru/"
}],
"authors": [
{
"name": "Anatoly Fenric",
"email": "[email protected]",
"homepage": "https://anatoly.fenric.ru/"
}
],
"require": {
"php": "^7.1",
"psr/http-factory": "^1.0",
"psr/http-message": "^1.0",
"sunrise/http-header": "^1.0",
"sunrise/stream": "^1.0",
"sunrise/uri": "^1.0"
},
Expand Down
19 changes: 3 additions & 16 deletions src/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
use Psr\Http\Message\MessageInterface;
use Psr\Http\Message\StreamInterface;
use Sunrise\Http\Header\HeaderInterface;

/**
* Hypertext Transfer Protocol Message
Expand All @@ -26,20 +27,6 @@
class Message implements MessageInterface
{

/**
* Regular Expression for a token validation
*
* @link https://tools.ietf.org/html/rfc7230#section-3.2
*/
protected const RFC7230_TOKEN = '/^[\x21\x23-\x27\x2A\x2B\x2D\x2E\x30-\x39\x41-\x5A\x5E-\x7A\x7C\x7E]+$/';

/**
* Regular Expression for a field-value validation
*
* @link https://tools.ietf.org/html/rfc7230#section-3.2
*/
protected const RFC7230_FIELD_VALUE = '/^[\x09\x20-\x7E\x80-\xFF]*$/';

/**
* Protocol version for the message
*
Expand Down Expand Up @@ -247,7 +234,7 @@ protected function validateHeaderName($headerName) : void
{
throw new \InvalidArgumentException('Header name must be a string');
}
else if (! \preg_match(self::RFC7230_TOKEN, $headerName))
else if (! \preg_match(HeaderInterface::RFC7230_TOKEN, $headerName))
{
throw new \InvalidArgumentException(\sprintf('The given header name "%s" is not valid', $headerName));
}
Expand Down Expand Up @@ -282,7 +269,7 @@ protected function validateHeaderValue($headerValue) : void
{
throw new \InvalidArgumentException('Header value must be a string or an array containing only strings');
}
else if (! \preg_match(self::RFC7230_FIELD_VALUE, $oneOf))
else if (! \preg_match(HeaderInterface::RFC7230_FIELD_VALUE, $oneOf))
{
throw new \InvalidArgumentException(\sprintf('The given header value "%s" is not valid', $oneOf));
}
Expand Down
3 changes: 2 additions & 1 deletion src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\UriInterface;
use Sunrise\Http\Header\HeaderInterface;

/**
* HTTP Request Message
Expand Down Expand Up @@ -168,7 +169,7 @@ protected function validateMethod($method) : void
{
throw new \InvalidArgumentException('HTTP method must be a string');
}
else if (! \preg_match(self::RFC7230_TOKEN, $method))
else if (! \preg_match(HeaderInterface::RFC7230_TOKEN, $method))
{
throw new \InvalidArgumentException(\sprintf('The given method "%s" is not valid', $method));
}
Expand Down
3 changes: 2 additions & 1 deletion src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* Import classes
*/
use Psr\Http\Message\ResponseInterface;
use Sunrise\Http\Header\HeaderInterface;

/**
* HTTP Response Message
Expand Down Expand Up @@ -116,7 +117,7 @@ protected function validateReasonPhrase($reasonPhrase) : void
{
throw new \InvalidArgumentException('HTTP reason-phrase must be a string');
}
else if (! \preg_match(self::RFC7230_FIELD_VALUE, $reasonPhrase))
else if (! \preg_match(HeaderInterface::RFC7230_FIELD_VALUE, $reasonPhrase))
{
throw new \InvalidArgumentException(\sprintf('The given reason-phrase "%s" is not valid', $reasonPhrase));
}
Expand Down

0 comments on commit 739fdd4

Please sign in to comment.