Merge pull request #9390 from nextcloud/bugfix/noid/no-body-for-no-content

Do not send a body for "No content", "Not modified" and others
This commit is contained in:
blizzz 2018-05-09 23:41:56 +02:00 committed by GitHub
commit 72dc01460e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 55 additions and 3 deletions

View File

@ -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() {