feat: Improve HTTP response handling in web server with proper headers
This commit is contained in:
parent
bb46a69eba
commit
5ce7cd43a4
@ -60,22 +60,31 @@ class TempWebServer:
|
|||||||
|
|
||||||
if response is None:
|
if response is None:
|
||||||
response = self._get_status_page(sensors, ac_monitor, heater_monitor, schedule_monitor)
|
response = self._get_status_page(sensors, ac_monitor, heater_monitor, schedule_monitor)
|
||||||
|
|
||||||
# ===== START: Send response =====
|
# ===== START: Send response with proper HTTP headers =====
|
||||||
print("DEBUG: Sending response ({} bytes)".format(len(response)))
|
print("DEBUG: Sending response ({} bytes)".format(len(response)))
|
||||||
try:
|
try:
|
||||||
conn.send(response.encode('utf-8')) # ← Changed to 'conn'
|
# Check if response already has HTTP headers (like redirects)
|
||||||
|
if response.startswith('HTTP/1.1'):
|
||||||
|
# Response already has headers (redirect), send as-is
|
||||||
|
conn.sendall(response.encode('utf-8'))
|
||||||
|
else:
|
||||||
|
# HTML response needs headers added first
|
||||||
|
conn.send(b'HTTP/1.1 200 OK\r\n')
|
||||||
|
conn.send(b'Content-Type: text/html; charset=utf-8\r\n')
|
||||||
|
conn.send('Content-Length: {}\r\n'.format(len(response.encode('utf-8'))).encode('utf-8'))
|
||||||
|
conn.send(b'Connection: close\r\n')
|
||||||
|
conn.send(b'\r\n') # Blank line separates headers from body
|
||||||
|
conn.sendall(response.encode('utf-8'))
|
||||||
|
|
||||||
print("DEBUG: Response sent successfully")
|
print("DEBUG: Response sent successfully")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print("ERROR: Failed to send response: {}".format(e))
|
print("ERROR: Failed to send response: {}".format(e))
|
||||||
finally:
|
finally:
|
||||||
conn.close() # ← Changed to 'conn'
|
conn.close()
|
||||||
print("DEBUG: Client connection closed")
|
print("DEBUG: Client connection closed")
|
||||||
# ===== END: Send response =====
|
# ===== END: Send response =====
|
||||||
|
|
||||||
conn.send('HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=utf-8\r\nConnection: close\r\n\r\n')
|
|
||||||
conn.sendall(response.encode('utf-8'))
|
|
||||||
conn.close()
|
|
||||||
except OSError:
|
except OSError:
|
||||||
pass
|
pass
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user