From eddb1253d918511c7fb393930bd3d436cac1d452 Mon Sep 17 00:00:00 2001 From: jelhan Date: Thu, 26 Dec 2013 15:11:27 +0100 Subject: [PATCH] using HTTP status code --- api.php | 75 +++++++++++++++++++++++++++-------- classes/class.datahandler.php | 12 +++--- classes/class.result.php | 7 ++-- js/croodle.js | 37 +++++------------ 4 files changed, 78 insertions(+), 53 deletions(-) diff --git a/api.php b/api.php index 2c9afbb..c00e744 100644 --- a/api.php +++ b/api.php @@ -4,7 +4,16 @@ require_once "classes/class.request.php"; require_once "classes/class.result.php"; require_once "classes/class.datahandler.php"; -if (isset($_REQUEST['action'])) { +$result = new Result(); + +// check if an action ist set +if (!isset($_REQUEST['action'])) { + $result->status = 400; + $result->errorMsg = "No action specified."; +} +else { + // process the action + $action = (string) $_REQUEST['action']; switch ($action) { @@ -14,16 +23,9 @@ if (isset($_REQUEST['action'])) { $request = new Request(); $request->id = (string) $_GET['id']; - $result = new Result(); - $datahandler = new DataHandler($request, $result); $datahandler->get(); - header('Content-Type: application/json; charset=utf-8'); - header('Strict-Transport-Security: max-age=86400'); - header("Content-Security-Policy: script-src 'self'"); - - echo json_encode($result); break; // write new data or update existing data @@ -43,21 +45,62 @@ if (isset($_REQUEST['action'])) { } $request->data = (string) $_POST["data"]; - $result = new Result(); - $datahandler = new DataHandler($request, $result); $datahandler->set(); - header('Content-Type: application/json; charset=utf-8'); - header('Strict-Transport-Security: max-age=86400'); - header("Content-Security-Policy: script-src 'self'"); - - echo json_encode($result); break; - + + // handling not known action types default: + $result->status = 400; + $result->errorMsg = "Specified action is not defined."; + break; } } +// send response + +// set http status code +switch ($result->status) { + case "200": + header("HTTP/1.0 200 OK"); + break; + + case "400": + header("HTTP/1.0 400 Bad Request"); + break; + + case "404": + header("HTTP/1.0 404 Not Found"); + break; + + case "409": + header("HTTP/1.0 409 Conflict"); + break; + + case "421": + header("HTTP/1.0 421 There are too many connections from your internet address"); + break; + + case "500": + header("HTTP/1.0 500 Internal Server Error"); + break; + + default: + header("HTTP/1.0 500 Internal Server Error"); + break; +} + +// set content-type and charset +header('Content-Type: application/json; charset=utf-8'); + +// force browser to stay on httpS connection for 1 day +header('Strict-Transport-Security: max-age=86400'); + +// forbidde browser to load javascript from an external locatoin +header("Content-Security-Policy: script-src 'self'"); + +// send data as encoded json +echo json_encode($result); ?> diff --git a/classes/class.datahandler.php b/classes/class.datahandler.php index f56d076..289031d 100644 --- a/classes/class.datahandler.php +++ b/classes/class.datahandler.php @@ -37,12 +37,11 @@ class DataHandler $data = $this->_readData(); if ($data === false) { - $this->result->result = false; + $this->result->status = 404; $this->result->errorMsg = 'there is no data with this identifier or data could not be read'; return false; } - $this->result->result = true; $this->result->version = md5(json_encode($data)); $this->result->data = $data; @@ -58,7 +57,7 @@ class DataHandler if ($data_org !== false) { // check if version is out of date if (md5(json_encode($data_org)) !== $this->request->version) { - $this->result->result = false; + $this->result->status = 409; $this->result->errorMsg = 'used version is out of date'; return false; } @@ -66,7 +65,7 @@ class DataHandler else { // check traficLimiter if (!$this->_traficLimiterCanPass()) { - $this->result->result = false; + $this->result->status = 421; $this->result->errorMsg = 'to many request in last ' . self::TRAFIC_LIMITER . ' seconds from your IP address'; return false; } @@ -78,7 +77,6 @@ class DataHandler } $this->result->version = md5(json_encode($this->_readData())); - $this->result->result = true; return true; } @@ -190,7 +188,7 @@ class DataHandler { if (!file_exists(self::DATA_FOLDER.$this->request->id."/")) { if (!mkdir(self::DATA_FOLDER.$this->request->id)) { - $this->result->result = false; + $this->result->status = 500; $this->result->errorMsg = 'data could not be written'; return false; } @@ -215,7 +213,7 @@ class DataHandler protected function _writeDatum($typ, $data) { if(file_put_contents(self::DATA_FOLDER.$this->request->id.'/'.$typ, $data, LOCK_EX) === false) { - $this->result->result = false; + $this->result->status = 500; $this->result->errorMsg = 'data could not be written to '.$typ; return false; } diff --git a/classes/class.result.php b/classes/class.result.php index 13da5b7..788d48a 100644 --- a/classes/class.result.php +++ b/classes/class.result.php @@ -2,7 +2,7 @@ class result implements JsonSerializable { - protected $result = false; + protected $status = "200"; protected $version = ''; protected $id = ''; protected $data = ''; @@ -24,8 +24,8 @@ class result implements JsonSerializable } switch ($name) { - case 'result': - if (!is_bool($value)) { + case 'status': + if (!is_int($value)) { throw new Exception ("wrong data type"); } break; @@ -48,7 +48,6 @@ class result implements JsonSerializable public function jsonSerialize() { $container = new stdClass(); - $container->result = $this->result; $container->version = $this->version; $container->id = $this->id; $container->data = $this->data; diff --git a/js/croodle.js b/js/croodle.js index 82f04a5..5d13d2a 100644 --- a/js/croodle.js +++ b/js/croodle.js @@ -10,24 +10,16 @@ DataHandler = function () { } }) .done(function(result) { - if (result.result === true) { - result.data.data = JSON.parse(sjcl.decrypt($(location).attr('hash').substring(1), result.data.data)); - - for (i = 0; i < result.data.user.length; i++) { - result.data.user[i] = JSON.parse(sjcl.decrypt($(location).attr('hash').substring(1), result.data.user[i])); - } - - done(result); - } - else { - console.log ('Api reported an error.'); - console.log (result.errorMsg); - - alert('Could not read requested data!\nerror message: ' + result.errorMsg); - } + result.data.data = JSON.parse(sjcl.decrypt($(location).attr('hash').substring(1), result.data.data)); + + for (i = 0; i < result.data.user.length; i++) { + result.data.user[i] = JSON.parse(sjcl.decrypt($(location).attr('hash').substring(1), result.data.user[i])); + } + + done(result); }) .fail(function(result) { - fail(result); + fail(result.responseJSON); }); }; @@ -51,18 +43,10 @@ DataHandler = function () { } }) .done(function(result) { - if (result.result === true) { - done(result); - } - else { - console.log('Api reported an error.'); - console.log(result.errorMsg); - - alert('Could not save data:\nerror message: ' + result.errorMsg); - } + done(result); }) .fail(function(result) { - fail(result); + fail(result.responseJSON); }); }; }; @@ -84,6 +68,7 @@ Poll = function (id) { }; this.Failed = function(result) { + alert('Could not read requested data!\nerror message: ' + result.errorMsg); console.log("Datahandler fehlgeschlagen."); console.log(result); };