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 redundant and technically not defined for the application/json media type according to RFC 8259. JSON text is required to be encoded using UTF-8 for interoperability. While many clients handle this parameter, adding it is non-standard and can occasionally cause issues with strict parsers.

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

The change to explicitly use .encode('utf-8') is redundant in Python 3, as utf-8 is the default encoding. Additionally, the separators optimization mentioned in the PR description was already present in the code. To actually achieve the goal of minimizing network payload size (especially if the data contains non-ASCII characters), consider adding ensure_ascii=False. This prevents json.dumps from escaping non-ASCII characters as 6-byte Unicode sequences, allowing them to be encoded as compact literal UTF-8 characters instead.

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


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