|
| 1 | +-- Copyright 2019-2022 Kong Inc. |
| 2 | + |
| 3 | +-- Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +-- you may not use this file except in compliance with the License. |
| 5 | +-- You may obtain a copy of the License at |
| 6 | + |
| 7 | +-- http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + |
| 9 | +-- Unless required by applicable law or agreed to in writing, software |
| 10 | +-- distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +-- See the License for the specific language governing permissions and |
| 13 | +-- limitations under the License. |
| 14 | + |
| 15 | +local _M = {} |
| 16 | + |
| 17 | +local ffi = require("ffi") |
| 18 | +local base = require("resty.core.base") |
| 19 | +local select = select |
| 20 | +base.allows_subsystem("http") |
| 21 | + |
| 22 | +ffi.cdef([[ |
| 23 | +int |
| 24 | +ngx_http_lua_ffi_set_next_upstream(ngx_http_request_t *r, uint32_t next_upstream, char **err); |
| 25 | +const uint32_t ngx_http_lua_kong_next_upstream_mask_error; |
| 26 | +const uint32_t ngx_http_lua_kong_next_upstream_mask_timeout; |
| 27 | +const uint32_t ngx_http_lua_kong_next_upstream_mask_invalid_header; |
| 28 | +const uint32_t ngx_http_lua_kong_next_upstream_mask_http_500; |
| 29 | +const uint32_t ngx_http_lua_kong_next_upstream_mask_http_502; |
| 30 | +const uint32_t ngx_http_lua_kong_next_upstream_mask_http_503; |
| 31 | +const uint32_t ngx_http_lua_kong_next_upstream_mask_http_504; |
| 32 | +const uint32_t ngx_http_lua_kong_next_upstream_mask_http_403; |
| 33 | +const uint32_t ngx_http_lua_kong_next_upstream_mask_http_404; |
| 34 | +const uint32_t ngx_http_lua_kong_next_upstream_mask_http_429; |
| 35 | +const uint32_t ngx_http_lua_kong_next_upstream_mask_off; |
| 36 | +const uint32_t ngx_http_lua_kong_next_upstream_mask_non_idempotent; |
| 37 | +]]) |
| 38 | + |
| 39 | +local type = type |
| 40 | +local C = ffi.C |
| 41 | +local get_request = base.get_request |
| 42 | +local ffi_str = ffi.string |
| 43 | + |
| 44 | +local NGX_OK = ngx.OK |
| 45 | + |
| 46 | +local next_upstream_table = { |
| 47 | + error = C.ngx_http_lua_kong_next_upstream_mask_error, |
| 48 | + timeout = C.ngx_http_lua_kong_next_upstream_mask_timeout, |
| 49 | + invalid_header = C.ngx_http_lua_kong_next_upstream_mask_invalid_header, |
| 50 | + http_500 = C.ngx_http_lua_kong_next_upstream_mask_http_500, |
| 51 | + http_502 = C.ngx_http_lua_kong_next_upstream_mask_http_502, |
| 52 | + http_503 = C.ngx_http_lua_kong_next_upstream_mask_http_503, |
| 53 | + http_504 = C.ngx_http_lua_kong_next_upstream_mask_http_504, |
| 54 | + http_403 = C.ngx_http_lua_kong_next_upstream_mask_http_403, |
| 55 | + http_404 = C.ngx_http_lua_kong_next_upstream_mask_http_404, |
| 56 | + http_429 = C.ngx_http_lua_kong_next_upstream_mask_http_429, |
| 57 | + off = C.ngx_http_lua_kong_next_upstream_mask_off, |
| 58 | + non_idempotent = C.ngx_http_lua_kong_next_upstream_mask_non_idempotent, |
| 59 | +} |
| 60 | + |
| 61 | +function _M.set_next_upstream(...) |
| 62 | + local nargs = select("#", ...) |
| 63 | + if nargs == 0 then |
| 64 | + return "no argument" |
| 65 | + end |
| 66 | + |
| 67 | + local r = get_request() |
| 68 | + if not r then |
| 69 | + return "no request found" |
| 70 | + end |
| 71 | + |
| 72 | + local arg_table = { ... } |
| 73 | + local next_upstream = 0 |
| 74 | + for i = 1, nargs do |
| 75 | + local v = arg_table[i] |
| 76 | + if type(v) ~= "string" then |
| 77 | + return "argument #" .. i .. " is not a string" |
| 78 | + end |
| 79 | + |
| 80 | + local next_upstream_value = next_upstream_table[v] |
| 81 | + if not next_upstream_value then |
| 82 | + return "argument #" .. i .. " is not a valid argument" |
| 83 | + end |
| 84 | + |
| 85 | + next_upstream = bit.bor(next_upstream, next_upstream_value) |
| 86 | + end |
| 87 | + |
| 88 | + local err = ffi.new("char *[1]") |
| 89 | + local rc = C.ngx_http_lua_ffi_set_next_upstream(r, next_upstream, err) |
| 90 | + |
| 91 | + if rc ~= NGX_OK then |
| 92 | + return "failed to set upstream next: " .. ffi_str(err[0]) |
| 93 | + end |
| 94 | + |
| 95 | + return nil |
| 96 | +end |
| 97 | + |
| 98 | +return _M |
0 commit comments