Python: Fix header conversion to byte-pair on scope building#2125
Python: Fix header conversion to byte-pair on scope building#2125morganabc wants to merge 2 commits intocloudflare:mainfrom morganabc:patch-1
Conversation
|
All contributors have signed the CLA ✍️ ✅ |
|
I have read the CLA Document and I hereby sign the CLA |
hoodmane
left a comment
There was a problem hiding this comment.
Code change makes sense. Ideally we ought to add a test.
| from js import URL | ||
|
|
||
| headers = [tuple(x) for x in req.headers] | ||
| headers = [(k.lower().encode(), v.encode()) for k, v in req.headers] |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Also is there way to test this locally? I want to build miniflare with master workerd, but building workers-sdk doesnt works..
There was a problem hiding this comment.
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...
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Oh, so I can use workerd directly! Thanks, now I can test few things.
|
That's not the only trouble w/ ASGI and binary strings. We also need to decode resulting headers in |
Confirmed! This patch fixed the problem with b'' I've seen before |
Yes, right. I fixed it. Thanks! |
src/pyodide/internal/asgi.py
Outdated
| if got["type"] == "http.response.start": | ||
| status = got["status"] | ||
| headers = got["headers"] | ||
| headers = [(k.decode(), v.decode()) for k, v in got['headers']] |
There was a problem hiding this comment.
@morgan9e I'd use double quotes here to be compliant w/ folks' formatting standards
|
Since #2096 merged, we're actually running Python tests on workerd so it should be possible to add a test now. |
|
Agreed we should make it independent. Currently the only thing in there that is dependent on fastapi is |
|
Hey @morgan9e, would love to get this merged in, are you still planning to create a test for this? |
Yep, thanks I'll do it ASAP. |
I added the test, I wrote sample ASGI Application rather than using FastAPI because I thought it was better to seperate it from FastAPI. |
|
Sorry, I accidently merged and removed upstream branch, I reopened it. #3142 |

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.