protocol.lua 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. -- Copyright 2008 Freifunk Leipzig / Jo-Philipp Wich <jow@openwrt.org>
  2. -- Copyright 2017 Matthias Schiffer <mschiffer@universe-factory.net>
  3. -- Licensed to the public under the Apache License 2.0.
  4. -- This class contains several functions useful for http message- and content
  5. -- decoding and to retrive form data from raw http messages.
  6. module("gluon.web.http.protocol", package.seeall)
  7. HTTP_MAX_CONTENT = 1024*8 -- 8 kB maximum content size
  8. local function pump(src, snk)
  9. while true do
  10. local chunk, src_err = src()
  11. local ret, snk_err = snk(chunk, src_err)
  12. if not (chunk and ret) then
  13. local err = src_err or snk_err
  14. if err then
  15. return nil, err
  16. else
  17. return true
  18. end
  19. end
  20. end
  21. end
  22. function urlencode(s)
  23. return (string.gsub(s, '[^a-zA-Z0-9%-_%.~]',
  24. function(c)
  25. local ret = ''
  26. for i = 1, string.len(c) do
  27. ret = ret .. string.format('%%%02X', string.byte(c, i, i))
  28. end
  29. return ret
  30. end
  31. ))
  32. end
  33. -- the "+" sign to " " - and return the decoded string.
  34. function urldecode(str, no_plus)
  35. local function chrdec(hex)
  36. return string.char(tonumber(hex, 16))
  37. end
  38. if type(str) == "string" then
  39. if not no_plus then
  40. str = str:gsub("+", " ")
  41. end
  42. str = str:gsub("%%(%x%x)", chrdec)
  43. end
  44. return str
  45. end
  46. local function initval(tbl, key)
  47. if not tbl[key] then
  48. tbl[key] = {}
  49. end
  50. table.insert(tbl[key], "")
  51. end
  52. local function appendval(tbl, key, chunk)
  53. local t = tbl[key]
  54. t[#t] = t[#t] .. chunk
  55. end
  56. -- from given url or string. Returns a table with urldecoded values.
  57. -- Simple parameters are stored as string values associated with the parameter
  58. -- name within the table. Parameters with multiple values are stored as array
  59. -- containing the corresponding values.
  60. function urldecode_params(url)
  61. local params = {}
  62. if url:find("?") then
  63. url = url:gsub("^.+%?([^?]+)", "%1")
  64. end
  65. for pair in url:gmatch("[^&;]+") do
  66. -- find key and value
  67. local key = urldecode(pair:match("^([^=]+)"))
  68. local val = urldecode(pair:match("^[^=]+=(.+)$"))
  69. -- store
  70. if key and key:len() > 0 then
  71. initval(params, key)
  72. if val then
  73. appendval(params, key, val)
  74. end
  75. end
  76. end
  77. return params
  78. end
  79. -- Content-Type. Stores all extracted data associated with its parameter name
  80. -- in the params table withing the given message object. Multiple parameter
  81. -- values are stored as tables, ordinary ones as strings.
  82. -- If an optional file callback function is given then it is feeded with the
  83. -- file contents chunk by chunk and only the extracted file name is stored
  84. -- within the params table. The callback function will be called subsequently
  85. -- with three arguments:
  86. -- o Table containing decoded (name, file) and raw (headers) mime header data
  87. -- o String value containing a chunk of the file data
  88. -- o Boolean which indicates wheather the current chunk is the last one (eof)
  89. function mimedecode_message_body(src, msg, filecb)
  90. if msg and msg.env.CONTENT_TYPE then
  91. msg.mime_boundary = msg.env.CONTENT_TYPE:match("^multipart/form%-data; boundary=(.+)$")
  92. end
  93. if not msg.mime_boundary then
  94. return nil, "Invalid Content-Type found"
  95. end
  96. local tlen = 0
  97. local inhdr = false
  98. local field = nil
  99. local store = nil
  100. local lchunk = nil
  101. local function parse_headers(chunk, field)
  102. local stat
  103. repeat
  104. chunk, stat = chunk:gsub(
  105. "^([A-Z][A-Za-z0-9%-_]+): +([^\r\n]+)\r\n",
  106. function(k,v)
  107. field.headers[k] = v
  108. return ""
  109. end
  110. )
  111. until stat == 0
  112. chunk, stat = chunk:gsub("^\r\n","")
  113. -- End of headers
  114. if stat > 0 then
  115. if field.headers["Content-Disposition"] then
  116. if field.headers["Content-Disposition"]:match("^form%-data; ") then
  117. field.name = field.headers["Content-Disposition"]:match('name="(.-)"')
  118. field.file = field.headers["Content-Disposition"]:match('filename="(.+)"$')
  119. end
  120. end
  121. if not field.headers["Content-Type"] then
  122. field.headers["Content-Type"] = "text/plain"
  123. end
  124. if field.name then
  125. initval(msg.params, field.name)
  126. if field.file then
  127. appendval(msg.params, field.name, field.file)
  128. store = filecb
  129. else
  130. store = function(hdr, buf, eof)
  131. appendval(msg.params, field.name, buf)
  132. end
  133. end
  134. else
  135. store = nil
  136. end
  137. return chunk, true
  138. end
  139. return chunk, false
  140. end
  141. local function snk(chunk)
  142. tlen = tlen + (chunk and #chunk or 0)
  143. if msg.env.CONTENT_LENGTH and tlen > tonumber(msg.env.CONTENT_LENGTH) + 2 then
  144. return nil, "Message body size exceeds Content-Length"
  145. end
  146. if chunk and not lchunk then
  147. lchunk = "\r\n" .. chunk
  148. elseif lchunk then
  149. local data = lchunk .. (chunk or "")
  150. local spos, epos, found
  151. repeat
  152. spos, epos = data:find("\r\n--" .. msg.mime_boundary .. "\r\n", 1, true)
  153. if not spos then
  154. spos, epos = data:find("\r\n--" .. msg.mime_boundary .. "--\r\n", 1, true)
  155. end
  156. if spos then
  157. local predata = data:sub(1, spos - 1)
  158. if inhdr then
  159. predata, eof = parse_headers(predata, field)
  160. if not eof then
  161. return nil, "Invalid MIME section header"
  162. elseif not field.name then
  163. return nil, "Invalid Content-Disposition header"
  164. end
  165. end
  166. if store then
  167. store(field, predata, true)
  168. end
  169. field = { headers = { } }
  170. found = true
  171. data, eof = parse_headers(data:sub(epos + 1, #data), field)
  172. inhdr = not eof
  173. end
  174. until not spos
  175. if found then
  176. -- We found at least some boundary. Save
  177. -- the unparsed remaining data for the
  178. -- next chunk.
  179. lchunk, data = data, nil
  180. else
  181. -- There was a complete chunk without a boundary. Parse it as headers or
  182. -- append it as data, depending on our current state.
  183. if inhdr then
  184. lchunk, eof = parse_headers(data, field)
  185. inhdr = not eof
  186. else
  187. -- We're inside data, so append the data. Note that we only append
  188. -- lchunk, not all of data, since there is a chance that chunk
  189. -- contains half a boundary. Assuming that each chunk is at least the
  190. -- boundary in size, this should prevent problems
  191. if store then
  192. store(field, lchunk, false)
  193. end
  194. lchunk, chunk = chunk, nil
  195. end
  196. end
  197. end
  198. return true
  199. end
  200. return pump(src, snk)
  201. end
  202. -- This function will examine the Content-Type within the given message object
  203. -- to select the appropriate content decoder.
  204. -- Currently only the multipart/form-data mime type is supported.
  205. function parse_message_body(src, msg, filecb)
  206. if not (msg.env.REQUEST_METHOD == "POST" and msg.env.CONTENT_TYPE) then
  207. return
  208. end
  209. if msg.env.CONTENT_TYPE:match("^multipart/form%-data") then
  210. return mimedecode_message_body(src, msg, filecb)
  211. end
  212. end