Skip to content

Commit 5221717

Browse files
authored
Fix processing of content-type headers with directives (#18)
1 parent d7333a4 commit 5221717

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,14 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/)
55
and this project adheres to [Semantic Versioning](http://semver.org/).
66

7+
## [3.0.3] - 2018-01-08
8+
9+
### Changed
10+
- Fixed issue where `Content-type` headers with directives (e.g. `Content-type: application/json; charset=utf-8`) are processed correctly
11+
712
## [3.0.2] - 2018-01-03
13+
14+
### Changed
815
- Added URI matching tests into `EndpointTestCases`. Updating to this version will result in existing passing tests using said trait being skipped until good and bad URI matches are added into the test case.
916

1017
## [3.0.1] - 2017-12-01

src/Dispatcher.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,17 @@ private function parseInput(): ParsedInput
177177
// don't use $_POST because additional content types are supported.
178178
// Since PSR-7 doesn't specify parsing the body of most MIME-types,
179179
// we'll hand off to our own set of parsers.
180-
$cth = $this->request->getHeader('Content-type');
181-
if ($cth) {
180+
$header = $this->request->getHeader('Content-type');
181+
if ($header) {
182+
$directives = explode(';', $header[0]);
183+
if (!count($directives)) {
184+
throw new OutOfBoundsException('Invalid Content-type header', 400);
185+
}
186+
$mediaType = array_shift($directives);
187+
// Future: trim and format directives; e.g. ' charset=utf-8' =>
188+
// ['charset' => 'utf-8']
182189
list($parser_class) = (new ClassMapper($this->parser_list))
183-
->search($cth[0]);
190+
->search($mediaType);
184191
if (!$parser_class) {
185192
throw new OutOfBoundsException('Unsupported Content-type', 400);
186193
}

tests/DispatcherTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,25 @@ public function testUnsupportedContentTypeReachesErrorHandler()
417417
);
418418
}
419419

420+
/**
421+
* @covers ::dispatch
422+
*/
423+
public function testMatchingContentTypeWithDirectives()
424+
{
425+
$contentType = 'application/json; charset=utf-8';
426+
$req = $this->getMockRequestWithUriPath('/user/5', 'POST');
427+
$req->expects($this->any())
428+
->method('getHeader')
429+
->with('Content-type')
430+
->will($this->returnValue([$contentType]));
431+
$response = (new Dispatcher())
432+
->setEndpointList($this->getEndpointListForFixture())
433+
->setParserList($this->getDefaultParserList())
434+
->setRequest($req)
435+
->dispatch();
436+
$this->checkResponse($response, 200);
437+
}
438+
420439
/** @covers ::dispatch */
421440
public function testFailedAuthenticationReachesErrorHandler()
422441
{

0 commit comments

Comments
 (0)