deckerego / ampule

A tiny HTTP server made for CircuitPython WiFi devices (like the ESP32)
MIT License
60 stars 10 forks source link

ERR_CONTENT_LENGTH_MISMATCH on long response strings #11

Closed andreasbrett closed 2 years ago

andreasbrett commented 2 years ago

When serving responses longer than 2572 chars, a ERR_CONTENT_LENGTH_MISMATCH is caught by the browser:

@ampule.route("/")
def light_set(request):
    headersCss = {
        "Content-Type": "text/css; charset=UTF-8",
        "Access-Control-Allow-Origin": '*',
        "Access-Control-Allow-Methods": 'GET, POST',
        "Access-Control-Allow-Headers": 'Origin, Accept, Content-Type, X-Requested-With, X-CSRF-Token'
    }
    content = readFile("test.txt")
    return (200, headersCss, content)

By sending an empty header instead this increases from 2572 to 2776 chars:

@ampule.route("/")
def light_set(request):
    content = readFile("test.txt")
    return (200, {}, content)

Here's a test endpoint evaluating this behaviour. While requesting /test/2776 works and returns a string of 2776 "x", /test/2777 will result in a ERR_CONTENT_LENGTH_MISMATCH.

@ampule.route("/test/<length>")
def light_set(request, length):
    output = "X" + "x"*(int(length)-2) + "X"
    return (200, {}, output)
andreasbrett commented 2 years ago

seems to be a specific issue for ESP32-S2 (which I used in my tests). https://github.com/deckerego/ampule/pull/14 fixes this for me.

deckerego commented 2 years ago

Thanks @andreasbrett - reviewing now.