using HTTP status code
This commit is contained in:
parent
c156be8ae4
commit
eddb1253d9
4 changed files with 78 additions and 53 deletions
75
api.php
75
api.php
|
@ -4,7 +4,16 @@ require_once "classes/class.request.php";
|
||||||
require_once "classes/class.result.php";
|
require_once "classes/class.result.php";
|
||||||
require_once "classes/class.datahandler.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'];
|
$action = (string) $_REQUEST['action'];
|
||||||
|
|
||||||
switch ($action) {
|
switch ($action) {
|
||||||
|
@ -14,16 +23,9 @@ if (isset($_REQUEST['action'])) {
|
||||||
$request = new Request();
|
$request = new Request();
|
||||||
$request->id = (string) $_GET['id'];
|
$request->id = (string) $_GET['id'];
|
||||||
|
|
||||||
$result = new Result();
|
|
||||||
|
|
||||||
$datahandler = new DataHandler($request, $result);
|
$datahandler = new DataHandler($request, $result);
|
||||||
$datahandler->get();
|
$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;
|
break;
|
||||||
|
|
||||||
// write new data or update existing data
|
// write new data or update existing data
|
||||||
|
@ -43,21 +45,62 @@ if (isset($_REQUEST['action'])) {
|
||||||
}
|
}
|
||||||
$request->data = (string) $_POST["data"];
|
$request->data = (string) $_POST["data"];
|
||||||
|
|
||||||
$result = new Result();
|
|
||||||
|
|
||||||
$datahandler = new DataHandler($request, $result);
|
$datahandler = new DataHandler($request, $result);
|
||||||
$datahandler->set();
|
$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;
|
break;
|
||||||
|
|
||||||
|
// handling not known action types
|
||||||
default:
|
default:
|
||||||
|
$result->status = 400;
|
||||||
|
$result->errorMsg = "Specified action is not defined.";
|
||||||
|
|
||||||
break;
|
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);
|
||||||
?>
|
?>
|
||||||
|
|
|
@ -37,12 +37,11 @@ class DataHandler
|
||||||
|
|
||||||
$data = $this->_readData();
|
$data = $this->_readData();
|
||||||
if ($data === false) {
|
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';
|
$this->result->errorMsg = 'there is no data with this identifier or data could not be read';
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->result->result = true;
|
|
||||||
$this->result->version = md5(json_encode($data));
|
$this->result->version = md5(json_encode($data));
|
||||||
$this->result->data = $data;
|
$this->result->data = $data;
|
||||||
|
|
||||||
|
@ -58,7 +57,7 @@ class DataHandler
|
||||||
if ($data_org !== false) {
|
if ($data_org !== false) {
|
||||||
// check if version is out of date
|
// check if version is out of date
|
||||||
if (md5(json_encode($data_org)) !== $this->request->version) {
|
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';
|
$this->result->errorMsg = 'used version is out of date';
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -66,7 +65,7 @@ class DataHandler
|
||||||
else {
|
else {
|
||||||
// check traficLimiter
|
// check traficLimiter
|
||||||
if (!$this->_traficLimiterCanPass()) {
|
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';
|
$this->result->errorMsg = 'to many request in last ' . self::TRAFIC_LIMITER . ' seconds from your IP address';
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -78,7 +77,6 @@ class DataHandler
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->result->version = md5(json_encode($this->_readData()));
|
$this->result->version = md5(json_encode($this->_readData()));
|
||||||
$this->result->result = true;
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -190,7 +188,7 @@ class DataHandler
|
||||||
{
|
{
|
||||||
if (!file_exists(self::DATA_FOLDER.$this->request->id."/")) {
|
if (!file_exists(self::DATA_FOLDER.$this->request->id."/")) {
|
||||||
if (!mkdir(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';
|
$this->result->errorMsg = 'data could not be written';
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -215,7 +213,7 @@ class DataHandler
|
||||||
protected function _writeDatum($typ, $data)
|
protected function _writeDatum($typ, $data)
|
||||||
{
|
{
|
||||||
if(file_put_contents(self::DATA_FOLDER.$this->request->id.'/'.$typ, $data, LOCK_EX) === false) {
|
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;
|
$this->result->errorMsg = 'data could not be written to '.$typ;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
class result implements JsonSerializable
|
class result implements JsonSerializable
|
||||||
{
|
{
|
||||||
protected $result = false;
|
protected $status = "200";
|
||||||
protected $version = '';
|
protected $version = '';
|
||||||
protected $id = '';
|
protected $id = '';
|
||||||
protected $data = '';
|
protected $data = '';
|
||||||
|
@ -24,8 +24,8 @@ class result implements JsonSerializable
|
||||||
}
|
}
|
||||||
|
|
||||||
switch ($name) {
|
switch ($name) {
|
||||||
case 'result':
|
case 'status':
|
||||||
if (!is_bool($value)) {
|
if (!is_int($value)) {
|
||||||
throw new Exception ("wrong data type");
|
throw new Exception ("wrong data type");
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
@ -48,7 +48,6 @@ class result implements JsonSerializable
|
||||||
|
|
||||||
public function jsonSerialize() {
|
public function jsonSerialize() {
|
||||||
$container = new stdClass();
|
$container = new stdClass();
|
||||||
$container->result = $this->result;
|
|
||||||
$container->version = $this->version;
|
$container->version = $this->version;
|
||||||
$container->id = $this->id;
|
$container->id = $this->id;
|
||||||
$container->data = $this->data;
|
$container->data = $this->data;
|
||||||
|
|
|
@ -10,24 +10,16 @@ DataHandler = function () {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.done(function(result) {
|
.done(function(result) {
|
||||||
if (result.result === true) {
|
result.data.data = JSON.parse(sjcl.decrypt($(location).attr('hash').substring(1), result.data.data));
|
||||||
result.data.data = JSON.parse(sjcl.decrypt($(location).attr('hash').substring(1), result.data.data));
|
|
||||||
|
for (i = 0; i < result.data.user.length; i++) {
|
||||||
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]));
|
||||||
result.data.user[i] = JSON.parse(sjcl.decrypt($(location).attr('hash').substring(1), result.data.user[i]));
|
}
|
||||||
}
|
|
||||||
|
done(result);
|
||||||
done(result);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
console.log ('Api reported an error.');
|
|
||||||
console.log (result.errorMsg);
|
|
||||||
|
|
||||||
alert('Could not read requested data!\nerror message: ' + result.errorMsg);
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
.fail(function(result) {
|
.fail(function(result) {
|
||||||
fail(result);
|
fail(result.responseJSON);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -51,18 +43,10 @@ DataHandler = function () {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.done(function(result) {
|
.done(function(result) {
|
||||||
if (result.result === true) {
|
done(result);
|
||||||
done(result);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
console.log('Api reported an error.');
|
|
||||||
console.log(result.errorMsg);
|
|
||||||
|
|
||||||
alert('Could not save data:\nerror message: ' + result.errorMsg);
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
.fail(function(result) {
|
.fail(function(result) {
|
||||||
fail(result);
|
fail(result.responseJSON);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
@ -84,6 +68,7 @@ Poll = function (id) {
|
||||||
};
|
};
|
||||||
|
|
||||||
this.Failed = function(result) {
|
this.Failed = function(result) {
|
||||||
|
alert('Could not read requested data!\nerror message: ' + result.errorMsg);
|
||||||
console.log("Datahandler fehlgeschlagen.");
|
console.log("Datahandler fehlgeschlagen.");
|
||||||
console.log(result);
|
console.log(result);
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue