moonbridge

changeset 170:4d5cf5cacc68

Use abstraction for socket:read_yield(...) in HTTP module
author jbe
date Tue Jun 16 00:55:10 2015 +0200 (2015-06-16)
parents a9433e394eb7
children 32d7423a6753
files moonbridge_http.lua
line diff
     1.1 --- a/moonbridge_http.lua	Mon Jun 15 02:39:49 2015 +0200
     1.2 +++ b/moonbridge_http.lua	Tue Jun 16 00:55:10 2015 +0200
     1.3 @@ -440,6 +440,17 @@
     1.4            return survive
     1.5          end
     1.6        end
     1.7 +      -- read function
     1.8 +      local function read(...)
     1.9 +        local data, status = socket:read_yield(...)
    1.10 +        if data == nil then
    1.11 +          request_error(true, "400 Bad Request", "Read error")
    1.12 +        end
    1.13 +        if status == "eof" then
    1.14 +          request_error(true, "400 Bad Request", "Unexpected EOF")
    1.15 +        end
    1.16 +        return data
    1.17 +      end
    1.18        -- callback for request body streaming:
    1.19        local process_body_chunk
    1.20        -- reads a number of bytes from the socket,
    1.21 @@ -453,13 +464,7 @@
    1.22            else
    1.23              limit = remaining
    1.24            end
    1.25 -          local chunk = socket:read_yield(limit)
    1.26 -          if not chunk then
    1.27 -            request_error(true, "400 Bad Request", "Read error while reading chunk of request body")
    1.28 -          end
    1.29 -          if #chunk ~= limit then
    1.30 -            request_error(true, "400 Bad Request", "Unexpected EOF while reading chunk of request body")
    1.31 -          end
    1.32 +          local chunk = read(limit)
    1.33            remaining = remaining - limit
    1.34            if process_body_chunk then
    1.35              process_body_chunk(chunk)
    1.36 @@ -470,13 +475,7 @@
    1.37        local function read_body()
    1.38          if request.headers_flags["Transfer-Encoding"]["chunked"] then
    1.39            while true do
    1.40 -            local line, status = socket:read_yield(32 + remaining_body_size_limit, "\n")
    1.41 -            if not line then
    1.42 -              request_error(true, "400 Bad Request", "Read error while reading next chunk of request body")
    1.43 -            end
    1.44 -            if status == "eof" then
    1.45 -              request_error(true, "400 Bad Request", "Unexpected EOF while reading next chunk of request body")
    1.46 -            end
    1.47 +            local line = read(32 + remaining_body_size_limit, "\n")
    1.48              local zeros, lenstr = string.match(line, "^(0*)([1-9A-Fa-f]+[0-9A-Fa-f]*)\r?\n$")
    1.49              local chunkext
    1.50              if lenstr then
    1.51 @@ -494,13 +493,13 @@
    1.52              end
    1.53              if len == 0 then break end
    1.54              read_body_bytes(len)
    1.55 -            local term = socket:read_yield(2, "\n")
    1.56 +            local term = read(2, "\n")
    1.57              if term ~= "\r\n" and term ~= "\n" then
    1.58                request_error(true, "400 Bad Request", "Encoding error while reading chunk of request body")
    1.59              end
    1.60            end
    1.61            while true do
    1.62 -            local line = socket:read_yield(2 + remaining_body_size_limit, "\n")
    1.63 +            local line = read(2 + remaining_body_size_limit, "\n")
    1.64              if line == "\r\n" or line == "\n" then break end
    1.65              remaining_body_size_limit = remaining_body_size_limit - #line
    1.66              if remaining_body_size_limit < 0 then

Impressum / About Us