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..b1bfb00 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; @@ -229,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]); } } @@ -348,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/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(); } /* 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() {