Skip to content

Commit

Permalink
chore: error response stream fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
alexpop-zengenti committed Apr 30, 2024
1 parent b3841b9 commit d0a3a4e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ private async Task HandleResponse(HttpContext context, EndpointResponse response
SetResponseHeaders(context, response.Headers);

// The content may have changed due to resolving pagelets and re-writing static paths, so get the actual length.
var responseContent = response.ToStream();
var responseContent = response.ToStream(true);
if (responseContent is MemoryStream)
{
context.Response.ContentLength = responseContent.Length;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public async Task<EndpointResponse> Invoke(
{
logLevel = LogLevel.Warning;

var endpointResponseStream = endpointResponse.ToStream();
var endpointResponseStream = endpointResponse.ToStream(true);
if (endpointResponseStream is { Length: > 0 } and MemoryStream stream)
{
using var reader = new StreamReader(stream);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,15 @@ public EndpointResponse(

public PageletPerformanceData? PageletPerformanceData { get; }

public Stream? ToStream()
public Stream? ToStream(bool resetPosition = false)
{
if (StreamContent != null)
{
StreamContent.Position = 0;
if (resetPosition)
{
StreamContent.Position = 0;
}

return StreamContent;
}

Expand All @@ -61,11 +65,15 @@ public EndpointResponse(
}

var stream = new MemoryStream();
// TODO: find a solution to write to stream without closing it
var writer = new StreamWriter(stream);

using var writer = new StreamWriter(stream, leaveOpen: true);
writer.Write(StringContent);
writer.Flush();
stream.Position = 0;
if (resetPosition)
{
stream.Position = 0;
}

return stream;
}

Expand Down

0 comments on commit d0a3a4e

Please sign in to comment.