Skip to content

Commit

Permalink
feat(http): add WSLINK_HTTP_HEADERS for HTTP header addon
Browse files Browse the repository at this point in the history
  • Loading branch information
jourdain committed Jun 19, 2024
1 parent f25f758 commit 7d16203
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
1 change: 1 addition & 0 deletions python/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Those only apply for the Python server and launcher.
* WSLINK_LAUNCHER_DELETE - If set to 1 this will enable the DELETE endpoint for killing a running session
* WSLINK_MAX_MSG_SIZE - Number of bytes for a message size (default: 4194304)
* WSLINK_HEART_BEAT - Number of seconds between heartbeats (default: 30)
* WSLINK_HTTP_HEADERS - Path to json file containing HTTP headers to be added

License
-------
Expand Down
24 changes: 14 additions & 10 deletions python/src/wslink/backends/aiohttp/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import asyncio
import os
import logging
import sys
import uuid
import json
from pathlib import Path

from wslink.protocol import WslinkHandler, AbstractWebApp

Expand All @@ -15,6 +16,10 @@
MSG_OVERHEAD = int(os.environ.get("WSLINK_MSG_OVERHEAD", 4096))
MAX_MSG_SIZE = int(os.environ.get("WSLINK_MAX_MSG_SIZE", 4194304))
HEART_BEAT = int(os.environ.get("WSLINK_HEART_BEAT", 30)) # 30 seconds
HTTP_HEADERS: str | None = os.environ.get("WSLINK_HTTP_HEADERS") # path to json file

if HTTP_HEADERS and Path(HTTP_HEADERS).exists():
HTTP_HEADERS: dict = json.loads(Path(HTTP_HEADERS).read_text())

logger = logging.getLogger(__name__)

Expand All @@ -35,25 +40,24 @@ def _fix_path(path):
return path


# -----------------------------------------------------------------------------
# Needed for WASM/sharedArrayBuffer
# => we should find a way to dynamically provided needed header
# -----------------------------------------------------------------------------
@aiohttp_web.middleware
async def shared_array_buffer_headers(request: aiohttp_web.Request, handler):
async def http_headers(request: aiohttp_web.Request, handler):
response: aiohttp_web.Response = await handler(request)
response.headers.setdefault("Cross-Origin-Opener-Policy", "same-origin")
response.headers.setdefault("Cross-Origin-Embedder-Policy", "require-corp")
response.headers.setdefault("Access-Control-Allow-Origin", "*")
response.headers.setdefault("Cache-Control", "no-store")
for k, v in HTTP_HEADERS.items():
response.headers.setdefault(k, v)

return response


# -----------------------------------------------------------------------------
class WebAppServer(AbstractWebApp):
def __init__(self, server_config):
AbstractWebApp.__init__(self, server_config)
self.set_app(aiohttp_web.Application(middlewares=[shared_array_buffer_headers]))
if HTTP_HEADERS:
self.set_app(aiohttp_web.Application(middlewares=[http_headers]))
else:
self.set_app(aiohttp_web.Application())
self._ws_handlers = []
self._site = None
self._runner = None
Expand Down

0 comments on commit 7d16203

Please sign in to comment.