Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1 | /** |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | * |
| 19 | * |
| 20 | */ |
Jake Farrell | 6f1e992 | 2011-04-13 21:09:02 +0000 | [diff] [blame] | 21 | |
| 22 | using System; |
| 23 | using System.Web; |
Roger Meier | 212022c | 2011-09-02 21:45:44 +0000 | [diff] [blame] | 24 | using System.Net; |
| 25 | using System.IO; |
Jake Farrell | 6f1e992 | 2011-04-13 21:09:02 +0000 | [diff] [blame] | 26 | |
| 27 | using Thrift.Protocol; |
| 28 | |
| 29 | namespace Thrift.Transport |
| 30 | { |
| 31 | public class THttpHandler : IHttpHandler |
| 32 | { |
| 33 | protected TProcessor processor; |
| 34 | |
| 35 | protected TProtocolFactory inputProtocolFactory; |
| 36 | protected TProtocolFactory outputProtocolFactory; |
| 37 | |
Roger Meier | 212022c | 2011-09-02 21:45:44 +0000 | [diff] [blame] | 38 | protected const string contentType = "application/x-thrift"; |
| 39 | protected System.Text.Encoding encoding = System.Text.Encoding.UTF8; |
| 40 | |
Jake Farrell | 6f1e992 | 2011-04-13 21:09:02 +0000 | [diff] [blame] | 41 | public THttpHandler(TProcessor processor) |
| 42 | : this(processor, new TBinaryProtocol.Factory()) |
| 43 | { |
| 44 | |
| 45 | } |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 46 | |
Jake Farrell | 6f1e992 | 2011-04-13 21:09:02 +0000 | [diff] [blame] | 47 | public THttpHandler(TProcessor processor, TProtocolFactory protocolFactory) |
| 48 | : this(processor, protocolFactory, protocolFactory) |
| 49 | { |
| 50 | |
| 51 | } |
| 52 | |
| 53 | public THttpHandler(TProcessor processor, TProtocolFactory inputProtocolFactory, TProtocolFactory outputProtocolFactory) |
| 54 | { |
| 55 | this.processor = processor; |
| 56 | this.inputProtocolFactory = inputProtocolFactory; |
| 57 | this.outputProtocolFactory = outputProtocolFactory; |
| 58 | } |
| 59 | |
Roger Meier | 212022c | 2011-09-02 21:45:44 +0000 | [diff] [blame] | 60 | public void ProcessRequest(HttpListenerContext context) |
| 61 | { |
| 62 | context.Response.ContentType = contentType; |
| 63 | context.Response.ContentEncoding = encoding; |
| 64 | ProcessRequest(context.Request.InputStream, context.Response.OutputStream); |
| 65 | } |
| 66 | |
Jake Farrell | 6f1e992 | 2011-04-13 21:09:02 +0000 | [diff] [blame] | 67 | public void ProcessRequest(HttpContext context) |
| 68 | { |
Roger Meier | 212022c | 2011-09-02 21:45:44 +0000 | [diff] [blame] | 69 | context.Response.ContentType = contentType; |
| 70 | context.Response.ContentEncoding = encoding; |
| 71 | ProcessRequest(context.Request.InputStream, context.Response.OutputStream); |
| 72 | } |
Jake Farrell | 6f1e992 | 2011-04-13 21:09:02 +0000 | [diff] [blame] | 73 | |
Roger Meier | 212022c | 2011-09-02 21:45:44 +0000 | [diff] [blame] | 74 | public void ProcessRequest(Stream input, Stream output) |
| 75 | { |
| 76 | TTransport transport = new TStreamTransport(input,output); |
Jake Farrell | 6f1e992 | 2011-04-13 21:09:02 +0000 | [diff] [blame] | 77 | |
Jake Farrell | 6f1e992 | 2011-04-13 21:09:02 +0000 | [diff] [blame] | 78 | try |
| 79 | { |
Jens Geyer | 057be5a | 2014-04-16 22:43:08 +0200 | [diff] [blame] | 80 | var inputProtocol = inputProtocolFactory.GetProtocol(transport); |
| 81 | var outputProtocol = outputProtocolFactory.GetProtocol(transport); |
Jake Farrell | 6f1e992 | 2011-04-13 21:09:02 +0000 | [diff] [blame] | 82 | |
Jens Geyer | 057be5a | 2014-04-16 22:43:08 +0200 | [diff] [blame] | 83 | while (processor.Process(inputProtocol, outputProtocol)) |
| 84 | { |
| 85 | } |
Jake Farrell | 6f1e992 | 2011-04-13 21:09:02 +0000 | [diff] [blame] | 86 | } |
| 87 | catch (TTransportException) |
| 88 | { |
| 89 | // Client died, just move on |
| 90 | } |
Jens Geyer | 057be5a | 2014-04-16 22:43:08 +0200 | [diff] [blame] | 91 | finally |
Jake Farrell | 6f1e992 | 2011-04-13 21:09:02 +0000 | [diff] [blame] | 92 | { |
Jens Geyer | 057be5a | 2014-04-16 22:43:08 +0200 | [diff] [blame] | 93 | transport.Close(); |
Jake Farrell | 6f1e992 | 2011-04-13 21:09:02 +0000 | [diff] [blame] | 94 | } |
Jake Farrell | 6f1e992 | 2011-04-13 21:09:02 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | public bool IsReusable |
| 98 | { |
| 99 | get { return true; } |
| 100 | } |
| 101 | } |
| 102 | } |