THRIFT-3833 haxe http server implementation (by embeding into php web server)
Client: Haxe
Patch: Oleksii Prudkyi <Oleksii.Prudkyi@gmail.com> + some modifications by Jens Geyer

This closes #1013
This closes #1020
diff --git a/test/haxe/Makefile.am b/test/haxe/Makefile.am
index 6094452..1a32185 100644
--- a/test/haxe/Makefile.am
+++ b/test/haxe/Makefile.am
@@ -23,11 +23,12 @@
 
 BIN_CPP = bin/Main-debug
 BIN_PHP = bin/php/Main-debug.php
+BIN_PHP_WEB = bin/php-web-server/Main-debug.php
 
 gen-haxe/thrift/test/ThriftTest.hx: $(THRIFTTEST)
 	$(THRIFTCMD) $(THRIFTTEST)
 
-all-local: $(BIN_CPP) $(BIN_PHP)
+all-local: $(BIN_CPP) $(BIN_PHP) $(BIN_PHP_WEB)
 
 $(BIN_CPP): \
 		src/*.hx \
@@ -41,6 +42,11 @@
 		gen-haxe/thrift/test/ThriftTest.hx
 	$(HAXE) --cwd .  php.hxml
 
+$(BIN_PHP_WEB): \
+		src/*.hx \
+		../../lib/haxe/src/org/apache/thrift/**/*.hx \
+		gen-haxe/thrift/test/ThriftTest.hx
+	$(HAXE) --cwd .  php-web-server.hxml
 
 
 
@@ -56,20 +62,30 @@
 clean-local:
 	$(RM) -r gen-haxe bin
 
-check: check_cpp check_php
+.NOTPARALLEL:
+
+check: check_cpp \
+	check_php \
+	check_php_web 
 
 check_cpp: $(BIN_CPP) 
-	timeout 10 $(BIN_CPP) server &
+	timeout 20 $(BIN_CPP) server &
 	sleep 1
 	$(BIN_CPP) client
 	sleep 10
 
 check_php: $(BIN_PHP) 
-	timeout 10 php -f $(BIN_PHP) server &
+	timeout 20 php -f $(BIN_PHP) server &
 	sleep 1
 	php -f $(BIN_PHP) client
 	sleep 10
 
+check_php_web: $(BIN_PHP_WEB) $(BIN_CPP)
+	timeout 20 php -S 127.0.0.1:9090 router.php &
+	sleep 1
+	$(BIN_CPP) client --transport http
+	sleep 10
+
 
 EXTRA_DIST = \
              src \
diff --git a/test/haxe/php-web-server.hxml b/test/haxe/php-web-server.hxml
new file mode 100644
index 0000000..395a852
--- /dev/null
+++ b/test/haxe/php-web-server.hxml
@@ -0,0 +1,43 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+ 
+#integrate files to classpath
+-cp src
+-cp gen-haxe
+-cp ../../lib/haxe/src
+
+#this class wil be used as entry point for your app.
+-main Main
+
+#PHP target
+-php bin/php-web-server/
+--php-front Main-debug.php
+
+#defines
+-D phpwebserver
+
+
+#Add debug information
+-debug
+
+#dead code elimination : remove unused code
+#"-dce no" : do not remove unused code
+#"-dce std" : remove unused code in the std lib (default)
+#"-dce full" : remove all unused code
+-dce full
diff --git a/test/haxe/router.php b/test/haxe/router.php
new file mode 100644
index 0000000..e34135c
--- /dev/null
+++ b/test/haxe/router.php
@@ -0,0 +1,31 @@
+<?php
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ * @package thrift
+ */
+
+
+
+//router file to run testing web server
+
+//set_time_limit(1);
+
+require_once  dirname(__FILE__) . '/bin/php-web-server/Main-debug.php';
+
+
diff --git a/test/haxe/src/Arguments.hx b/test/haxe/src/Arguments.hx
index cae91df..cc10749 100644
--- a/test/haxe/src/Arguments.hx
+++ b/test/haxe/src/Arguments.hx
@@ -72,12 +72,18 @@
 
     public function new() {
         #if sys
-        try {
+          #if !phpwebserver
+          try {
               ParseArgs();
-        } catch (e : String) {
+          } catch (e : String) {
             trace(GetHelp());
             throw e;
-        }
+          }
+          #else
+            //forcing server
+            server = true;
+            transport = http;
+          #end
         #else
         trace("WN: Platform does not support program arguments, using defaults.");
         #end
diff --git a/test/haxe/src/Main.hx b/test/haxe/src/Main.hx
index 30c04a6..9eb828f 100644
--- a/test/haxe/src/Main.hx
+++ b/test/haxe/src/Main.hx
@@ -31,6 +31,15 @@
 class Main
 {
     static function main() {
+        #if phpwebserver
+        initPhpWebServer();
+        //check method
+        if(php.Web.getMethod() != 'POST') {
+          Sys.println('http endpoint for thrift test server');
+          return;
+        }
+        #end
+
         try {
             var args = new Arguments();
 
@@ -48,4 +57,27 @@
         }
     }
 
+    #if phpwebserver
+    private static function initPhpWebServer()
+    {
+        //remap trace to error log
+        haxe.Log.trace = function(v:Dynamic, ?infos:haxe.PosInfos)
+        {
+          // handle trace
+          var newValue : Dynamic;
+          if (infos != null && infos.customParams!=null) {
+            var extra:String = "";
+            for( v in infos.customParams )
+              extra += "," + v;
+            newValue = v + extra;
+          }
+          else {
+            newValue = v;
+          }
+          var msg = infos != null ? infos.fileName + ':' + infos.lineNumber + ': ' : '';
+          Sys.stderr().writeString('${msg}${newValue}\n');
+        }
+    }
+    #end
+
 }
diff --git a/test/haxe/src/TestClient.hx b/test/haxe/src/TestClient.hx
index 9436865..aa496dc 100644
--- a/test/haxe/src/TestClient.hx
+++ b/test/haxe/src/TestClient.hx
@@ -212,7 +212,9 @@
             case socket:
                 transport = new TSocket(args.host, args.port);
             case http:
-                transport = new THttpClient(args.host);
+                var uri = 'http://${args.host}:${args.port}';
+                trace('- http client : ${uri}');
+                transport = new THttpClient(uri);
             default:
                 throw "Unhandled transport";
         }
diff --git a/test/haxe/src/TestServer.hx b/test/haxe/src/TestServer.hx
index 8f4604a..450c8f2 100644
--- a/test/haxe/src/TestServer.hx
+++ b/test/haxe/src/TestServer.hx
@@ -42,8 +42,18 @@
                 transport = new TServerSocket( args.port);
             case http:
                 trace("- http");
-                throw "HTTP server not implemented yet";
+                #if !phpwebserver
+                  throw "HTTP server not implemented yet";
                  //transport = new THttpServer( targetHost);
+                #else
+                transport =    new TWrappingServerTransport(
+                        new TStreamTransport(
+                          new TFileStream("php://input", Read),
+                          new TFileStream("php://output", Append)
+                          )
+                        );
+
+                #end
             default:
                 throw "Unhandled transport";
             }
@@ -84,7 +94,12 @@
             switch( args.servertype)
             {
             case simple:
-                server = new TSimpleServer( processor, transport, transfactory, protfactory);
+                var simpleServer = new TSimpleServer( processor, transport, transfactory, protfactory);
+                #if phpwebserver
+                simpleServer.runOnce = true;
+                #end
+                server = simpleServer;
+
             default:
                 throw "Unhandled server type";
             }