blob: 4115ef95aaa6dbdfbf00d1a63f7f6340457e97ea [file] [log] [blame]
Jens Geyerd5436f52014-10-03 19:50:38 +02001/**
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 Farrell6f1e9922011-04-13 21:09:02 +000021
22using System;
23using System.Web;
Roger Meier212022c2011-09-02 21:45:44 +000024using System.Net;
25using System.IO;
Jake Farrell6f1e9922011-04-13 21:09:02 +000026
27using Thrift.Protocol;
28
29namespace Thrift.Transport
30{
31 public class THttpHandler : IHttpHandler
32 {
33 protected TProcessor processor;
34
35 protected TProtocolFactory inputProtocolFactory;
36 protected TProtocolFactory outputProtocolFactory;
37
Roger Meier212022c2011-09-02 21:45:44 +000038 protected const string contentType = "application/x-thrift";
39 protected System.Text.Encoding encoding = System.Text.Encoding.UTF8;
40
Jake Farrell6f1e9922011-04-13 21:09:02 +000041 public THttpHandler(TProcessor processor)
42 : this(processor, new TBinaryProtocol.Factory())
43 {
44
45 }
Jens Geyerd5436f52014-10-03 19:50:38 +020046
Jake Farrell6f1e9922011-04-13 21:09:02 +000047 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 Meier212022c2011-09-02 21:45:44 +000060 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 Farrell6f1e9922011-04-13 21:09:02 +000067 public void ProcessRequest(HttpContext context)
68 {
Roger Meier212022c2011-09-02 21:45:44 +000069 context.Response.ContentType = contentType;
70 context.Response.ContentEncoding = encoding;
71 ProcessRequest(context.Request.InputStream, context.Response.OutputStream);
72 }
Jake Farrell6f1e9922011-04-13 21:09:02 +000073
Roger Meier212022c2011-09-02 21:45:44 +000074 public void ProcessRequest(Stream input, Stream output)
75 {
76 TTransport transport = new TStreamTransport(input,output);
Jake Farrell6f1e9922011-04-13 21:09:02 +000077
Jake Farrell6f1e9922011-04-13 21:09:02 +000078 try
79 {
Jens Geyer057be5a2014-04-16 22:43:08 +020080 var inputProtocol = inputProtocolFactory.GetProtocol(transport);
81 var outputProtocol = outputProtocolFactory.GetProtocol(transport);
Jake Farrell6f1e9922011-04-13 21:09:02 +000082
Jens Geyer057be5a2014-04-16 22:43:08 +020083 while (processor.Process(inputProtocol, outputProtocol))
84 {
85 }
Jake Farrell6f1e9922011-04-13 21:09:02 +000086 }
87 catch (TTransportException)
88 {
89 // Client died, just move on
90 }
Jens Geyer057be5a2014-04-16 22:43:08 +020091 finally
Jake Farrell6f1e9922011-04-13 21:09:02 +000092 {
Jens Geyer057be5a2014-04-16 22:43:08 +020093 transport.Close();
Jake Farrell6f1e9922011-04-13 21:09:02 +000094 }
Jake Farrell6f1e9922011-04-13 21:09:02 +000095 }
96
97 public bool IsReusable
98 {
99 get { return true; }
100 }
101 }
102}