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 2 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 = false) : base(stream, ownStream)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same thing here

{
this.length = length;
}
Expand Down
3 changes: 2 additions & 1 deletion Hydra/WebSocket/WebSocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,8 @@ private async ValueTask<SemaphoreSlimLock> FullWriterLock(CancellationToken canc
currentStream = new WebSocketMaskedStream(
new SizedStream(reader.Reader.AsStream(false),
(int)frameInfo.Value.Length),
frameInfo.Value.MaskingKey);
frameInfo.Value.MaskingKey,
true);
}
else if (frameInfo.Value.Opcode is not WebSocketOpcode.Text and not WebSocketOpcode.Binary) throw new NonFrameableMessageFramedException();
else currentStream = null; // TODO: Fragmented stream
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, bool ownStream) : base(stream, ownStream)
{
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 = false)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer if this was not a default parameter, since there's no real reason one should be the default over the other in which case the behaviour should probably always be specified by the caller.

{
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