THRIFT-322: IHttpHandler for Thrift
Client lib: C#
Patch By: nilshu

Adding HTTPHandler to csharp client lib.



git-svn-id: https://svn.apache.org/repos/asf/thrift/trunk@1091921 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/lib/csharp/Makefile.am b/lib/csharp/Makefile.am
index 8110319..17b8eba 100644
--- a/lib/csharp/Makefile.am
+++ b/lib/csharp/Makefile.am
@@ -18,6 +18,7 @@
 #
 
 THRIFTCODE= \
+            /r:System.Web \
             src/Collections/THashSet.cs \
             src/Protocol/TBase.cs \
             src/Protocol/TBase64Utils.cs \
@@ -49,6 +50,7 @@
             src/Transport/TServerSocket.cs \
             src/Transport/TTransportFactory.cs \
             src/Transport/THttpClient.cs \
+            src/Transport/THttpHandler.cs \
             src/TProcessor.cs \
             src/TApplicationException.cs
 
diff --git a/lib/csharp/src/Thrift.csproj b/lib/csharp/src/Thrift.csproj
index 7eeb6fe..23fe018 100644
--- a/lib/csharp/src/Thrift.csproj
+++ b/lib/csharp/src/Thrift.csproj
@@ -52,6 +52,7 @@
     <Reference Include="System.Core">
       <RequiredTargetFramework>3.5</RequiredTargetFramework>
     </Reference>
+    <Reference Include="System.Web" />
   </ItemGroup>
   <ItemGroup>
     <Compile Include="Collections\THashSet.cs" />
@@ -81,6 +82,7 @@
     <Compile Include="Transport\TBufferedTransport.cs" />
     <Compile Include="Transport\TFramedTransport.cs" />
     <Compile Include="Transport\THttpClient.cs" />
+    <Compile Include="Transport\THttpHandler.cs" />
     <Compile Include="Transport\TServerSocket.cs" />
     <Compile Include="Transport\TServerTransport.cs" />
     <Compile Include="Transport\TSocket.cs" />
diff --git a/lib/csharp/src/Transport/THttpHandler.cs b/lib/csharp/src/Transport/THttpHandler.cs
new file mode 100644
index 0000000..7fe750d
--- /dev/null
+++ b/lib/csharp/src/Transport/THttpHandler.cs
@@ -0,0 +1,81 @@
+//
+//  THttpHandler.cs
+//
+//  Authors:
+//		Fredrik Hedberg <fhedberg@availo.com>
+//
+//  Distributed under the Apache Public License
+//
+
+using System;
+using System.Web;
+
+using Thrift.Protocol;
+
+namespace Thrift.Transport
+{
+    public class THttpHandler : IHttpHandler
+    {
+        protected TProcessor processor;
+
+        protected TProtocolFactory inputProtocolFactory;
+        protected TProtocolFactory outputProtocolFactory;
+
+        public THttpHandler(TProcessor processor)
+            : this(processor, new TBinaryProtocol.Factory())
+        {
+
+        }
+        
+        public THttpHandler(TProcessor processor, TProtocolFactory protocolFactory)
+            : this(processor, protocolFactory, protocolFactory)
+        {
+
+        }
+
+        public THttpHandler(TProcessor processor, TProtocolFactory inputProtocolFactory, TProtocolFactory outputProtocolFactory)
+        {
+            this.processor = processor;
+            this.inputProtocolFactory = inputProtocolFactory;
+            this.outputProtocolFactory = outputProtocolFactory;
+        }
+
+        public void ProcessRequest(HttpContext context)
+        {
+            context.Response.ContentType = "application/x-thrift";
+            context.Response.ContentEncoding = System.Text.Encoding.UTF8;
+
+            TTransport transport = new TStreamTransport(context.Request.InputStream, context.Response.OutputStream);
+
+            TProtocol inputProtocol = null;
+            TProtocol outputProtocol = null;
+
+            try
+            {
+                inputProtocol = inputProtocolFactory.GetProtocol(transport);
+                outputProtocol = outputProtocolFactory.GetProtocol(transport);
+
+                while (processor.Process(inputProtocol, outputProtocol)) { }
+            }
+            catch (TTransportException)
+            {
+                // Client died, just move on
+            }
+            catch (TApplicationException tx)
+            {
+                Console.Error.Write(tx);
+            }
+            catch (Exception x)
+            {
+                Console.Error.Write(x);
+            }
+
+            transport.Close();
+        }
+
+        public bool IsReusable
+        {
+            get { return true; }
+        }
+    }
+}