#62 - Abort the request for read/write failures.

This commit is contained in:
Chris Ross 2014-09-08 16:06:51 -07:00
parent 400b561958
commit 9fc4e8704b
5 changed files with 32 additions and 24 deletions

View File

@ -411,7 +411,7 @@ namespace Microsoft.Net.Http.Server
context.Abort(); context.Abort();
} }
// This is only called while processing incoming requests. We don't have to worry about cancelling // This is only called while processing incoming requests. We don't have to worry about canceling
// any response writes. // any response writes.
[SuppressMessage("Microsoft.Usage", "CA1806:DoNotIgnoreMethodResults", Justification = [SuppressMessage("Microsoft.Usage", "CA1806:DoNotIgnoreMethodResults", Justification =
"It is safe to ignore the return value on a cancel operation because the connection is being closed")] "It is safe to ignore the return value on a cancel operation because the connection is being closed")]

View File

@ -107,6 +107,12 @@ namespace Microsoft.Net.Http.Server
throw new InvalidOperationException(Resources.Exception_ReadOnlyStream); throw new InvalidOperationException(Resources.Exception_ReadOnlyStream);
} }
internal void Abort()
{
_closed = true;
_requestContext.Abort();
}
private void ValidateReadBuffer(byte[] buffer, int offset, int size) private void ValidateReadBuffer(byte[] buffer, int offset, int size)
{ {
if (buffer == null) if (buffer == null)
@ -174,6 +180,7 @@ namespace Microsoft.Net.Http.Server
{ {
Exception exception = new WebListenerException((int)statusCode); Exception exception = new WebListenerException((int)statusCode);
LogHelper.LogException(_requestContext.Logger, "Read", exception); LogHelper.LogException(_requestContext.Logger, "Read", exception);
Abort();
throw exception; throw exception;
} }
UpdateAfterRead(statusCode, dataRead); UpdateAfterRead(statusCode, dataRead);
@ -270,6 +277,7 @@ namespace Microsoft.Net.Http.Server
{ {
Exception exception = new WebListenerException((int)statusCode); Exception exception = new WebListenerException((int)statusCode);
LogHelper.LogException(_requestContext.Logger, "BeginRead", exception); LogHelper.LogException(_requestContext.Logger, "BeginRead", exception);
Abort();
throw exception; throw exception;
} }
} }
@ -376,6 +384,7 @@ namespace Microsoft.Net.Http.Server
catch (Exception e) catch (Exception e)
{ {
asyncResult.Dispose(); asyncResult.Dispose();
Abort();
LogHelper.LogException(_requestContext.Logger, "ReadAsync", e); LogHelper.LogException(_requestContext.Logger, "ReadAsync", e);
throw; throw;
} }
@ -394,6 +403,7 @@ namespace Microsoft.Net.Http.Server
{ {
Exception exception = new WebListenerException((int)statusCode); Exception exception = new WebListenerException((int)statusCode);
LogHelper.LogException(_requestContext.Logger, "ReadAsync", exception); LogHelper.LogException(_requestContext.Logger, "ReadAsync", exception);
Abort();
throw exception; throw exception;
} }
} }

View File

@ -166,6 +166,7 @@ namespace Microsoft.Net.Http.Server
} }
} }
Dispose(); Dispose();
_requestStream.Abort();
} }
[SuppressMessage("Microsoft.Usage", "CA2216:DisposableTypesShouldDeclareFinalizer", Justification = "The disposable resource referenced does have a finalizer.")] [SuppressMessage("Microsoft.Usage", "CA2216:DisposableTypesShouldDeclareFinalizer", Justification = "The disposable resource referenced does have a finalizer.")]

View File

@ -119,8 +119,7 @@ namespace Microsoft.Net.Http.Server
catch (Exception e) catch (Exception e)
{ {
LogHelper.LogException(_requestContext.Logger, "Flush", e); LogHelper.LogException(_requestContext.Logger, "Flush", e);
_closed = true; Abort();
_requestContext.Abort();
throw; throw;
} }
} }
@ -172,8 +171,7 @@ namespace Microsoft.Net.Http.Server
{ {
LogHelper.LogException(_requestContext.Logger, "FlushAsync", e); LogHelper.LogException(_requestContext.Logger, "FlushAsync", e);
asyncResult.Dispose(); asyncResult.Dispose();
_closed = true; Abort();
_requestContext.Abort();
throw; throw;
} }
@ -211,6 +209,12 @@ namespace Microsoft.Net.Http.Server
#endregion #endregion
internal void Abort()
{
_closed = true;
_requestContext.Abort();
}
private UnsafeNclNativeMethods.HttpApi.HTTP_FLAGS ComputeLeftToWrite(bool endOfRequest = false) private UnsafeNclNativeMethods.HttpApi.HTTP_FLAGS ComputeLeftToWrite(bool endOfRequest = false)
{ {
UnsafeNclNativeMethods.HttpApi.HTTP_FLAGS flags = UnsafeNclNativeMethods.HttpApi.HTTP_FLAGS.NONE; UnsafeNclNativeMethods.HttpApi.HTTP_FLAGS flags = UnsafeNclNativeMethods.HttpApi.HTTP_FLAGS.NONE;
@ -349,8 +353,7 @@ namespace Microsoft.Net.Http.Server
{ {
Exception exception = new WebListenerException((int)statusCode); Exception exception = new WebListenerException((int)statusCode);
LogHelper.LogException(_requestContext.Logger, "Write", exception); LogHelper.LogException(_requestContext.Logger, "Write", exception);
_closed = true; Abort();
_requestContext.Abort();
throw exception; throw exception;
} }
UpdateWritenCount(dataToWrite); UpdateWritenCount(dataToWrite);
@ -428,8 +431,7 @@ namespace Microsoft.Net.Http.Server
{ {
LogHelper.LogException(_requestContext.Logger, "BeginWrite", e); LogHelper.LogException(_requestContext.Logger, "BeginWrite", e);
asyncResult.Dispose(); asyncResult.Dispose();
_closed = true; Abort();
_requestContext.Abort();
throw; throw;
} }
@ -444,8 +446,7 @@ namespace Microsoft.Net.Http.Server
{ {
Exception exception = new WebListenerException((int)statusCode); Exception exception = new WebListenerException((int)statusCode);
LogHelper.LogException(_requestContext.Logger, "BeginWrite", exception); LogHelper.LogException(_requestContext.Logger, "BeginWrite", exception);
_closed = true; Abort();
_requestContext.Abort();
throw exception; throw exception;
} }
} }
@ -456,7 +457,7 @@ namespace Microsoft.Net.Http.Server
asyncResult.IOCompleted(statusCode, bytesSent); asyncResult.IOCompleted(statusCode, bytesSent);
} }
// Last write, cache it for special cancelation handling. // Last write, cache it for special cancellation handling.
if ((flags & UnsafeNclNativeMethods.HttpApi.HTTP_FLAGS.HTTP_SEND_RESPONSE_FLAG_MORE_DATA) == 0) if ((flags & UnsafeNclNativeMethods.HttpApi.HTTP_FLAGS.HTTP_SEND_RESPONSE_FLAG_MORE_DATA) == 0)
{ {
_lastWrite = asyncResult; _lastWrite = asyncResult;
@ -488,14 +489,13 @@ namespace Microsoft.Net.Http.Server
try try
{ {
// wait & then check for errors // wait & then check for errors
// TODO: Gracefull re-throw // TODO: Graceful re-throw
castedAsyncResult.Task.Wait(); castedAsyncResult.Task.Wait();
} }
catch (Exception exception) catch (Exception exception)
{ {
LogHelper.LogException(_requestContext.Logger, "EndWrite", exception); LogHelper.LogException(_requestContext.Logger, "EndWrite", exception);
_closed = true; Abort();
_requestContext.Abort();
throw; throw;
} }
} }
@ -576,8 +576,7 @@ namespace Microsoft.Net.Http.Server
{ {
LogHelper.LogException(_requestContext.Logger, "WriteAsync", e); LogHelper.LogException(_requestContext.Logger, "WriteAsync", e);
asyncResult.Dispose(); asyncResult.Dispose();
_closed = true; Abort();
_requestContext.Abort();
throw; throw;
} }
@ -592,8 +591,7 @@ namespace Microsoft.Net.Http.Server
{ {
Exception exception = new WebListenerException((int)statusCode); Exception exception = new WebListenerException((int)statusCode);
LogHelper.LogException(_requestContext.Logger, "WriteAsync", exception); LogHelper.LogException(_requestContext.Logger, "WriteAsync", exception);
_closed = true; Abort();
_requestContext.Abort();
throw exception; throw exception;
} }
} }
@ -699,8 +697,7 @@ namespace Microsoft.Net.Http.Server
{ {
LogHelper.LogException(_requestContext.Logger, "SendFileAsync", e); LogHelper.LogException(_requestContext.Logger, "SendFileAsync", e);
asyncResult.Dispose(); asyncResult.Dispose();
_closed = true; Abort();
_requestContext.Abort();
throw; throw;
} }
@ -715,8 +712,7 @@ namespace Microsoft.Net.Http.Server
{ {
Exception exception = new WebListenerException((int)statusCode); Exception exception = new WebListenerException((int)statusCode);
LogHelper.LogException(_requestContext.Logger, "SendFileAsync", exception); LogHelper.LogException(_requestContext.Logger, "SendFileAsync", exception);
_closed = true; Abort();
_requestContext.Abort();
throw exception; throw exception;
} }
} }

View File

@ -355,6 +355,7 @@ namespace Microsoft.Net.Http.Server
} }
} }
Dispose(); Dispose();
_responseStream.Abort();
} }
/*++ /*++
@ -362,7 +363,7 @@ namespace Microsoft.Net.Http.Server
GetChunkHeader GetChunkHeader
A private utility routine to convert an integer to a chunk header, A private utility routine to convert an integer to a chunk header,
which is an ASCII hex number followed by a CRLF. The header is retuned which is an ASCII hex number followed by a CRLF. The header is returned
as a byte array. as a byte array.
Input: Input: