From 7cf6b860b5cfa5e0d717b31a45c18fb182637543 Mon Sep 17 00:00:00 2001 From: Noah Hilverling Date: Fri, 14 Oct 2016 16:12:47 +0200 Subject: [PATCH 1/9] Response: Add contentType attribute refs #12161 --- library/Icinga/Web/Response.php | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/library/Icinga/Web/Response.php b/library/Icinga/Web/Response.php index af3e1e27e..71536c239 100644 --- a/library/Icinga/Web/Response.php +++ b/library/Icinga/Web/Response.php @@ -54,6 +54,13 @@ class Response extends Zend_Controller_Response_Http */ protected $rerenderLayout = false; + /** + * Content type of this response + * + * @var string + */ + protected $contentType = 'text/html'; + /** * Get the auto-refresh interval * @@ -205,6 +212,30 @@ class Response extends Zend_Controller_Response_Http return $this; } + /** + * Set the content type of this response + * + * @param string $contentType + * + * @return $this + */ + public function setContentType($contentType) + { + $this->contentType = $contentType; + return $this; + } + + /** + * Get the content type of this response + * + * @return string + */ + public function getContentType() + { + return $this->contentType; + } + + /** * Entry point for HTTP responses in JSON format * From af5c578adfd1497a98e202ca24300d30ad4fe2ff Mon Sep 17 00:00:00 2001 From: Noah Hilverling Date: Fri, 14 Oct 2016 16:16:13 +0200 Subject: [PATCH 2/9] Response: Set header Content-Type for every response by default refs #12161 --- library/Icinga/Web/Response.php | 2 ++ library/Icinga/Web/Response/JsonResponse.php | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/library/Icinga/Web/Response.php b/library/Icinga/Web/Response.php index 71536c239..11773a594 100644 --- a/library/Icinga/Web/Response.php +++ b/library/Icinga/Web/Response.php @@ -275,6 +275,8 @@ class Response extends Zend_Controller_Response_Http $this->setRedirect($redirectUrl->getAbsoluteUrl()); } } + + $this->setHeader("Content-Type", $this->getContentType(), true); } /** diff --git a/library/Icinga/Web/Response/JsonResponse.php b/library/Icinga/Web/Response/JsonResponse.php index 8a9ba7d33..21614149f 100644 --- a/library/Icinga/Web/Response/JsonResponse.php +++ b/library/Icinga/Web/Response/JsonResponse.php @@ -191,7 +191,7 @@ class JsonResponse extends Response */ public function sendHeaders() { - $this->setHeader('Content-Type', 'application/json', true); + $this->setContentType('application/json'); parent::sendHeaders(); } From 4df9696ed97e5b2cfe0451ff3d8c6a902620b99e Mon Sep 17 00:00:00 2001 From: Noah Hilverling Date: Mon, 17 Oct 2016 08:29:51 +0200 Subject: [PATCH 3/9] StyleSheet: Modify send method to use setContentType of Response class refs #12161 --- library/Icinga/Web/StyleSheet.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/Icinga/Web/StyleSheet.php b/library/Icinga/Web/StyleSheet.php index 549e11c5c..e74f3c955 100644 --- a/library/Icinga/Web/StyleSheet.php +++ b/library/Icinga/Web/StyleSheet.php @@ -197,7 +197,7 @@ class StyleSheet $response ->setHeader('Cache-Control', 'public', true) ->setHeader('ETag', $etag, true) - ->setHeader('Content-Type', 'text/css', true); + ->setContentType('text/css'); $cacheFile = 'icinga-' . $etag . ($minified ? '.min' : '') . '.css'; $cache = FileCache::instance(); From c3276d4341bbbde86f6a0f52dbc8535a7cdb2ff5 Mon Sep 17 00:00:00 2001 From: Noah Hilverling Date: Mon, 17 Oct 2016 16:03:33 +0200 Subject: [PATCH 4/9] Response: Remove empty line after method getContentType refs #12161 --- library/Icinga/Web/Response.php | 1 - 1 file changed, 1 deletion(-) diff --git a/library/Icinga/Web/Response.php b/library/Icinga/Web/Response.php index 11773a594..cdb377efd 100644 --- a/library/Icinga/Web/Response.php +++ b/library/Icinga/Web/Response.php @@ -235,7 +235,6 @@ class Response extends Zend_Controller_Response_Http return $this->contentType; } - /** * Entry point for HTTP responses in JSON format * From 703561f8749acc064ffdbf5c90b599790e5efef6 Mon Sep 17 00:00:00 2001 From: Noah Hilverling Date: Mon, 17 Oct 2016 16:33:51 +0200 Subject: [PATCH 5/9] JsonResponse: Set content type at initialization and not at sendHeaders refs #12161 --- library/Icinga/Web/Response/JsonResponse.php | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/library/Icinga/Web/Response/JsonResponse.php b/library/Icinga/Web/Response/JsonResponse.php index 21614149f..47a2ccd74 100644 --- a/library/Icinga/Web/Response/JsonResponse.php +++ b/library/Icinga/Web/Response/JsonResponse.php @@ -67,6 +67,13 @@ class JsonResponse extends Response */ protected $successData; + /** + * Content type of this response + * + * @var string + */ + protected $contentType = 'application/json'; + /** * Get the JSON encoding options * @@ -186,15 +193,6 @@ class JsonResponse extends Response echo json_encode($body, $this->getEncodingOptions()); } - /** - * {@inheritdoc} - */ - public function sendHeaders() - { - $this->setContentType('application/json'); - parent::sendHeaders(); - } - /** * {@inheritdoc} */ From c927c3244261ca36dd341652e1791c069a16f01a Mon Sep 17 00:00:00 2001 From: Noah Hilverling Date: Mon, 17 Oct 2016 16:34:07 +0200 Subject: [PATCH 6/9] Response: Only set header Content-Type in method prepare if not already set refs #12161 --- library/Icinga/Web/Response.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/Icinga/Web/Response.php b/library/Icinga/Web/Response.php index cdb377efd..d96972de6 100644 --- a/library/Icinga/Web/Response.php +++ b/library/Icinga/Web/Response.php @@ -275,7 +275,7 @@ class Response extends Zend_Controller_Response_Http } } - $this->setHeader("Content-Type", $this->getContentType(), true); + $this->setHeader('Content-Type', $this->getContentType(), false); } /** From f95c7893530418dc4fce9b18d257308a9dba9750 Mon Sep 17 00:00:00 2001 From: Noah Hilverling Date: Tue, 18 Oct 2016 09:58:33 +0200 Subject: [PATCH 7/9] Response: Add method getHeader refs #12161 --- library/Icinga/Web/Response.php | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/library/Icinga/Web/Response.php b/library/Icinga/Web/Response.php index d96972de6..2a90a97b1 100644 --- a/library/Icinga/Web/Response.php +++ b/library/Icinga/Web/Response.php @@ -153,6 +153,31 @@ class Response extends Zend_Controller_Response_Http return $this; } + /** + * Get an array of every header value with a specified name + * + * @param string $name + * @param bool $lastOnly If this is true, the last value will be returned as a string. + * + * @return null|array|string + */ + public function getHeader($name, $lastOnly = false) + { + $result = ($lastOnly ? null : array()); + $headers = $this->getHeaders(); + foreach ($headers as $header) { + if ($header['name'] === $name) { + if ($lastOnly) { + $result = $header['value']; + } else { + $result[] = $header['value']; + } + } + } + + return $result; + } + /** * Get the request * From e5c736aab4b707eba9288cf5e12ffda937bbc32b Mon Sep 17 00:00:00 2001 From: Noah Hilverling Date: Tue, 18 Oct 2016 10:13:58 +0200 Subject: [PATCH 8/9] Response: Use method getHeader to check if Content-Type is set already refs #12161 --- library/Icinga/Web/Response.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/library/Icinga/Web/Response.php b/library/Icinga/Web/Response.php index 2a90a97b1..ac2573f8d 100644 --- a/library/Icinga/Web/Response.php +++ b/library/Icinga/Web/Response.php @@ -300,7 +300,9 @@ class Response extends Zend_Controller_Response_Http } } - $this->setHeader('Content-Type', $this->getContentType(), false); + if (!$this->getHeader('Content-Type', true)) { + $this->setHeader('Content-Type', $this->getContentType()); + } } /** From 888ac98007a74dd8b45f0a61b329d0d2928812a8 Mon Sep 17 00:00:00 2001 From: Noah Hilverling Date: Tue, 18 Oct 2016 11:07:24 +0200 Subject: [PATCH 9/9] Response: Improve documentation for method getHeader refs #12161 --- library/Icinga/Web/Response.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/library/Icinga/Web/Response.php b/library/Icinga/Web/Response.php index ac2573f8d..a42f48fde 100644 --- a/library/Icinga/Web/Response.php +++ b/library/Icinga/Web/Response.php @@ -154,12 +154,12 @@ class Response extends Zend_Controller_Response_Http } /** - * Get an array of every header value with a specified name + * Get an array of all header values for the given name * - * @param string $name - * @param bool $lastOnly If this is true, the last value will be returned as a string. + * @param string $name The name of the header + * @param bool $lastOnly If this is true, the last value will be returned as a string * - * @return null|array|string + * @return null|array|string */ public function getHeader($name, $lastOnly = false) { @@ -300,7 +300,7 @@ class Response extends Zend_Controller_Response_Http } } - if (!$this->getHeader('Content-Type', true)) { + if (! $this->getHeader('Content-Type', true)) { $this->setHeader('Content-Type', $this->getContentType()); } }