# HG changeset patch # User jbe # Date 1434408910 -7200 # Node ID 4d5cf5cacc68c5ea7d1dbf46e5049103651e9f3b # Parent a9433e394eb7558c69269a3892ab6affc022667b Use abstraction for socket:read_yield(...) in HTTP module diff -r a9433e394eb7 -r 4d5cf5cacc68 moonbridge_http.lua --- a/moonbridge_http.lua Mon Jun 15 02:39:49 2015 +0200 +++ b/moonbridge_http.lua Tue Jun 16 00:55:10 2015 +0200 @@ -440,6 +440,17 @@ return survive end end + -- read function + local function read(...) + local data, status = socket:read_yield(...) + if data == nil then + request_error(true, "400 Bad Request", "Read error") + end + if status == "eof" then + request_error(true, "400 Bad Request", "Unexpected EOF") + end + return data + end -- callback for request body streaming: local process_body_chunk -- reads a number of bytes from the socket, @@ -453,13 +464,7 @@ else limit = remaining end - local chunk = socket:read_yield(limit) - if not chunk then - request_error(true, "400 Bad Request", "Read error while reading chunk of request body") - end - if #chunk ~= limit then - request_error(true, "400 Bad Request", "Unexpected EOF while reading chunk of request body") - end + local chunk = read(limit) remaining = remaining - limit if process_body_chunk then process_body_chunk(chunk) @@ -470,13 +475,7 @@ local function read_body() if request.headers_flags["Transfer-Encoding"]["chunked"] then while true do - local line, status = socket:read_yield(32 + remaining_body_size_limit, "\n") - if not line then - request_error(true, "400 Bad Request", "Read error while reading next chunk of request body") - end - if status == "eof" then - request_error(true, "400 Bad Request", "Unexpected EOF while reading next chunk of request body") - end + local line = read(32 + remaining_body_size_limit, "\n") local zeros, lenstr = string.match(line, "^(0*)([1-9A-Fa-f]+[0-9A-Fa-f]*)\r?\n$") local chunkext if lenstr then @@ -494,13 +493,13 @@ end if len == 0 then break end read_body_bytes(len) - local term = socket:read_yield(2, "\n") + local term = read(2, "\n") if term ~= "\r\n" and term ~= "\n" then request_error(true, "400 Bad Request", "Encoding error while reading chunk of request body") end end while true do - local line = socket:read_yield(2 + remaining_body_size_limit, "\n") + local line = read(2 + remaining_body_size_limit, "\n") if line == "\r\n" or line == "\n" then break end remaining_body_size_limit = remaining_body_size_limit - #line if remaining_body_size_limit < 0 then