File tree Expand file tree Collapse file tree 2 files changed +32
-1
lines changed
Expand file tree Collapse file tree 2 files changed +32
-1
lines changed Original file line number Diff line number Diff line change 2222use function preg_match ;
2323use function sprintf ;
2424use function strtolower ;
25+ use function trim ;
2526
2627/**
2728 * Trait implementing the methods defined in `Psr\Http\Message\MessageInterface`.
@@ -371,6 +372,8 @@ private function registerProtocolVersion(string $protocol): void
371372 }
372373
373374 /**
375+ * @see https://tools.ietf.org/html/rfc7230#section-3.2
376+ *
374377 * @param mixed $name
375378 * @return string
376379 * @throws InvalidArgumentException for invalid header name.
@@ -388,6 +391,8 @@ private function normalizeHeaderName($name): string
388391 }
389392
390393 /**
394+ * @see https://tools.ietf.org/html/rfc7230#section-3.2
395+ *
391396 * @param mixed $value
392397 * @return array
393398 * @throws InvalidArgumentException for invalid header name.
@@ -402,16 +407,20 @@ private function normalizeHeaderValue($value): array
402407 );
403408 }
404409
410+ $ normalizedValues = [];
411+
405412 foreach ($ value as $ v ) {
406413 if ((!is_string ($ v ) && !is_numeric ($ v )) || !preg_match ('/^[ \t\x21-\x7E\x80-\xFF]*$/D ' , (string ) $ v )) {
407414 throw new InvalidArgumentException (sprintf (
408415 '"%s" is not valid header value. ' ,
409416 (is_object ($ v ) ? get_class ($ v ) : (is_string ($ v ) ? $ v : gettype ($ v )))
410417 ));
411418 }
419+
420+ $ normalizedValues [] = trim ((string ) $ v , " \t" );
412421 }
413422
414- return $ value ;
423+ return $ normalizedValues ;
415424 }
416425
417426 /**
Original file line number Diff line number Diff line change @@ -199,6 +199,28 @@ public function testGetHeaderLineNotFound(): void
199199 $ this ->assertSame ('' , $ this ->message ->getHeaderLine ('Name ' ));
200200 }
201201
202+ /**
203+ * @return array
204+ */
205+ public function trimmedHeaderValuesProvider (): array
206+ {
207+ return [
208+ [(new Message ())->withHeader ('Name ' , [" \t \tValue \t \t " ])],
209+ [(new Message ())->withHeader ('Name ' , " \t \tValue \t \t " )],
210+ [(new Message ())->withAddedHeader ('Name ' , " \t \tValue \t \t " )],
211+ ];
212+ }
213+
214+ /**
215+ * @dataProvider trimmedHeaderValuesProvider
216+ */
217+ public function testHeaderValuesAreTrimmed (Message $ message ): void
218+ {
219+ $ this ->assertSame (['Name ' => ['Value ' ]], $ message ->getHeaders ());
220+ $ this ->assertSame ('Value ' , $ message ->getHeaderLine ('name ' ));
221+ $ this ->assertSame (['Value ' ], $ message ->getHeader ('name ' ));
222+ }
223+
202224 /**
203225 * @return array
204226 */
You can’t perform that action at this time.
0 commit comments