Jake Farrell | 6f1e992 | 2011-04-13 21:09:02 +0000 | [diff] [blame^] | 1 | // |
| 2 | // THttpHandler.cs |
| 3 | // |
| 4 | // Authors: |
| 5 | // Fredrik Hedberg <fhedberg@availo.com> |
| 6 | // |
| 7 | // Distributed under the Apache Public License |
| 8 | // |
| 9 | |
| 10 | using System; |
| 11 | using System.Web; |
| 12 | |
| 13 | using Thrift.Protocol; |
| 14 | |
| 15 | namespace Thrift.Transport |
| 16 | { |
| 17 | public class THttpHandler : IHttpHandler |
| 18 | { |
| 19 | protected TProcessor processor; |
| 20 | |
| 21 | protected TProtocolFactory inputProtocolFactory; |
| 22 | protected TProtocolFactory outputProtocolFactory; |
| 23 | |
| 24 | public THttpHandler(TProcessor processor) |
| 25 | : this(processor, new TBinaryProtocol.Factory()) |
| 26 | { |
| 27 | |
| 28 | } |
| 29 | |
| 30 | public THttpHandler(TProcessor processor, TProtocolFactory protocolFactory) |
| 31 | : this(processor, protocolFactory, protocolFactory) |
| 32 | { |
| 33 | |
| 34 | } |
| 35 | |
| 36 | public THttpHandler(TProcessor processor, TProtocolFactory inputProtocolFactory, TProtocolFactory outputProtocolFactory) |
| 37 | { |
| 38 | this.processor = processor; |
| 39 | this.inputProtocolFactory = inputProtocolFactory; |
| 40 | this.outputProtocolFactory = outputProtocolFactory; |
| 41 | } |
| 42 | |
| 43 | public void ProcessRequest(HttpContext context) |
| 44 | { |
| 45 | context.Response.ContentType = "application/x-thrift"; |
| 46 | context.Response.ContentEncoding = System.Text.Encoding.UTF8; |
| 47 | |
| 48 | TTransport transport = new TStreamTransport(context.Request.InputStream, context.Response.OutputStream); |
| 49 | |
| 50 | TProtocol inputProtocol = null; |
| 51 | TProtocol outputProtocol = null; |
| 52 | |
| 53 | try |
| 54 | { |
| 55 | inputProtocol = inputProtocolFactory.GetProtocol(transport); |
| 56 | outputProtocol = outputProtocolFactory.GetProtocol(transport); |
| 57 | |
| 58 | while (processor.Process(inputProtocol, outputProtocol)) { } |
| 59 | } |
| 60 | catch (TTransportException) |
| 61 | { |
| 62 | // Client died, just move on |
| 63 | } |
| 64 | catch (TApplicationException tx) |
| 65 | { |
| 66 | Console.Error.Write(tx); |
| 67 | } |
| 68 | catch (Exception x) |
| 69 | { |
| 70 | Console.Error.Write(x); |
| 71 | } |
| 72 | |
| 73 | transport.Close(); |
| 74 | } |
| 75 | |
| 76 | public bool IsReusable |
| 77 | { |
| 78 | get { return true; } |
| 79 | } |
| 80 | } |
| 81 | } |