Skip to content

Commit 3d36fac

Browse files
imrove wsgi adapter to match standard
1 parent 001d1de commit 3d36fac

3 files changed

Lines changed: 26 additions & 15 deletions

File tree

slack_bolt/adapter/wsgi/handler.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
from typing import Any, Callable, Dict, Iterable, List, Tuple
1+
from typing import TYPE_CHECKING, Iterable
22

33
from slack_bolt import App
4+
5+
if TYPE_CHECKING:
6+
from wsgiref.types import StartResponse, WSGIEnvironment
7+
48
from slack_bolt.adapter.wsgi.http_request import WsgiHttpRequest
59
from slack_bolt.adapter.wsgi.http_response import WsgiHttpResponse
610
from slack_bolt.request import BoltRequest
@@ -69,14 +73,17 @@ def _get_http_response(self, request: WsgiHttpRequest) -> WsgiHttpResponse:
6973

7074
def __call__(
7175
self,
72-
environ: Dict[str, Any],
73-
start_response: Callable[..., Any],
76+
environ: "WSGIEnvironment",
77+
start_response: "StartResponse",
7478
) -> Iterable[bytes]:
7579
request = WsgiHttpRequest(environ)
76-
if "HTTP" in request.protocol:
80+
if request.protocol.startswith("HTTP"):
7781
response: WsgiHttpResponse = self._get_http_response(
7882
request=request,
7983
)
80-
start_response(response.status, response.get_headers())
81-
return response.get_body()
82-
raise TypeError(f"Unsupported SERVER_PROTOCOL: {request.protocol}")
84+
else:
85+
response = WsgiHttpResponse(
86+
status=400, headers={"content-type": ["text/plain;charset=utf-8"]}, body="Bad Request"
87+
)
88+
start_response(response.status, response.get_headers())
89+
return response.get_body()

slack_bolt/adapter/wsgi/http_request.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
from typing import Any, Dict, Sequence, Union
1+
from typing import TYPE_CHECKING, Dict, Sequence, Union
2+
3+
if TYPE_CHECKING:
4+
from wsgiref.types import WSGIEnvironment
25

36
from .internals import ENCODING
47

@@ -12,7 +15,7 @@ class WsgiHttpRequest:
1215

1316
__slots__ = ("method", "path", "query_string", "protocol", "environ")
1417

15-
def __init__(self, environ: Dict[str, Any]):
18+
def __init__(self, environ: "WSGIEnvironment"):
1619
self.method: str = environ.get("REQUEST_METHOD", "GET")
1720
self.path: str = environ.get("PATH_INFO", "")
1821
self.query_string: str = environ.get("QUERY_STRING", "")
@@ -33,5 +36,5 @@ def get_headers(self) -> Dict[str, Union[str, Sequence[str]]]:
3336
def get_body(self) -> str:
3437
if "wsgi.input" not in self.environ:
3538
return ""
36-
content_length = int(self.environ.get("CONTENT_LENGTH", 0))
39+
content_length = int(self.environ.get("CONTENT_LENGTH") or 0)
3740
return self.environ["wsgi.input"].read(content_length).decode(ENCODING)

slack_bolt/adapter/wsgi/http_response.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from http import HTTPStatus
2-
from typing import Dict, Iterable, List, Sequence, Tuple
2+
from typing import Dict, Iterable, List, Optional, Sequence, Tuple
33

44
from .internals import ENCODING
55

@@ -13,18 +13,19 @@ class WsgiHttpResponse:
1313

1414
__slots__ = ("status", "_headers", "_body")
1515

16-
def __init__(self, status: int, headers: Dict[str, Sequence[str]] = {}, body: str = ""):
16+
def __init__(self, status: int, headers: Optional[Dict[str, Sequence[str]]] = None, body: str = ""):
1717
_status = HTTPStatus(status)
1818
self.status = f"{_status.value} {_status.phrase}"
19-
self._headers = headers
19+
self._headers = headers or {}
2020
self._body = bytes(body, ENCODING)
2121

2222
def get_headers(self) -> List[Tuple[str, str]]:
2323
headers: List[Tuple[str, str]] = []
24-
for key, value in self._headers.items():
24+
for key, values in self._headers.items():
2525
if key.lower() == "content-length":
2626
continue
27-
headers.append((key, value[0]))
27+
for v in values:
28+
headers.append((key, v))
2829

2930
headers.append(("content-length", str(len(self._body))))
3031
return headers

0 commit comments

Comments
 (0)