Bryan Duxbury | 6235947 | 2010-06-24 20:34:34 +0000 | [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 | */ |
| 21 | |
| 22 | using System; |
| 23 | using System.Collections.Generic; |
| 24 | using System.IO; |
| 25 | using System.Net; |
Roger Meier | 284a9b5 | 2011-12-08 13:39:56 +0000 | [diff] [blame] | 26 | using System.Threading; |
Bryan Duxbury | 6235947 | 2010-06-24 20:34:34 +0000 | [diff] [blame] | 27 | |
| 28 | namespace Thrift.Transport |
| 29 | { |
Roger Meier | b1ec4cc | 2012-04-11 21:21:41 +0000 | [diff] [blame] | 30 | public class THttpClient : TTransport, IDisposable |
Bryan Duxbury | 6235947 | 2010-06-24 20:34:34 +0000 | [diff] [blame] | 31 | { |
| 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 Meier | 284a9b5 | 2011-12-08 13:39:56 +0000 | [diff] [blame] | 123 | #if !SILVERLIGHT |
Bryan Duxbury | 6235947 | 2010-06-24 20:34:34 +0000 | [diff] [blame] | 124 | public override void Flush() |
| 125 | { |
Bryan Duxbury | ea67a78 | 2010-08-06 17:50:51 +0000 | [diff] [blame] | 126 | try |
| 127 | { |
| 128 | SendRequest(); |
| 129 | } |
| 130 | finally |
| 131 | { |
| 132 | outputStream = new MemoryStream(); |
| 133 | } |
Bryan Duxbury | 6235947 | 2010-06-24 20:34:34 +0000 | [diff] [blame] | 134 | } |
| 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 Meier | b1ec4cc | 2012-04-11 21:21:41 +0000 | [diff] [blame] | 145 | using (Stream requestStream = connection.GetRequestStream()) |
| 146 | { |
| 147 | requestStream.Write(data, 0, data.Length); |
| 148 | inputStream = connection.GetResponse().GetResponseStream(); |
| 149 | } |
Bryan Duxbury | 6235947 | 2010-06-24 20:34:34 +0000 | [diff] [blame] | 150 | } |
| 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 Meier | 284a9b5 | 2011-12-08 13:39:56 +0000 | [diff] [blame] | 160 | #endif |
Roger Meier | b1ec4cc | 2012-04-11 21:21:41 +0000 | [diff] [blame] | 161 | private HttpWebRequest CreateRequest() |
Bryan Duxbury | 6235947 | 2010-06-24 20:34:34 +0000 | [diff] [blame] | 162 | { |
| 163 | HttpWebRequest connection = (HttpWebRequest)WebRequest.Create(uri); |
| 164 | |
Roger Meier | 284a9b5 | 2011-12-08 13:39:56 +0000 | [diff] [blame] | 165 | #if !SILVERLIGHT |
Bryan Duxbury | 6235947 | 2010-06-24 20:34:34 +0000 | [diff] [blame] | 166 | if (connectTimeout > 0) |
| 167 | { |
| 168 | connection.Timeout = connectTimeout; |
| 169 | } |
| 170 | if (readTimeout > 0) |
| 171 | { |
| 172 | connection.ReadWriteTimeout = readTimeout; |
| 173 | } |
Roger Meier | 284a9b5 | 2011-12-08 13:39:56 +0000 | [diff] [blame] | 174 | #endif |
Bryan Duxbury | 6235947 | 2010-06-24 20:34:34 +0000 | [diff] [blame] | 175 | // 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 Meier | 284a9b5 | 2011-12-08 13:39:56 +0000 | [diff] [blame] | 180 | #if !SILVERLIGHT |
Bryan Duxbury | 6235947 | 2010-06-24 20:34:34 +0000 | [diff] [blame] | 181 | connection.ProtocolVersion = HttpVersion.Version10; |
Roger Meier | 284a9b5 | 2011-12-08 13:39:56 +0000 | [diff] [blame] | 182 | #endif |
Bryan Duxbury | 6235947 | 2010-06-24 20:34:34 +0000 | [diff] [blame] | 183 | |
Roger Meier | 284a9b5 | 2011-12-08 13:39:56 +0000 | [diff] [blame] | 184 | //add custom headers here |
Bryan Duxbury | 6235947 | 2010-06-24 20:34:34 +0000 | [diff] [blame] | 185 | foreach (KeyValuePair<string, string> item in customHeaders) |
| 186 | { |
Roger Meier | 284a9b5 | 2011-12-08 13:39:56 +0000 | [diff] [blame] | 187 | #if !SILVERLIGHT |
Bryan Duxbury | 6235947 | 2010-06-24 20:34:34 +0000 | [diff] [blame] | 188 | connection.Headers.Add(item.Key, item.Value); |
Roger Meier | 284a9b5 | 2011-12-08 13:39:56 +0000 | [diff] [blame] | 189 | #else |
| 190 | connection.Headers[item.Key] = item.Value; |
| 191 | #endif |
Bryan Duxbury | 6235947 | 2010-06-24 20:34:34 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Jake Farrell | 12ac2ac | 2011-12-09 02:21:37 +0000 | [diff] [blame] | 194 | #if !SILVERLIGHT |
Bryan Duxbury | 6235947 | 2010-06-24 20:34:34 +0000 | [diff] [blame] | 195 | connection.Proxy = null; |
Jake Farrell | 12ac2ac | 2011-12-09 02:21:37 +0000 | [diff] [blame] | 196 | #endif |
Bryan Duxbury | 6235947 | 2010-06-24 20:34:34 +0000 | [diff] [blame] | 197 | |
Roger Meier | 284a9b5 | 2011-12-08 13:39:56 +0000 | [diff] [blame] | 198 | return connection; |
Bryan Duxbury | 6235947 | 2010-06-24 20:34:34 +0000 | [diff] [blame] | 199 | } |
Roger Meier | 284a9b5 | 2011-12-08 13:39:56 +0000 | [diff] [blame] | 200 | |
| 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 Meier | b1ec4cc | 2012-04-11 21:21:41 +0000 | [diff] [blame] | 361 | #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 Duxbury | 6235947 | 2010-06-24 20:34:34 +0000 | [diff] [blame] | 381 | } |