Skip to content

Commit 66b2f87

Browse files
committed
Http: Review Headers
- Move `Headers` methods to `HttpUtil` and refactor: - `getContentLength()` - `getMultipartBoundary()` - `getPreferences()` - `mergePreferences()` - `getRetryAfter()` - In `addLine()`: - Throw an exception on invalid header field syntax or line folding even if `$strict` is `false` - Throw `LogicException` if called after headers are applied via another method - Throw `InvalidHeaderException` instead of `InvalidArgumentException` - In `map()`, return the instance if values are equivalent - Fix `addLine()` issue where invalid line folding in the first trailer may not be detected - Fix `getHeaderLine()` issue where values with unquoted commas may be inadvertently modified - Use `getLastHeaderValue()` when retrieving `Content-Type` headers - Use `getOnlyHeaderValue()` when retrieving `Location` headers
1 parent b799f14 commit 66b2f87

File tree

11 files changed

+542
-572
lines changed

11 files changed

+542
-572
lines changed

src/Toolkit/Contract/Http/HeadersInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Salient\Contract\Collection\DictionaryInterface;
66
use Salient\Contract\Core\Arrayable;
77
use Salient\Contract\Core\Immutable;
8+
use Salient\Contract\Http\Exception\InvalidHeaderException;
89
use LogicException;
910
use Stringable;
1011

@@ -37,6 +38,7 @@ public function __construct($items = []);
3738
* @return static
3839
* @throws LogicException if headers have been applied to the instance via
3940
* another method.
41+
* @throws InvalidHeaderException if `$line` is invalid.
4042
*/
4143
public function addLine(string $line);
4244

src/Toolkit/Curler/Curler.php

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use Salient\Contract\Curler\CurlerMiddlewareInterface;
1717
use Salient\Contract\Curler\CurlerPageRequestInterface;
1818
use Salient\Contract\Curler\CurlerPagerInterface;
19-
use Salient\Contract\Http\Exception\InvalidHeaderException as InvalidHeaderExceptionInterface;
2019
use Salient\Contract\Http\Exception\StreamEncapsulationException;
2120
use Salient\Contract\Http\Message\MultipartStreamInterface;
2221
use Salient\Contract\Http\Message\ResponseInterface;
@@ -307,12 +306,7 @@ private function responseIsJson(ResponseInterface $response): bool
307306
if (!$headers->hasHeader(self::HEADER_CONTENT_TYPE)) {
308307
return $this->ExpectJson;
309308
}
310-
try {
311-
$contentType = $headers->getOnlyHeaderValue(self::HEADER_CONTENT_TYPE);
312-
} catch (InvalidHeaderExceptionInterface $ex) {
313-
$this->debug($ex->getMessage());
314-
return false;
315-
}
309+
$contentType = $headers->getLastHeaderValue(self::HEADER_CONTENT_TYPE);
316310
return HttpUtil::mediaTypeIs($contentType, self::TYPE_JSON);
317311
}
318312

@@ -1139,7 +1133,7 @@ private function getResponse(PsrRequestInterface $request): ResponseInterface
11391133
}
11401134

11411135
if ($size === null || $size > 0) {
1142-
$size ??= Headers::from($request)->getContentLength();
1136+
$size ??= HttpUtil::getContentLength($request);
11431137
if ($size !== null && $size <= static::MAX_INPUT_LENGTH) {
11441138
$body = (string) $body;
11451139
$opt[\CURLOPT_POSTFIELDS] = $body;
@@ -1369,8 +1363,7 @@ static function ($handle, string $data) use (&$bodyIn): int {
13691363
$redirects !== false
13701364
&& $code >= 300
13711365
&& $code < 400
1372-
&& count($location = $headersIn->getHeader(self::HEADER_LOCATION)) === 1
1373-
&& ($location = $location[0]) !== ''
1366+
&& ($location = $headersIn->getOnlyHeaderValue(self::HEADER_LOCATION)) !== ''
13741367
) {
13751368
if (!$redirects) {
13761369
throw new TooManyRedirectsException(sprintf(
@@ -1422,7 +1415,7 @@ static function ($handle, string $data) use (&$bodyIn): int {
14221415
!$this->RetryAfterTooManyRequests
14231416
|| $retrying
14241417
|| $code !== 429
1425-
|| ($after = $headersIn->getRetryAfter()) === null
1418+
|| ($after = HttpUtil::getRetryAfter($headersIn)) === null
14261419
|| ($this->RetryAfterMaxSeconds !== 0 && $after > $this->RetryAfterMaxSeconds)
14271420
) {
14281421
break;

src/Toolkit/Curler/Pager/ODataPager.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use Salient\Contract\Curler\CurlerPagerInterface;
99
use Salient\Contract\Http\Message\ResponseInterface;
1010
use Salient\Curler\CurlerPage;
11-
use Salient\Http\Headers;
11+
use Salient\Http\HttpUtil;
1212
use Salient\Http\Uri;
1313
use Salient\Utility\Exception\InvalidArgumentTypeException;
1414

@@ -41,7 +41,7 @@ public function getFirstRequest(
4141
return $request;
4242
}
4343

44-
$prefs = Headers::from($request)->getPreferences();
44+
$prefs = HttpUtil::getPreferences($request);
4545
if (
4646
isset($prefs['odata.maxpagesize'])
4747
&& $prefs['odata.maxpagesize']['value'] === (string) $this->MaxPageSize
@@ -53,7 +53,7 @@ public function getFirstRequest(
5353

5454
return $request->withHeader(
5555
self::HEADER_PREFER,
56-
Headers::mergePreferences($prefs),
56+
HttpUtil::mergePreferences($prefs),
5757
);
5858
}
5959

0 commit comments

Comments
 (0)