Skip to content

Commit

Permalink
refactor(azure-functions) lua-resty-http 0.16.1 (#17)
Browse files Browse the repository at this point in the history
### Summary

`lua-resty-http` deprecated some of its functions, this PR updates
`azure-functions` plugin to use the non-deprecated versions.
  • Loading branch information
bungle authored Sep 7, 2021
1 parent 10e8f30 commit 00432ae
Showing 1 changed file with 18 additions and 31 deletions.
49 changes: 18 additions & 31 deletions kong/plugins/azure-functions/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ local http = require "resty.http"


local kong = kong
local fmt = string.format
local var = ngx.var
local pairs = pairs
local server_header = meta._SERVER_TOKENS
Expand All @@ -22,7 +23,7 @@ end

local azure = {
PRIORITY = 749,
VERSION = "1.0.0",
VERSION = "1.0.1",
}


Expand All @@ -47,28 +48,11 @@ function azure:access(config)
end
config = conf

local client = http.new()
local request_method = kong.request.get_method()
local request_body = kong.request.get_raw_body()
local request_headers = kong.request.get_headers()
local request_args = kong.request.get_query()

client:set_timeout(config.timeout)

local ok, err = client:connect(config.host, config.port)
if not ok then
kong.log.err("could not connect to Azure service: ", err)
return kong.response.exit(500, { message = "An unexpected error ocurred" })
end

if config.https then
local ok2, err2 = client:ssl_handshake(false, config.host, config.https_verify)
if not ok2 then
kong.log.err("could not perform SSL handshake : ", err2)
return kong.response.exit(500, { message = "An unexpected error ocurred" })
end
end

local upstream_uri = var.upstream_uri
local path = conf.path
local end1 = path:sub(-1, -1)
Expand All @@ -93,14 +77,22 @@ function azure:access(config)
request_headers["x-functions-key"] = config.apikey
request_headers["x-functions-clientid"] = config.clientid

local res
res, err = client:request {
method = request_method,
path = path,
body = request_body,
query = request_args,

local scheme = config.https and "https" or "http"
local uri = conf.port and fmt("%s://%s:%d", scheme, conf.host, conf.port)
or fmt("%s://%s", scheme, conf.host)

local client = http.new()
client:set_timeout(config.timeout)
local res, err = client:request_uri(uri, {
method = request_method,
path = path,
body = request_body,
query = request_args,
headers = request_headers,
}
ssl_verify = config.https_verify,
keepalive_timeout = conf.keepalive,
})

if not res then
kong.log.err(err)
Expand All @@ -109,7 +101,7 @@ function azure:access(config)

local response_headers = res.headers
local response_status = res.status
local response_content = res:read_body()
local response_content = res.body

if var.http2 then
response_headers["Connection"] = nil
Expand All @@ -119,11 +111,6 @@ function azure:access(config)
response_headers["Transfer-Encoding"] = nil
end

ok, err = client:set_keepalive(config.keepalive)
if not ok then
kong.log.err("could not keepalive connection: ", err)
end

return send(response_status, response_content, response_headers)
end

Expand Down

0 comments on commit 00432ae

Please sign in to comment.