@@ -8,6 +8,7 @@ local ctypes = require "resty.openssl.aux.ctypes"
8
8
ffi .cdef [[
9
9
unsigned long ERR_peek_error (void );
10
10
unsigned long ERR_peek_last_error_line (const char ** file , int * line );
11
+ unsigned long ERR_get_error_line (const char ** file , int * line );
11
12
void ERR_clear_error (void );
12
13
void ERR_error_string_n (unsigned long e , char * buf , size_t len );
13
14
]]
@@ -16,16 +17,21 @@ local constchar_ptrptr = ffi.typeof("const char*[1]")
16
17
17
18
local buf = ffi .new (' char[256]' )
18
19
19
- local function format_error (ctx , code )
20
+ local function format_error (ctx , code , all_errors )
20
21
local errors = {}
21
22
if code then
22
23
table.insert (errors , string.format (" code: %d" , code or 0 ))
23
24
end
24
25
-- get the OpenSSL errors
25
- if C .ERR_peek_error () ~= 0 then
26
+ while C .ERR_peek_error () ~= 0 do
26
27
local line = ctypes .ptr_of_int ()
27
28
local path = constchar_ptrptr ()
28
- local code = C .ERR_peek_last_error_line (path , line )
29
+ local code
30
+ if all_errors then
31
+ code = C .ERR_get_error_line (path , line )
32
+ else
33
+ code = C .ERR_peek_last_error_line (path , line )
34
+ end
29
35
30
36
local abs_path = ffi_str (path [0 ])
31
37
-- ../crypto/asn1/a_d2i_fp.c => crypto/asn1/a_d2i_fp.c
@@ -34,20 +40,30 @@ local function format_error(ctx, code)
34
40
abs_path = abs_path :sub (start + 1 )
35
41
end
36
42
37
- C .ERR_clear_error ()
38
43
C .ERR_error_string_n (code , buf , ffi_sizeof (buf ))
39
44
table.insert (errors , string.format (" %s:%d:%s" ,
40
45
abs_path , line [0 ], ffi_str (buf ))
41
46
)
47
+
48
+ if not all_errors then
49
+ break
50
+ end
42
51
end
43
52
53
+ C .ERR_clear_error ()
54
+
44
55
if # errors > 0 then
45
56
return string.format (" %s%s%s" , (ctx or " " ), (ctx and " : " or " " ), table.concat (errors , " " ))
46
57
else
47
58
return string.format (" %s failed" , ctx )
48
59
end
49
60
end
50
61
62
+ local function format_all_error (ctx , code )
63
+ return format_error (ctx , code , true )
64
+ end
65
+
51
66
return {
52
67
format_error = format_error ,
68
+ format_all_error = format_all_error ,
53
69
}
0 commit comments