Skip to content

Commit

Permalink
Merge branch 'content-type' into development
Browse files Browse the repository at this point in the history
  • Loading branch information
natanfelles committed Nov 24, 2023
2 parents 3a64914 + dcb0efc commit 2bc1a00
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 28 deletions.
25 changes: 2 additions & 23 deletions src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -514,13 +514,9 @@ protected function negotiateCsp() : void
* Negotiates the Content-Type header, setting the MIME type "text/html" if
* the response body is not empty.
*
* If the response body is empty, it checks servers to set the header to an
* empty value, which causes the server to remove the Content-Type header,
* If the response body is empty, it removes the Content-Type header,
* and it will not appear to the client from the request.
*
* The header will also not be set on the PHP Development Server when the
* body is empty.
*
* This prevents the Content-Type from appearing without it being needed in,
* for example, REST API responses.
*
Expand All @@ -532,24 +528,7 @@ protected function negotiateContentType() : void
$this->setContentType('text/html');
return;
}
$software = (string) $this->getRequest()->getServer('SERVER_SOFTWARE');
$software = \strtolower($software);
// These servers remove headers if they are set to an empty value:
$servers = [
'apache',
'lighttpd',
'nginx',
];
foreach ($servers as $server) {
if (\str_contains($software, $server)) {
$this->setHeader(Header::CONTENT_TYPE, '');
return;
}
}
// Prevent PHP Development Server from setting the default Content-Type:
if (\str_contains($software, 'php')) {
\ini_set('default_mimetype', '');
}
\ini_set('default_mimetype', '');
}

/**
Expand Down
9 changes: 4 additions & 5 deletions tests/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,7 @@ public function testContentTypeNotSetIfBodyIsEmpty() : void
\ob_end_clean();
self::assertNull($this->response->getHeader('content-type'));
self::assertSame('', $this->response->getBody());
self::assertSame('', \ini_get('default_mimetype'));
}

/**
Expand All @@ -611,6 +612,7 @@ public function testContentTypeAutoSetIfBodyIsNotEmpty() : void
$this->response->getHeader('content-type')
);
self::assertSame('Foo', $this->response->getBody());
self::assertNotSame('', \ini_get('default_mimetype'));
}

/**
Expand Down Expand Up @@ -639,11 +641,8 @@ public function testContentTypeEmptyOnServer(string $software) : void
\ob_start();
$this->response->send();
\ob_end_clean();
self::assertSame(
'',
$this->response->getHeader('content-type')
);
self::assertSame('Content-Type:', xdebug_get_headers()[1]);
self::assertNull($this->response->getHeader('content-type'));
self::assertSame('', \ini_get('default_mimetype'));
}

/**
Expand Down

0 comments on commit 2bc1a00

Please sign in to comment.