moonbridge

diff moonbridge_http.lua @ 184:97d3ca77c86a

Code cleanup, renamed functions, added documentation for HTTP module
author jbe
date Fri Jun 19 18:24:58 2015 +0200 (2015-06-19)
parents 478d6237e17a
children f7ed5b923331
line diff
     1.1 --- a/moonbridge_http.lua	Fri Jun 19 16:54:33 2015 +0200
     1.2 +++ b/moonbridge_http.lua	Fri Jun 19 18:24:58 2015 +0200
     1.3 @@ -647,7 +647,7 @@
     1.4                local afterbound = false  -- interpret 2 bytes after boundary ("\r\n" or "--")
     1.5                local terminated = false  -- final boundary read
     1.6                local bigchunk = ""
     1.7 -              request:set_request_body_streamer(function(chunk)
     1.8 +              request:stream_request_body(function(chunk)
     1.9                  if chunk == nil then
    1.10                    if not terminated then
    1.11                      request_error(true, "400 Bad Request", "Premature end of multipart/form-data request body")
    1.12 @@ -808,6 +808,15 @@
    1.13          send(socket:write(key, ": ", value, "\r\n"))
    1.14          state = old_state
    1.15        end
    1.16 +      -- method to announce (and enforce) connection close after sending the
    1.17 +      -- response:
    1.18 +      function request:close_after_finish()
    1.19 +        assert_not_faulty()
    1.20 +        if state == "headers_sent" or state == "finished" then
    1.21 +          error("All HTTP headers have already been sent")
    1.22 +        end
    1.23 +        close_requested = true
    1.24 +      end
    1.25        -- function to terminate header section in response, optionally flushing:
    1.26        -- (may be called multiple times unless response is finished)
    1.27        local function finish_headers(with_flush)
    1.28 @@ -935,7 +944,7 @@
    1.29          streamed_post_param_patterns[#streamed_post_param_patterns+1] = {pattern, callback}
    1.30        end
    1.31        -- method to register request body stream handler
    1.32 -      function request:set_request_body_streamer(callback)
    1.33 +      function request:stream_request_body(callback)
    1.34          if state ~= "init" then
    1.35            error("Cannot setup request body streamer at this stage anymore")
    1.36          end
    1.37 @@ -962,8 +971,8 @@
    1.38          consume_all()
    1.39        end
    1.40        -- method to stream request body
    1.41 -      function request:stream_request_body(callback)
    1.42 -        request:set_request_body_streamer(function(chunk)
    1.43 +      function request:stream_request_body_now(callback)
    1.44 +        request:stream_request_body(function(chunk)
    1.45            if chunk ~= nil then
    1.46              callback(chunk)
    1.47            end
    1.48 @@ -972,9 +981,13 @@
    1.49        end
    1.50        -- metamethod to read special attibutes of request object:
    1.51        function request_mt:__index(key, value)
    1.52 -        if key == "body" then
    1.53 +        if key == "faulty" then
    1.54 +          return state == "faulty"
    1.55 +        elseif key == "fresh" then
    1.56 +          return state == "init" and process_body_chunk == nil
    1.57 +        elseif key == "body" then
    1.58            local chunks = {}
    1.59 -          request:stream_request_body(function(chunk)
    1.60 +          request:stream_request_body_now(function(chunk)
    1.61              chunks[#chunks+1] = chunk
    1.62            end)
    1.63            self.body = table.concat(chunks)

Impressum / About Us