Merge pull request #2408 from Shopify/updated-buffered-backends

Read backends data even if buffered to temp file
This commit is contained in:
k8s-ci-robot 2018-04-24 14:09:02 -07:00 committed by GitHub
commit 9533aa45cc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 64 additions and 4 deletions

View file

@ -7,6 +7,26 @@ function _M.get_backends_data()
return configuration_data:get("backends")
end
local function fetch_request_body()
ngx.req.read_body()
local body = ngx.req.get_body_data()
if not body then
-- request body might've been written to tmp file if body > client_body_buffer_size
local file_name = ngx.req.get_body_file()
local file = io.open(file_name, "rb")
if not file then
return nil
end
body = file:read("*all")
file:close()
end
return body
end
function _M.call()
if ngx.var.request_method ~= "POST" and ngx.var.request_method ~= "GET" then
ngx.status = ngx.HTTP_BAD_REQUEST
@ -26,11 +46,16 @@ function _M.call()
return
end
ngx.req.read_body()
local backends = fetch_request_body()
if not backends then
ngx.log(ngx.ERR, "dynamic-configuration: unable to read valid request body")
ngx.status = ngx.HTTP_BAD_REQUEST
return
end
local success, err = configuration_data:set("backends", ngx.req.get_body_data())
local success, err = configuration_data:set("backends", backends)
if not success then
ngx.log(ngx.ERR, "error while saving configuration: " .. tostring(err))
ngx.log(ngx.ERR, "dynamic-configuration: error updating configuration: " .. tostring(err))
ngx.status = ngx.HTTP_BAD_REQUEST
return
end