Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

using System.Diagnostics;
using System.IO;
using System.Net.Sockets;
using System.Runtime.ExceptionServices;
Expand Down Expand Up @@ -103,9 +104,24 @@ protected virtual int ReadCore(byte[] buffer, int offset, int size)
return nread;
}

if (_remainingBody > 0)
{
size = (int)Math.Min(_remainingBody, (long)size);
}

nread = _stream.Read(buffer, offset, size);
if (nread > 0 && _remainingBody > 0)

if (_remainingBody > 0)
{
if (nread == 0)
{
throw new HttpListenerException((int)HttpStatusCode.BadRequest);
}

Debug.Assert(nread <= _remainingBody);
_remainingBody -= nread;
}

return nread;
}

Expand Down Expand Up @@ -138,7 +154,7 @@ protected virtual IAsyncResult BeginReadCore(byte[] buffer, int offset, int size
// for HTTP pipelining
if (_remainingBody >= 0 && size > _remainingBody)
{
size = (int)Math.Min(int.MaxValue, _remainingBody);
size = (int)Math.Min(_remainingBody, (long)size);
}

return _stream.BeginRead(buffer, offset, size, cback, state);
Expand Down Expand Up @@ -182,8 +198,14 @@ public override int EndRead(IAsyncResult asyncResult)
ExceptionDispatchInfo.Throw(e.InnerException);
}

if (_remainingBody > 0 && nread > 0)
if (_remainingBody > 0)
{
if (nread == 0)
{
throw new HttpListenerException((int)HttpStatusCode.BadRequest);
}

Debug.Assert(nread <= _remainingBody);
_remainingBody -= nread;
}

Expand Down
10 changes: 1 addition & 9 deletions src/System.Net.HttpListener/tests/GetContextHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,7 @@ public static class Helpers

public static void WaitForSocketShutdown(Socket socket)
{
if (PlatformDetection.IsWindows || PlatformDetection.IsOSX)
{
socket.Shutdown(SocketShutdown.Both);
while (SocketConnected(socket));
}
else
{
socket.Close();
}
socket.Close();
}

public static bool SocketConnected(Socket socket)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,6 @@ public async Task CloseResponseEntity_SendMoreThanContentLength_ThrowsInvalidOpe
}
}

[ActiveIssue(20246)] // CI hanging frequently
[ConditionalTheory(nameof(PlatformDetection) + "." + nameof(PlatformDetection.IsNotOneCoreUAP))]
[InlineData(true)]
[InlineData(false)]
Expand Down
10 changes: 2 additions & 8 deletions src/System.Net.HttpListener/tests/HttpRequestStreamTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ public async Task EndRead_CalledTwice_ThrowsInvalidOperationException(bool chunk
}
}

[ConditionalFact(nameof(Helpers) + "." + nameof(Helpers.IsWindowsImplementationAndNotUap))] // [ActiveIssue(20246, TestPlatforms.AnyUnix)] // CI hanging frequently, [ActiveIssue(19983, platforms: TestPlatforms.AnyUnix)] // No exception thrown
[ConditionalFact(nameof(PlatformDetection) + "." + nameof(PlatformDetection.IsNotOneCoreUAP))]
public async Task Read_FromClosedConnectionAsynchronously_ThrowsHttpListenerException()
{
const string Text = "Some-String";
Expand All @@ -505,13 +505,10 @@ public async Task Read_FromClosedConnectionAsynchronously_ThrowsHttpListenerExce
byte[] buffer = new byte[expected.Length];
await Assert.ThrowsAsync<HttpListenerException>(() => context.Request.InputStream.ReadAsync(buffer, 0, buffer.Length));
await Assert.ThrowsAsync<HttpListenerException>(() => context.Request.InputStream.ReadAsync(buffer, 0, buffer.Length));

// Closing a response from a closed client if no writing has failed should fail.
Assert.Throws<HttpListenerException>(() => context.Response.Close());
}
}

[ConditionalFact(nameof(Helpers) + "." + nameof(Helpers.IsWindowsImplementationAndNotUap))] // [ActiveIssue(20246, TestPlatforms.AnyUnix)] // CI hanging frequently, [ActiveIssue(19983, platforms: TestPlatforms.AnyUnix)] // No exception thrown
[ConditionalFact(nameof(PlatformDetection) + "." + nameof(PlatformDetection.IsNotOneCoreUAP))]
public async Task Read_FromClosedConnectionSynchronously_ThrowsHttpListenerException()
{
const string Text = "Some-String";
Expand All @@ -534,9 +531,6 @@ public async Task Read_FromClosedConnectionSynchronously_ThrowsHttpListenerExcep
byte[] buffer = new byte[expected.Length];
Assert.Throws<HttpListenerException>(() => context.Request.InputStream.Read(buffer, 0, buffer.Length));
Assert.Throws<HttpListenerException>(() => context.Request.InputStream.Read(buffer, 0, buffer.Length));

// Closing a response from a closed client if no writing has occured should fail.
Assert.Throws<HttpListenerException>(() => context.Response.Close());
}
}
}
Expand Down