From 171eda416ab4a1d975499e6e2d19ca5e29e5d840 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20=C4=8Cekan?= Date: Thu, 2 Oct 2014 17:52:39 +0200 Subject: [PATCH 1/7] Update Server.php I cannot choice in class for httpStatus - can send only 200,400 and 500 and its little :( --- RestService/Server.php | 40 +++++++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/RestService/Server.php b/RestService/Server.php index 8436792..55f1dd2 100644 --- a/RestService/Server.php +++ b/RestService/Server.php @@ -61,6 +61,13 @@ class Server * @var Client */ protected $client; + + /** + * Http status code + * + * @var integer + */ + protected $httpStatus; /** * List of excluded methods. @@ -217,6 +224,19 @@ public function setHttpStatusCodes($pWithStatusCode) return $this; } + + /** + * Set custom Http Code + * + * @param integer $httpStatus + * @return Server $this + */ + public function setHttpStatus($httpStatus) + { + $this->httpStatus = $httpStatus; + + return $this; + } /** * @@ -226,6 +246,16 @@ public function getHttpStatusCodes() { return $this->withStatusCode; } + + /** + * Get custom Http Code + * + * @return integer + */ + public function getHttpStatus() + { + return $this->httpStatus; + } /** * Set the check access function/method. @@ -425,7 +455,7 @@ public function sendBadRequest($pCode, $pMessage) if (is_object($pMessage) && $pMessage->xdebug_message) $pMessage = $pMessage->xdebug_message; $msg = array('error' => $pCode, 'message' => $pMessage); if (!$this->getClient()) throw new \Exception('client_not_found_in_ServerController'); - return $this->getClient()->sendResponse('400', $msg); + return $this->getClient()->sendResponse($msg, 400); } /** @@ -440,7 +470,7 @@ public function sendError($pCode, $pMessage) if (is_object($pMessage) && $pMessage->xdebug_message) $pMessage = $pMessage->xdebug_message; $msg = array('error' => $pCode, 'message' => $pMessage); if (!$this->getClient()) throw new \Exception('client_not_found_in_ServerController'); - return $this->getClient()->sendResponse('500', $msg); + return $this->getClient()->sendResponse($msg, 500); } /** @@ -466,7 +496,7 @@ public function sendException($pException) } if (!$this->getClient()) throw new \Exception('Client not found in ServerController'); - return $this->getClient()->sendResponse('500', $msg); + return $this->getClient()->sendResponse($msg, 500); } @@ -680,9 +710,9 @@ public function normalizeUrl(&$pUrl) * * @param $pData */ - public function send($pData) + public function send($pData, $httpStatus = 200) { - return $this->getClient()->sendResponse(200, array('data' => $pData)); + return $this->getClient()->sendResponse(array('data' => $pData), $this->httpStatus? $this->httpStatus : $httpStatus); } /** From 2300e5c2794bf068c59db55054b10392dc3ccd4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20=C4=8Cekan?= Date: Thu, 2 Oct 2014 17:57:58 +0200 Subject: [PATCH 2/7] Update Client.php required parameters must be before optional parameters. --- RestService/Client.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/RestService/Client.php b/RestService/Client.php index 67a5576..3cba9fa 100644 --- a/RestService/Client.php +++ b/RestService/Client.php @@ -126,7 +126,7 @@ public function getController() * @param string $pHttpCode * @param $pMessage */ - public function sendResponse($pHttpCode = '200', $pMessage) + public function sendResponse($pMessage, $pHttpCode = '200') { $suppressStatusCode = isset($_GET['_suppress_status_code']) ? $_GET['_suppress_status_code'] : false; if ($this->controller->getHttpStatusCodes() && From b6f206cec762c8a63c29a1f3820504f080cb23c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20=C4=8Cekan?= Date: Fri, 3 Oct 2014 00:17:12 +0200 Subject: [PATCH 3/7] Update Server.php bug fix --- RestService/Server.php | 77 +++++++++++++++++++++++++----------------- 1 file changed, 46 insertions(+), 31 deletions(-) diff --git a/RestService/Server.php b/RestService/Server.php index 55f1dd2..0c68c24 100644 --- a/RestService/Server.php +++ b/RestService/Server.php @@ -61,13 +61,6 @@ class Server * @var Client */ protected $client; - - /** - * Http status code - * - * @var integer - */ - protected $httpStatus; /** * List of excluded methods. @@ -130,6 +123,13 @@ class Server * @var boolean */ protected $withStatusCode = true; + + /** + * Custom user httpCode + * + * @var integer + */ + protected $httpStatusCode = 200; /** * @var callable @@ -226,15 +226,15 @@ public function setHttpStatusCodes($pWithStatusCode) } /** - * Set custom Http Code - * - * @param integer $httpStatus - * @return Server $this + * Setting up http code + * + * @param integer $httpStatusCode + * @return Server $this */ - public function setHttpStatus($httpStatus) + + public function setHttpStatusCode($httpStatusCode) { - $this->httpStatus = $httpStatus; - + $this->httpStatusCode = $httpStatusCode; return $this; } @@ -248,13 +248,12 @@ public function getHttpStatusCodes() } /** - * Get custom Http Code * * @return integer */ - public function getHttpStatus() + public function getHttpStatusCode() { - return $this->httpStatus; + return $this->httpStatusCode; } /** @@ -452,10 +451,13 @@ public function getClient() */ public function sendBadRequest($pCode, $pMessage) { - if (is_object($pMessage) && $pMessage->xdebug_message) $pMessage = $pMessage->xdebug_message; - $msg = array('error' => $pCode, 'message' => $pMessage); - if (!$this->getClient()) throw new \Exception('client_not_found_in_ServerController'); - return $this->getClient()->sendResponse($msg, 400); + if (is_object($pMessage) && $pMessage->xdebug_message) { + $pMessage = $pMessage->xdebug_message; + } + if (!$this->getClient()) { + throw new \Exception('client_not_found_in_ServerController'); + } + return $this->setHttpStatusCode(400)->send(array('error' => $pCode, 'message' => $pMessage)); } /** @@ -467,10 +469,13 @@ public function sendBadRequest($pCode, $pMessage) */ public function sendError($pCode, $pMessage) { - if (is_object($pMessage) && $pMessage->xdebug_message) $pMessage = $pMessage->xdebug_message; - $msg = array('error' => $pCode, 'message' => $pMessage); - if (!$this->getClient()) throw new \Exception('client_not_found_in_ServerController'); - return $this->getClient()->sendResponse($msg, 500); + if (is_object($pMessage) && $pMessage->xdebug_message) { + $pMessage = $pMessage->xdebug_message; + } + if (!$this->getClient()) { + throw new \Exception('client_not_found_in_ServerController'); + } + return $this->setHttpStatusCode(500)->send(array('error' => $pCode, 'message' => $pMessage)); } /** @@ -485,7 +490,9 @@ public function sendException($pException) } $message = $pException->getMessage(); - if (is_object($message) && $message->xdebug_message) $message = $message->xdebug_message; + if (is_object($message) && $message->xdebug_message) { + $message = $message->xdebug_message; + } $msg = array('error' => get_class($pException), 'message' => $message); @@ -495,9 +502,10 @@ public function sendException($pException) $msg['trace'] = $pException->getTraceAsString(); } - if (!$this->getClient()) throw new \Exception('Client not found in ServerController'); - return $this->getClient()->sendResponse($msg, 500); - + if (!$this->getClient()) { + throw new \Exception('Client not found in ServerController'); + } + return $this->setHttpStatusCode(500)->send($msg); } /** @@ -710,9 +718,16 @@ public function normalizeUrl(&$pUrl) * * @param $pData */ - public function send($pData, $httpStatus = 200) + public function send($pData) { - return $this->getClient()->sendResponse(array('data' => $pData), $this->httpStatus? $this->httpStatus : $httpStatus); + $msg = array(); + if( substr($this->httpStatusCode, 0, 1) == '2' ) + { + $msg['data'] = $pData; + } else { + $msg = $pData; + } + return $this->getClient()->sendResponse($msg, ($this->httpStatusCode? $this->httpStatusCode : 200)); } /** From 76f3d2b1574e12cfc848b410d31aa11a6a5f3934 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20=C4=8Cekan?= Date: Fri, 3 Oct 2014 00:28:08 +0200 Subject: [PATCH 4/7] new http method ;) --- RestService/Client.php | 1 + 1 file changed, 1 insertion(+) diff --git a/RestService/Client.php b/RestService/Client.php index 3cba9fa..4e4bf08 100644 --- a/RestService/Client.php +++ b/RestService/Client.php @@ -83,6 +83,7 @@ class Client 415 => 'Unsupported Media Type', 416 => 'Requested Range Not Satisfiable', 417 => 'Expectation Failed', + 418 => 'I\'m a Tea Pot', 500 => 'Internal Server Error', 501 => 'Not Implemented', 502 => 'Bad Gateway', From 781e614ec0a541f23cc6e2f68c0b58a239d76731 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20=C4=8Cekan?= Date: Fri, 3 Oct 2014 07:17:46 +0200 Subject: [PATCH 5/7] Update InternalClient.php required parameters must be before optional parameters. --- RestService/InternalClient.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/RestService/InternalClient.php b/RestService/InternalClient.php index e9eda18..753aea7 100644 --- a/RestService/InternalClient.php +++ b/RestService/InternalClient.php @@ -10,7 +10,7 @@ */ class InternalClient extends Client { - public function sendResponse($pHttpCode = '200', $pMessage) + public function sendResponse($pMessage, $pHttpCode = '200') { $pMessage = array_reverse($pMessage, true); $pMessage['status'] = $pHttpCode+0; From ff802fbf9e557e5864a1961e097a3126f27b7f37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20=C4=8Cekan?= Date: Fri, 3 Oct 2014 09:54:11 +0200 Subject: [PATCH 6/7] Update Server.php Customize http codes for sendError and sendBadRequest Parsing phpInput from content_type Parsing available from: json string and url string --- RestService/Server.php | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/RestService/Server.php b/RestService/Server.php index 0c68c24..6583197 100644 --- a/RestService/Server.php +++ b/RestService/Server.php @@ -170,6 +170,7 @@ public function __construct($pTriggerUrl, $pControllerClass = null, $pParentCont } else { $this->setClient(new Client($this)); + $this->parsePhpInput(); } $this->setClass($pControllerClass); @@ -451,13 +452,14 @@ public function getClient() */ public function sendBadRequest($pCode, $pMessage) { + $httpCode = ($this->httpStatusCode && substr($this->httpStatusCode, 0, 1) === '4')? $this->httpStatusCode : 400; if (is_object($pMessage) && $pMessage->xdebug_message) { $pMessage = $pMessage->xdebug_message; } if (!$this->getClient()) { throw new \Exception('client_not_found_in_ServerController'); } - return $this->setHttpStatusCode(400)->send(array('error' => $pCode, 'message' => $pMessage)); + return $this->setHttpStatusCode($httpCode)->send(array('error' => $pCode, 'message' => $pMessage)); } /** @@ -469,13 +471,14 @@ public function sendBadRequest($pCode, $pMessage) */ public function sendError($pCode, $pMessage) { + $httpCode = ($this->httpStatusCode && substr($this->httpStatusCode, 0, 1) === '5')? $this->httpStatusCode : 400; if (is_object($pMessage) && $pMessage->xdebug_message) { $pMessage = $pMessage->xdebug_message; } if (!$this->getClient()) { throw new \Exception('client_not_found_in_ServerController'); } - return $this->setHttpStatusCode(500)->send(array('error' => $pCode, 'message' => $pMessage)); + return $this->setHttpStatusCode($httpCode)->send(array('error' => $pCode, 'message' => $pMessage)); } /** @@ -721,13 +724,14 @@ public function normalizeUrl(&$pUrl) public function send($pData) { $msg = array(); - if( substr($this->httpStatusCode, 0, 1) == '2' ) + $httpCode = ($this->httpStatusCode? $this->httpStatusCode : 200); + if( substr($this->httpStatusCode, 0, 1) === '2' ) { $msg['data'] = $pData; } else { $msg = $pData; } - return $this->getClient()->sendResponse($msg, ($this->httpStatusCode? $this->httpStatusCode : 200)); + return $this->getClient()->sendResponse($msg, $httpCode); } /** @@ -1252,5 +1256,29 @@ public function findRoute($pUri, $pMethod = '_all_') return false; } + + protected function parsePhpInput() + { + $input = $this->getPhpInput(); + if($input) + { + $data = array(); + if(isset($_SERVER['CONTENT_TYPE']) && strpos($_SERVER['CONTENT_TYPE'], 'application/json') !== false) + { + $data = (array) json_decode($input); + } + //xml parser ? + if( empty($data) ) + { + parse_str($input, $data); + } + $_POST = array_merge($_POST, $data); + } + } + + protected function getPhpInput() + { + return file_get_contents('php://input'); + } } From 955fc1dc1880876d4870de6f5c5281f671b3af2e Mon Sep 17 00:00:00 2001 From: DJDaca Date: Sat, 4 Oct 2014 01:13:00 +0200 Subject: [PATCH 7/7] repair indication --- RestService/Server.php | 43 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 22 deletions(-) diff --git a/RestService/Server.php b/RestService/Server.php index 6583197..0ed98e4 100644 --- a/RestService/Server.php +++ b/RestService/Server.php @@ -1258,27 +1258,26 @@ public function findRoute($pUri, $pMethod = '_all_') } protected function parsePhpInput() - { - $input = $this->getPhpInput(); - if($input) - { - $data = array(); - if(isset($_SERVER['CONTENT_TYPE']) && strpos($_SERVER['CONTENT_TYPE'], 'application/json') !== false) - { - $data = (array) json_decode($input); - } - //xml parser ? - if( empty($data) ) - { - parse_str($input, $data); - } - $_POST = array_merge($_POST, $data); - } - } - - protected function getPhpInput() - { - return file_get_contents('php://input'); - } + { + $input = $this->getPhpInput(); + if($input) + { + $data = array(); + if(isset($_SERVER['CONTENT_TYPE']) && strpos($_SERVER['CONTENT_TYPE'], 'application/json') !== false) + { + $data = (array) json_decode($input); + } + //xml parser ? + if( empty($data) ) + { + parse_str($input, $data); + } + $_POST = array_merge($_POST, $data); + } + } + protected function getPhpInput() + { + return file_get_contents('php://input'); + } }