|
| 1 | +-- Copyright (C) Yichun Zhang (agentzh) |
| 2 | + |
| 3 | + |
| 4 | +local ffi = require 'ffi' |
| 5 | +local base = require "resty.core.base" |
| 6 | +require "resty.core.phase" -- for ngx.get_phase |
| 7 | + |
| 8 | + |
| 9 | +local C = ffi.C |
| 10 | +local ffi_str = ffi.string |
| 11 | +local FFI_AGAIN = base.FFI_AGAIN |
| 12 | +local FFI_OK = base.FFI_OK |
| 13 | +local get_request = base.get_request |
| 14 | +local get_string_buf = base.get_string_buf |
| 15 | +local getmetatable = getmetatable |
| 16 | +local ngx = ngx |
| 17 | +local ngx_phase = ngx.get_phase |
| 18 | + |
| 19 | + |
| 20 | +local _M = { |
| 21 | + version = base.version |
| 22 | +} |
| 23 | + |
| 24 | + |
| 25 | +ffi.cdef[[ |
| 26 | + typedef unsigned char u_char; |
| 27 | + |
| 28 | + void ngx_http_lua_ffi_get_setby_param(ngx_http_request_t *r, int idx, |
| 29 | + u_char **data, size_t *len); |
| 30 | + int ngx_http_lua_ffi_get_body_filter_param_eof(ngx_http_request_t *r); |
| 31 | + int ngx_http_lua_ffi_get_body_filter_param_body(ngx_http_request_t *r, |
| 32 | + u_char **data_p, size_t *len_p); |
| 33 | + int ngx_http_lua_ffi_copy_body_filter_param_body(ngx_http_request_t *r, |
| 34 | + u_char *data); |
| 35 | +]] |
| 36 | +local data_p = ffi.new("unsigned char*[1]") |
| 37 | +local len_p = ffi.new("size_t[1]") |
| 38 | + |
| 39 | + |
| 40 | +local function get_setby_param(r, idx) |
| 41 | + C.ngx_http_lua_ffi_get_setby_param(r, idx, data_p, len_p) |
| 42 | + if len_p[0] == 0 then |
| 43 | + return nil |
| 44 | + end |
| 45 | + |
| 46 | + return ffi_str(data_p[0], len_p[0]) |
| 47 | +end |
| 48 | + |
| 49 | + |
| 50 | +local function get_body_filter_param(r, idx) |
| 51 | + if idx == 1 then |
| 52 | + data_p[0] = nil |
| 53 | + local rc = C.ngx_http_lua_ffi_get_body_filter_param_body(r, data_p, |
| 54 | + len_p) |
| 55 | + if rc == FFI_AGAIN then |
| 56 | + local buf = get_string_buf(len_p[0]) |
| 57 | + assert(C.ngx_http_lua_ffi_copy_body_filter_param_body(r, buf) |
| 58 | + == FFI_OK) |
| 59 | + return ffi_str(buf, len_p[0]) |
| 60 | + end |
| 61 | + |
| 62 | + if len_p[0] == 0 then |
| 63 | + return "" |
| 64 | + end |
| 65 | + |
| 66 | + return ffi_str(data_p[0], len_p[0]) |
| 67 | + |
| 68 | + elseif idx == 2 then |
| 69 | + local rc = C.ngx_http_lua_ffi_get_body_filter_param_eof(r) |
| 70 | + return rc == 1 |
| 71 | + |
| 72 | + else |
| 73 | + return nil |
| 74 | + end |
| 75 | +end |
| 76 | + |
| 77 | + |
| 78 | +local function get_param(tb, idx) |
| 79 | + local r = get_request() |
| 80 | + if not r then |
| 81 | + error("no request found") |
| 82 | + end |
| 83 | + |
| 84 | + local phase = ngx_phase() |
| 85 | + if phase == "set" then |
| 86 | + return get_setby_param(r, idx) |
| 87 | + end |
| 88 | + |
| 89 | + if phase == "body_filter" then |
| 90 | + return get_body_filter_param(r, idx) |
| 91 | + end |
| 92 | + |
| 93 | + error("API disabled in the current context") |
| 94 | +end |
| 95 | + |
| 96 | + |
| 97 | +do |
| 98 | + local mt = getmetatable(ngx.arg) |
| 99 | + mt.__index = get_param |
| 100 | +end |
| 101 | + |
| 102 | + |
| 103 | +return _M |
0 commit comments