Skip to content

Commit 7814daa

Browse files
committed
Add no-cache headers to webserver endpoints
Prevents browsers and proxies from caching stale inverter data by adding Cache-Control, Pragma, and Expires headers to all HTTP responses.
1 parent 2c2935d commit 7814daa

1 file changed

Lines changed: 12 additions & 0 deletions

File tree

SunGather/exports/webserver.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,23 +80,35 @@ def do_GET(self):
8080
if self.path.startswith('/metrics'):
8181
self.send_response(200)
8282
self.send_header("Content-type", "text/plain")
83+
self.send_header("Cache-Control", "no-store, no-cache, must-revalidate, max-age=0")
84+
self.send_header("Pragma", "no-cache")
85+
self.send_header("Expires", "0")
8386
self.end_headers()
8487
self.wfile.write(bytes(export_webserver.metrics, "utf-8"))
8588
elif self.path.startswith('/config'):
8689
self.send_response(200)
8790
self.send_header("Content-type", "text/html")
91+
self.send_header("Cache-Control", "no-store, no-cache, must-revalidate, max-age=0")
92+
self.send_header("Pragma", "no-cache")
93+
self.send_header("Expires", "0")
8894
self.end_headers()
8995
self.wfile.write(bytes(export_webserver.config, "utf-8"))
9096
parsed_data = parse_qs(urlparse(self.path).query)
9197
logging.info(f"{parsed_data}")
9298
elif self.path.startswith('/json'):
9399
self.send_response(200)
94100
self.send_header("Content-type", "application/json")
101+
self.send_header("Cache-Control", "no-store, no-cache, must-revalidate, max-age=0")
102+
self.send_header("Pragma", "no-cache")
103+
self.send_header("Expires", "0")
95104
self.end_headers()
96105
self.wfile.write(bytes(export_webserver.json, "utf-8"))
97106
else:
98107
self.send_response(200)
99108
self.send_header("Content-type", "text/html")
109+
self.send_header("Cache-Control", "no-store, no-cache, must-revalidate, max-age=0")
110+
self.send_header("Pragma", "no-cache")
111+
self.send_header("Expires", "0")
100112
self.end_headers()
101113
self.wfile.write(bytes("<html><head><title>SunGather</title>", "utf-8"))
102114
self.wfile.write(bytes("<meta charset='UTF-8'><meta http-equiv='refresh' content='15'>", "utf-8"))

0 commit comments

Comments
 (0)