From d2f372e0bb3590c0e9fd17b2dd7e6927c89bc894 Mon Sep 17 00:00:00 2001 From: Juvenn Woo Date: Wed, 13 Jan 2016 12:08:08 +0800 Subject: [PATCH 1/4] Fix: get body from slim request instead of response --- src/LeanCloud/Engine/SlimEngine.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/LeanCloud/Engine/SlimEngine.php b/src/LeanCloud/Engine/SlimEngine.php index 51dfca9..a118d96 100644 --- a/src/LeanCloud/Engine/SlimEngine.php +++ b/src/LeanCloud/Engine/SlimEngine.php @@ -32,7 +32,7 @@ protected function getHeaderLine($key) { * @return string */ protected function getBody() { - return $this->response->getBody()->getContents(); + return $this->request->getBody()->getContents(); } /* From 1abdb9ae91b0e51dec0484f93d04e1075a01aea6 Mon Sep 17 00:00:00 2001 From: Juvenn Woo Date: Wed, 13 Jan 2016 14:31:44 +0800 Subject: [PATCH 2/4] LeanEngine: add Laravel middleware --- src/LeanCloud/Engine/LaravelEngine.php | 54 ++++++++++++++++++++++++++ src/LeanCloud/Engine/LeanEngine.php | 26 +++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 src/LeanCloud/Engine/LaravelEngine.php diff --git a/src/LeanCloud/Engine/LaravelEngine.php b/src/LeanCloud/Engine/LaravelEngine.php new file mode 100644 index 0000000..22e7c45 --- /dev/null +++ b/src/LeanCloud/Engine/LaravelEngine.php @@ -0,0 +1,54 @@ +request->header($key); + } + + /** + * Get request body string + * + * @return string + */ + protected function getBody() { + return $this->request->getContent(); + } + + /** + * Laravel middleware entry point + * + * @param \Illuminate\Http\Reuqest $request Laravel request + * @param \Closure $next Laravel closure + * @return mixed + */ + public function handle($request, $next) { + $this->request = $request; + $this->dispatch($request->method(), + $request->url()); + return $next($this->request); + } +} + diff --git a/src/LeanCloud/Engine/LeanEngine.php b/src/LeanCloud/Engine/LeanEngine.php index 47477b1..ed3e590 100644 --- a/src/LeanCloud/Engine/LeanEngine.php +++ b/src/LeanCloud/Engine/LeanEngine.php @@ -152,9 +152,17 @@ private function retrieveHeader($keys) { /** * Extract variant headers into env + * + * PHP prepends `HTTP_` to user-defined headers, so `X-MY-VAR` + * would be populated as `HTTP_X_MY_VAR`. But 3rd party frameworks + * (e.g. Laravel) may overwrite the behavior, and populate it as + * cleaner `X_MY_VAR`. So we try to retrieve header value from both + * versions. + * */ private function parseHeaders() { $this->env["ORIGIN"] = $this->retrieveHeader(array( + "ORIGIN", "HTTP_ORIGIN" )); $this->env["CONTENT_TYPE"] = $this->retrieveHeader(array( @@ -162,37 +170,55 @@ private function parseHeaders() { "HTTP_CONTENT_TYPE" )); $this->env["REMOTE_ADDR"] = $this->retrieveHeader(array( + "X_REAL_IP", "HTTP_X_REAL_IP", + "X_FORWARDED_FOR", "HTTP_X_FORWARDED_FOR", "REMOTE_ADDR" )); $this->env["LC_ID"] = $this->retrieveHeader(array( + "X_LC_ID", "HTTP_X_LC_ID", + "X_AVOSCLOUD_APPLICATION_ID", "HTTP_X_AVOSCLOUD_APPLICATION_ID", + "X_ULURU_APPLICATION_ID", "HTTP_X_ULURU_APPLICATION_ID" )); $this->env["LC_KEY"] = $this->retrieveHeader(array( + "X_LC_KEY", "HTTP_X_LC_KEY", + "X_AVOSCLOUD_APPLICATION_KEY", "HTTP_X_AVOSCLOUD_APPLICATION_KEY", + "X_ULURU_APPLICATION_KEY", "HTTP_X_ULURU_APPLICATION_KEY" )); $this->env["LC_MASTER_KEY"] = $this->retrieveHeader(array( + "X_AVOSCLOUD_MASTER_KEY", "HTTP_X_AVOSCLOUD_MASTER_KEY", + "X_ULURU_MASTER_KEY", "HTTP_X_ULURU_MASTER_KEY" )); $this->env["LC_SESSION"] = $this->retrieveHeader(array( + "X_LC_SESSION", "HTTP_X_LC_SESSION", + "X_AVOSCLOUD_SESSION_TOKEN", "HTTP_X_AVOSCLOUD_SESSION_TOKEN", + "X_ULURU_SESSION_TOKEN", "HTTP_X_ULURU_SESSION_TOKEN" )); $this->env["LC_SIGN"] = $this->retrieveHeader(array( + "X_LC_SIGN", "HTTP_X_LC_SIGN", + "X_AVOSCLOUD_REQUEST_SIGN", "HTTP_X_AVOSCLOUD_REQUEST_SIGN" )); $prod = $this->retrieveHeader(array( + "X_LC_PROD", "HTTP_X_LC_PROD", + "X_AVOSCLOUD_APPLICATION_PRODUCTION", "HTTP_X_AVOSCLOUD_APPLICATION_PRODUCTION", + "X_ULURU_APPLICATION_PRODUCTION", "HTTP_X_ULURU_APPLICATION_PRODUCTION" )); $this->env["useProd"] = true; From 09e0498b7242345c63dc2117808eca4da1a259d3 Mon Sep 17 00:00:00 2001 From: Juvenn Woo Date: Fri, 6 May 2016 16:52:03 +0800 Subject: [PATCH 3/4] LeanEngine: Preserve __type field in hook functions --- src/LeanCloud/Engine/LeanEngine.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/LeanCloud/Engine/LeanEngine.php b/src/LeanCloud/Engine/LeanEngine.php index ed3e590..10312c8 100644 --- a/src/LeanCloud/Engine/LeanEngine.php +++ b/src/LeanCloud/Engine/LeanEngine.php @@ -255,8 +255,10 @@ private function parsePlainBody($body) { true; $this->env["useMaster"] = false; // remove internal fields set by API + // note we need to preserve `__type` field for object decoding + // see #61 forEach($data as $key) { - if ($key[0] === "_") { + if ($key[0] === "_" && $key[1] !== "_") { unset($data[$key]); } } From caacf0503dfc4d2e627862bc374cc236605756bb Mon Sep 17 00:00:00 2001 From: Juvenn Woo Date: Thu, 12 May 2016 17:53:57 +0800 Subject: [PATCH 4/4] Ops metadatas should return result object instead of array --- src/LeanCloud/Engine/LeanEngine.php | 2 +- tests/engine/LeanEngineTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/LeanCloud/Engine/LeanEngine.php b/src/LeanCloud/Engine/LeanEngine.php index 10312c8..b1bfb00 100644 --- a/src/LeanCloud/Engine/LeanEngine.php +++ b/src/LeanCloud/Engine/LeanEngine.php @@ -376,7 +376,7 @@ protected function dispatch($method, $url) { $this->processSession(); if (strpos($pathParts["extra"], "/_ops/metadatas") === 0) { if ($this->env["useMaster"]) { - $this->renderJSON(Cloud::getKeys()); + $this->renderJSON(array("result" => Cloud::getKeys())); } else { $this->renderError("Unauthorized.", 401, 401); } diff --git a/tests/engine/LeanEngineTest.php b/tests/engine/LeanEngineTest.php index f5e591b..3a96841 100644 --- a/tests/engine/LeanEngineTest.php +++ b/tests/engine/LeanEngineTest.php @@ -66,7 +66,7 @@ public function testPingEngine() { public function testGetFuncitonMetadata() { $resp = $this->request("/1/functions/_ops/metadatas", "GET"); - $this->assertContains("hello", $resp); + $this->assertContains("hello", $resp["result"]); } public function testCloudFunctionHello() {