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

Write the HTTP response only when all output has been formatted #858

Open
wants to merge 4 commits into
base: release-8.x
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,20 @@ internal static async Task WriteToStreamAsync(

ODataPath path = request.ODataFeature().Path;
IEdmNavigationSource targetNavigationSource = path.GetNavigationSource();
HttpResponse response = request.HttpContext.Response;
var responseBody = new MemoryStream();
Copy link

Choose a reason for hiding this comment

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

Assume to use memory stream pooling

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you mean Microsoft.IO.RecyclableMemoryStream ?

var responseHeaders = request.HttpContext.Response.Headers;

// serialize a response
string preferHeader = RequestPreferenceHelpers.GetRequestPreferHeader(requestHeaders);
string annotationFilter = null;
if (!string.IsNullOrEmpty(preferHeader))
{
ODataMessageWrapper messageWrapper = ODataMessageWrapperHelper.Create(response.Body, response.Headers);
ODataMessageWrapper messageWrapper = ODataMessageWrapperHelper.Create(responseBody, responseHeaders);
messageWrapper.SetHeader(RequestPreferenceHelpers.PreferHeaderName, preferHeader);
annotationFilter = messageWrapper.PreferHeader().AnnotationFilter;
}

IODataResponseMessageAsync responseMessage = ODataMessageWrapperHelper.Create(new StreamWrapper(response.Body), response.Headers, request.GetRouteServices());
IODataResponseMessageAsync responseMessage = ODataMessageWrapperHelper.Create(new StreamWrapper(responseBody), responseHeaders, request.GetRouteServices());
if (annotationFilter != null)
{
responseMessage.PreferenceAppliedHeader().AnnotationFilter = annotationFilter;
Expand Down Expand Up @@ -150,6 +151,7 @@ internal static async Task WriteToStreamAsync(
}

await serializer.WriteObjectAsync(value, type, messageWriter, writeContext).ConfigureAwait(false);
await request.HttpContext.Response.Body.WriteAsync(responseBody.ToArray());
Copy link

Choose a reason for hiding this comment

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

do we really need to create array of unknown size here?

Copy link
Contributor Author

@wedgberto wedgberto Mar 10, 2023

Choose a reason for hiding this comment

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

i used responseBody.CopyTo(request.HttpContext.Response.Body) initially, but it didn't work how I expected so I resorted to this. I'll find a better way

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ha, I was missing rewinding the buffer response body stream back to position zero

}
}

Expand Down