-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Pdk refine with optional cache to improvement performance #13981
base: master
Are you sure you want to change the base?
Changes from all commits
c4aebf1
e5626cd
7c9ea30
ca17150
e0dc602
78cb953
e46dffb
ec8abf2
9c7520a
f4ce86a
bcdc538
40f7218
71e2a62
d5c781b
a64667b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -61,8 +61,6 @@ local function new(self) | |
local MIN_PORT = 1 | ||
local MAX_PORT = 65535 | ||
|
||
local CONTENT_TYPE = "Content-Type" | ||
|
||
local CONTENT_TYPE_POST = "application/x-www-form-urlencoded" | ||
local CONTENT_TYPE_JSON = "application/json" | ||
local CONTENT_TYPE_FORM_DATA = "multipart/form-data" | ||
|
@@ -98,7 +96,7 @@ local function new(self) | |
function _REQUEST.get_scheme() | ||
check_phase(PHASES.request) | ||
|
||
return var.scheme | ||
return ngx.ctx.scheme or var.scheme | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
end | ||
|
||
|
||
|
@@ -116,7 +114,7 @@ local function new(self) | |
function _REQUEST.get_host() | ||
check_phase(PHASES.request) | ||
|
||
return var.host | ||
return ngx.ctx.host or var.host | ||
end | ||
|
||
|
||
|
@@ -437,7 +435,7 @@ local function new(self) | |
function _REQUEST.get_raw_path() | ||
check_phase(PHASES.request) | ||
|
||
local uri = var.request_uri or "" | ||
local uri = ngx.ctx.request_uri or var.request_uri or "" | ||
local s = find(uri, "?", 2, true) | ||
return s and sub(uri, 1, s - 1) or uri | ||
end | ||
|
@@ -456,7 +454,7 @@ local function new(self) | |
-- kong.request.get_path_with_query() -- "/v1/movies?movie=foo" | ||
function _REQUEST.get_path_with_query() | ||
check_phase(PHASES.request) | ||
return var.request_uri or "" | ||
return ngx.ctx.request_uri or var.request_uri or "" | ||
end | ||
|
||
|
||
|
@@ -619,14 +617,14 @@ local function new(self) | |
-- kong.request.get_header("Host") -- "foo.com" | ||
-- kong.request.get_header("x-custom-header") -- "bla" | ||
-- kong.request.get_header("X-Another") -- "foo bar" | ||
function _REQUEST.get_header(name) | ||
function _REQUEST.get_header(name, ctx) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
check_phase(PHASES.request) | ||
|
||
if type(name) ~= "string" then | ||
error("header name must be a string", 2) | ||
end | ||
|
||
return http_get_header(name) | ||
return http_get_header(name, ctx) | ||
end | ||
|
||
|
||
|
@@ -820,8 +818,7 @@ local function new(self) | |
-- body.age -- "42" | ||
function _REQUEST.get_body(mimetype, max_args, max_allowed_file_size) | ||
check_phase(before_content) | ||
|
||
local content_type = mimetype or _REQUEST.get_header(CONTENT_TYPE) | ||
local content_type = mimetype or ngx.var.http_content_type | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "Content-Type" is a built-in single value header, which means |
||
if not content_type then | ||
return nil, "missing content type" | ||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -285,10 +285,11 @@ local function new(pdk, major_version) | |
-- -- or `access` phase prior calling this function. | ||
-- | ||
-- local body = kong.service.response.get_raw_body() | ||
function response.get_raw_body() | ||
function response.get_raw_body(buffered_proxying) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just a minor refinement so that if we could reduce the reads of |
||
check_phase(header_body_log) | ||
local ctx = ngx.ctx | ||
if not ctx.buffered_proxying then | ||
local buffered = buffered_proxying or ctx.buffered_proxying | ||
if not buffered then | ||
error("service body is only available with buffered proxying " .. | ||
"(see: kong.service.request.enable_buffering function)", 2) | ||
end | ||
|
@@ -313,7 +314,8 @@ local function new(pdk, major_version) | |
-- local body = kong.service.response.get_body() | ||
function response.get_body(mimetype, max_args) | ||
check_phase(header_body_log) | ||
if not ngx.ctx.buffered_proxying then | ||
local bufferred_proxying = ngx.ctx.buffered_proxying | ||
if not bufferred_proxying then | ||
error("service body is only available with buffered proxying " .. | ||
"(see: kong.service.request.enable_buffering function)", 2) | ||
end | ||
|
@@ -344,7 +346,7 @@ local function new(pdk, major_version) | |
end | ||
end | ||
|
||
local body = response.get_raw_body() | ||
local body = response.get_raw_body(bufferred_proxying) | ||
local pargs, err = ngx.decode_args(body, max_args or MAX_POST_ARGS_DEFAULT) | ||
if not pargs then | ||
return nil, err, CONTENT_TYPE_POST | ||
|
@@ -353,7 +355,7 @@ local function new(pdk, major_version) | |
return pargs, nil, CONTENT_TYPE_POST | ||
|
||
elseif find(content_type_lower, CONTENT_TYPE_JSON, 1, true) == 1 then | ||
local body = response.get_raw_body() | ||
local body = response.get_raw_body(bufferred_proxying) | ||
local json = cjson.decode_with_array_mt(body) | ||
if type(json) ~= "table" then | ||
return nil, "invalid json body", CONTENT_TYPE_JSON | ||
|
@@ -362,7 +364,7 @@ local function new(pdk, major_version) | |
return json, nil, CONTENT_TYPE_JSON | ||
|
||
elseif find(content_type_lower, CONTENT_TYPE_FORM_DATA, 1, true) == 1 then | ||
local body = response.get_raw_body() | ||
local body = response.get_raw_body(bufferred_proxying) | ||
|
||
local parts = multipart(body, content_type) | ||
if not parts then | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,9 +28,9 @@ local _M = {} | |
-- @param {table} conf Plugin config | ||
-- @return {string} public_key | ||
-- @return {string} private_key | ||
local function retrieve_credentials(header_name, conf) | ||
local function retrieve_credentials(header_name, conf, ctx) | ||
local username, password | ||
local authorization_header = kong.request.get_header(header_name) | ||
local authorization_header = kong.request.get_header(header_name, ctx) | ||
|
||
if authorization_header then | ||
local iterator, iter_err = re_gmatch(authorization_header, "\\s*[Bb]asic\\s*(.+)", "oj") | ||
|
@@ -158,21 +158,22 @@ end | |
|
||
local function do_authentication(conf) | ||
local www_authenticate = "Basic realm=\"" .. conf.realm .. "\"" | ||
local ctx = {} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use a local |
||
|
||
-- If both headers are missing, return 401 | ||
if not (kong.request.get_header("authorization") or kong.request.get_header("proxy-authorization")) then | ||
if not (kong.request.get_header("authorization", ctx) or kong.request.get_header("proxy-authorization", ctx)) then | ||
return false, unauthorized("Unauthorized", www_authenticate) | ||
end | ||
|
||
local credential | ||
local given_username, given_password = retrieve_credentials("proxy-authorization", conf) | ||
local given_username, given_password = retrieve_credentials("proxy-authorization", conf, ctx) | ||
if given_username and given_password then | ||
credential = load_credential_from_db(given_username) | ||
end | ||
|
||
-- Try with the authorization header | ||
if not credential then | ||
given_username, given_password = retrieve_credentials("authorization", conf) | ||
given_username, given_password = retrieve_credentials("authorization", conf, ctx) | ||
if given_username and given_password then | ||
credential = load_credential_from_db(given_username) | ||
else | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ngx.req.read_body
only need to be called once in a request lifetime. Although multiple calls in the same requestdon't re-read data to buffer multiple times, there are still some extra cost of check logic. So we could just avoid it to gain better performance.