Skip to content

Commit

Permalink
script: support multiple header values (#3013)
Browse files Browse the repository at this point in the history
Introduce `add` and `values` methods to set and get multiple header values.

Signed-off-by: Alexander Yastrebov <[email protected]>
  • Loading branch information
AlexanderYastrebov authored Apr 9, 2024
1 parent 2982fd2 commit 20d4dd7
Show file tree
Hide file tree
Showing 4 changed files with 214 additions and 57 deletions.
26 changes: 26 additions & 0 deletions docs/reference/scripts.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,32 @@ ctx.request.header["Authorization"] = nil -- delete authorization header
Response headers `ctx.response.header` work the same way - this is of course only valid in the `response()` phase.

### Multiple header values

Request and response header tables provide access to a first value of a header.

To access multiple values use `add` and `values` methods:

```lua
function request(ctx, params)
ctx.request.header.add("X-Foo", "Bar")
ctx.request.header.add("X-Foo", "Baz")

-- all X-Foo values
for _, v in pairs(ctx.request.header.values("X-Foo")) do
print(v)
end

-- all values
for k, _ in ctx.request.header() do
for _, v in pairs(ctx.request.header.values(k)) do
print(k, "=", v)
end
end
end
```


## Other request fields

* `backend_url` - (read only) returns the backend url specified in the route
Expand Down
58 changes: 56 additions & 2 deletions script/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,10 @@ func getRequestValue(f filters.FilterContext) func(*lua.LState) int {
switch key {
case "header":
if header == nil {
header = s.CreateTable(0, 0)
header = s.CreateTable(0, 2)
header.RawSetString("add", s.NewFunction(addRequestHeader(f)))
header.RawSetString("values", s.NewFunction(requestHeaderValues(f)))

mt := s.CreateTable(0, 3)
mt.RawSetString("__index", s.NewFunction(getRequestHeader(f)))
mt.RawSetString("__newindex", s.NewFunction(setRequestHeader(f)))
Expand Down Expand Up @@ -556,7 +559,10 @@ func getResponseValue(f filters.FilterContext) func(*lua.LState) int {
switch key {
case "header":
if header == nil {
header = s.CreateTable(0, 0)
header = s.CreateTable(0, 2)
header.RawSetString("add", s.NewFunction(addResponseHeader(f)))
header.RawSetString("values", s.NewFunction(responseHeaderValues(f)))

mt := s.CreateTable(0, 3)
mt.RawSetString("__index", s.NewFunction(getResponseHeader(f)))
mt.RawSetString("__newindex", s.NewFunction(setResponseHeader(f)))
Expand Down Expand Up @@ -677,6 +683,30 @@ func setRequestHeader(f filters.FilterContext) func(*lua.LState) int {
}
}

func addRequestHeader(f filters.FilterContext) func(*lua.LState) int {
return func(s *lua.LState) int {
value := s.ToString(-1)
name := s.ToString(-2)
if name != "" && value != "" {
f.Request().Header.Add(name, value)
}
return 0
}
}

func requestHeaderValues(f filters.FilterContext) func(*lua.LState) int {
return func(s *lua.LState) int {
name := s.ToString(-1)
values := f.Request().Header.Values(name)
res := s.CreateTable(len(values), 0)
for _, v := range values {
res.Append(lua.LString(v))
}
s.Push(res)
return 1
}
}

func iterateRequestHeader(f filters.FilterContext) func(*lua.LState) int {
// https://www.lua.org/pil/7.2.html
return func(s *lua.LState) int {
Expand Down Expand Up @@ -755,6 +785,30 @@ func setResponseHeader(f filters.FilterContext) func(*lua.LState) int {
}
}

func addResponseHeader(f filters.FilterContext) func(*lua.LState) int {
return func(s *lua.LState) int {
value := s.ToString(-1)
name := s.ToString(-2)
if name != "" && value != "" {
f.Response().Header.Add(name, value)
}
return 0
}
}

func responseHeaderValues(f filters.FilterContext) func(*lua.LState) int {
return func(s *lua.LState) int {
name := s.ToString(-1)
values := f.Response().Header.Values(name)
res := s.CreateTable(len(values), 0)
for _, v := range values {
res.Append(lua.LString(v))
}
s.Push(res)
return 1
}
}

func iterateResponseHeader(f filters.FilterContext) func(*lua.LState) int {
return func(s *lua.LState) int {
s.Push(s.NewFunction(nextHeader(f.Response().Header)))
Expand Down
Loading

0 comments on commit 20d4dd7

Please sign in to comment.