File: session.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 (109 lines) | stat: -rw-r--r-- 2,414 bytes parent folder | download | duplicates (3)
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
-- Copyright (C) Yichun Zhang (agentzh)


local base = require "resty.core.base"
base.allows_subsystem('http')


local ffi = require "ffi"
local C = ffi.C
local ffi_str = ffi.string
local get_request = base.get_request
local error = error
local errmsg = base.get_errmsg_ptr()
local get_string_buf = base.get_string_buf
local FFI_ERROR = base.FFI_ERROR


ffi.cdef[[
int ngx_http_lua_ffi_ssl_set_serialized_session(ngx_http_request_t *r,
    const unsigned char *buf, int len, char **err);

int ngx_http_lua_ffi_ssl_get_serialized_session(ngx_http_request_t *r,
    char *buf, char **err);

int ngx_http_lua_ffi_ssl_get_session_id(ngx_http_request_t *r,
    char *buf, char **err);

int ngx_http_lua_ffi_ssl_get_serialized_session_size(ngx_http_request_t *r,
    char **err);

int ngx_http_lua_ffi_ssl_get_session_id_size(ngx_http_request_t *r,
    char **err);
]]


local _M = { version = base.version }


-- return session, err
function _M.get_serialized_session()
    local r = get_request()
    if not r then
        error("no request found")
    end

    local len = C.ngx_http_lua_ffi_ssl_get_serialized_session_size(r, errmsg)

    if len < 0 then
        return nil, ffi_str(errmsg[0])
    end

    if len > 4096 then
         return nil, "session too big to serialize"
    end
    local buf = get_string_buf(len)

    local rc = C.ngx_http_lua_ffi_ssl_get_serialized_session(r, buf, errmsg)

    if rc == FFI_ERROR then
         return nil, ffi_str(errmsg[0])
    end

    return ffi_str(buf, len)
end


-- return session_id, err
function _M.get_session_id()
    local r = get_request()
    if not r then
        error("no request found")
    end

    local len = C.ngx_http_lua_ffi_ssl_get_session_id_size(r, errmsg)

    if len < 0 then
        return nil, ffi_str(errmsg[0])
    end

    local buf = get_string_buf(len)

    local rc = C.ngx_http_lua_ffi_ssl_get_session_id(r, buf, errmsg)

    if rc == FFI_ERROR then
        return nil, ffi_str(errmsg[0])
    end

    return ffi_str(buf, len)
end


-- return ok, err
function _M.set_serialized_session(sess)
    local r = get_request()
    if not r then
        error("no request found")
    end

    local rc = C.ngx_http_lua_ffi_ssl_set_serialized_session(r, sess, #sess,
                                                             errmsg)
    if rc == FFI_ERROR then
        return nil, ffi_str(errmsg[0])
    end

    return true
end


return _M