Skip to content

Commit 5aa7f2f

Browse files
committed
Add Cache-Control to headers.
1 parent f0f7767 commit 5aa7f2f

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

adafruit_httpserver/response.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class HTTPResponse:
2929
status: HTTPStatus
3030
headers: Dict[str, str]
3131
content_type: str
32-
32+
cache: Optional[int]
3333
filename: Optional[str]
3434
root_path: str
3535

@@ -41,6 +41,7 @@ def __init__( # pylint: disable=too-many-arguments
4141
body: str = "",
4242
headers: Dict[str, str] = None,
4343
content_type: str = MIMEType.TYPE_TXT,
44+
cache: Optional[int] = 0,
4445
filename: Optional[str] = None,
4546
root_path: str = "",
4647
http_version: str = "HTTP/1.1",
@@ -54,6 +55,7 @@ def __init__( # pylint: disable=too-many-arguments
5455
self.body = body
5556
self.headers = headers or {}
5657
self.content_type = content_type
58+
self.cache = cache
5759
self.filename = filename
5860
self.root_path = root_path
5961
self.http_version = http_version
@@ -64,6 +66,7 @@ def _construct_response_bytes( # pylint: disable=too-many-arguments
6466
status: HTTPStatus = CommonHTTPStatus.OK_200,
6567
content_type: str = MIMEType.TYPE_TXT,
6668
content_length: Union[int, None] = None,
69+
cache: int = 0,
6770
headers: Dict[str, str] = None,
6871
body: str = "",
6972
) -> bytes:
@@ -77,6 +80,8 @@ def _construct_response_bytes( # pylint: disable=too-many-arguments
7780
headers.setdefault("Content-Length", content_length or len(body))
7881
headers.setdefault("Connection", "close")
7982

83+
response += f"Cache-Control: max-age={cache}\r\n"
84+
8085
for header, value in headers.items():
8186
response += f"{header}: {value}\r\n"
8287

@@ -128,6 +133,7 @@ def _send_response( # pylint: disable=too-many-arguments
128133
self._construct_response_bytes(
129134
status=status,
130135
content_type=content_type,
136+
cache=self.cache,
131137
headers=headers,
132138
body=body,
133139
),
@@ -147,6 +153,7 @@ def _send_file_response( # pylint: disable=too-many-arguments
147153
status=self.status,
148154
content_type=MIMEType.from_file_name(filename),
149155
content_length=file_length,
156+
cache=self.cache,
150157
headers=headers,
151158
),
152159
)

adafruit_httpserver/server.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ def poll(self):
167167
# If no handler exists and request method is GET, try to serve a file.
168168
elif request.method == HTTPMethod.GET:
169169
response = HTTPResponse(
170-
filename=request.path, root_path=self.root_path
170+
filename=request.path, root_path=self.root_path, cache=604800
171171
)
172172

173173
# If no handler exists and request method is not GET, return 400 Bad Request.

0 commit comments

Comments
 (0)