Skip to content

Commit

Permalink
feat(http-log): support charset in content-type header (#10533)
Browse files Browse the repository at this point in the history
  • Loading branch information
tysoekong authored Mar 22, 2023
1 parent a2a54a3 commit 99eff6d
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 7 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@
[#9746](https://github.com/Kong/kong/pull/9746)
- **Proxy-Cache**: add `ignore_uri_case` to configuring cache-key uri to be handled as lowercase
[#10453](https://github.com/Kong/kong/pull/10453)
- **HTTP-Log**: add `application/json; charset=utf-8` option for the `Content-Type` header
in the http-log plugin, for log collectors that require that character set declaration.
[#10533](https://github.com/Kong/kong/pull/10533)

#### PDK

Expand Down
2 changes: 1 addition & 1 deletion kong/plugins/http-log/schema.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ return {
-- NOTE: any field added here must be also included in the handler's get_queue_id method
{ http_endpoint = typedefs.url({ required = true, encrypted = true, referenceable = true }) }, -- encrypted = true is a Kong-Enterprise exclusive feature, does nothing in Kong CE
{ method = { type = "string", default = "POST", one_of = { "POST", "PUT", "PATCH" }, }, },
{ content_type = { type = "string", default = "application/json", one_of = { "application/json" }, }, },
{ content_type = { type = "string", default = "application/json", one_of = { "application/json", "application/json; charset=utf-8" }, }, },
{ timeout = { type = "number", default = 10000 }, },
{ keepalive = { type = "number", default = 60000 }, },
{ retry_count = { type = "integer", default = 10 }, },
Expand Down
98 changes: 92 additions & 6 deletions spec/03-plugins/03-http-log/01-log_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,49 @@ for _, strategy in helpers.each_strategy() do
}
}

local service1 = bp.services:insert{
local service1_2 = bp.services:insert{
protocol = "http",
host = helpers.mock_upstream_host,
port = helpers.mock_upstream_port,
}

local route1 = bp.routes:insert {
hosts = { "http_logging.test" },
service = service1
local route1_2 = bp.routes:insert {
hosts = { "content_type_application_json_http_logging.test" },
service = service1_2
}

bp.plugins:insert {
route = { id = route1.id },
route = { id = route1_2.id },
name = "http-log",
config = {
http_endpoint = "http://" .. helpers.mock_upstream_host
.. ":"
.. helpers.mock_upstream_port
.. "/post_log/http"
.. "/post_log/http2",
content_type = "application/json"
}
}

local service1_3 = bp.services:insert{
protocol = "http",
host = helpers.mock_upstream_host,
port = helpers.mock_upstream_port,
}

local route1_3 = bp.routes:insert {
hosts = { "content_type_application_json_charset_utf_8_http_logging.test" },
service = service1_3
}

bp.plugins:insert {
route = { id = route1_3.id },
name = "http-log",
config = {
http_endpoint = "http://" .. helpers.mock_upstream_host
.. ":"
.. helpers.mock_upstream_port
.. "/post_log/http3",
content_type = "application/json; charset=utf-8"
}
}

Expand Down Expand Up @@ -298,6 +322,68 @@ for _, strategy in helpers.each_strategy() do
end, 10)
end)

it("logs to HTTP with content-type 'application/json'", function()
local res = assert(proxy_client:send({
method = "GET",
path = "/status/200",
headers = {
["Host"] = "content_type_application_json_http_logging.test"
}
}))
assert.res_status(200, res)

helpers.wait_until(function()
local client = assert(helpers.http_client(helpers.mock_upstream_host,
helpers.mock_upstream_port))
local res = assert(client:send {
method = "GET",
path = "/read_log/http2",
headers = {
Accept = "application/json"
}
})
local raw = assert.res_status(200, res)
local body = cjson.decode(raw)

if #body.entries == 1 then
assert.same("127.0.0.1", body.entries[1].client_ip)
assert.same(body.entries[1].log_req_headers['content-type'] or "", "application/json")
return true
end
end, 10)
end)

it("logs to HTTP with content-type 'application/json; charset=utf-8'", function()
local res = assert(proxy_client:send({
method = "GET",
path = "/status/200",
headers = {
["Host"] = "content_type_application_json_charset_utf_8_http_logging.test"
}
}))
assert.res_status(200, res)

helpers.wait_until(function()
local client = assert(helpers.http_client(helpers.mock_upstream_host,
helpers.mock_upstream_port))
local res = assert(client:send {
method = "GET",
path = "/read_log/http3",
headers = {
Accept = "application/json"
}
})
local raw = assert.res_status(200, res)
local body = cjson.decode(raw)

if #body.entries == 1 then
assert.same("127.0.0.1", body.entries[1].client_ip)
assert.same(body.entries[1].log_req_headers['content-type'] or "", "application/json; charset=utf-8")
return true
end
end, 10)
end)

describe("custom log values by lua", function()
it("logs custom values", function()
local res = assert(proxy_client:send({
Expand Down

1 comment on commit 99eff6d

@khcp-gha-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bazel Build

Docker image available kong/kong:99eff6d20fd7ffda816af56b3ce06873b611b6d1
Artifacts available https://github.com/Kong/kong/actions/runs/4490446801

Please sign in to comment.