Skip to content

Content-Length and multibyte characters in HTTPResponse #28

Closed
@jun2sak

Description

@jun2sak

I am building a tiny web server using adafruit_httpserver.server and circuitpython 8.0.0-beta.5 on ESP32-S2.
When multibyte characters (Japanese) are included in response body, the content-length header value received by a web client looks shorter, and some tailing characters in the body are missing.

page_html = """
<html>
  <head><title>test</title></head>
  <body>
   some text in multibyte language here..
  </body>
</html>
"""

@server.route("/test")
def base(request):
  return HTTPResponse(content_type='text/html;charset=UTF-8',body=page_html)
$ curl -v http://192.168.xx.xx/test
 :
< HTTP/1.1 200 OK
< Content-Length: 117
< Content-Type: text/html;charset=UTF-8
< Connection: close
<
* Excess found in a read: excess = 10, size = 117, maxdownload = 117, bytecount = 0
(html response are shown, but the last several characters in body are missing)

Looking into adafruit_httpserver/response.py, content-length is calculated as len(body), as in,
response_headers.setdefault("Content-Length", content_length or len(body))
and the whole response are sent after converted into bytes.
If I replace it with len(body.encode("utf-8")), the above trouble disappears, but I'm not sure this modification is right.

response.py#L78

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions