-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add support for asgi 3.0 while maintaining 2.0 support. (#1)
* fix: add support for asgi 3.0 while maintaining 2.0 support. * fix: OPTIONS does not make sense in Access-Control-Allow-Methods * fix: non-CORS OPTIONS requests do not need CORS headers. * fix: inclusion of safelisted headers has meaning and should not be done by default. * style: 204 is fine for successful preflights. * fix: 403 looks to be the explicit response mentioned in the living fetch spec. * fix: origin header only varies the response when there are multiple origins. * test: expand coverage and test that non http forwarding works. * fix: use asgiref to do asgi2 compatibility.
- Loading branch information
Showing
6 changed files
with
900 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
asgiref==3.3.4 | ||
asgiref==3.7.2 | ||
coverage==5.5 | ||
pytest==6.2.4 | ||
pytest-cov==2.12.0 | ||
starlette==0.14.2 | ||
twine==3.4.1 | ||
wheel==0.36.2 | ||
pytest-asyncio==0.15.1 | ||
setuptools==56.2.0 | ||
setuptools==56.2.0 | ||
channels>=3.0.4,<4.0.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,64 @@ | ||
from starlette.applications import Starlette | ||
from starlette.responses import JSONResponse | ||
from starlette.routing import Route | ||
|
||
async def app(scope, receive, send): | ||
if scope["type"] == "websocket": | ||
await send({"type": "websocket.accept"}) | ||
while True: | ||
message = await receive() | ||
if message["type"] == "websocket.disconnect": | ||
break | ||
elif message["type"] == "websocket.receive": | ||
# echo | ||
await send({ | ||
"type": "websocket.send", | ||
"text": message["text"], | ||
}) | ||
return | ||
elif scope['type'] == 'http': | ||
is_get = scope['method'] == 'GET' | ||
is_options = scope['method'] == 'OPTIONS' | ||
is_homepage = scope['path'] == '/' | ||
if is_get and is_homepage: | ||
await send({ | ||
'type': 'http.response.start', | ||
'status': 200, | ||
'headers': [ | ||
(b'content-length', b'17'), | ||
(b'content-type', b'application/json'), | ||
], | ||
}) | ||
await send({ | ||
'type': 'http.response.body', | ||
'body': b'{"hello":"world"}', | ||
}) | ||
return | ||
elif is_options and is_homepage: | ||
await send({ | ||
'type': 'http.response.start', | ||
'status': 200, | ||
'headers': [ | ||
(b'allow', b'GET, OPTIONS'), | ||
(b'content-length', b'2'), | ||
], | ||
}) | ||
await send({ | ||
'type': 'http.response.body', | ||
'body': b'OK', | ||
}) | ||
return | ||
await send({ | ||
'type': 'http.response.start', | ||
'status': 500, | ||
'headers': [ | ||
(b'content-type', b'text/plain'), | ||
], | ||
}) | ||
await send({ | ||
'type': 'http.response.body', | ||
'body': b'Internal Server Error', | ||
}) | ||
|
||
async def homepage(request): | ||
return JSONResponse({'hello': 'world'}) | ||
|
||
|
||
app = Starlette(debug=True, routes=[ | ||
Route('/', homepage), | ||
]) | ||
class ASGI2app(): | ||
def __init__(self, scope): | ||
self.scope = scope | ||
async def __call__(self, receive, send): | ||
return await app(self.scope, receive, send) |
Oops, something went wrong.