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; |
Roger Meier | 212022c | 2011-09-02 21:45:44 +0000 | [diff] [blame] | 12 | using System.Net; |
| 13 | using System.IO; |
Jake Farrell | 6f1e992 | 2011-04-13 21:09:02 +0000 | [diff] [blame] | 14 | |
| 15 | using Thrift.Protocol; |
| 16 | |
| 17 | namespace Thrift.Transport |
| 18 | { |
| 19 | public class THttpHandler : IHttpHandler |
| 20 | { |
| 21 | protected TProcessor processor; |
| 22 | |
| 23 | protected TProtocolFactory inputProtocolFactory; |
| 24 | protected TProtocolFactory outputProtocolFactory; |
| 25 | |
Roger Meier | 212022c | 2011-09-02 21:45:44 +0000 | [diff] [blame] | 26 | protected const string contentType = "application/x-thrift"; |
| 27 | protected System.Text.Encoding encoding = System.Text.Encoding.UTF8; |
| 28 | |
Jake Farrell | 6f1e992 | 2011-04-13 21:09:02 +0000 | [diff] [blame] | 29 | public THttpHandler(TProcessor processor) |
| 30 | : this(processor, new TBinaryProtocol.Factory()) |
| 31 | { |
| 32 | |
| 33 | } |
| 34 | |
| 35 | public THttpHandler(TProcessor processor, TProtocolFactory protocolFactory) |
| 36 | : this(processor, protocolFactory, protocolFactory) |
| 37 | { |
| 38 | |
| 39 | } |
| 40 | |
| 41 | public THttpHandler(TProcessor processor, TProtocolFactory inputProtocolFactory, TProtocolFactory outputProtocolFactory) |
| 42 | { |
| 43 | this.processor = processor; |
| 44 | this.inputProtocolFactory = inputProtocolFactory; |
| 45 | this.outputProtocolFactory = outputProtocolFactory; |
| 46 | } |
| 47 | |
Roger Meier | 212022c | 2011-09-02 21:45:44 +0000 | [diff] [blame] | 48 | public void ProcessRequest(HttpListenerContext context) |
| 49 | { |
| 50 | context.Response.ContentType = contentType; |
| 51 | context.Response.ContentEncoding = encoding; |
| 52 | ProcessRequest(context.Request.InputStream, context.Response.OutputStream); |
| 53 | } |
| 54 | |
Jake Farrell | 6f1e992 | 2011-04-13 21:09:02 +0000 | [diff] [blame] | 55 | public void ProcessRequest(HttpContext context) |
| 56 | { |
Roger Meier | 212022c | 2011-09-02 21:45:44 +0000 | [diff] [blame] | 57 | context.Response.ContentType = contentType; |
| 58 | context.Response.ContentEncoding = encoding; |
| 59 | ProcessRequest(context.Request.InputStream, context.Response.OutputStream); |
| 60 | } |
Jake Farrell | 6f1e992 | 2011-04-13 21:09:02 +0000 | [diff] [blame] | 61 | |
Roger Meier | 212022c | 2011-09-02 21:45:44 +0000 | [diff] [blame] | 62 | public void ProcessRequest(Stream input, Stream output) |
| 63 | { |
| 64 | TTransport transport = new TStreamTransport(input,output); |
Jake Farrell | 6f1e992 | 2011-04-13 21:09:02 +0000 | [diff] [blame] | 65 | |
Jake Farrell | 6f1e992 | 2011-04-13 21:09:02 +0000 | [diff] [blame] | 66 | try |
| 67 | { |
Jens Geyer | 057be5a | 2014-04-16 22:43:08 +0200 | [diff] [blame^] | 68 | var inputProtocol = inputProtocolFactory.GetProtocol(transport); |
| 69 | var outputProtocol = outputProtocolFactory.GetProtocol(transport); |
Jake Farrell | 6f1e992 | 2011-04-13 21:09:02 +0000 | [diff] [blame] | 70 | |
Jens Geyer | 057be5a | 2014-04-16 22:43:08 +0200 | [diff] [blame^] | 71 | while (processor.Process(inputProtocol, outputProtocol)) |
| 72 | { |
| 73 | } |
Jake Farrell | 6f1e992 | 2011-04-13 21:09:02 +0000 | [diff] [blame] | 74 | } |
| 75 | catch (TTransportException) |
| 76 | { |
| 77 | // Client died, just move on |
| 78 | } |
Jens Geyer | 057be5a | 2014-04-16 22:43:08 +0200 | [diff] [blame^] | 79 | finally |
Jake Farrell | 6f1e992 | 2011-04-13 21:09:02 +0000 | [diff] [blame] | 80 | { |
Jens Geyer | 057be5a | 2014-04-16 22:43:08 +0200 | [diff] [blame^] | 81 | transport.Close(); |
Jake Farrell | 6f1e992 | 2011-04-13 21:09:02 +0000 | [diff] [blame] | 82 | } |
Jake Farrell | 6f1e992 | 2011-04-13 21:09:02 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | public bool IsReusable |
| 86 | { |
| 87 | get { return true; } |
| 88 | } |
| 89 | } |
| 90 | } |