From 6f1e992f6c9a55d9584de1f2195c7295045e328e Mon Sep 17 00:00:00 2001 From: Jake Farrell Date: Wed, 13 Apr 2011 21:09:02 +0000 Subject: [PATCH] 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 --- lib/csharp/Makefile.am | 2 + lib/csharp/src/Thrift.csproj | 2 + lib/csharp/src/Transport/THttpHandler.cs | 81 ++++++++++++++++++++++++ 3 files changed, 85 insertions(+) create mode 100644 lib/csharp/src/Transport/THttpHandler.cs diff --git a/lib/csharp/Makefile.am b/lib/csharp/Makefile.am index 8110319c..17b8eba4 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 @@ THRIFTCODE= \ 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 7eeb6fed..23fe0185 100644 --- a/lib/csharp/src/Thrift.csproj +++ b/lib/csharp/src/Thrift.csproj @@ -52,6 +52,7 @@ 3.5 + @@ -81,6 +82,7 @@ + diff --git a/lib/csharp/src/Transport/THttpHandler.cs b/lib/csharp/src/Transport/THttpHandler.cs new file mode 100644 index 00000000..7fe750de --- /dev/null +++ b/lib/csharp/src/Transport/THttpHandler.cs @@ -0,0 +1,81 @@ +// +// THttpHandler.cs +// +// Authors: +// Fredrik Hedberg +// +// 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; } + } + } +} -- 2.17.1