Skip to content

Commit

Permalink
Merge pull request #129 from Beyley/fix-response-length
Browse files Browse the repository at this point in the history
Don't keep our own count of response length, use ResponseStream.Length
  • Loading branch information
jvyden committed May 13, 2024
2 parents 2262fba + c159aaf commit bd3e67f
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions Bunkum.Listener/Request/ListenerContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,6 @@ public virtual long ContentLength
/// </summary>
public string? ResponseType;

private int _responseLength;

// ReSharper disable once MemberCanBePrivate.Global

/// <summary>
Expand All @@ -122,7 +120,6 @@ public virtual long ContentLength
public void Write(ReadOnlySpan<byte> buffer)
{
this.ResponseStream.Write(buffer);
this._responseLength += buffer.Length;
}

/// <summary>
Expand All @@ -133,10 +130,12 @@ public virtual async Task FlushResponseAndClose()
if (this.ResponseType != null)
this.ResponseHeaders.Add("Content-Type", this.ResponseType);

if(this._responseLength != 0)
this.ResponseHeaders.Add("Content-Length", this._responseLength.ToString());
int responseLength = (int)this.ResponseStream.Length;

if(responseLength != 0)
this.ResponseHeaders.Add("Content-Length", responseLength.ToString());

ArraySegment<byte> dataSlice = new(this.ResponseStream.GetBuffer(), 0, this._responseLength);
ArraySegment<byte> dataSlice = new(this.ResponseStream.GetBuffer(), 0, responseLength);

await this.SendResponse(this.ResponseCode, dataSlice);
}
Expand Down

0 comments on commit bd3e67f

Please sign in to comment.