Skip to content

Commit

Permalink
Do not override custom Content-Type Header
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan authored Nov 8, 2017
1 parent 912bed4 commit 0e65d38
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,19 @@ public static function init() {
* adds a header for sending later
* @param String $header
* @param Int|null $httpcode
* @param Boolean|true $replace
* @param Boolean|true $replace (meaning of replace see: http://php.net/manual/en/function.header.php)
* @param Boolean|false $onlyIfNotExists (add the header only if no other header of this type (e.g. Content-Type) exists
* @return Void
*/
public static function addHeader(string $header, int $httcode=null, bool $replace=true) {
if (!isset(self::$_headers[$header]) || $replace) {
self::$_headers[$header]=Array();
public static function addHeader(string $header, int $httcode=null, bool $replace=true, bool $onlyIfNotExists=false) {
$headerKey = explode(':', $header)[0];

if ($onlyIfNotExists && isset(self::$_headers[$headerKey])) return;

if (!isset(self::$_headers[$headerKey]) || $replace) {
self::$_headers[$headerKey]=Array();
}
self::$_headers[$header][]=Array($httcode, $replace);
self::$_headers[$headerKey][]=Array($httcode, $replace, $header);
}


Expand Down Expand Up @@ -75,11 +80,11 @@ public static function addHeaderServerError() {
*/
public static function setOutput($data) {
if ($data===null) {
self::addHeader('Content-Type: text/html; charset=UTF-8');
self::addHeader('Content-Type: text/html; charset=UTF-8', null, true, true);
self::$_output='';

} elseif (self::$_outputFormat=='json' || (!is_scalar($data) && !$data instanceof Template)) {
Response::addHeader('Content-Type: application/json; charset=UTF-8');
Response::addHeader('Content-Type: application/json; charset=UTF-8', null, true, true);

//if (is_array($data) && !isset($data['success'])) $data['success']=true;
$options=null;
Expand All @@ -89,7 +94,7 @@ public static function setOutput($data) {
self::$_output=json_encode($data, $options);

} else {
self::addHeader('Content-Type: text/html; charset=UTF-8');
self::addHeader('Content-Type: text/html; charset=UTF-8', null, true, true);

// disable layout on ajax requests
if ($data instanceof Template && Request::isAjax()) $data->disableLayout();
Expand Down Expand Up @@ -167,9 +172,9 @@ public static function setException(\JSMF\Exception $e) {
*/
public static function output() {
// send headers
foreach (self::$_headers as $header => $headers) {
foreach (self::$_headers as $headerKey => $headers) {
foreach ($headers as $headerData) {
header($header, $headerData[1], $headerData[0]);
header($headerData[2], $headerData[1], $headerData[0]);
}
}

Expand Down

0 comments on commit 0e65d38

Please sign in to comment.