@@ -33,6 +33,7 @@ static int ngx_stream_lua_socket_tcp_connect(lua_State *L);
3333#if (NGX_STREAM_SSL )
3434static int ngx_stream_lua_socket_tcp_sslhandshake (lua_State * L );
3535static int ngx_stream_lua_socket_tcp_serversslhandshake (lua_State * L );
36+ static int ngx_stream_lua_socket_tcp_get_ssl_session (lua_State * L );
3637#endif
3738static int ngx_stream_lua_socket_tcp_receive (lua_State * L );
3839static int ngx_stream_lua_socket_tcp_receiveany (lua_State * L );
@@ -373,6 +374,9 @@ ngx_stream_lua_inject_socket_tcp_api(ngx_log_t *log, lua_State *L)
373374 lua_pushcfunction (L , ngx_stream_lua_socket_tcp_sslhandshake );
374375 lua_setfield (L , -2 , "sslhandshake" );
375376
377+ lua_pushcfunction (L , ngx_stream_lua_socket_tcp_get_ssl_session );
378+ lua_setfield (L , -2 , "getsslsession" );
379+
376380#endif
377381
378382 lua_pushcfunction (L , ngx_stream_lua_socket_tcp_receive );
@@ -1659,6 +1663,82 @@ ngx_stream_lua_socket_conn_error_retval_handler(ngx_stream_lua_request_t *r,
16591663
16601664
16611665#if (NGX_STREAM_SSL )
1666+ static int
1667+ ngx_stream_lua_socket_tcp_get_ssl_session (lua_State * L )
1668+ {
1669+ int n ;
1670+
1671+ ngx_connection_t * c ;
1672+ ngx_ssl_session_t * ssl_session , * * ud ;
1673+ ngx_stream_lua_request_t * r ;
1674+ ngx_stream_lua_socket_tcp_upstream_t * u ;
1675+
1676+
1677+ n = lua_gettop (L );
1678+ if (n < 1 ) {
1679+ return luaL_error (L , "getsslsession: expecting 1 "
1680+ "argument, but seen %d" , n );
1681+ }
1682+
1683+ r = ngx_stream_lua_get_req (L );
1684+ if (r == NULL ) {
1685+ return luaL_error (L , "no request found" );
1686+ }
1687+
1688+ ngx_log_debug0 (NGX_LOG_DEBUG_STREAM , r -> connection -> log , 0 ,
1689+ "stream lua tcp socket getsslsession" );
1690+
1691+ luaL_checktype (L , 1 , LUA_TTABLE );
1692+
1693+ lua_rawgeti (L , 1 , SOCKET_CTX_INDEX );
1694+ u = lua_touserdata (L , -1 );
1695+
1696+ if (u == NULL
1697+ || u -> peer .connection == NULL
1698+ || u -> read_closed
1699+ || u -> write_closed )
1700+ {
1701+ lua_pushnil (L );
1702+ lua_pushliteral (L , "closed" );
1703+ return 2 ;
1704+ }
1705+
1706+ if (u -> request != r ) {
1707+ return luaL_error (L , "bad request" );
1708+ }
1709+
1710+ c = u -> peer .connection ;
1711+ if (c == NULL ) {
1712+ lua_pushnil (L );
1713+ lua_pushliteral (L , "connection not found" );
1714+ return 2 ;
1715+ }
1716+
1717+ ssl_session = ngx_ssl_get_session (c );
1718+ if (ssl_session == NULL ) {
1719+ lua_pushnil (L );
1720+ lua_pushliteral (L , "no session" );
1721+ return 2 ;
1722+ }
1723+
1724+ if (!SSL_SESSION_is_resumable (ssl_session )) {
1725+ ngx_ssl_free_session (ssl_session );
1726+ lua_pushnil (L );
1727+ lua_pushliteral (L , "not resumable session" );
1728+ return 2 ;
1729+ }
1730+
1731+ ud = lua_newuserdata (L , sizeof (ngx_ssl_session_t * ));
1732+ * ud = ssl_session ;
1733+ /* set up the __gc metamethod */
1734+ lua_pushlightuserdata (L , ngx_stream_lua_lightudata_mask (
1735+ ssl_session_metatable_key ));
1736+ lua_rawget (L , LUA_REGISTRYINDEX );
1737+ lua_setmetatable (L , -2 );
1738+
1739+ return 1 ;
1740+ }
1741+
16621742
16631743static int
16641744ngx_stream_lua_socket_tcp_sslhandshake (lua_State * L )
0 commit comments