@@ -94,8 +94,26 @@ local function get_boundary()
9494 return match (header , " ;%s*boundary=([^\" ,;]+)" )
9595end
9696
97+ local function read_line_construct (sock , lf_line_break )
98+ local line_end = " \r\n "
99+ if lf_line_break then
100+ line_end = " \n "
101+ end
102+ local read_line , err = sock :receiveuntil (line_end )
103+ if lf_line_break and read_line then
104+ return function (arg )
105+ local ret , err_inner = read_line (arg )
106+ if ret and ret :sub (- 1 ,- 1 ) == ' \r ' then
107+ ret = ret :sub (1 ,- 2 )
108+ end
109+ return ret , err_inner
110+ end
111+ else
112+ return read_line , err
113+ end
114+ end
97115
98- function _M .new (self , chunk_size , max_line_size , preserve_body )
116+ function _M .new (self , chunk_size , max_line_size , preserve_body , lf_line_break )
99117 local boundary = get_boundary ()
100118
101119 -- print("boundary: ", boundary)
@@ -121,7 +139,7 @@ function _M.new(self, chunk_size, max_line_size, preserve_body)
121139 return nil , err
122140 end
123141
124- local read_line , err = sock : receiveuntil ( " \r\n " )
142+ local read_line , err = read_line_construct ( sock , lf_line_break )
125143 if not read_line then
126144 return nil , err
127145 end
@@ -134,7 +152,8 @@ function _M.new(self, chunk_size, max_line_size, preserve_body)
134152 read_line = read_line ,
135153 boundary = boundary ,
136154 state = STATE_BEGIN ,
137- preserve_body = preserve_body
155+ preserve_body = preserve_body ,
156+ lf_line_break = lf_line_break
138157 }, mt )
139158end
140159
@@ -202,7 +221,17 @@ local function read_body_part(self)
202221 if not chunk then
203222 local sock = self .sock
204223
205- local data = sock :receive (2 )
224+ local data , err = sock :receive (1 )
225+
226+ -- partial lookahead: -- or CRLF?
227+ if data == " -" or data == " \r " then
228+ local next
229+ next , err = sock :receive (1 )
230+ if next then
231+ data = data .. next
232+ end
233+ end
234+
206235 if data == " --" then
207236 local ok , err = discard_rest (self )
208237 if not ok then
@@ -213,7 +242,7 @@ local function read_body_part(self)
213242 return " part_end"
214243 end
215244
216- if data ~= " \r\n " then
245+ if data ~= " \r\n " and not ( data == " \n " and self . lf_line_break ) then
217246 local ok , err = discard_line (self )
218247 if not ok then
219248 return nil , nil , err
0 commit comments