blob: 106f840ddbb3506e914cd668299b220b67fa7db4 [file] [log] [blame]
Bryan Duxbury62359472010-06-24 20:34:34 +00001/**
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
22using System;
23using System.Collections.Generic;
24using System.IO;
25using System.Net;
Roger Meier284a9b52011-12-08 13:39:56 +000026using System.Threading;
Bryan Duxbury62359472010-06-24 20:34:34 +000027
28namespace Thrift.Transport
29{
Roger Meierb1ec4cc2012-04-11 21:21:41 +000030 public class THttpClient : TTransport, IDisposable
Bryan Duxbury62359472010-06-24 20:34:34 +000031 {
32 private readonly Uri uri;
33 private Stream inputStream;
34 private MemoryStream outputStream = new MemoryStream();
35 private int connectTimeout = 0;
36 private int readTimeout = 0;
37 private IDictionary<String, String> customHeaders = new Dictionary<string, string>();
38
39 public THttpClient(Uri u)
40 {
41 uri = u;
42 }
43
44 public int ConnectTimeout
45 {
46 set
47 {
48 connectTimeout = value;
49 }
50 }
51
52 public int ReadTimeout
53 {
54 set
55 {
56 readTimeout = value;
57 }
58 }
59
60 public IDictionary<String, String> CustomHeaders
61 {
62 get
63 {
64 return customHeaders;
65 }
66 }
67
68 public override bool IsOpen
69 {
70 get
71 {
72 return true;
73 }
74 }
75
76 public override void Open()
77 {
78 }
79
80 public override void Close()
81 {
82 if (inputStream != null)
83 {
84 inputStream.Close();
85 inputStream = null;
86 }
87 if (outputStream != null)
88 {
89 outputStream.Close();
90 outputStream = null;
91 }
92 }
93
94 public override int Read(byte[] buf, int off, int len)
95 {
96 if (inputStream == null)
97 {
98 throw new TTransportException(TTransportException.ExceptionType.NotOpen, "No request has been sent");
99 }
100
101 try
102 {
103 int ret = inputStream.Read(buf, off, len);
104
105 if (ret == -1)
106 {
107 throw new TTransportException(TTransportException.ExceptionType.EndOfFile, "No more data available");
108 }
109
110 return ret;
111 }
112 catch (IOException iox)
113 {
114 throw new TTransportException(TTransportException.ExceptionType.Unknown, iox.ToString());
115 }
116 }
117
118 public override void Write(byte[] buf, int off, int len)
119 {
120 outputStream.Write(buf, off, len);
121 }
122
Roger Meier284a9b52011-12-08 13:39:56 +0000123#if !SILVERLIGHT
Bryan Duxbury62359472010-06-24 20:34:34 +0000124 public override void Flush()
125 {
Bryan Duxburyea67a782010-08-06 17:50:51 +0000126 try
127 {
128 SendRequest();
129 }
130 finally
131 {
132 outputStream = new MemoryStream();
133 }
Bryan Duxbury62359472010-06-24 20:34:34 +0000134 }
135
136 private void SendRequest()
137 {
138 try
139 {
140 HttpWebRequest connection = CreateRequest();
141
142 byte[] data = outputStream.ToArray();
143 connection.ContentLength = data.Length;
144
Roger Meierb1ec4cc2012-04-11 21:21:41 +0000145 using (Stream requestStream = connection.GetRequestStream())
146 {
147 requestStream.Write(data, 0, data.Length);
148 inputStream = connection.GetResponse().GetResponseStream();
149 }
Bryan Duxbury62359472010-06-24 20:34:34 +0000150 }
151 catch (IOException iox)
152 {
153 throw new TTransportException(TTransportException.ExceptionType.Unknown, iox.ToString());
154 }
155 catch (WebException wx)
156 {
157 throw new TTransportException(TTransportException.ExceptionType.Unknown, "Couldn't connect to server: " + wx);
158 }
159 }
Roger Meier284a9b52011-12-08 13:39:56 +0000160#endif
Roger Meierb1ec4cc2012-04-11 21:21:41 +0000161 private HttpWebRequest CreateRequest()
Bryan Duxbury62359472010-06-24 20:34:34 +0000162 {
163 HttpWebRequest connection = (HttpWebRequest)WebRequest.Create(uri);
164
Roger Meier284a9b52011-12-08 13:39:56 +0000165#if !SILVERLIGHT
Bryan Duxbury62359472010-06-24 20:34:34 +0000166 if (connectTimeout > 0)
167 {
168 connection.Timeout = connectTimeout;
169 }
170 if (readTimeout > 0)
171 {
172 connection.ReadWriteTimeout = readTimeout;
173 }
Roger Meier284a9b52011-12-08 13:39:56 +0000174#endif
Bryan Duxbury62359472010-06-24 20:34:34 +0000175 // Make the request
176 connection.ContentType = "application/x-thrift";
177 connection.Accept = "application/x-thrift";
178 connection.UserAgent = "C#/THttpClient";
179 connection.Method = "POST";
Roger Meier284a9b52011-12-08 13:39:56 +0000180#if !SILVERLIGHT
Bryan Duxbury62359472010-06-24 20:34:34 +0000181 connection.ProtocolVersion = HttpVersion.Version10;
Roger Meier284a9b52011-12-08 13:39:56 +0000182#endif
Bryan Duxbury62359472010-06-24 20:34:34 +0000183
Roger Meier284a9b52011-12-08 13:39:56 +0000184 //add custom headers here
Bryan Duxbury62359472010-06-24 20:34:34 +0000185 foreach (KeyValuePair<string, string> item in customHeaders)
186 {
Roger Meier284a9b52011-12-08 13:39:56 +0000187#if !SILVERLIGHT
Bryan Duxbury62359472010-06-24 20:34:34 +0000188 connection.Headers.Add(item.Key, item.Value);
Roger Meier284a9b52011-12-08 13:39:56 +0000189#else
190 connection.Headers[item.Key] = item.Value;
191#endif
Bryan Duxbury62359472010-06-24 20:34:34 +0000192 }
193
Jake Farrell12ac2ac2011-12-09 02:21:37 +0000194#if !SILVERLIGHT
Bryan Duxbury62359472010-06-24 20:34:34 +0000195 connection.Proxy = null;
Jake Farrell12ac2ac2011-12-09 02:21:37 +0000196#endif
Bryan Duxbury62359472010-06-24 20:34:34 +0000197
Roger Meier284a9b52011-12-08 13:39:56 +0000198 return connection;
Bryan Duxbury62359472010-06-24 20:34:34 +0000199 }
Roger Meier284a9b52011-12-08 13:39:56 +0000200
201#if SILVERLIGHT
202 public override IAsyncResult BeginFlush(AsyncCallback callback, object state)
203 {
204 // Extract request and reset buffer
205 var data = outputStream.ToArray();
206
207 //requestBuffer_ = new MemoryStream();
208
209 try
210 {
211 // Create connection object
212 var flushAsyncResult = new FlushAsyncResult(callback, state);
213 flushAsyncResult.Connection = CreateRequest();
214
215 flushAsyncResult.Data = data;
216
217
218 flushAsyncResult.Connection.BeginGetRequestStream(GetRequestStreamCallback, flushAsyncResult);
219 return flushAsyncResult;
220
221 }
222 catch (IOException iox)
223 {
224 throw new TTransportException(iox.ToString());
225 }
226 }
227
228 public override void EndFlush(IAsyncResult asyncResult)
229 {
230 try
231 {
232 var flushAsyncResult = (FlushAsyncResult) asyncResult;
233
234 if (!flushAsyncResult.IsCompleted)
235 {
236 var waitHandle = flushAsyncResult.AsyncWaitHandle;
237 waitHandle.WaitOne(); // blocking INFINITEly
238 waitHandle.Close();
239 }
240
241 if (flushAsyncResult.AsyncException != null)
242 {
243 throw flushAsyncResult.AsyncException;
244 }
245 } finally
246 {
247 outputStream = new MemoryStream();
248 }
249
250 }
251
252
253 private void GetRequestStreamCallback(IAsyncResult asynchronousResult)
254 {
255 var flushAsyncResult = (FlushAsyncResult)asynchronousResult.AsyncState;
256 try
257 {
258 var reqStream = flushAsyncResult.Connection.EndGetRequestStream(asynchronousResult);
259 reqStream.Write(flushAsyncResult.Data, 0, flushAsyncResult.Data.Length);
260 reqStream.Flush();
261 reqStream.Close();
262
263 // Start the asynchronous operation to get the response
264 flushAsyncResult.Connection.BeginGetResponse(GetResponseCallback, flushAsyncResult);
265 }
266 catch (Exception exception)
267 {
268 flushAsyncResult.AsyncException = new TTransportException(exception.ToString());
269 flushAsyncResult.UpdateStatusToComplete();
270 flushAsyncResult.NotifyCallbackWhenAvailable();
271 }
272 }
273
274 private void GetResponseCallback(IAsyncResult asynchronousResult)
275 {
276 var flushAsyncResult = (FlushAsyncResult)asynchronousResult.AsyncState;
277 try
278 {
279 inputStream = flushAsyncResult.Connection.EndGetResponse(asynchronousResult).GetResponseStream();
280 }
281 catch (Exception exception)
282 {
283 flushAsyncResult.AsyncException = new TTransportException(exception.ToString());
284 }
285 flushAsyncResult.UpdateStatusToComplete();
286 flushAsyncResult.NotifyCallbackWhenAvailable();
287 }
288
289 // Based on http://msmvps.com/blogs/luisabreu/archive/2009/06/15/multithreading-implementing-the-iasyncresult-interface.aspx
290 class FlushAsyncResult : IAsyncResult
291 {
292 private volatile Boolean _isCompleted;
293 private ManualResetEvent _evt;
294 private readonly AsyncCallback _cbMethod;
295 private readonly Object _state;
296
297 public FlushAsyncResult(AsyncCallback cbMethod, Object state)
298 {
299 _cbMethod = cbMethod;
300 _state = state;
301 }
302
303 internal byte[] Data { get; set; }
304 internal HttpWebRequest Connection { get; set; }
305 internal TTransportException AsyncException { get; set; }
306
307 public object AsyncState
308 {
309 get { return _state; }
310 }
311 public WaitHandle AsyncWaitHandle
312 {
313 get { return GetEvtHandle(); }
314 }
315 public bool CompletedSynchronously
316 {
317 get { return false; }
318 }
319 public bool IsCompleted
320 {
321 get { return _isCompleted; }
322 }
323 private readonly Object _locker = new Object();
324 private ManualResetEvent GetEvtHandle()
325 {
326 lock (_locker)
327 {
328 if (_evt == null)
329 {
330 _evt = new ManualResetEvent(false);
331 }
332 if (_isCompleted)
333 {
334 _evt.Set();
335 }
336 }
337 return _evt;
338 }
339 internal void UpdateStatusToComplete()
340 {
341 _isCompleted = true; //1. set _iscompleted to true
342 lock (_locker)
343 {
344 if (_evt != null)
345 {
346 _evt.Set(); //2. set the event, when it exists
347 }
348 }
349 }
350
351 internal void NotifyCallbackWhenAvailable()
352 {
353 if (_cbMethod != null)
354 {
355 _cbMethod(this);
356 }
357 }
358 }
359
360#endif
Roger Meierb1ec4cc2012-04-11 21:21:41 +0000361#region " IDisposable Support "
362 private bool _IsDisposed;
363
364 // IDisposable
365 protected override void Dispose(bool disposing)
366 {
367 if (!_IsDisposed)
368 {
369 if (disposing)
370 {
371 if (inputStream != null)
372 inputStream.Dispose();
373 if (outputStream != null)
374 outputStream.Dispose();
375 }
376 }
377 _IsDisposed = true;
378 }
379#endregion
380 }
Bryan Duxbury62359472010-06-24 20:34:34 +0000381}