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/tutorial/haxe/Makefile.am b/tutorial/haxe/Makefile.am
index 139bccb..e9c8820 100644
--- a/tutorial/haxe/Makefile.am
+++ b/tutorial/haxe/Makefile.am
@@ -18,17 +18,20 @@
 #
 
 THRIFT = $(top_builddir)/compiler/cpp/thrift
+
+BIN_CPP = bin/Main-debug
 BIN_PHP = bin/php/Main-debug.php
+BIN_PHP_WEB = bin/php-web-server/Main-debug.php
 
 
 gen-haxe/tutorial/calculator.hx gen-haxe/shared/shared_service.hx: $(top_srcdir)/tutorial/tutorial.thrift
 	$(THRIFT) --gen haxe -r $<
 
-all-local: bin/Main-debug $(BIN_PHP)
+all-local: $(BIN_CPP) $(BIN_PHP) $(BIN_PHP_WEB)
 
 check: gen-haxe/tutorial/calculator.hx
 
-bin/Main-debug: \
+$(BIN_CPP): \
 		src/*.hx \
 		../../lib/haxe/src/org/apache/thrift/**/*.hx \
 		gen-haxe/tutorial/calculator.hx
@@ -40,30 +43,42 @@
 		gen-haxe/tutorial/calculator.hx
 	$(HAXE) --cwd .  php.hxml
 
+$(BIN_PHP_WEB): \
+		src/*.hx \
+		../../lib/haxe/src/org/apache/thrift/**/*.hx \
+		gen-haxe/tutorial/calculator.hx
+	$(HAXE) --cwd .  php-web-server.hxml
+
 tutorialserver: all
-	bin/Main-debug server
+	$(BIN_CPP) server
 
 tutorialserver_php: all
 	php -f $(BIN_PHP) server
 
 tutorialclient: all
-	bin/Main-debug
+	$(BIN_CPP)
 
 tutorialclient_php: all
 	php -f $(BIN_PHP) 
 
 tutorialsecureserver: all
-	bin/Main-debug server secure
+	$(BIN_CPP) server secure
 
 tutorialsecureserver_php: all
 	php -f $(BIN_PHP) server secure
 
 tutorialsecureclient: all
-	bin/Main-debug secure
+	$(BIN_CPP) secure
 
 tutorialsecureclient_php: all
 	php -f $(BIN_PHP) secure
 
+tutorialserver_php_http: all
+	php -S 127.0.0.1:9090 router.php
+
+tutorialclient_http: all
+	$(BIN_CPP) client http
+
 clean-local:
 	$(RM) -r gen-haxe bin
 
diff --git a/tutorial/haxe/php-web-server.hxml b/tutorial/haxe/php-web-server.hxml
new file mode 100644
index 0000000..395a852
--- /dev/null
+++ b/tutorial/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/tutorial/haxe/router.php b/tutorial/haxe/router.php
new file mode 100644
index 0000000..e34135c
--- /dev/null
+++ b/tutorial/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/tutorial/haxe/src/Main.hx b/tutorial/haxe/src/Main.hx
index 8f168c1..6bebe71 100644
--- a/tutorial/haxe/src/Main.hx
+++ b/tutorial/haxe/src/Main.hx
@@ -51,7 +51,8 @@
     private static var targetPort : Int = 9090;
 
     static function main() {
-        #if ! (flash || js)
+
+        #if ! (flash || js || phpwebserver)
         try {
               ParseArgs();
         } catch (e : String) {
@@ -59,6 +60,17 @@
             trace(GetHelp());
             return;
         }
+
+        #elseif  phpwebserver
+        //forcing server
+        server = true;
+        trns = http;
+        initPhpWebServer();
+        //check method
+        if(php.Web.getMethod() != 'POST') {
+          Sys.println('http endpoint for thrift test server');
+          return;
+        }
         #end
 
         try {
@@ -73,6 +85,29 @@
         trace("Completed.");
     }
 
+    #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
+
 
     #if ! (flash || js)
 
@@ -154,8 +189,9 @@
              trace('- socket transport $targetHost:$targetPort');
             transport = new TSocket( targetHost, targetPort);
         case http:
-             trace('- HTTP transport $targetHost');
-            transport = new THttpClient( targetHost);
+            var uri = 'http://${targetHost}:${targetPort}';
+            trace('- HTTP transport $uri');
+            transport = new THttpClient(uri);
         default:
             throw "Unhandled transport";
         }
@@ -267,9 +303,20 @@
             transport = new TServerSocket( targetPort);
             #end
         case http:
-            throw "HTTP server not implemented yet";
-             //trace("- http transport");
-             //transport = new THttpClient( targetHost);
+            #if !phpwebserver
+              throw "HTTP server not implemented yet";
+              //trace("- http transport");
+              //transport = new THttpClient( targetHost);
+            #else
+              trace("- http transport");
+              transport = new TWrappingServerTransport(
+                      new TStreamTransport(
+                        new TFileStream("php://input", Read),
+                        new TFileStream("php://output", Append)
+                        )
+                      );
+
+            #end
         default:
             throw "Unhandled transport";
         }
@@ -301,6 +348,10 @@
         var handler = new CalculatorHandler();
         var processor = new CalculatorProcessor(handler);
         var server = new TSimpleServer( processor, transport, transfactory, protfactory);
+        #if phpwebserver
+        server.runOnce = true;
+        #end
+
         return server;
     }
 
@@ -321,4 +372,4 @@
     }
 
 }
- 
\ No newline at end of file
+