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

fix: pathsend handling in middlewares #2616

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

gi0baro
Copy link
Contributor

@gi0baro gi0baro commented Jun 10, 2024

Summary

Closes #2613

It seems that when I introduced support for pathsend in #2435 I didn't check the included middlewares to also support the relevant ASGI messages. Affected Starlette versions 0.36.0 and onwards when running on a server supporting pathsend.

Note: this is still in draft since I'd like to plan some tests for the involved code; also the proposed solution isn't necessarily the best one, probably someone like @Kludex might review this and give his feedbacks before proceeding further.
Update: I added tests just to verify the code. In case you're unhappy with the implementation, we can refactor that and keep the tests.

Checklist

  • I understand that this PR may be closed in case there was no previous discussion. (This doesn't apply to typos!)
  • I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • I've updated the documentation accordingly.

@gi0baro gi0baro force-pushed the 2613-fix-pathsend-middlewares branch 3 times, most recently from d7b0358 to 6916ede Compare June 10, 2024 15:21
async with recv_stream:
async for message in recv_stream:
if message["type"] == "http.response.pathsend":
Copy link

Choose a reason for hiding this comment

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

Hi there! wouldn't it make sense to just read the file and stream it through here? Would be better for compatibility, no? Or maybe have some flag like "supports_message_pass_through"?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Reading the file from Python would be just the standard ASGI behaviour and defeat the whole pathsend idea (see for ref https://asgi.readthedocs.io/en/latest/extensions.html#path-send).

About the flag: I'm not sure I get the point around it. How should we evaluate such flag? pathsend messages will be returned by Starlette only if the relevant extension is present in the ASGI scope as the server supports it.

Copy link

Choose a reason for hiding this comment

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

I was thinking about random middlewares out there, deriving from this BaseMiddleware that do not know pathsend. To keep those running, returning the message instead of bytes could be opt-in via a flag/class property

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@aersam but even if you subclass BaseMiddleware, there's no way you can interact with that code. If the middleware completely overrides __call__ then it should be compliant with ASGI messages IMHO.

@gi0baro gi0baro marked this pull request as ready for review June 12, 2024 16:09
@gi0baro gi0baro force-pushed the 2613-fix-pathsend-middlewares branch 2 times, most recently from 271c3b5 to 2107b5e Compare June 12, 2024 16:13
@gi0baro gi0baro changed the title fix: pathsend handling in middlewares (#2613) fix: pathsend handling in middlewares Jun 12, 2024
@gi0baro gi0baro force-pushed the 2613-fix-pathsend-middlewares branch from 2107b5e to 5b15ea9 Compare June 12, 2024 16:18
@Kludex Kludex added the hold Don't merge it label Jun 12, 2024
@gi0baro gi0baro force-pushed the 2613-fix-pathsend-middlewares branch from 5b15ea9 to 4f01bf6 Compare June 12, 2024 16:25
@gi0baro
Copy link
Contributor Author

gi0baro commented Jun 24, 2024

@Kludex any updates on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
hold Don't merge it
Projects
None yet
Development

Successfully merging this pull request may close these issues.

pathsend causing issues with BaseHTTPMiddleware
3 participants