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

Python: Fix header conversion to byte-pair on scope building #2125

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

morgan9e
Copy link

@morgan9e morgan9e commented May 14, 2024

headers: These are byte strings of the exact byte sequences sent by the client/to be sent by the server. While modern HTTP standards say that headers should be ASCII, older ones did not and allowed a wider range of characters. Frameworks/applications should decode headers as they deem appropriate.
(https://asgi.readthedocs.io/en/latest/specs/www.html#wsgi-compatibility)

Workerd uses Pyodide JSProxy object when passing fetch(request, env) to Python, but JSProxy objects translates into string, not byte-string.

In order to match ASGI specification, it needs to be translated into byte-pair when building scopes.

Copy link

github-actions bot commented May 14, 2024

CLA Assistant Lite bot All contributors have signed the CLA ✍️ ✅

@morgan9e
Copy link
Author

I have read the CLA Document and I hereby sign the CLA

github-actions bot added a commit that referenced this pull request May 14, 2024
@morgan9e morgan9e marked this pull request as ready for review May 14, 2024 23:21
@morgan9e morgan9e requested review from a team as code owners May 14, 2024 23:21
@morgan9e morgan9e requested review from jasnell and hoodmane May 14, 2024 23:21
Copy link
Contributor

@hoodmane hoodmane left a comment

Choose a reason for hiding this comment

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

Code change makes sense. Ideally we ought to add a test.

@morgan9e morgan9e requested a review from hoodmane May 29, 2024 08:57
@@ -25,7 +25,7 @@ def acquire_js_buffer(pybuffer):
def request_to_scope(req, env, ws=False):
from js import URL

headers = [tuple(x) for x in req.headers]
headers = [(k.lower().encode(), v.encode()) for k, v in req.headers]
Copy link
Contributor

Choose a reason for hiding this comment

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

In lieu of adding a test, can we add a comment here with an example that cares about this being bytes so that we can add a test ourselves later?

Copy link
Author

Choose a reason for hiding this comment

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

It assumes FastAPI so that would be a main concern, but its for ASGI spec though so should I add for FastAPI or just asserting for spec?

Like, anything for

@app.get("/example")
  async def example(request: Request):
    request.headers.get("content-type") # this will error if header is not bytes.

perhaps?

Copy link
Author

Choose a reason for hiding this comment

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

Also is there way to test this locally? I want to build miniflare with master workerd, but building workers-sdk doesnt works..

Copy link
Contributor

Choose a reason for hiding this comment

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

You can build workerd by itself and then set an environment variable to tell wrangler to use your custom build. @dom96 are there good instructions somewhere that we can point contributors to?

I guess I should check it out and test it myself...

Copy link
Collaborator

Choose a reason for hiding this comment

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

May be easier to build and run workerd locally. These are probably the best instructions we have for doing this: https://github.com/cloudflare/workerd?tab=readme-ov-file#configuring-workerd.

We also have some sample Python worker configs in the repo already, for example https://github.com/cloudflare/workerd/tree/main/samples/pyodide-fastapi

Copy link
Author

@morgan9e morgan9e May 30, 2024

Choose a reason for hiding this comment

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

Oh, so I can use workerd directly! Thanks, now I can test few things.

@jasnell jasnell added the python Issues/PRs relating to Python Workers label May 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
python Issues/PRs relating to Python Workers
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants