Skip to content

Starlette middleware not respecting "capture_body" flag #1545

Closed
@frnkvieira

Description

@frnkvieira

Hello :)

While investigating a bug regarding very slow uploads, we discovered two things about the Starlette middleware.

  • It does not honor the "capture_body" flag by consuming the stream regardless the flag is turned on or not. It replaces the original stream with a mocked one, making the Starlette request.stream() feature useless because the request is already entirely in memory (using a lot of memory in case of file uploads). I think the following piece of code could check the flag and behave differently.
    body = b""
    while True:
    message = await receive()
    if not message:
    break
    if message["type"] == "http.request":
    b = message.get("body", b"")
    if b:
    body += b
    if not message.get("more_body", False):
    break
    if message["type"] == "http.disconnect":
    break
  • The same block of code above uses byte concatenation instead of a byte array which is fine for very small buffers but prohibitively slow with anything bigger because of the quadratic behavior. Gigabyte-sized streams do not work at all.

I would happily send a pull request for that, but I would like some opinions first, especially after seeing this (#1528), which seems promising.

Anyway, Elastic APM is awesome. Thanks for the great work!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions