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; |
Jens Geyer | c0ad368 | 2014-05-08 22:31:34 +0200 | [diff] [blame] | 27 | using System.Linq; |
| 28 | using System.Security.Cryptography.X509Certificates; |
Bryan Duxbury | 6235947 | 2010-06-24 20:34:34 +0000 | [diff] [blame] | 29 | |
| 30 | namespace Thrift.Transport |
| 31 | { |
Jens Geyer | c0ad368 | 2014-05-08 22:31:34 +0200 | [diff] [blame] | 32 | |
| 33 | public class THttpClient : TTransport, IDisposable |
| 34 | { |
| 35 | private readonly Uri uri; |
| 36 | private readonly X509Certificate[] certificates; |
| 37 | private Stream inputStream; |
| 38 | private MemoryStream outputStream = new MemoryStream(); |
Jake Farrell | 5e022aa | 2012-05-18 00:33:54 +0000 | [diff] [blame] | 39 | |
| 40 | // Timeouts in milliseconds |
| 41 | private int connectTimeout = 30000; |
| 42 | |
| 43 | private int readTimeout = 30000; |
| 44 | |
Jens Geyer | c0ad368 | 2014-05-08 22:31:34 +0200 | [diff] [blame] | 45 | private IDictionary<String, String> customHeaders = new Dictionary<string, string>(); |
Bryan Duxbury | 6235947 | 2010-06-24 20:34:34 +0000 | [diff] [blame] | 46 | |
Jake Farrell | 86d2a4a | 2012-05-19 14:29:15 +0000 | [diff] [blame] | 47 | #if !SILVERLIGHT |
Jake Farrell | 5e022aa | 2012-05-18 00:33:54 +0000 | [diff] [blame] | 48 | private IWebProxy proxy = WebRequest.DefaultWebProxy; |
Jens Geyer | b080f68 | 2014-02-22 21:10:45 +0100 | [diff] [blame] | 49 | #endif |
| 50 | |
Jens Geyer | c0ad368 | 2014-05-08 22:31:34 +0200 | [diff] [blame] | 51 | public THttpClient(Uri u) |
| 52 | : this(u, Enumerable.Empty<X509Certificate>()) |
Bryan Duxbury | 6235947 | 2010-06-24 20:34:34 +0000 | [diff] [blame] | 53 | { |
Bryan Duxbury | 6235947 | 2010-06-24 20:34:34 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Jens Geyer | c0ad368 | 2014-05-08 22:31:34 +0200 | [diff] [blame] | 56 | public THttpClient(Uri u, IEnumerable<X509Certificate> certificates) |
| 57 | { |
| 58 | uri = u; |
| 59 | this.certificates = (certificates ?? Enumerable.Empty<X509Certificate>()).ToArray(); |
| 60 | } |
| 61 | |
| 62 | public int ConnectTimeout |
Bryan Duxbury | 6235947 | 2010-06-24 20:34:34 +0000 | [diff] [blame] | 63 | { |
| 64 | set |
| 65 | { |
| 66 | connectTimeout = value; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | public int ReadTimeout |
| 71 | { |
| 72 | set |
| 73 | { |
| 74 | readTimeout = value; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | public IDictionary<String, String> CustomHeaders |
| 79 | { |
| 80 | get |
| 81 | { |
| 82 | return customHeaders; |
| 83 | } |
| 84 | } |
| 85 | |
Jake Farrell | 86d2a4a | 2012-05-19 14:29:15 +0000 | [diff] [blame] | 86 | #if !SILVERLIGHT |
Jake Farrell | 5e022aa | 2012-05-18 00:33:54 +0000 | [diff] [blame] | 87 | public IWebProxy Proxy |
| 88 | { |
| 89 | set |
| 90 | { |
| 91 | proxy = value; |
| 92 | } |
| 93 | } |
Jake Farrell | 86d2a4a | 2012-05-19 14:29:15 +0000 | [diff] [blame] | 94 | #endif |
Jake Farrell | 5e022aa | 2012-05-18 00:33:54 +0000 | [diff] [blame] | 95 | |
Bryan Duxbury | 6235947 | 2010-06-24 20:34:34 +0000 | [diff] [blame] | 96 | public override bool IsOpen |
| 97 | { |
| 98 | get |
| 99 | { |
| 100 | return true; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | public override void Open() |
| 105 | { |
| 106 | } |
| 107 | |
| 108 | public override void Close() |
| 109 | { |
| 110 | if (inputStream != null) |
| 111 | { |
| 112 | inputStream.Close(); |
| 113 | inputStream = null; |
| 114 | } |
| 115 | if (outputStream != null) |
| 116 | { |
| 117 | outputStream.Close(); |
| 118 | outputStream = null; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | public override int Read(byte[] buf, int off, int len) |
| 123 | { |
| 124 | if (inputStream == null) |
| 125 | { |
| 126 | throw new TTransportException(TTransportException.ExceptionType.NotOpen, "No request has been sent"); |
| 127 | } |
| 128 | |
| 129 | try |
| 130 | { |
| 131 | int ret = inputStream.Read(buf, off, len); |
| 132 | |
| 133 | if (ret == -1) |
| 134 | { |
| 135 | throw new TTransportException(TTransportException.ExceptionType.EndOfFile, "No more data available"); |
| 136 | } |
| 137 | |
| 138 | return ret; |
| 139 | } |
| 140 | catch (IOException iox) |
| 141 | { |
| 142 | throw new TTransportException(TTransportException.ExceptionType.Unknown, iox.ToString()); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | public override void Write(byte[] buf, int off, int len) |
| 147 | { |
| 148 | outputStream.Write(buf, off, len); |
| 149 | } |
| 150 | |
Roger Meier | 284a9b5 | 2011-12-08 13:39:56 +0000 | [diff] [blame] | 151 | #if !SILVERLIGHT |
Bryan Duxbury | 6235947 | 2010-06-24 20:34:34 +0000 | [diff] [blame] | 152 | public override void Flush() |
| 153 | { |
Bryan Duxbury | ea67a78 | 2010-08-06 17:50:51 +0000 | [diff] [blame] | 154 | try |
| 155 | { |
| 156 | SendRequest(); |
| 157 | } |
| 158 | finally |
| 159 | { |
| 160 | outputStream = new MemoryStream(); |
| 161 | } |
Bryan Duxbury | 6235947 | 2010-06-24 20:34:34 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | private void SendRequest() |
| 165 | { |
| 166 | try |
| 167 | { |
| 168 | HttpWebRequest connection = CreateRequest(); |
| 169 | |
| 170 | byte[] data = outputStream.ToArray(); |
| 171 | connection.ContentLength = data.Length; |
| 172 | |
Roger Meier | b1ec4cc | 2012-04-11 21:21:41 +0000 | [diff] [blame] | 173 | using (Stream requestStream = connection.GetRequestStream()) |
| 174 | { |
| 175 | requestStream.Write(data, 0, data.Length); |
Jens Geyer | 7dce7b2 | 2014-07-25 22:00:44 +0200 | [diff] [blame^] | 176 | |
| 177 | // Resolve HTTP hang that can happens after successive calls by making sure |
| 178 | // that we release the response and response stream. To support this, we copy |
| 179 | // the response to a memory stream. |
| 180 | using (var response = connection.GetResponse()) |
| 181 | { |
| 182 | using (var responseStream = response.GetResponseStream()) |
| 183 | { |
| 184 | // Copy the response to a memory stream so that we can |
| 185 | // cleanly close the response and response stream. |
| 186 | inputStream = new MemoryStream(); |
| 187 | byte[] buffer = new byte[8096]; |
| 188 | int bytesRead; |
| 189 | while ((bytesRead = responseStream.Read(buffer, 0, buffer.Length)) > 0) |
| 190 | { |
| 191 | inputStream.Write (buffer, 0, bytesRead); |
| 192 | } |
| 193 | inputStream.Seek(0, 0); |
| 194 | } |
| 195 | } |
Roger Meier | b1ec4cc | 2012-04-11 21:21:41 +0000 | [diff] [blame] | 196 | } |
Bryan Duxbury | 6235947 | 2010-06-24 20:34:34 +0000 | [diff] [blame] | 197 | } |
| 198 | catch (IOException iox) |
| 199 | { |
| 200 | throw new TTransportException(TTransportException.ExceptionType.Unknown, iox.ToString()); |
| 201 | } |
| 202 | catch (WebException wx) |
| 203 | { |
| 204 | throw new TTransportException(TTransportException.ExceptionType.Unknown, "Couldn't connect to server: " + wx); |
| 205 | } |
| 206 | } |
Roger Meier | 284a9b5 | 2011-12-08 13:39:56 +0000 | [diff] [blame] | 207 | #endif |
Jens Geyer | b080f68 | 2014-02-22 21:10:45 +0100 | [diff] [blame] | 208 | private HttpWebRequest CreateRequest() |
Bryan Duxbury | 6235947 | 2010-06-24 20:34:34 +0000 | [diff] [blame] | 209 | { |
| 210 | HttpWebRequest connection = (HttpWebRequest)WebRequest.Create(uri); |
| 211 | |
Jens Geyer | c0ad368 | 2014-05-08 22:31:34 +0200 | [diff] [blame] | 212 | |
Roger Meier | 284a9b5 | 2011-12-08 13:39:56 +0000 | [diff] [blame] | 213 | #if !SILVERLIGHT |
Jens Geyer | c0ad368 | 2014-05-08 22:31:34 +0200 | [diff] [blame] | 214 | // Adding certificates through code is not supported with WP7 Silverlight |
| 215 | // see "Windows Phone 7 and Certificates_FINAL_121610.pdf" |
| 216 | connection.ClientCertificates.AddRange(certificates); |
| 217 | |
Bryan Duxbury | 6235947 | 2010-06-24 20:34:34 +0000 | [diff] [blame] | 218 | if (connectTimeout > 0) |
| 219 | { |
| 220 | connection.Timeout = connectTimeout; |
| 221 | } |
| 222 | if (readTimeout > 0) |
| 223 | { |
| 224 | connection.ReadWriteTimeout = readTimeout; |
| 225 | } |
Roger Meier | 284a9b5 | 2011-12-08 13:39:56 +0000 | [diff] [blame] | 226 | #endif |
Bryan Duxbury | 6235947 | 2010-06-24 20:34:34 +0000 | [diff] [blame] | 227 | // Make the request |
| 228 | connection.ContentType = "application/x-thrift"; |
| 229 | connection.Accept = "application/x-thrift"; |
| 230 | connection.UserAgent = "C#/THttpClient"; |
| 231 | connection.Method = "POST"; |
Roger Meier | 284a9b5 | 2011-12-08 13:39:56 +0000 | [diff] [blame] | 232 | #if !SILVERLIGHT |
Bryan Duxbury | 6235947 | 2010-06-24 20:34:34 +0000 | [diff] [blame] | 233 | connection.ProtocolVersion = HttpVersion.Version10; |
Roger Meier | 284a9b5 | 2011-12-08 13:39:56 +0000 | [diff] [blame] | 234 | #endif |
Bryan Duxbury | 6235947 | 2010-06-24 20:34:34 +0000 | [diff] [blame] | 235 | |
Roger Meier | 284a9b5 | 2011-12-08 13:39:56 +0000 | [diff] [blame] | 236 | //add custom headers here |
Bryan Duxbury | 6235947 | 2010-06-24 20:34:34 +0000 | [diff] [blame] | 237 | foreach (KeyValuePair<string, string> item in customHeaders) |
| 238 | { |
Roger Meier | 284a9b5 | 2011-12-08 13:39:56 +0000 | [diff] [blame] | 239 | #if !SILVERLIGHT |
Bryan Duxbury | 6235947 | 2010-06-24 20:34:34 +0000 | [diff] [blame] | 240 | connection.Headers.Add(item.Key, item.Value); |
Roger Meier | 284a9b5 | 2011-12-08 13:39:56 +0000 | [diff] [blame] | 241 | #else |
| 242 | connection.Headers[item.Key] = item.Value; |
| 243 | #endif |
Bryan Duxbury | 6235947 | 2010-06-24 20:34:34 +0000 | [diff] [blame] | 244 | } |
| 245 | |
Jake Farrell | 12ac2ac | 2011-12-09 02:21:37 +0000 | [diff] [blame] | 246 | #if !SILVERLIGHT |
Jake Farrell | 5e022aa | 2012-05-18 00:33:54 +0000 | [diff] [blame] | 247 | connection.Proxy = proxy; |
Jake Farrell | 12ac2ac | 2011-12-09 02:21:37 +0000 | [diff] [blame] | 248 | #endif |
Bryan Duxbury | 6235947 | 2010-06-24 20:34:34 +0000 | [diff] [blame] | 249 | |
Roger Meier | 284a9b5 | 2011-12-08 13:39:56 +0000 | [diff] [blame] | 250 | return connection; |
Bryan Duxbury | 6235947 | 2010-06-24 20:34:34 +0000 | [diff] [blame] | 251 | } |
Roger Meier | 284a9b5 | 2011-12-08 13:39:56 +0000 | [diff] [blame] | 252 | |
Roger Meier | 284a9b5 | 2011-12-08 13:39:56 +0000 | [diff] [blame] | 253 | public override IAsyncResult BeginFlush(AsyncCallback callback, object state) |
| 254 | { |
| 255 | // Extract request and reset buffer |
| 256 | var data = outputStream.ToArray(); |
| 257 | |
| 258 | //requestBuffer_ = new MemoryStream(); |
| 259 | |
| 260 | try |
| 261 | { |
| 262 | // Create connection object |
| 263 | var flushAsyncResult = new FlushAsyncResult(callback, state); |
| 264 | flushAsyncResult.Connection = CreateRequest(); |
| 265 | |
| 266 | flushAsyncResult.Data = data; |
| 267 | |
| 268 | |
| 269 | flushAsyncResult.Connection.BeginGetRequestStream(GetRequestStreamCallback, flushAsyncResult); |
| 270 | return flushAsyncResult; |
| 271 | |
| 272 | } |
| 273 | catch (IOException iox) |
| 274 | { |
| 275 | throw new TTransportException(iox.ToString()); |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | public override void EndFlush(IAsyncResult asyncResult) |
| 280 | { |
| 281 | try |
| 282 | { |
| 283 | var flushAsyncResult = (FlushAsyncResult) asyncResult; |
| 284 | |
| 285 | if (!flushAsyncResult.IsCompleted) |
| 286 | { |
| 287 | var waitHandle = flushAsyncResult.AsyncWaitHandle; |
| 288 | waitHandle.WaitOne(); // blocking INFINITEly |
| 289 | waitHandle.Close(); |
| 290 | } |
| 291 | |
| 292 | if (flushAsyncResult.AsyncException != null) |
| 293 | { |
| 294 | throw flushAsyncResult.AsyncException; |
| 295 | } |
| 296 | } finally |
| 297 | { |
| 298 | outputStream = new MemoryStream(); |
| 299 | } |
| 300 | |
| 301 | } |
| 302 | |
Roger Meier | 284a9b5 | 2011-12-08 13:39:56 +0000 | [diff] [blame] | 303 | private void GetRequestStreamCallback(IAsyncResult asynchronousResult) |
| 304 | { |
| 305 | var flushAsyncResult = (FlushAsyncResult)asynchronousResult.AsyncState; |
| 306 | try |
| 307 | { |
| 308 | var reqStream = flushAsyncResult.Connection.EndGetRequestStream(asynchronousResult); |
| 309 | reqStream.Write(flushAsyncResult.Data, 0, flushAsyncResult.Data.Length); |
| 310 | reqStream.Flush(); |
| 311 | reqStream.Close(); |
| 312 | |
| 313 | // Start the asynchronous operation to get the response |
| 314 | flushAsyncResult.Connection.BeginGetResponse(GetResponseCallback, flushAsyncResult); |
| 315 | } |
| 316 | catch (Exception exception) |
| 317 | { |
| 318 | flushAsyncResult.AsyncException = new TTransportException(exception.ToString()); |
| 319 | flushAsyncResult.UpdateStatusToComplete(); |
| 320 | flushAsyncResult.NotifyCallbackWhenAvailable(); |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | private void GetResponseCallback(IAsyncResult asynchronousResult) |
| 325 | { |
| 326 | var flushAsyncResult = (FlushAsyncResult)asynchronousResult.AsyncState; |
| 327 | try |
| 328 | { |
| 329 | inputStream = flushAsyncResult.Connection.EndGetResponse(asynchronousResult).GetResponseStream(); |
| 330 | } |
| 331 | catch (Exception exception) |
| 332 | { |
| 333 | flushAsyncResult.AsyncException = new TTransportException(exception.ToString()); |
| 334 | } |
| 335 | flushAsyncResult.UpdateStatusToComplete(); |
| 336 | flushAsyncResult.NotifyCallbackWhenAvailable(); |
| 337 | } |
| 338 | |
| 339 | // Based on http://msmvps.com/blogs/luisabreu/archive/2009/06/15/multithreading-implementing-the-iasyncresult-interface.aspx |
| 340 | class FlushAsyncResult : IAsyncResult |
| 341 | { |
| 342 | private volatile Boolean _isCompleted; |
| 343 | private ManualResetEvent _evt; |
| 344 | private readonly AsyncCallback _cbMethod; |
| 345 | private readonly Object _state; |
| 346 | |
| 347 | public FlushAsyncResult(AsyncCallback cbMethod, Object state) |
| 348 | { |
| 349 | _cbMethod = cbMethod; |
| 350 | _state = state; |
| 351 | } |
| 352 | |
| 353 | internal byte[] Data { get; set; } |
| 354 | internal HttpWebRequest Connection { get; set; } |
| 355 | internal TTransportException AsyncException { get; set; } |
| 356 | |
| 357 | public object AsyncState |
| 358 | { |
| 359 | get { return _state; } |
| 360 | } |
| 361 | public WaitHandle AsyncWaitHandle |
| 362 | { |
| 363 | get { return GetEvtHandle(); } |
| 364 | } |
| 365 | public bool CompletedSynchronously |
| 366 | { |
| 367 | get { return false; } |
| 368 | } |
| 369 | public bool IsCompleted |
| 370 | { |
| 371 | get { return _isCompleted; } |
| 372 | } |
| 373 | private readonly Object _locker = new Object(); |
| 374 | private ManualResetEvent GetEvtHandle() |
| 375 | { |
| 376 | lock (_locker) |
| 377 | { |
| 378 | if (_evt == null) |
| 379 | { |
| 380 | _evt = new ManualResetEvent(false); |
| 381 | } |
| 382 | if (_isCompleted) |
| 383 | { |
| 384 | _evt.Set(); |
| 385 | } |
| 386 | } |
| 387 | return _evt; |
| 388 | } |
| 389 | internal void UpdateStatusToComplete() |
| 390 | { |
| 391 | _isCompleted = true; //1. set _iscompleted to true |
| 392 | lock (_locker) |
| 393 | { |
| 394 | if (_evt != null) |
| 395 | { |
| 396 | _evt.Set(); //2. set the event, when it exists |
| 397 | } |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | internal void NotifyCallbackWhenAvailable() |
| 402 | { |
| 403 | if (_cbMethod != null) |
| 404 | { |
| 405 | _cbMethod(this); |
| 406 | } |
| 407 | } |
| 408 | } |
| 409 | |
Roger Meier | b1ec4cc | 2012-04-11 21:21:41 +0000 | [diff] [blame] | 410 | #region " IDisposable Support " |
| 411 | private bool _IsDisposed; |
| 412 | |
| 413 | // IDisposable |
| 414 | protected override void Dispose(bool disposing) |
| 415 | { |
| 416 | if (!_IsDisposed) |
| 417 | { |
| 418 | if (disposing) |
| 419 | { |
| 420 | if (inputStream != null) |
| 421 | inputStream.Dispose(); |
| 422 | if (outputStream != null) |
| 423 | outputStream.Dispose(); |
| 424 | } |
| 425 | } |
| 426 | _IsDisposed = true; |
| 427 | } |
| 428 | #endregion |
| 429 | } |
Bryan Duxbury | 6235947 | 2010-06-24 20:34:34 +0000 | [diff] [blame] | 430 | } |