diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bff5440 --- /dev/null +++ b/.gitignore @@ -0,0 +1,33 @@ + + + + +################# +## Eclipse +################# + +*.pydevproject +.project +.metadata +bin/ +tmp/ +*.tmp +*.bak +*.swp +*~.nib +local.properties +.classpath +.settings/ +.loadpath + +# External tool builders +.externalToolBuilders/ + +# Locally stored "Eclipse launch configurations" +*.launch + +# CDT-specific +.cproject + +# PDT-specific +.buildpath diff --git a/RestService/Client.php b/RestService/Client.php index feaa63a..4bb1da3 100644 --- a/RestService/Client.php +++ b/RestService/Client.php @@ -120,6 +120,23 @@ public function getController() return $this->controller; } + /** + * @param string $out + */ + protected function output($out){ + if( !ini_get('zlib.output_compression') && 'ob_gzhandler' != ini_get('output_handler') && isset($_SERVER['HTTP_ACCEPT_ENCODING']) ){ + header('Vary: Accept-Encoding'); // Handle proxies + if( false !== stripos($_SERVER['HTTP_ACCEPT_ENCODING'], 'deflate') && function_exists('gzdeflate') ){ + header('Content-Encoding: deflate'); + $out = gzdeflate($out, 3); + } elseif( false !== stripos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') && function_exists('gzencode') ){ + header('Content-Encoding: gzip'); + $out = gzencode($out, 3); + } + } + echo $out; + } + /** * Sends the actual response. * @@ -145,7 +162,7 @@ public function sendResponse($pHttpCode = '200', $pMessage) $pMessage = array_reverse($pMessage, true); $method = $this->getOutputFormatMethod($this->getOutputFormat()); - echo $this->$method($pMessage); + self::output($this->$method($pMessage)); exit; } @@ -237,6 +254,27 @@ public function asJSON($pMessage) return $result; } + function prepareUTF8($matches){ + return json_decode('"'.$matches[1].'"'); + } + + /** + * Format data to JSON + * + * @param string $data The original data to process. + * @return string JSON string. + */ + public function jsonEncode($data) + { + if (defined('JSON_UNESCAPED_UNICODE')) { + return json_encode($data, JSON_UNESCAPED_UNICODE); + }else{ + return preg_replace_callback('/((\\\u[01-9a-fA-F]{4})+)/', + array($this, 'prepareUTF8'), json_encode($data) + ); + } + } + /** * Indents a flat JSON string to make it more human-readable. * @@ -248,7 +286,7 @@ public function asJSON($pMessage) */ public function jsonFormat($json) { - if (!is_string($json)) $json = json_encode($json); + if (!is_string($json)) $json = $this->jsonEncode($json); $result = ''; $pos = 0; diff --git a/Test/Controller/MyRoutes.php b/Test/Controller/MyRoutes.php index ed3e3ec..664ed18 100644 --- a/Test/Controller/MyRoutes.php +++ b/Test/Controller/MyRoutes.php @@ -57,4 +57,13 @@ public function getTest() return 'test'; } + /** + * @param string $text + * @return string + */ + public function postEcho($text) + { + return $text; + } + } diff --git a/Test/Synthetic/UnicodeTest.php b/Test/Synthetic/UnicodeTest.php new file mode 100644 index 0000000..01abd4e --- /dev/null +++ b/Test/Synthetic/UnicodeTest.php @@ -0,0 +1,48 @@ +restService = Server::create('/', new MyRoutes) + ->setClient('RestService\\InternalClient') + ->collectRoutes(); + } + + private function assertEcho($test_string){ + $response = $this->restService->simulateCall("/echo?text=$test_string", 'post'); + $this->assertEquals("{ + \"status\": 200, + \"data\": \"$test_string\" +}", $response); + } + + public function testUnicode() + { + $this->assertEcho('ru: В чащах юга жил бы цитрус? Да, но фальшивый экземпляр!'); + $this->assertEcho('da: Quizdeltagerne spiste jordbær med fløde, mens cirkusklovnen Wolther spillede på xylofon'); + $this->assertEcho('de: Falsches Üben von Xylophonmusik quält jeden größeren Zwerg'); + $this->assertEcho('el: Γαζέες καὶ μυρτιὲς δὲν θὰ βρῶ πιὰ στὸ χρυσαφὶ ξέφωτο'); + $this->assertEcho('es: El pingüino Wenceslao hizo kilómetros bajo exhaustiva lluvia y frío, añoraba a su querido cachorro.'); + $this->assertEcho('fr: Le cœur déçu mais l\'âme plutôt naïve, Louÿs rêva de crapaüter en canoë au delà des îles, près du mälström où brûlent les novæ.'); + $this->assertEcho('ga: D\'fhuascail Íosa, Úrmhac na hÓighe Beannaithe, pór Éava agus Ádhaimh'); + $this->assertEcho('hu: Árvíztűrő tükörfúrógép'); + $this->assertEcho('is: Kæmi ný öxi hér ykist þjófum nú bæði víl og ádrepa'); + $this->assertEcho('jp1: いろはにほへとちりぬるを わかよたれそつねならむ うゐのおくやまけふこえて あさきゆめみしゑひもせす'); + $this->assertEcho('jp2: イロハニホヘト チリヌルヲ ワカヨタレソ ツネナラム ウヰノオクヤマ ケフコエテ アサキユメミシ ヱヒモセスン'); + $this->assertEcho('iw: דג סקרן שט בים מאוכזב ולפתע מצא לו חברה איך הקליטה'); + $this->assertEcho('pl: Pchnąć w tę łódź jeża lub ośm skrzyń fig'); + $this->assertEcho('th: ๏ เป็นมนุษย์สุดประเสริฐเลิศคุณค่า กว่าบรรดาฝูงสัตว์เดรัจฉาน จงฝ่าฟันพัฒนาวิชาการ อย่าล้างผลาญฤๅเข่นฆ่าบีฑาใคร ไม่ถือโทษโกรธแช่งซัดฮึดฮัดด่า หัดอภัยเหมือนกีฬาอัชฌาสัย ปฏิบัติประพฤติกฎกำหนดใจ พูดจาให้จ๊ะๆ จ๋าๆ น่าฟังเอย ฯ'); + $this->assertEcho('tr: Pijamalı hasta, yağız şoföre çabucak güvendi'); + } +} \ No newline at end of file