Skip to content

Instantly share code, notes, and snippets.

@waaaaRapy
Created September 23, 2016 21:47
Show Gist options
  • Select an option

  • Save waaaaRapy/c0ddf3c694ae6af8b2028a01a1158b63 to your computer and use it in GitHub Desktop.

Select an option

Save waaaaRapy/c0ddf3c694ae6af8b2028a01a1158b63 to your computer and use it in GitHub Desktop.
Toshiba FlashAir Lua -- send large binary response using chunked transfer
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()
@waaaaRapy
Copy link
Author

waaaaRapy commented Sep 23, 2016

As of firmware version FA9CAW3AW3.00.01, io.stdout:write() seems to be not working (moreover, the card freeze when io.stdout:flush() is called and it can crush it's file system.) The other way to send response directly to a HTTP request is using print(), but it emits trailing \r\n so 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\n so you can use print() for sending these chunks.

Note: As far as you want to retrive a part of file, partial GET request from client side XMLHttpRequest with Range header would work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment