Author: jfarrell
Date: Wed Apr 13 21:09:02 2011
New Revision: 1091921

URL: http://svn.apache.org/viewvc?rev=1091921&view=rev
Log:
THRIFT-322: IHttpHandler for Thrift
Client lib: C#
Patch By: nilshu

Adding HTTPHandler to csharp client lib.


Added:
    thrift/trunk/lib/csharp/src/Transport/THttpHandler.cs
Modified:
    thrift/trunk/lib/csharp/Makefile.am
    thrift/trunk/lib/csharp/src/Thrift.csproj

Modified: thrift/trunk/lib/csharp/Makefile.am
URL: 
http://svn.apache.org/viewvc/thrift/trunk/lib/csharp/Makefile.am?rev=1091921&r1=1091920&r2=1091921&view=diff
==============================================================================
--- thrift/trunk/lib/csharp/Makefile.am (original)
+++ thrift/trunk/lib/csharp/Makefile.am Wed Apr 13 21:09:02 2011
@@ -18,6 +18,7 @@
 #
 
 THRIFTCODE= \
+            /r:System.Web \
             src/Collections/THashSet.cs \
             src/Protocol/TBase.cs \
             src/Protocol/TBase64Utils.cs \
@@ -49,6 +50,7 @@ THRIFTCODE= \
             src/Transport/TServerSocket.cs \
             src/Transport/TTransportFactory.cs \
             src/Transport/THttpClient.cs \
+            src/Transport/THttpHandler.cs \
             src/TProcessor.cs \
             src/TApplicationException.cs
 

Modified: thrift/trunk/lib/csharp/src/Thrift.csproj
URL: 
http://svn.apache.org/viewvc/thrift/trunk/lib/csharp/src/Thrift.csproj?rev=1091921&r1=1091920&r2=1091921&view=diff
==============================================================================
--- thrift/trunk/lib/csharp/src/Thrift.csproj (original)
+++ thrift/trunk/lib/csharp/src/Thrift.csproj Wed Apr 13 21:09:02 2011
@@ -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" />

Added: thrift/trunk/lib/csharp/src/Transport/THttpHandler.cs
URL: 
http://svn.apache.org/viewvc/thrift/trunk/lib/csharp/src/Transport/THttpHandler.cs?rev=1091921&view=auto
==============================================================================
--- thrift/trunk/lib/csharp/src/Transport/THttpHandler.cs (added)
+++ thrift/trunk/lib/csharp/src/Transport/THttpHandler.cs Wed Apr 13 21:09:02 
2011
@@ -0,0 +1,81 @@
+//
+//  THttpHandler.cs
+//
+//  Authors:
+//             Fredrik Hedberg <[email protected]>
+//
+//  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; }
+        }
+    }
+}


Reply via email to