From fb23dde10af35eea3b0f0b14459630305618ac3a Mon Sep 17 00:00:00 2001 From: Juuso Lapinlampi Date: Tue, 29 Sep 2015 21:53:01 +0000 Subject: Remove default undefined behavior from Response class Let's say some user decides to use output "xml". Unexpectedly, the user would instead receive JSON output. This is undefined behavior in its own way and violates the principle of least surprise. Problems could arise if we added "xml" later with actual application/xml or text/xml content. Right now upload.php overrides an empty output argument to json instead, so this seems safe to do. Any non-existent output will now return a 400 Bad Request error. --- static/classes/Response.class.php | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'static/classes') diff --git a/static/classes/Response.class.php b/static/classes/Response.class.php index 25d101c..cb34ab4 100755 --- a/static/classes/Response.class.php +++ b/static/classes/Response.class.php @@ -45,9 +45,13 @@ class Response header('Content-Type: text/plain; charset=UTF-8'); $this->type = $response_type; break; - default: + case 'json': header('Content-Type: application/json; charset=UTF-8'); - $this->type = 'json'; + $this->type = $response_type; + break; + default: + http_response_code(400); + echo 'Invalid response type. Valid options are: csv, html, text, json.'; break; } } @@ -66,7 +70,7 @@ class Response case 'text': $response = $this->text_error($code, $desc); break; - default: + case 'json': $response = $this->json_error($code, $desc); break; } @@ -89,7 +93,7 @@ class Response case 'text': $response = $this->text_success($files); break; - default: + case 'json': $response = $this->json_success($files); break; } -- cgit v1.2.3