Skip to content

Ability to enable StreamCopyHttpContent when body is empty #2086

@arminbashizade

Description

@arminbashizade

What should we add or change to make your life better?

(somehow similar to #618)
provide ability to enable copying content when the request body is originally empty, but will be populated later

Why is this important to you?

I have a transformer that adds a dynamic attribute (different for each request) to body for some routes, and I follow instructions outlined in #1601:

// deserialize, add attribute, serialize into bodyStr
var bytes = Encoding.UTF8.GetBytes(bodyStr);
context.HttpContext.Request.Body = new MemoryStream(bytes);
context.ProxyRequest.Content!.Headers.ContentLength = bytes.Length;

for example:

{
  "attr1": "value1"
}
// becomes
{
  "attr1": "value1",
  "attr2": "dynamic value"
}
////////////////
{}
// becomes
{
  "attr2": "dynamic value"
}
////////////////

// also becomes
{
  "attr2": "dynamic value"
}

this works fine when body is not empty, and by that I mean ContentLength == 0, however when the body is empty ProxyRequest.Content is set to null in HttpForwarder here:
https://github.com/microsoft/reverse-proxy/blob/b6f00851fc1032706c275b414244a95ef70d3a26/src/ReverseProxy/Forwarder/HttpForwarder.cs#L561-L584

and it's too late to set ContentLength or override IHttpRequestBodyDetectionFeature because transformers are applied after that:
https://github.com/microsoft/reverse-proxy/blob/b6f00851fc1032706c275b414244a95ef70d3a26/src/ReverseProxy/Forwarder/HttpForwarder.cs#L417-L423

I guess I can directly set context.ProxyRequest.Content in the specific case where body is empty (can you please confirm there's no risk here?) but I'd rather not adding an exception case. Is this something I can achieve with existing implementation?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type: IdeaThis issue is a high-level idea for discussion.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions