Guest User

Untitled

a guest
Oct 20th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. function get_multiform_data(body, ctype)
  2. local body = req.body
  3. local ctype = req.headers["Content-Type"]
  4. --Find boundry
  5. local s, e = ctype:find("boundary=.+")
  6. local boundry = "--"..ctype:sub(s, e):gsub("boundary=", ""):gsub(";", ""):gsub("%-", "%-")
  7. s, e = body:find(boundry.."\r\n")
  8. local t = {}
  9. local eor_reached = false
  10. while (not eor_reached and s ~= nil) do
  11. local ss, se = string.find(body, boundry, e)
  12. se = se+2
  13. if (body:sub(se-1,se) == "--") then
  14. eor_reached = true
  15. end
  16. --Search for content disposition header
  17. local cont_disp = body:match("Content%-Disposition:.-\r\n", e-1):sub(33):gsub("[\r\n]+.+", "")
  18. local cont_type = body:match("Content%-Type:.-\r\n", e-1)
  19. --This tells if we have a file
  20. if (cont_type ~= nil) then
  21. cont_type = cont_type:sub(15, -3)
  22. p(cont_type)
  23. end
  24. --Basically just find this and remove it, help the parser out.
  25. local sm = cont_disp:find(";")
  26. if (sm ~= nil) then
  27. cont_disp = cont_disp:sub(1,sm-1)..cont_disp:sub(sm+1)
  28. end
  29. --Find the end of Content-Disposition. Could have done this better, in case it sends headers.
  30. local _, cont_end = body:find("Content%-Disposition:.-\r\n\r\n", e-1)
  31. local ct = {}
  32. --Find a key/value pair
  33. local cs, ce = cont_disp:find(".-=\".-\"")
  34. while (cs) do
  35. --Get our pair.
  36. local cb = cont_disp:sub(cs, ce)
  37. --Find the key
  38. local cd_key = cb:match(".-=\""):sub(1, -3)
  39. --Find the value
  40. local cd_value = cb:match("=\".-\""):sub(3, -2)
  41. --Store
  42. ct[cd_key] = cd_value
  43. --Find the next one
  44. cs, ce = cont_disp:find(".-=\".-\"", ce)
  45. --But it's not super accurate.
  46. if (cs ~= nil) then cs = cs + 2 end
  47. end
  48. --Do we have a file?
  49. if (ct.filename ~= nil) then
  50. --Yes, add it like so
  51. t[ct.name] = {
  52. data = body:sub(cont_end+1, ss-3),
  53. headers = ct,
  54. mime = cont_type
  55. }
  56. else
  57. --No. Just add the data.
  58. t[ct.name] = body:sub(cont_end+1, ss-3);
  59. end
  60. s, e = ss, se
  61. end
  62. return t
  63. end
Add Comment
Please, Sign In to add comment