Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

updated HttpHelper.cs to v0.1.0.0

  • Loading branch information...
commit 478ad16fa1d1ff9b949cf156eff4ef318c204f0f 1 parent 88298b3
@prabirshrestha prabirshrestha authored
Showing with 147 additions and 143 deletions.
  1. +147 −143 HttpHelper.cs
View
290 HttpHelper.cs
@@ -17,7 +17,6 @@
using System.Net;
using System.Text;
using System.Threading;
-
#if FLUENTHTTP_CORE_TPL
using System.Threading.Tasks;
#endif
@@ -489,6 +488,7 @@ public virtual Stream GetResponseStream()
public class WebExceptionWrapper : Exception
{
private readonly WebException _webException;
+ private readonly WebExceptionStatus _status = WebExceptionStatus.UnknownError;
protected WebExceptionWrapper() { }
@@ -496,6 +496,7 @@ public WebExceptionWrapper(WebException webException)
: base(webException == null ? null : webException.Message, webException == null ? null : webException.InnerException)
{
_webException = webException;
+ _status = webException == null ? WebExceptionStatus.UnknownError : webException.Status;
}
#if (!SILVERLIGHT)
@@ -523,6 +524,11 @@ public virtual WebException ActualWebException
{
get { return _webException; }
}
+
+ public virtual WebExceptionStatus Status
+ {
+ get { return _status; }
+ }
}
}
@@ -533,16 +539,6 @@ namespace FluentHttp
internal delegate void OpenWriteCompletedEventHandler(object sender, OpenWriteCompletedEventArgs e);
-#if FLUENTHTTP_CORE_STREAM
-
- internal delegate void StreamCopyCompletedDelegate(Stream input, Stream output, bool cancelled, Exception exception);
-
- delegate void CopyStreamDoneDelegate(bool cancelled, Exception exception);
-
-#endif
-
- internal delegate bool HttpWebRequestCancelDelegate();
-
internal class OpenReadCompletedEventArgs : AsyncCompletedEventArgs
{
private readonly Stream _result;
@@ -687,20 +683,46 @@ public virtual void OpenReadAsync(object userToken)
{
if (_httpWebResponse == null)
{
- IAsyncResult asyncResult = _httpWebRequest.BeginGetResponse(ar => ResponseCallback(ar, userToken), null);
+ WebExceptionWrapper webExceptionWrapper = null;
+ try
+ {
+ IAsyncResult asyncResult = _httpWebRequest.BeginGetResponse(ar => ResponseCallback(ar, userToken), null);
- int timeout = 0;
+ int timeout = 0;
#if !SILVERLIGHT
- if (HttpWebRequest.Timeout > 0)
- timeout = HttpWebRequest.Timeout;
+ if (HttpWebRequest.Timeout > 0)
+ timeout = HttpWebRequest.Timeout;
#endif
- if (Timeout > 0)
- timeout = Timeout;
+ if (Timeout > 0)
+ timeout = Timeout;
- if (timeout > 0)
- ThreadPool.RegisterWaitForSingleObject(asyncResult.AsyncWaitHandle, ScanTimoutCallback, userToken, timeout, true);
+ if (timeout > 0)
+ ThreadPool.RegisterWaitForSingleObject(asyncResult.AsyncWaitHandle, ScanTimoutCallback, userToken, timeout, true);
+ }
+ catch (WebException webException)
+ {
+ if (webException.Response != null)
+ _httpWebResponse = new HttpWebResponseWrapper((HttpWebResponse)webException.Response);
+ webExceptionWrapper = new WebExceptionWrapper(webException);
+ _innerException = webExceptionWrapper;
+ }
+ catch (WebExceptionWrapper webException)
+ {
+ _httpWebResponse = webException.GetResponse();
+ webExceptionWrapper = webException;
+ _innerException = webExceptionWrapper;
+ }
+ catch (Exception ex)
+ {
+ webExceptionWrapper = new WebExceptionWrapper(new WebException("An error occurred performing a http web request.", ex));
+ }
+ finally
+ {
+ if (webExceptionWrapper != null)
+ OnOpenReadCompleted(new OpenReadCompletedEventArgs(null, webExceptionWrapper, webExceptionWrapper.Status == WebExceptionStatus.RequestCanceled, userToken));
+ }
}
else
ResponseCallback(null, userToken);
@@ -713,31 +735,53 @@ public virtual void OpenReadAsync()
public virtual void OpenWriteAsync(object userToken)
{
- _httpWebRequest.BeginGetRequestStream(
- ar =>
- {
- Stream stream = null;
- Exception exception = null;
+ WebExceptionWrapper webExceptionWrapper = null;
- try
- {
- stream = _httpWebRequest.EndGetRequestStream(ar);
- }
- catch (WebException webException)
- {
- exception = new WebExceptionWrapper(webException);
- }
- catch (WebExceptionWrapper webException)
- {
- exception = webException;
- }
- catch (Exception ex)
+ try
+ {
+ _httpWebRequest.BeginGetRequestStream(
+ ar =>
{
- exception = new WebExceptionWrapper(new WebException("An error occurred performing a http web request.", ex));
- }
+ Stream stream = null;
+ Exception exception = null;
+
+ try
+ {
+ stream = _httpWebRequest.EndGetRequestStream(ar);
+ }
+ catch (WebException webException)
+ {
+ exception = new WebExceptionWrapper(webException);
+ }
+ catch (WebExceptionWrapper webException)
+ {
+ exception = webException;
+ }
+ catch (Exception ex)
+ {
+ exception = new WebExceptionWrapper(new WebException("An error occurred performing a http web request.", ex));
+ }
- OnOpenWriteCompleted(new OpenWriteCompletedEventArgs(stream, exception, false, userToken));
- }, userToken);
+ OnOpenWriteCompleted(new OpenWriteCompletedEventArgs(stream, exception, false, userToken));
+ }, userToken);
+ }
+ catch (WebException webException)
+ {
+ webExceptionWrapper = new WebExceptionWrapper(webException);
+ }
+ catch (WebExceptionWrapper webException)
+ {
+ webExceptionWrapper = webException;
+ }
+ catch (Exception ex)
+ {
+ webExceptionWrapper = new WebExceptionWrapper(new WebException("An error occurred performing a http web request.", ex));
+ }
+ finally
+ {
+ if (webExceptionWrapper != null)
+ OnOpenWriteCompleted(new OpenWriteCompletedEventArgs(null, webExceptionWrapper, webExceptionWrapper.Status == WebExceptionStatus.RequestCanceled, userToken));
+ }
}
public virtual void OpenWriteAsync()
@@ -747,7 +791,7 @@ public virtual void OpenWriteAsync()
private void ResponseCallback(IAsyncResult asyncResult, object userToken)
{
- Exception exception = null;
+ WebExceptionWrapper webExceptionWrapper = null;
Stream stream = null;
try
{
@@ -759,21 +803,22 @@ private void ResponseCallback(IAsyncResult asyncResult, object userToken)
{
if (webException.Response != null)
_httpWebResponse = new HttpWebResponseWrapper((HttpWebResponse)webException.Response);
- _innerException = new WebExceptionWrapper(webException);
- exception = _innerException;
+ webExceptionWrapper = new WebExceptionWrapper(webException);
+ _innerException = webExceptionWrapper;
}
catch (WebExceptionWrapper webException)
{
_httpWebResponse = webException.GetResponse();
- _innerException = webException;
- exception = _innerException;
+ webExceptionWrapper = webException;
+ _innerException = webExceptionWrapper;
}
catch (Exception ex)
{
- exception = new WebExceptionWrapper(new WebException("An error occurred performing a http web request.", ex));
+ webExceptionWrapper = new WebExceptionWrapper(new WebException("An error occurred performing a http web request.", ex));
+ _innerException = webExceptionWrapper;
}
- OnOpenReadCompleted(new OpenReadCompletedEventArgs(stream, exception, false, userToken));
+ OnOpenReadCompleted(new OpenReadCompletedEventArgs(stream, webExceptionWrapper, webExceptionWrapper != null && webExceptionWrapper.Status == WebExceptionStatus.RequestCanceled, userToken));
}
private void ScanTimoutCallback(object state, bool timedOut)
@@ -784,13 +829,10 @@ private void ScanTimoutCallback(object state, bool timedOut)
#if FLUENTHTTP_CORE_TPL
- static void TransferCompletionToTask<T>(TaskCompletionSource<T> tcs, bool requireMatch, AsyncCompletedEventArgs e, Func<T> getResult, Action unregisterHandler)
+ static void TransferCompletionToTask<T>(TaskCompletionSource<T> tcs, AsyncCompletedEventArgs e, Func<T> getResult, Action unregisterHandler)
{
- if (requireMatch)
- {
- if (e.UserState != tcs)
- return;
- }
+ if (e.UserState != tcs)
+ return;
try
{
@@ -804,42 +846,71 @@ static void TransferCompletionToTask<T>(TaskCompletionSource<T> tcs, bool requir
}
}
- public virtual Task<Stream> OpenReadAsyncTask()
+ public virtual Task<Stream> OpenReadTaskAsync(CancellationToken cancellationToken)
{
- var tcs = new TaskCompletionSource<Stream>(_httpWebRequest);
- OpenReadCompletedEventHandler handler = null;
- handler = (sender, e) => TransferCompletionToTask(tcs, true, e, () => e.Result, () => OpenReadCompleted -= handler);
- OpenReadCompleted += handler;
-
- try
+ var tcs = new TaskCompletionSource<Stream>(this);
+ if (cancellationToken.IsCancellationRequested)
{
- OpenReadAsync(tcs);
+ tcs.TrySetCanceled();
}
- catch
+ else
{
- OpenReadCompleted -= handler;
- throw;
+ var ctr = cancellationToken.Register(CancelAsync);
+ OpenReadCompletedEventHandler handler = null;
+ handler = (sender, e) => TransferCompletionToTask(tcs, e, () => e.Result, () => { ctr.Dispose(); OpenReadCompleted -= handler; });
+ OpenReadCompleted += handler;
+
+ try
+ {
+ OpenReadAsync(tcs);
+ if (cancellationToken.IsCancellationRequested)
+ CancelAsync();
+ }
+ catch
+ {
+ OpenReadCompleted -= handler;
+ throw;
+ }
}
return tcs.Task;
}
- public virtual Task<Stream> OpenWriteAsyncTask()
+ public virtual Task<Stream> OpenReadTaskAsync()
{
- var tcs = new TaskCompletionSource<Stream>(_httpWebRequest);
+ return OpenReadTaskAsync(CancellationToken.None);
+ }
- OpenWriteCompletedEventHandler handler = null;
- handler = (sender, e) => TransferCompletionToTask(tcs, true, e, () => e.Result, () => OpenWriteCompleted -= handler);
- OpenWriteCompleted += handler;
+ public virtual Task<Stream> OpenWriteTaskAsync()
+ {
+ return OpenWriteTaskAsync(CancellationToken.None);
+ }
- try
+ public virtual Task<Stream> OpenWriteTaskAsync(CancellationToken cancellationToken)
+ {
+ var tcs = new TaskCompletionSource<Stream>(this);
+ if (cancellationToken.IsCancellationRequested)
{
- OpenReadAsync(tcs);
+ tcs.TrySetCanceled();
}
- catch
+ else
{
- OpenWriteCompleted -= handler;
- throw;
+ var ctr = cancellationToken.Register(CancelAsync);
+ OpenWriteCompletedEventHandler handler = null;
+ handler = (sender, e) => TransferCompletionToTask(tcs, e, () => e.Result, () => { ctr.Dispose(); OpenWriteCompleted -= handler; });
+ OpenWriteCompleted += handler;
+
+ try
+ {
+ OpenWriteAsync(tcs);
+ if (cancellationToken.IsCancellationRequested)
+ CancelAsync();
+ }
+ catch
+ {
+ OpenWriteCompleted -= handler;
+ throw;
+ }
}
return tcs.Task;
@@ -847,32 +918,9 @@ public virtual Task<Stream> OpenWriteAsyncTask()
#endif
- private HttpWebRequestCancelDelegate _cancelFunc;
-
- public void CancelAsync(HttpWebRequestCancelDelegate cancelFunc)
- {
- lock (this)
- {
- _cancelFunc = cancelFunc;
- }
- }
-
public void CancelAsync()
{
- CancelAsync(() => true);
- }
-
- public bool IsCancelled
- {
- get
- {
- if (_cancelFunc != null && _cancelFunc())
- {
- HttpWebRequest.Abort();
- return true;
- }
- return false;
- }
+ HttpWebRequest.Abort();
}
protected virtual void OnOpenReadCompleted(OpenReadCompletedEventArgs args)
@@ -1748,50 +1796,6 @@ public void ReadStream(Stream input, int? bufferSize, bool flushInput)
}
}
- public void CopyStreamAsync(Stream input, Stream output, int? bufferSize, bool flushInput, bool flushOutput, StreamCopyCompletedDelegate completed)
- {
- byte[] buffer = new byte[bufferSize ?? 1024 * 4];
- var asyncOp = AsyncOperationManager.CreateOperation(null);
-
- CopyStreamDoneDelegate done = (c, e) =>
- {
- if (completed != null) asyncOp.Post(delegate
- {
- completed(input, output, c, e);
- }, null);
- };
-
- AsyncCallback rc = null;
- rc = readResult =>
- {
- try
- {
- int read = input.EndRead(readResult);
- if (read > 0)
- {
- if (flushInput) input.Flush();
- output.BeginWrite(buffer, 0, read, writeResult =>
- {
- try
- {
- output.EndWrite(writeResult);
- if (flushOutput) output.Flush();
- if (IsCancelled)
- done(true, null);
- else
- input.BeginRead(buffer, 0, buffer.Length, rc, null);
- }
- catch (Exception exc) { done(false, exc); }
- }, null);
- }
- else done(false, null);
- }
- catch (Exception exc) { done(false, exc); }
- };
-
- input.BeginRead(buffer, 0, buffer.Length, rc, null);
- }
-
#if FLUENTHTTP_CORE_TPL
public static System.Threading.Tasks.Task<int> ReadTask(Stream stream, byte[] buffer, int offset, int count)
Please sign in to comment.
Something went wrong with that request. Please try again.