lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


It was thus said that the Great zhiguo zhao once stated:
> Hi all,
> 
> attachment is post data(to upload file), when I use below code to parse
> only success on plain text file, failed with any binary file.
> My quest is  lpeg only support plain text? and if support binary even
> partly, how to update my lpeg code to parse attachment if support binary.
> 
> thank you for you option or help.
> 

  I use LPeg to parse CGI data and while my code isn't in a form to be
published quite yet [1], I do know that I can handle binary data just fine. 
The main part of the code that will probably help you the most is:

  local boundary = lpeg.P("--" .. separator)
  local hdrs     = core.parse_headers(mime._HEADERS,contentdisp._HEADERS)
  local body     = lpeg.C((lpeg.P(1) - boundary)^0)
  local section  = boundary 
                 * core.CRLF 
                 * lpeg.Ct(lpeg.Cg(hdrs,"headers") * lpeg.Cg(body,"body"))
  local sections = lpeg.Ct(section^1) * boundary * lpeg.P"--" * core.CRLF
  
  local tmp = sections:match(data)

hdrs just parses the MIME headers for each section, body is what parses the
actual body of each section and is text/binary agnostic.  

  -spc

[1]	It's 741 lines of Lua/LPeg code just to parse multipart/form-data
	and what I have works for my usecase; I can't guarentee it will work
	for all websites.