Skip to content

Commit 2af2946

Browse files
committed
feat(http): add helper func to strip forbidden headers
Signed-off-by: Vaughn Dice <vdice@akamai.com>
1 parent e9887a3 commit 2af2946

2 files changed

Lines changed: 26 additions & 2 deletions

File tree

examples/outgoing-request/app.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from spin_sdk import http
1+
from spin_sdk import http
22
from spin_sdk.http import Request, Response, send
33

44
class WasiHttpHandler030Rc20260315(http.Handler):
@@ -12,4 +12,6 @@ async def handle_request(self, request: Request) -> Response:
1212
bytes("Please specify `url` header", "utf-8")
1313
)
1414

15-
return await send(Request("GET", url, {}, None))
15+
resp = await send(Request("GET", url, {}, None))
16+
resp.headers = http.strip_forbidden_headers(resp.headers)
17+
return resp

src/spin_sdk/http/__init__.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,28 @@ async def send(request: Request) -> Response:
206206
bytes(body)
207207
)
208208

209+
def strip_forbidden_headers(headers:MutableMapping[str, str]) -> MutableMapping[str, str]:
210+
"""
211+
Strips forbidden headers for requests and responses originating from guest apps, per wasmtime/Spin
212+
"""
213+
# See https://github.com/bytecodealliance/wasmtime/blob/e9e1665c5ef150d618bd8c21fb355c063596d6f7/crates/wasi-http/src/lib.rs#L42-L52
214+
for header in [
215+
"connection",
216+
"keep-alive",
217+
"proxy-authenticate",
218+
"proxy-authorization",
219+
"proxy-connection",
220+
"transfer-encoding",
221+
"upgrade",
222+
"host",
223+
"http2-settings"
224+
]:
225+
try:
226+
del headers[header]
227+
except KeyError:
228+
pass
229+
return headers
230+
209231
async def copy(bytes:bytes, tx:ByteStreamWriter):
210232
with tx:
211233
if bytes is not None:

0 commit comments

Comments
 (0)