updated-packages
This commit is contained in:
127
vendor/guzzlehttp/psr7/src/MessageTrait.php
vendored
127
vendor/guzzlehttp/psr7/src/MessageTrait.php
vendored
@@ -1,4 +1,5 @@
|
||||
<?php
|
||||
|
||||
namespace GuzzleHttp\Psr7;
|
||||
|
||||
use Psr\Http\Message\StreamInterface;
|
||||
@@ -17,7 +18,7 @@ trait MessageTrait
|
||||
/** @var string */
|
||||
private $protocol = '1.1';
|
||||
|
||||
/** @var StreamInterface */
|
||||
/** @var StreamInterface|null */
|
||||
private $stream;
|
||||
|
||||
public function getProtocolVersion()
|
||||
@@ -66,11 +67,8 @@ trait MessageTrait
|
||||
|
||||
public function withHeader($header, $value)
|
||||
{
|
||||
if (!is_array($value)) {
|
||||
$value = [$value];
|
||||
}
|
||||
|
||||
$value = $this->trimHeaderValues($value);
|
||||
$this->assertHeader($header);
|
||||
$value = $this->normalizeHeaderValue($value);
|
||||
$normalized = strtolower($header);
|
||||
|
||||
$new = clone $this;
|
||||
@@ -85,11 +83,8 @@ trait MessageTrait
|
||||
|
||||
public function withAddedHeader($header, $value)
|
||||
{
|
||||
if (!is_array($value)) {
|
||||
$value = [$value];
|
||||
}
|
||||
|
||||
$value = $this->trimHeaderValues($value);
|
||||
$this->assertHeader($header);
|
||||
$value = $this->normalizeHeaderValue($value);
|
||||
$normalized = strtolower($header);
|
||||
|
||||
$new = clone $this;
|
||||
@@ -123,7 +118,7 @@ trait MessageTrait
|
||||
public function getBody()
|
||||
{
|
||||
if (!$this->stream) {
|
||||
$this->stream = stream_for('');
|
||||
$this->stream = Utils::streamFor('');
|
||||
}
|
||||
|
||||
return $this->stream;
|
||||
@@ -144,11 +139,13 @@ trait MessageTrait
|
||||
{
|
||||
$this->headerNames = $this->headers = [];
|
||||
foreach ($headers as $header => $value) {
|
||||
if (!is_array($value)) {
|
||||
$value = [$value];
|
||||
if (is_int($header)) {
|
||||
// Numeric array keys are converted to int by PHP but having a header name '123' is not forbidden by the spec
|
||||
// and also allowed in withHeader(). So we need to cast it to string again for the following assertion to pass.
|
||||
$header = (string) $header;
|
||||
}
|
||||
|
||||
$value = $this->trimHeaderValues($value);
|
||||
$this->assertHeader($header);
|
||||
$value = $this->normalizeHeaderValue($value);
|
||||
$normalized = strtolower($header);
|
||||
if (isset($this->headerNames[$normalized])) {
|
||||
$header = $this->headerNames[$normalized];
|
||||
@@ -160,6 +157,24 @@ trait MessageTrait
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $value
|
||||
*
|
||||
* @return string[]
|
||||
*/
|
||||
private function normalizeHeaderValue($value)
|
||||
{
|
||||
if (!is_array($value)) {
|
||||
return $this->trimAndValidateHeaderValues([$value]);
|
||||
}
|
||||
|
||||
if (count($value) === 0) {
|
||||
throw new \InvalidArgumentException('Header value can not be an empty array.');
|
||||
}
|
||||
|
||||
return $this->trimAndValidateHeaderValues($value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Trims whitespace from the header values.
|
||||
*
|
||||
@@ -168,16 +183,88 @@ trait MessageTrait
|
||||
* header-field = field-name ":" OWS field-value OWS
|
||||
* OWS = *( SP / HTAB )
|
||||
*
|
||||
* @param string[] $values Header values
|
||||
* @param mixed[] $values Header values
|
||||
*
|
||||
* @return string[] Trimmed header values
|
||||
*
|
||||
* @see https://tools.ietf.org/html/rfc7230#section-3.2.4
|
||||
*/
|
||||
private function trimHeaderValues(array $values)
|
||||
private function trimAndValidateHeaderValues(array $values)
|
||||
{
|
||||
return array_map(function ($value) {
|
||||
return trim($value, " \t");
|
||||
}, $values);
|
||||
if (!is_scalar($value) && null !== $value) {
|
||||
throw new \InvalidArgumentException(sprintf(
|
||||
'Header value must be scalar or null but %s provided.',
|
||||
is_object($value) ? get_class($value) : gettype($value)
|
||||
));
|
||||
}
|
||||
|
||||
$trimmed = trim((string) $value, " \t");
|
||||
$this->assertValue($trimmed);
|
||||
|
||||
return $trimmed;
|
||||
}, array_values($values));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see https://tools.ietf.org/html/rfc7230#section-3.2
|
||||
*
|
||||
* @param mixed $header
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function assertHeader($header)
|
||||
{
|
||||
if (!is_string($header)) {
|
||||
throw new \InvalidArgumentException(sprintf(
|
||||
'Header name must be a string but %s provided.',
|
||||
is_object($header) ? get_class($header) : gettype($header)
|
||||
));
|
||||
}
|
||||
|
||||
if ($header === '') {
|
||||
throw new \InvalidArgumentException('Header name can not be empty.');
|
||||
}
|
||||
|
||||
if (! preg_match('/^[a-zA-Z0-9\'`#$%&*+.^_|~!-]+$/', $header)) {
|
||||
throw new \InvalidArgumentException(
|
||||
sprintf(
|
||||
'"%s" is not valid header name',
|
||||
$header
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $value
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @see https://tools.ietf.org/html/rfc7230#section-3.2
|
||||
*
|
||||
* field-value = *( field-content / obs-fold )
|
||||
* field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
|
||||
* field-vchar = VCHAR / obs-text
|
||||
* VCHAR = %x21-7E
|
||||
* obs-text = %x80-FF
|
||||
* obs-fold = CRLF 1*( SP / HTAB )
|
||||
*/
|
||||
private function assertValue($value)
|
||||
{
|
||||
// The regular expression intentionally does not support the obs-fold production, because as
|
||||
// per RFC 7230#3.2.4:
|
||||
//
|
||||
// A sender MUST NOT generate a message that includes
|
||||
// line folding (i.e., that has any field-value that contains a match to
|
||||
// the obs-fold rule) unless the message is intended for packaging
|
||||
// within the message/http media type.
|
||||
//
|
||||
// Clients must not send a request with line folding and a server sending folded headers is
|
||||
// likely very rare. Line folding is a fairly obscure feature of HTTP/1.1 and thus not accepting
|
||||
// folding is not likely to break any legitimate use case.
|
||||
if (! preg_match('/^[\x20\x09\x21-\x7E\x80-\xFF]*$/', $value)) {
|
||||
throw new \InvalidArgumentException(sprintf('"%s" is not valid header value', $value));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user