File: resp.lua

package info (click to toggle)
lua-resty-core 0.1.32-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,268 kB
  • sloc: sh: 207; perl: 143; makefile: 26
file content (52 lines) | stat: -rw-r--r-- 1,225 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
-- Copyright (C) Yichun Zhang. All rights reserved.


local ffi = require "ffi"
local C = ffi.C

local base = require "resty.core.base"
base.allows_subsystem('http')
local FFI_BAD_CONTEXT = base.FFI_BAD_CONTEXT
local core_response = require "resty.core.response"
local set_resp_header = core_response.set_resp_header
local get_request = base.get_request
local bypass_if_checks = core_response.bypass_if_checks

ffi.cdef[[
    int ngx_http_lua_ffi_set_resp_status_and_reason(ngx_http_request_t *r,
        int status, const char *reason, size_t reason_len);
]]


local _M = { version = base.version }


function _M.add_header(key, value)
    set_resp_header(nil, key, value, true)
end


function _M.set_status(status, reason)
    local r = get_request()

    if not r then
        error("no request found")
    end

    if type(status) ~= 'number' then
        status = tonumber(status)
    end

    local rc = C.ngx_http_lua_ffi_set_resp_status_and_reason(r, status,
                                                             reason, #reason)
    if rc == FFI_BAD_CONTEXT then
        error("API disabled in the current context", 2)
    end
end


function _M.bypass_if_checks()
    return bypass_if_checks()
end

return _M