Skip to content

Commit e66cfbf

Browse files
committed
Add Cache-Control to headers.
1 parent 11ae0bc commit e66cfbf

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

adafruit_httpserver/response.py

+8-1
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:
@@ -81,6 +84,8 @@ def _construct_response_bytes( # pylint: disable=too-many-arguments
8184
for header, value in response_headers.items():
8285
response += f"{header}: {value}\r\n"
8386

87+
response += f"Cache-Control: max-age={cache}\r\n"
88+
8489
response += f"\r\n{body}"
8590

8691
return response.encode("utf-8")
@@ -129,6 +134,7 @@ def _send_response( # pylint: disable=too-many-arguments
129134
self._construct_response_bytes(
130135
status=status,
131136
content_type=content_type,
137+
cache=self.cache,
132138
headers=headers,
133139
body=body,
134140
),
@@ -148,6 +154,7 @@ def _send_file_response( # pylint: disable=too-many-arguments
148154
status=self.status,
149155
content_type=MIMEType.from_file_name(filename),
150156
content_length=file_length,
157+
cache=self.cache,
151158
headers=headers,
152159
),
153160
)

adafruit_httpserver/server.py

+1-1
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)