Created
September 23, 2016 21:47
-
-
Save waaaaRapy/c0ddf3c694ae6af8b2028a01a1158b63 to your computer and use it in GitHub Desktop.
Toshiba FlashAir Lua -- send large binary response using chunked transfer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| print([[ | |
| HTTP/1.1 200 OK | |
| Content-Type: image/jpeg | |
| Transfer-Encoding: chunked | |
| ]]) | |
| IMG = "/img.jpg" | |
| chunk_size = 8192 | |
| size = lfs.attributes(IMG).size | |
| img = io.open(IMG) | |
| repeat | |
| size = size - chunk_size | |
| if (size < 0) then | |
| chunk_size = chunk_size + size | |
| size = 0 | |
| end | |
| print(string.format("%x", chunk_size)) | |
| print(img:read(chunk_size)) | |
| until size == 0 | |
| print("0") | |
| print("") | |
| img:close() |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
As of firmware version FA9CAW3AW3.00.01,
io.stdout:write()seems to be not working (moreover, the card freeze whenio.stdout:flush()is called and it can crush it's file system.) The other way to send response directly to a HTTP request is usingprint(), but it emits trailing\r\nso it can't send large binary data like embed jpeg preview image of raw image file in a row.A solution for this problem is using chunked transfer encoding defined in HTTP/1.1. In this way, the response may be split into small chunks trailing with
\r\nso you can useprint()for sending these chunks.Note: As far as you want to retrive a part of file, partial GET request from client side
XMLHttpRequestwith Range header would work.