webide-websocket.lua 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. --rewrite from https://github.com/creationix/nodemcu-webide
  2. local function decode(chunk)
  3. if #chunk < 2 then return end
  4. local second = string.byte(chunk, 2)
  5. local len = bit.band(second, 0x7f)
  6. local offset
  7. if len == 126 then
  8. if #chunk < 4 then return end
  9. len = bit.bor(bit.lshift(string.byte(chunk, 3), 8), string.byte(chunk, 4))
  10. offset = 4
  11. elseif len == 127 then
  12. if #chunk < 10 then return end
  13. -- Ignore lengths longer than 32bit
  14. len = bit.bor(bit.lshift(string.byte(chunk, 7), 24), bit.lshift(string.byte(chunk, 8), 16),
  15. bit.lshift(string.byte(chunk, 9), 8), string.byte(chunk, 10))
  16. offset = 10
  17. else
  18. offset = 2
  19. end
  20. local mask = bit.band(second, 0x80) > 0
  21. if mask then
  22. offset = offset + 4
  23. end
  24. if #chunk < offset + len then return end
  25. local first = string.byte(chunk, 1)
  26. local payload = string.sub(chunk, offset + 1, offset + len)
  27. assert(#payload == len, "Length mismatch")
  28. if mask then
  29. payload = crypto.mask(payload, string.sub(chunk, offset - 3, offset))
  30. end
  31. local extra = string.sub(chunk, offset + len + 1)
  32. local opcode = bit.band(first, 0xf)
  33. return extra, payload, opcode
  34. end
  35. local function encode(payload, opcode)
  36. opcode = opcode or 2
  37. assert(type(opcode) == "number", "opcode must be number")
  38. assert(type(payload) == "string", "payload must be string")
  39. local len = #payload
  40. local head = string.char(bit.bor(0x80, opcode), bit.bor(len < 126 and len or len < 0x10000 and 126 or 127))
  41. if len >= 0x10000 then
  42. -- 32 bit length is plenty, assume zero for rest
  43. head = head .. string.char(0, 0, 0, 0, bit.band(bit.rshift(len, 24), 0xff), bit.band(bit.rshift(len, 16), 0xff),
  44. bit.band(bit.rshift(len, 8), 0xff), bit.band(len, 0xff))
  45. elseif len >= 126 then
  46. head = head .. string.char(bit.band(bit.rshift(len, 8), 0xff), bit.band(len, 0xff))
  47. end
  48. return head .. payload
  49. end
  50. local guid = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
  51. local function acceptKey(key)
  52. return crypto.toBase64(crypto.hash("sha1", key .. guid))
  53. end
  54. -- Dispatch function
  55. return function(connection, payload)
  56. local buffer = false
  57. local socket = {}
  58. local queue = {}
  59. local waiting = false
  60. local function onSend()
  61. if queue[1] then
  62. local data = table.remove(queue, 1)
  63. return connection:send(data, onSend)
  64. end
  65. waiting = false
  66. end
  67. function socket.send(...)
  68. local data = encode(...)
  69. if not waiting then
  70. waiting = true
  71. connection:send(data, onSend)
  72. else
  73. queue[#queue + 1] = data
  74. end
  75. collectgarbage()
  76. print(node.heap())
  77. end
  78. connection:on("receive", function(_, chunk)
  79. if buffer then
  80. buffer = buffer .. chunk
  81. while true do
  82. local extra, payload, opcode = decode(buffer)
  83. if not extra then return end
  84. buffer = extra
  85. socket.onmessage(payload, opcode)
  86. end
  87. end
  88. end)
  89. connection:on("sent", function(_, _)
  90. if socket.onsent ~= nil then
  91. socket.onsent()
  92. end
  93. end)
  94. connection:on("disconnection", function(_, _)
  95. if socket.onclose ~= nil then
  96. socket.onclose()
  97. end
  98. end)
  99. local req = dofile("httpserver-request.lc")(payload)
  100. local key = payload:match("Sec%-WebSocket%-Key: ([A-Za-z0-9+/=]+)")
  101. local fileExists = file.open(req.uri.file, "r")
  102. file.close()
  103. if req.method == "GET" and key and fileExists then
  104. connection:send("HTTP/1.1 101 Switching Protocols\r\n" ..
  105. "Upgrade: websocket\r\nConnection: Upgrade\r\n" ..
  106. "Sec-WebSocket-Accept: " .. acceptKey(key) .. "\r\n\r\n",
  107. function() dofile(req.uri.file)(socket) end)
  108. buffer = ""
  109. else
  110. connection:send("HTTP/1.1 404 Not Found\r\nConnection: Close\r\n\r\n", connection.close)
  111. end
  112. end