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 | { |
| 30 | public class THttpClient : TTransport |
| 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 | |
| 145 | Stream requestStream = connection.GetRequestStream(); |
| 146 | requestStream.Write(data, 0, data.Length); |
| 147 | inputStream = connection.GetResponse().GetResponseStream(); |
| 148 | } |
| 149 | catch (IOException iox) |
| 150 | { |
| 151 | throw new TTransportException(TTransportException.ExceptionType.Unknown, iox.ToString()); |
| 152 | } |
| 153 | catch (WebException wx) |
| 154 | { |
| 155 | throw new TTransportException(TTransportException.ExceptionType.Unknown, "Couldn't connect to server: " + wx); |
| 156 | } |
| 157 | } |
Roger Meier | 284a9b5 | 2011-12-08 13:39:56 +0000 | [diff] [blame^] | 158 | #endif |
| 159 | private HttpWebRequest CreateRequest() |
Bryan Duxbury | 6235947 | 2010-06-24 20:34:34 +0000 | [diff] [blame] | 160 | { |
| 161 | HttpWebRequest connection = (HttpWebRequest)WebRequest.Create(uri); |
| 162 | |
Roger Meier | 284a9b5 | 2011-12-08 13:39:56 +0000 | [diff] [blame^] | 163 | #if !SILVERLIGHT |
Bryan Duxbury | 6235947 | 2010-06-24 20:34:34 +0000 | [diff] [blame] | 164 | if (connectTimeout > 0) |
| 165 | { |
| 166 | connection.Timeout = connectTimeout; |
| 167 | } |
| 168 | if (readTimeout > 0) |
| 169 | { |
| 170 | connection.ReadWriteTimeout = readTimeout; |
| 171 | } |
Roger Meier | 284a9b5 | 2011-12-08 13:39:56 +0000 | [diff] [blame^] | 172 | #endif |
Bryan Duxbury | 6235947 | 2010-06-24 20:34:34 +0000 | [diff] [blame] | 173 | // Make the request |
| 174 | connection.ContentType = "application/x-thrift"; |
| 175 | connection.Accept = "application/x-thrift"; |
| 176 | connection.UserAgent = "C#/THttpClient"; |
| 177 | connection.Method = "POST"; |
Roger Meier | 284a9b5 | 2011-12-08 13:39:56 +0000 | [diff] [blame^] | 178 | #if !SILVERLIGHT |
Bryan Duxbury | 6235947 | 2010-06-24 20:34:34 +0000 | [diff] [blame] | 179 | connection.ProtocolVersion = HttpVersion.Version10; |
Roger Meier | 284a9b5 | 2011-12-08 13:39:56 +0000 | [diff] [blame^] | 180 | #endif |
Bryan Duxbury | 6235947 | 2010-06-24 20:34:34 +0000 | [diff] [blame] | 181 | |
Roger Meier | 284a9b5 | 2011-12-08 13:39:56 +0000 | [diff] [blame^] | 182 | //add custom headers here |
Bryan Duxbury | 6235947 | 2010-06-24 20:34:34 +0000 | [diff] [blame] | 183 | foreach (KeyValuePair<string, string> item in customHeaders) |
| 184 | { |
Roger Meier | 284a9b5 | 2011-12-08 13:39:56 +0000 | [diff] [blame^] | 185 | #if !SILVERLIGHT |
Bryan Duxbury | 6235947 | 2010-06-24 20:34:34 +0000 | [diff] [blame] | 186 | connection.Headers.Add(item.Key, item.Value); |
Roger Meier | 284a9b5 | 2011-12-08 13:39:56 +0000 | [diff] [blame^] | 187 | #else |
| 188 | connection.Headers[item.Key] = item.Value; |
| 189 | #endif |
Bryan Duxbury | 6235947 | 2010-06-24 20:34:34 +0000 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | connection.Proxy = null; |
| 193 | |
Roger Meier | 284a9b5 | 2011-12-08 13:39:56 +0000 | [diff] [blame^] | 194 | return connection; |
Bryan Duxbury | 6235947 | 2010-06-24 20:34:34 +0000 | [diff] [blame] | 195 | } |
Roger Meier | 284a9b5 | 2011-12-08 13:39:56 +0000 | [diff] [blame^] | 196 | |
| 197 | #if SILVERLIGHT |
| 198 | public override IAsyncResult BeginFlush(AsyncCallback callback, object state) |
| 199 | { |
| 200 | // Extract request and reset buffer |
| 201 | var data = outputStream.ToArray(); |
| 202 | |
| 203 | //requestBuffer_ = new MemoryStream(); |
| 204 | |
| 205 | try |
| 206 | { |
| 207 | // Create connection object |
| 208 | var flushAsyncResult = new FlushAsyncResult(callback, state); |
| 209 | flushAsyncResult.Connection = CreateRequest(); |
| 210 | |
| 211 | flushAsyncResult.Data = data; |
| 212 | |
| 213 | |
| 214 | flushAsyncResult.Connection.BeginGetRequestStream(GetRequestStreamCallback, flushAsyncResult); |
| 215 | return flushAsyncResult; |
| 216 | |
| 217 | } |
| 218 | catch (IOException iox) |
| 219 | { |
| 220 | throw new TTransportException(iox.ToString()); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | public override void EndFlush(IAsyncResult asyncResult) |
| 225 | { |
| 226 | try |
| 227 | { |
| 228 | var flushAsyncResult = (FlushAsyncResult) asyncResult; |
| 229 | |
| 230 | if (!flushAsyncResult.IsCompleted) |
| 231 | { |
| 232 | var waitHandle = flushAsyncResult.AsyncWaitHandle; |
| 233 | waitHandle.WaitOne(); // blocking INFINITEly |
| 234 | waitHandle.Close(); |
| 235 | } |
| 236 | |
| 237 | if (flushAsyncResult.AsyncException != null) |
| 238 | { |
| 239 | throw flushAsyncResult.AsyncException; |
| 240 | } |
| 241 | } finally |
| 242 | { |
| 243 | outputStream = new MemoryStream(); |
| 244 | } |
| 245 | |
| 246 | } |
| 247 | |
| 248 | |
| 249 | private void GetRequestStreamCallback(IAsyncResult asynchronousResult) |
| 250 | { |
| 251 | var flushAsyncResult = (FlushAsyncResult)asynchronousResult.AsyncState; |
| 252 | try |
| 253 | { |
| 254 | var reqStream = flushAsyncResult.Connection.EndGetRequestStream(asynchronousResult); |
| 255 | reqStream.Write(flushAsyncResult.Data, 0, flushAsyncResult.Data.Length); |
| 256 | reqStream.Flush(); |
| 257 | reqStream.Close(); |
| 258 | |
| 259 | // Start the asynchronous operation to get the response |
| 260 | flushAsyncResult.Connection.BeginGetResponse(GetResponseCallback, flushAsyncResult); |
| 261 | } |
| 262 | catch (Exception exception) |
| 263 | { |
| 264 | flushAsyncResult.AsyncException = new TTransportException(exception.ToString()); |
| 265 | flushAsyncResult.UpdateStatusToComplete(); |
| 266 | flushAsyncResult.NotifyCallbackWhenAvailable(); |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | private void GetResponseCallback(IAsyncResult asynchronousResult) |
| 271 | { |
| 272 | var flushAsyncResult = (FlushAsyncResult)asynchronousResult.AsyncState; |
| 273 | try |
| 274 | { |
| 275 | inputStream = flushAsyncResult.Connection.EndGetResponse(asynchronousResult).GetResponseStream(); |
| 276 | } |
| 277 | catch (Exception exception) |
| 278 | { |
| 279 | flushAsyncResult.AsyncException = new TTransportException(exception.ToString()); |
| 280 | } |
| 281 | flushAsyncResult.UpdateStatusToComplete(); |
| 282 | flushAsyncResult.NotifyCallbackWhenAvailable(); |
| 283 | } |
| 284 | |
| 285 | // Based on http://msmvps.com/blogs/luisabreu/archive/2009/06/15/multithreading-implementing-the-iasyncresult-interface.aspx |
| 286 | class FlushAsyncResult : IAsyncResult |
| 287 | { |
| 288 | private volatile Boolean _isCompleted; |
| 289 | private ManualResetEvent _evt; |
| 290 | private readonly AsyncCallback _cbMethod; |
| 291 | private readonly Object _state; |
| 292 | |
| 293 | public FlushAsyncResult(AsyncCallback cbMethod, Object state) |
| 294 | { |
| 295 | _cbMethod = cbMethod; |
| 296 | _state = state; |
| 297 | } |
| 298 | |
| 299 | internal byte[] Data { get; set; } |
| 300 | internal HttpWebRequest Connection { get; set; } |
| 301 | internal TTransportException AsyncException { get; set; } |
| 302 | |
| 303 | public object AsyncState |
| 304 | { |
| 305 | get { return _state; } |
| 306 | } |
| 307 | public WaitHandle AsyncWaitHandle |
| 308 | { |
| 309 | get { return GetEvtHandle(); } |
| 310 | } |
| 311 | public bool CompletedSynchronously |
| 312 | { |
| 313 | get { return false; } |
| 314 | } |
| 315 | public bool IsCompleted |
| 316 | { |
| 317 | get { return _isCompleted; } |
| 318 | } |
| 319 | private readonly Object _locker = new Object(); |
| 320 | private ManualResetEvent GetEvtHandle() |
| 321 | { |
| 322 | lock (_locker) |
| 323 | { |
| 324 | if (_evt == null) |
| 325 | { |
| 326 | _evt = new ManualResetEvent(false); |
| 327 | } |
| 328 | if (_isCompleted) |
| 329 | { |
| 330 | _evt.Set(); |
| 331 | } |
| 332 | } |
| 333 | return _evt; |
| 334 | } |
| 335 | internal void UpdateStatusToComplete() |
| 336 | { |
| 337 | _isCompleted = true; //1. set _iscompleted to true |
| 338 | lock (_locker) |
| 339 | { |
| 340 | if (_evt != null) |
| 341 | { |
| 342 | _evt.Set(); //2. set the event, when it exists |
| 343 | } |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | internal void NotifyCallbackWhenAvailable() |
| 348 | { |
| 349 | if (_cbMethod != null) |
| 350 | { |
| 351 | _cbMethod(this); |
| 352 | } |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | #endif |
| 357 | } |
Bryan Duxbury | 6235947 | 2010-06-24 20:34:34 +0000 | [diff] [blame] | 358 | } |