diff options
author | Joas Schilling <coding@schilljs.com> | 2018-05-04 13:43:03 +0200 |
---|---|---|
committer | Joas Schilling <coding@schilljs.com> | 2018-05-04 13:46:13 +0200 |
commit | b4bacf46f3480cfb519917e73a921d0b61d0d447 (patch) | |
tree | 42373490e536e95a2932ca081f5e4a22e79a9195 | |
parent | dfe6d65410ba2f8ae671a7f75764639533c8c068 (diff) | |
download | nextcloud-server-b4bacf46f3480cfb519917e73a921d0b61d0d447.tar.gz nextcloud-server-b4bacf46f3480cfb519917e73a921d0b61d0d447.zip |
Do not send a body for "No content", "Not modified" and others
Signed-off-by: Joas Schilling <coding@schilljs.com>
-rw-r--r-- | lib/private/AppFramework/OCS/BaseResponse.php | 58 |
1 files changed, 55 insertions, 3 deletions
diff --git a/lib/private/AppFramework/OCS/BaseResponse.php b/lib/private/AppFramework/OCS/BaseResponse.php index 59b8660a382..b27784cfcf2 100644 --- a/lib/private/AppFramework/OCS/BaseResponse.php +++ b/lib/private/AppFramework/OCS/BaseResponse.php @@ -22,6 +22,7 @@ */ namespace OC\AppFramework\OCS; +use OCP\AppFramework\Http; use OCP\AppFramework\Http\DataResponse; use OCP\AppFramework\Http\EmptyContentSecurityPolicy; use OCP\AppFramework\Http\Response; @@ -85,9 +86,60 @@ abstract class BaseResponse extends Response { * @param string[] $meta * @return string */ - protected function renderResult($meta) { - // TODO rewrite functions - return \OC_API::renderResult($this->format, $meta, $this->data); + protected function renderResult(array $meta): string { + $status = $this->getStatus(); + if ($status === Http::STATUS_NO_CONTENT || + $status === Http::STATUS_NOT_MODIFIED || + ($status >= 100 && $status <= 199)) { + // Those status codes are not supposed to have a body: + // https://stackoverflow.com/q/8628725 + return ''; + } + + $response = [ + 'ocs' => [ + 'meta' => $meta, + 'data' => $this->data, + ], + ]; + + if ($this->format === 'json') { + return json_encode($response, JSON_HEX_TAG); + } + + $writer = new \XMLWriter(); + $writer->openMemory(); + $writer->setIndent(true); + $writer->startDocument(); + $this->toXML($response, $writer); + $writer->endDocument(); + return $writer->outputMemory(true); + + } + + /** + * @param array $array + * @param \XMLWriter $writer + */ + protected function toXML(array $array, \XMLWriter $writer) { + foreach ($array as $k => $v) { + if ($k[0] === '@') { + $writer->writeAttribute(substr($k, 1), $v); + continue; + } + + if (\is_numeric($k)) { + $k = 'element'; + } + + if (\is_array($v)) { + $writer->startElement($k); + $this->toXML($v, $writer); + $writer->endElement(); + } else { + $writer->writeElement($k, $v); + } + } } public function getOCSStatus() { |