adafruit / Adafruit_CircuitPython_HTTPServer

Simple HTTP Server for CircuitPython
MIT License
46 stars 30 forks source link

smaller buffer size for file read to send as HTTPResponse. #15

Closed FoamyGuy closed 2 years ago

FoamyGuy commented 2 years ago

This lowers the buffer size that files are read into for sending as HTTPResponse down to 2048 bytes which puts it under a limit that is affecting raspberry pi pico w #7077 for details.

This change allows responses larger than 2920 bytes to be returned successfully. Tested successfully up to 5408 bytes.

Interestingly this change also seems to make the response come back quicker as well. With currently released version I am/was getting 300-500ms fairly consistently for the response times. With the version from this PR it's been more 100-150 with a few dipping below 100ms even.

FoamyGuy commented 2 years ago

I tested this with both Pico W and ESP32-S2 Feather TFT using the reproducer script included in the core issue. Both work successfully with these changes even for files in excess of 2920 bytes.

jepler commented 2 years ago

Pico W has a bug where it won't send more than 2920 bytes (coincidentally?? 2*1460, a common TCP MSS value seen on wireless networks). However, the buffer size of 8192 bytes here is probably way too big.

The code should also deal with the fact that send() can return a value smaller than requested. A "sendall" wrapper function may need to be added so that values larger than one send() can handle are fully sent across multiple calls. I plan to PR the addition of sendall to the core, but an implementation here (or a max send so small it always works!) would be needed for airlift & for older versions of CircuitPython.