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 application/json media type does not define a charset parameter according to RFC 8259, Section 11. JSON is defined to be UTF-8 by default. Adding ; charset=utf-8 is redundant and technically non-standard, which can occasionally cause issues with strict HTTP clients or middleware that expect the standard 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

This change adds an explicit 'utf-8' argument to .encode(), which is already the default in Python 3. Additionally, the PR description mentions removing indent=2 and adding separators, but the code before this change (the LEFT side) already utilized these compact separators and did not have indentation.

To further optimize for payload size and efficiency as intended by this PR:

  1. Use ensure_ascii=False in json.dumps() to prevent non-ASCII characters from being escaped into 6-byte \uXXXX sequences, which reduces size for internationalized content.
  2. Consider calculating the payload length and sending a Content-Length header to allow for persistent HTTP connections (Keep-Alive), which significantly improves network efficiency.
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