Send a JSON (UTF-8) response with CORS headers. Safe against client disconnects.
(self, data, status_code: int = 200)
| 1057 | self.send_json_response({"error": f"Failed to rename session: {str(e)}"}, status_code=500) |
| 1058 | |
| 1059 | def send_json_response(self, data, status_code: int = 200): |
| 1060 | """Send a JSON (UTF-8) response with CORS headers. Safe against client disconnects.""" |
| 1061 | try: |
| 1062 | self.send_response(status_code) |
| 1063 | self.send_header('Content-Type', 'application/json') |
| 1064 | self.send_header('Access-Control-Allow-Origin', '*') |
| 1065 | self.send_header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS') |
| 1066 | self.send_header('Access-Control-Allow-Headers', 'Content-Type, Authorization') |
| 1067 | self.send_header('Access-Control-Allow-Credentials', 'true') |
| 1068 | self.end_headers() |
| 1069 | |
| 1070 | response_bytes = json.dumps(data, indent=2).encode('utf-8') |
| 1071 | self.wfile.write(response_bytes) |
| 1072 | except BrokenPipeError: |
| 1073 | # Client disconnected before we could finish sending |
| 1074 | print("⚠️ Client disconnected during response – ignoring.") |
| 1075 | except Exception as e: |
| 1076 | print(f"❌ Error sending response: {e}") |
| 1077 | |
| 1078 | def log_message(self, format, *args): |
| 1079 | """Custom log format""" |
no test coverage detected