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  */
21 
22 using System;
23 using System.Web;
24 using System.Net;
25 using System.IO;
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 
38         protected const string contentType = "application/x-thrift";
39         protected System.Text.Encoding encoding = System.Text.Encoding.UTF8;
40 
THttpHandler(TProcessor processor)41         public THttpHandler(TProcessor processor)
42             : this(processor, new TBinaryProtocol.Factory())
43         {
44 
45         }
46 
THttpHandler(TProcessor processor, TProtocolFactory protocolFactory)47         public THttpHandler(TProcessor processor, TProtocolFactory protocolFactory)
48             : this(processor, protocolFactory, protocolFactory)
49         {
50 
51         }
52 
THttpHandler(TProcessor processor, TProtocolFactory inputProtocolFactory, TProtocolFactory outputProtocolFactory)53         public THttpHandler(TProcessor processor, TProtocolFactory inputProtocolFactory, TProtocolFactory outputProtocolFactory)
54         {
55             this.processor = processor;
56             this.inputProtocolFactory = inputProtocolFactory;
57             this.outputProtocolFactory = outputProtocolFactory;
58         }
59 
ProcessRequest(HttpListenerContext context)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 
ProcessRequest(HttpContext context)67         public void ProcessRequest(HttpContext context)
68         {
69             context.Response.ContentType = contentType;
70             context.Response.ContentEncoding = encoding;
71             ProcessRequest(context.Request.InputStream, context.Response.OutputStream);
72         }
73 
ProcessRequest(Stream input, Stream output)74         public void ProcessRequest(Stream input, Stream output)
75         {
76             TTransport transport = new TStreamTransport(input,output);
77 
78             try
79             {
80                 var inputProtocol = inputProtocolFactory.GetProtocol(transport);
81                 var outputProtocol = outputProtocolFactory.GetProtocol(transport);
82 
83                 while (processor.Process(inputProtocol, outputProtocol))
84                 {
85                 }
86             }
87             catch (TTransportException)
88             {
89                 // Client died, just move on
90             }
91             finally
92             {
93                 transport.Close();
94             }
95         }
96 
97         public bool IsReusable
98         {
99             get { return true; }
100         }
101     }
102 }
103