Use exceptions to process incorrect requests
This commit is contained in:
parent
9d453c6f23
commit
48f8c3551e
@ -34,7 +34,6 @@
|
|||||||
* @source: http://www.webodf.org/
|
* @source: http://www.webodf.org/
|
||||||
* @source: http://gitorious.org/webodf/webodf/
|
* @source: http://gitorious.org/webodf/webodf/
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// OCP\JSON::checkLoggedIn();
|
// OCP\JSON::checkLoggedIn();
|
||||||
// OCP\JSON::checkAppEnabled('office');
|
// OCP\JSON::checkAppEnabled('office');
|
||||||
// session_write_close();
|
// session_write_close();
|
||||||
@ -50,11 +49,11 @@ function bogusSession($i){
|
|||||||
return $bs;
|
return $bs;
|
||||||
}
|
}
|
||||||
|
|
||||||
$request = new OCA\Office\Request();
|
try{
|
||||||
$command = $request->getParam('command');
|
$request = new OCA\Office\Request();
|
||||||
|
$command = $request->getParam('command');
|
||||||
$response = array();
|
$response = array();
|
||||||
switch ($command){
|
switch ($command){
|
||||||
case 'session-list':
|
case 'session-list':
|
||||||
$response["session_list"] = array(bogusSession(0), bogusSession(1));
|
$response["session_list"] = array(bogusSession(0), bogusSession(1));
|
||||||
break;
|
break;
|
||||||
@ -73,10 +72,14 @@ switch ($command){
|
|||||||
|
|
||||||
//if $postobject['args']['seq_head'] is the most recent op in the ops-table:
|
//if $postobject['args']['seq_head'] is the most recent op in the ops-table:
|
||||||
// append all ops in $postobject['args']['client_ops'] to the ops-table
|
// append all ops in $postobject['args']['client_ops'] to the ops-table
|
||||||
if ($seqHead>$currentHead){
|
if ($seqHead > $currentHead){
|
||||||
foreach ($ops as $op){
|
foreach ($ops as $op){
|
||||||
$op['opspec'] = json_encode($op['opspec']);
|
$op['opspec'] = json_encode($op['opspec']);
|
||||||
|
try{
|
||||||
OCA\Office\Op::add($op);
|
OCA\Office\Op::add($op);
|
||||||
|
} catch (Exception $e){
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// result: 'conflict',
|
// result: 'conflict',
|
||||||
@ -87,32 +90,37 @@ switch ($command){
|
|||||||
$last = end($response["ops"]);
|
$last = end($response["ops"]);
|
||||||
$response["headSeq"] = $last['seq'];
|
$response["headSeq"] = $last['seq'];
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
// Error :)
|
// Error - empty seq_head passed :)
|
||||||
|
throw new BadRequestException();
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
* try {
|
|
||||||
* OCA\Office\Op::add(
|
|
||||||
* array(
|
|
||||||
* 'es_id' => ES_ID,
|
|
||||||
* 'seq' => SEQ,
|
|
||||||
* 'member' => MEMBER,
|
|
||||||
* 'opspec' => OPSPEC
|
|
||||||
* )
|
|
||||||
* );
|
|
||||||
* } catch (Exception $e) {
|
|
||||||
*
|
|
||||||
* }
|
|
||||||
*/
|
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
$ex = new BadRequestException();
|
||||||
|
$ex->setBody("{err:'bad request: [" . $request->getRawRequest() . "]'}");
|
||||||
|
throw $ex;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
\OCP\JSON::success($response);
|
||||||
|
} catch (BadRequestException $e){
|
||||||
header('HTTP/1.1 400: BAD REQUEST');
|
header('HTTP/1.1 400: BAD REQUEST');
|
||||||
print("");
|
print("");
|
||||||
print("{err:'bad request: [$postbody]'}");
|
print($e->getBody());
|
||||||
print("");
|
print("");
|
||||||
exit();
|
|
||||||
}
|
}
|
||||||
|
exit();
|
||||||
|
|
||||||
\OCP\JSON::success($response);
|
class BadRequestException extends Exception {
|
||||||
|
|
||||||
|
protected $body = "";
|
||||||
|
|
||||||
|
public function setBody($body){
|
||||||
|
$this->body = $body;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getBody(){
|
||||||
|
return $this->body;
|
||||||
|
}
|
||||||
|
}
|
@ -5,10 +5,22 @@ namespace OCA\Office;
|
|||||||
class Request {
|
class Request {
|
||||||
protected $data = array();
|
protected $data = array();
|
||||||
|
|
||||||
|
protected $rawRequest = '';
|
||||||
|
|
||||||
public function __construct(){
|
public function __construct(){
|
||||||
$this->data = json_decode(file_get_contents('php://input'), true);
|
$this->rawRequest = file_get_contents('php://input');
|
||||||
|
$this->data = json_decode($this->rawRequest, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getRawRequest(){
|
||||||
|
return $this->rawRequest;
|
||||||
|
}
|
||||||
|
|
||||||
public function getParam($name){
|
public function getParam($name){
|
||||||
|
if (empty($name)){
|
||||||
|
return $this->data;
|
||||||
|
}
|
||||||
|
|
||||||
$path = explode('/', $name);
|
$path = explode('/', $name);
|
||||||
|
|
||||||
reset($path);
|
reset($path);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user