Skip to content

Commit

Permalink
refine annotatios
Browse files Browse the repository at this point in the history
  • Loading branch information
ProBrian committed Dec 13, 2024
1 parent 40f7218 commit 71e2a62
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 16 deletions.
1 change: 0 additions & 1 deletion kong/pdk/response.lua
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,6 @@ local function new(self, major_version)
end

local header_value = ngx.header[name]
-- local header_value = _RESPONSE.get_headers()[name]
if type(header_value) == "table" then
return header_value[1]
end
Expand Down
1 change: 0 additions & 1 deletion kong/pdk/service/request.lua
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,6 @@ local function new(self)
error("mime must be a string", 2)
end
if not mime then
-- mime = ngx.req.get_headers()[CONTENT_TYPE]
mime = ngx_var.http_content_type
if not mime then
return nil, "content type was neither explicitly given " ..
Expand Down
1 change: 0 additions & 1 deletion kong/plugins/aws-lambda/request-util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,6 @@ local function build_request_payload(conf)
end

if conf.forward_request_body then
-- local content_type = kong.request.get_header("content-type")
local content_type = ngx.var.http_content_type
local body_raw = read_request_body(conf.skip_large_bodies)
local body_args, err = kong.request.get_body()
Expand Down
54 changes: 54 additions & 0 deletions kong/plugins/bchen-hello/handler.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
-- This software is copyright Kong Inc. and its licensors.
-- Use of the software is subject to the agreement between your organization
-- and Kong Inc. If there is no such agreement, use is governed by and
-- subject to the terms of the Kong Master Software License Agreement found
-- at https://konghq.com/enterprisesoftwarelicense/.
-- [ END OF LICENSE 0867164ffc95e54f04670b5169c09574bdbd9bba ]
local get_header = require("kong.tools.http").get_header

local MyPluginHandler = {
PRIORITY = 1000,
VERSION = "0.0.1",
}

function MyPluginHandler:access()
-- ngx.ctx.get_headers_patch = 0
local start = os.clock()
for i=1,10 do
local headers = ngx.req.get_headers()
-- ngx.req.set_header("foo", tostring(i))
-- ngx.log(ngx.WARN, "bchen access", ngx.req.get_headers()["Content-Type"])
-- local v = headers["Content-Type"]
end
local elapsed = (os.clock() - start)

ngx.log(ngx.WARN, "bchen access", string.format(" elapsed time: %.10f, %d\n", elapsed, 1))

-- ngx.request.set_header("foo", "bar")
end

function MyPluginHandler:response(conf)

-- local content_type = kong.response.get_header("Content-Type")
kong.response.set_header("X-BChen-Plugin", "response")
local headers = kong.response.get_headers()
local headers2 = kong.service.response.get_headers()
local fmt = string.format
local string_tools = require "kong.tools.string"
local replace_dashes_lower = string_tools.replace_dashes_lower
-- ngx.log(ngx.WARN, "single: ", "multi-foo-r".." : "..tostring(kong.response.get_header("multi-foo-r")))

-- for k, v in pairs(headers) do
-- ngx.log(ngx.WARN, "resp header: ", k.." : "..tostring(v))
-- local var = fmt("upstream_http_%s", replace_dashes_lower(k))
-- if ngx.var[var] then
-- ngx.log(ngx.WARN, "upstream header: ", k)
-- end
-- end
-- for k, v in pairs(headers2) do
-- ngx.log(ngx.WARN, "service header: ", k.." : "..tostring(v))
-- end
end


return MyPluginHandler
22 changes: 22 additions & 0 deletions kong/plugins/bchen-hello/schema.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
-- This software is copyright Kong Inc. and its licensors.
-- Use of the software is subject to the agreement between your organization
-- and Kong Inc. If there is no such agreement, use is governed by and
-- subject to the terms of the Kong Master Software License Agreement found
-- at https://konghq.com/enterprisesoftwarelicense/.
-- [ END OF LICENSE 0867164ffc95e54f04670b5169c09574bdbd9bba ]

local PLUGIN_NAME = "bchen-hello"

local schema = {
name = PLUGIN_NAME,
fields = {
{ config = {
type = "record",
fields = {
},
},
},
},
}

return schema
2 changes: 0 additions & 2 deletions kong/plugins/grpc-web/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ local ngx_arg = ngx.arg
local ngx_var = ngx.var

local kong_request_get_path = kong.request.get_path
-- local kong_request_get_header = kong.request.get_header
local kong_request_get_method = kong.request.get_method
local kong_request_get_raw_body = kong.request.get_raw_body
local kong_response_exit = kong.response.exit
Expand Down Expand Up @@ -51,7 +50,6 @@ function grpc_web:access(conf)
end

local dec, err = deco.new(
-- kong_request_get_header("Content-Type"),
ngx.var.http_content_type,
uri, conf.proto)

Expand Down
11 changes: 5 additions & 6 deletions kong/plugins/hmac-auth/access.lua
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,12 @@ end

-- plugin assumes the request parameters being used for creating
-- signature by client are not changed by core or any other plugin
local function create_hash(request_uri, hmac_params, ctx)
local function create_hash(request_uri, hmac_params)
local signing_string = ""
local hmac_headers = hmac_params.hmac_headers

local count = #hmac_headers
local ctx = {}
for i = 1, count do
local header = hmac_headers[i]
local header_value = kong.request.get_header(header, ctx)
Expand Down Expand Up @@ -166,8 +167,8 @@ local function create_hash(request_uri, hmac_params, ctx)
end


local function validate_signature(hmac_params, ctx)
local signature_1 = create_hash(kong_request.get_path_with_query(), hmac_params, ctx)
local function validate_signature(hmac_params)
local signature_1 = create_hash(kong_request.get_path_with_query(), hmac_params)
local signature_2 = decode_base64(hmac_params.signature)
return signature_1 == signature_2
end
Expand Down Expand Up @@ -330,9 +331,7 @@ local function do_authentication(conf)

hmac_params.secret = credential.secret

-- since there are `clear_header` call before, we reset ctx, to get fresh headers
ctx = {}
if not validate_signature(hmac_params, ctx) then
if not validate_signature(hmac_params) then
return false, unauthorized(SIGNATURE_NOT_SAME, www_auth_content)
end

Expand Down
1 change: 0 additions & 1 deletion kong/plugins/oauth2/access.lua
Original file line number Diff line number Diff line change
Expand Up @@ -877,7 +877,6 @@ local function parse_access_token(conf)
parameters[ACCESS_TOKEN] = nil
kong.service.request.set_query(parameters)

-- local content_type = kong.request.get_header("content-type")
local content_type = ngx.var.http_content_type
local is_form_post = content_type and
string_find(content_type, "application/x-www-form-urlencoded", 1, true)
Expand Down
3 changes: 0 additions & 3 deletions kong/plugins/request-transformer/access.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ local table_insert = table.insert
local get_uri_args = kong.request.get_query
local set_uri_args = kong.service.request.set_query
local clear_header = kong.service.request.clear_header
-- local get_header = kong.request.get_header
local var = ngx.var
local set_header = kong.service.request.set_header
local get_headers = kong.request.get_headers
Expand Down Expand Up @@ -423,7 +422,6 @@ local function transform_multipart_body(conf, body, content_length, content_type
end

local function transform_body(conf, template_env)
-- local content_type_value = get_header(CONTENT_TYPE)
-- content-type is a builtin single value header, use var to fetch is faster.
local content_type_value = var.http_content_type
local content_type = get_content_type(content_type_value)
Expand Down Expand Up @@ -459,7 +457,6 @@ local function transform_method(conf)
if conf.http_method then
set_method(conf.http_method:upper())
if conf.http_method == "GET" or conf.http_method == "HEAD" or conf.http_method == "TRACE" then
-- local content_type_value = get_header(CONTENT_TYPE)
-- content-type is a builtin single value header, use var to fetch is faster.
local content_type_value = var.http_content_type
local content_type = get_content_type(content_type_value)
Expand Down
2 changes: 1 addition & 1 deletion t/01-pdk/08-response/02-get_header.t
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ X-Missing: nil
=== TEST 4: response.get_header() returns existing header will not by limited by get_headers() default max_headers configuration
=== TEST 4: response.get_header() returns existing header will not limited by get_headers() default max_headers configuration
--- http_config eval: $t::Util::HttpConfig
--- config
location = /t {
Expand Down

0 comments on commit 71e2a62

Please sign in to comment.