Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions simple_seismic_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,10 @@ def do_GET(self):

def send_json(self, data):
self.send_response(200)
self.send_header('Content-Type', 'application/json')
self.send_header('Content-Type', 'application/json; charset=utf-8')
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The charset=utf-8 parameter is not defined for the application/json media type according to RFC 8259 Section 11. JSON is required to be represented using UTF-8 (or UTF-16/32) and the default is UTF-8. Including this parameter is redundant and can occasionally cause compatibility issues with strict HTTP clients or proxies that do not expect parameters on this specific media type.

Suggested change
self.send_header('Content-Type', 'application/json; charset=utf-8')
self.send_header('Content-Type', 'application/json')

self.send_header('Access-Control-Allow-Origin', '*')
self.end_headers()
self.wfile.write(json.dumps(data, separators=(',', ':')).encode())
self.wfile.write(json.dumps(data, separators=(',', ':')).encode('utf-8'))
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Explicitly passing 'utf-8' to .encode() is redundant in Python 3 as it is the default encoding.

Furthermore, to better align with the optimization goals of this PR, consider adding ensure_ascii=False to json.dumps(). By default, json.dumps escapes non-ASCII characters into Unicode escape sequences (e.g., \uXXXX), which increases CPU overhead during serialization and results in larger payloads for internationalized data. Setting it to False allows the string to contain literal UTF-8 characters, which is more efficient for both serialization and transmission.

Suggested change
self.wfile.write(json.dumps(data, separators=(',', ':')).encode('utf-8'))
self.wfile.write(json.dumps(data, separators=(',', ':'), ensure_ascii=False).encode())


def log_message(self, format, *args):
"""Override to customize logging"""
Expand Down
Loading