@@ -5,17 +5,17 @@ current implementation supports [Lua 5.1](https://www.lua.org/manual/5.1/).
5
5
6
6
## Route filters
7
7
8
- The lua scripts can be added to a route description with the lua() filter,
8
+ The lua scripts can be added to a route description with the ` lua() ` filter,
9
9
the first parameter for the filter is the script. This can be either a file
10
10
name (ending with ` .lua ` ) or inline code, e.g. as
11
11
12
12
* file ` lua("/path/to/file.lua") ` - if a file path is not absolute, the path
13
13
is relative to skipper's working directory.
14
14
* inline ` lua("function request(c, p); print(c.request.url); end") `
15
15
16
- Any other additional parameters for the filter must be ` key=value ` strings.
17
- These will be passed as table to the called functions as second parameter .
18
- ** NOTE ** : Any parameter starting with "lua-" should not be used to pass
16
+ Any other additional parameters for the filter will be passed as
17
+ a second table parameter to the called functions.
18
+ > Any parameter starting with "lua-" should not be used to pass
19
19
values for the script - those will be used for configuring the filter.
20
20
21
21
## Script requirements
@@ -26,12 +26,24 @@ in the route as table like
26
26
``` lua
27
27
-- route looks like
28
28
--
29
- -- any: * -> lua("./test.lua", "myparam=foo", "other=bar") -> <shunt>
29
+ -- any: * -> lua("./test.lua", "myparam=foo", "other=bar", "justkey" ) -> <shunt>
30
30
--
31
31
function request (ctx , params )
32
- print (ctx .request .method .. " " .. ctx .request .url .. " -> " .. params .myparam )
32
+ print (params [1 ]) -- myparam=foo
33
+ print (params [2 ]) -- other=bar
34
+ print (params [3 ]) -- justkey
35
+ print (params [4 ]) -- nil
36
+ print (params .myparam ) -- foo
37
+ print (params .other ) -- bar
38
+ print (params .justkey ) -- (empty string)
39
+ print (params .x ) -- nil
33
40
end
34
41
```
42
+ > Parameter table allows index access as well as key-value access
43
+
44
+ ## print builtin
45
+
46
+ Lua ` print ` builtin function writes skipper info log messages.
35
47
36
48
## Available lua modules
37
49
@@ -60,29 +72,35 @@ in the request and accessing it in the response will most likely fail and lead
60
72
to hard debuggable errors. Use the ` ctx.state_bag ` to propagate values from
61
73
` request ` to ` response ` - and any other filter in the chain.
62
74
63
- # Request
75
+ # Request and response
64
76
65
- The ` request() ` function is run for an incoming request.
77
+ The ` request() ` function is run for an incoming request and ` response() ` for backend response .
66
78
67
79
## Headers
68
80
69
- Request headers can be accessed by accessing the ` ctx.request.header ` map like
81
+ Request headers can be accessed via ` ctx.request.header ` table like
70
82
``` lua
71
83
ua = ctx .request .header [" user-agent" ]
72
84
```
85
+ and iterated like
86
+ ``` lua
87
+ for k , v in ctx .request .header () do
88
+ print (k , " =" , v );
89
+ end
90
+ ```
91
+ > Headers table is a [ functable] ( http://lua-users.org/wiki/FuncTables ) that returns [ iterator] ( https://www.lua.org/pil/7.1.html )
73
92
74
93
Header names are normalized by the ` net/http ` go module
75
94
[ like usual] ( https://godoc.org/net/http#CanonicalHeaderKey ) . Setting a
76
- header is done by assigning to the headers map . Setting a header to ` nil ` or
95
+ header is done by assigning to the headers table . Setting a header to ` nil ` or
77
96
an empty string deletes the header - setting to ` nil ` is preferred.
78
97
79
98
``` lua
80
99
ctx .request .header [" user-agent" ] = " skipper.lua/0.0.1"
81
100
ctx .request .header [" Authorization" ] = nil -- delete authorization header
82
101
```
83
102
84
- Response headers work the same way by accessing / assigning to
85
- ` ctx.response.header ` - this is of course only valid in the ` response() ` phase.
103
+ Response headers ` ctx.response.header ` work the same way - this is of course only valid in the ` response() ` phase.
86
104
87
105
## Other request fields
88
106
@@ -97,6 +115,13 @@ Response headers work the same way by accessing / assigning to
97
115
* ` proto ` - (read only) something like "HTTP/1.1"
98
116
* ` method ` - (read only) request method, e.g. "GET" or "POST"
99
117
* ` url ` - (read/write) request URL as string
118
+ * ` url_path ` - (read/write) request URL path as string
119
+ * ` url_query ` - (read/write) request URL query parameter table, similar to headers
120
+ * ` url_raw_query ` - (read/write) encoded request URL query values, without '?' as string
121
+
122
+ ## Other response fields
123
+
124
+ * ` status_code ` - (read/write) response status code as number, e.g. 200
100
125
101
126
## Serving requests from lua
102
127
Requests can be served with ` ctx.serve(table) ` , you must return after this
@@ -109,6 +134,13 @@ call. Possible keys for the table:
109
134
See also [ redirect] ( #redirect ) and [ internal server error] ( #internal-server-error )
110
135
examples below
111
136
137
+ ## Path parameters
138
+
139
+ Path parameters (if any) can be read via ` ctx.path_param ` table
140
+ ```
141
+ Path("/api/:id") -> lua("function request(ctx, params); print(ctx.path_param.id); end") -> <shunt>
142
+ ```
143
+
112
144
## StateBag
113
145
114
146
The state bag can be used to pass values from one filter to another in the same
126
158
127
159
# Examples
128
160
129
- Note: the examples serve as examples. If there is a go based plugin available,
130
- use that instead. The overhead of calling lua is 4-5 times slower than pure go .
161
+ > The examples serve as examples. If there is a go based plugin available,
162
+ use that instead. For overhead estimate see [ benchmark ] ( #Benchmark ) .
131
163
132
164
## OAuth2 token as basic auth password
133
165
``` lua
@@ -200,6 +232,16 @@ function request(ctx, params)
200
232
end
201
233
```
202
234
235
+ ## set request header from params
236
+ ``` lua
237
+ function request (ctx , params )
238
+ ctx .request .header [params [1 ]] = params [2 ]
239
+ if params [1 ]:lower () == " host" then
240
+ ctx .request .outgoing_host = params [2 ]
241
+ end
242
+ end
243
+ ```
244
+
203
245
# Benchmark
204
246
205
247
## redirectTo vs lua redirect
0 commit comments