Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handler Stream Dispose Fix #9

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion Hydra/Http/HttpRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ private void Validate()
if (distinct.Length != 1 || !int.TryParse(distinct[0], out length)) throw new InvalidContentLengthException();
}

body = new SizedStream(reader.Stream, length);
body = new SizedStream(reader.Stream, length, false);
}
// if a request has neither Transfer-Encoding nor Content-Length headers it is assumed to have an empty body
else body = Stream.Null;
Expand Down
2 changes: 1 addition & 1 deletion Hydra/SizedStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ internal class SizedStream : WrapperStream
/// </summary>
/// <param name="stream">Stream to wrap</param>
/// <param name="length">Length to limit to</param>
internal SizedStream(Stream stream, int length) : base(stream)
internal SizedStream(Stream stream, int length, bool ownStream) : base(stream, ownStream)
{
this.length = length;
}
Expand Down
2 changes: 1 addition & 1 deletion Hydra/WebSocket/WebSocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ private async ValueTask<SemaphoreSlimLock> FullWriterLock(CancellationToken canc
{
currentStream = new WebSocketMaskedStream(
new SizedStream(reader.Reader.AsStream(false),
(int)frameInfo.Value.Length),
(int)frameInfo.Value.Length, true),
frameInfo.Value.MaskingKey);
}
else if (frameInfo.Value.Opcode is not WebSocketOpcode.Text and not WebSocketOpcode.Binary) throw new NonFrameableMessageFramedException();
Expand Down
2 changes: 1 addition & 1 deletion Hydra/WebSocket/WebSocketMaskedStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ internal class WebSocketMaskedStream : WrapperStream
{
private readonly WebSocketMasker masker;

internal WebSocketMaskedStream(Stream stream, uint maskingKey) : base(stream)
internal WebSocketMaskedStream(Stream stream, uint maskingKey) : base(stream, false)
{
masker = new(maskingKey);
}
Expand Down
6 changes: 4 additions & 2 deletions Hydra/WrapperStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ namespace Hydra
internal abstract class WrapperStream : ReadOnlyStream
{
private readonly Stream stream;
private readonly bool ownStream;

protected WrapperStream(Stream stream)
protected WrapperStream(Stream stream, bool ownStream)
{
this.stream = stream;
this.ownStream = ownStream;
}

public override long Length => stream.Length;
Expand All @@ -23,7 +25,7 @@ protected WrapperStream(Stream stream)

protected override void Dispose(bool disposing)
{
if (disposing) stream.Dispose();
if (ownStream && disposing) stream.Dispose();
base.Dispose(disposing);
}
public override async ValueTask DisposeAsync()
Expand Down