Skip to content
Snippets Groups Projects
Commit 2091e9d6 authored by Roman Tsisyk's avatar Roman Tsisyk
Browse files

Fix #433: box.session functions don't work properly

parent 36a574ae
No related branches found
No related tags found
No related merge requests found
...@@ -117,7 +117,7 @@ lbox_session_exists(struct lua_State *L) ...@@ -117,7 +117,7 @@ lbox_session_exists(struct lua_State *L)
luaL_error(L, "session.exists(sid): bad arguments"); luaL_error(L, "session.exists(sid): bad arguments");
uint32_t sid = luaL_checkint(L, -1); uint32_t sid = luaL_checkint(L, -1);
lua_pushnumber(L, session_exists(sid)); lua_pushboolean(L, session_find(sid) != NULL);
return 1; return 1;
} }
...@@ -131,7 +131,10 @@ lbox_session_fd(struct lua_State *L) ...@@ -131,7 +131,10 @@ lbox_session_fd(struct lua_State *L)
luaL_error(L, "session.fd(sid): bad arguments"); luaL_error(L, "session.fd(sid): bad arguments");
uint32_t sid = luaL_checkint(L, -1); uint32_t sid = luaL_checkint(L, -1);
lua_pushnumber(L, session_fd(sid)); struct session *session = session_find(sid);
if (session == NULL)
luaL_error(L, "session.fd(): session does not exit");
lua_pushinteger(L, session->fd);
return 1; return 1;
} }
...@@ -145,10 +148,21 @@ lbox_session_peer(struct lua_State *L) ...@@ -145,10 +148,21 @@ lbox_session_peer(struct lua_State *L)
if (lua_gettop(L) > 1) if (lua_gettop(L) > 1)
luaL_error(L, "session.peer(sid): bad arguments"); luaL_error(L, "session.peer(sid): bad arguments");
uint32_t sid = lua_gettop(L) == 1 ? int fd;
luaL_checkint(L, -1) : session()->id; if (lua_gettop(L) == 1) {
struct session *session = session_find(luaL_checkint(L, 1));
if (session == NULL)
luaL_error(L, "session.peer(): session does not exit");
fd = session->fd;
} else {
fd = session()->fd;
}
if (fd < 0) {
lua_pushnil(L); /* no associated peer */
return 1;
}
int fd = session_fd(sid);
struct sockaddr_storage addr; struct sockaddr_storage addr;
socklen_t addrlen = sizeof(addr); socklen_t addrlen = sizeof(addr);
sio_getpeername(fd, (struct sockaddr *)&addr, &addrlen); sio_getpeername(fd, (struct sockaddr *)&addr, &addrlen);
......
...@@ -131,15 +131,14 @@ session_destroy(struct session *session) ...@@ -131,15 +131,14 @@ session_destroy(struct session *session)
mempool_free(&session_pool, session); mempool_free(&session_pool, session);
} }
int struct session *
session_fd(uint32_t sid) session_find(uint32_t sid)
{ {
mh_int_t k = mh_i32ptr_find(session_registry, sid, NULL); mh_int_t k = mh_i32ptr_find(session_registry, sid, NULL);
if (k == mh_end(session_registry)) if (k == mh_end(session_registry))
return -1; return NULL;
struct session *session = (struct session *) return (struct session *)
mh_i32ptr_node(session_registry, k)->val; mh_i32ptr_node(session_registry, k)->val;
return session->fd;
} }
void void
......
...@@ -92,21 +92,10 @@ void ...@@ -92,21 +92,10 @@ void
session_destroy(struct session *); session_destroy(struct session *);
/** /**
* Return a file descriptor * Find a session by id.
* associated with a session, or -1 if the
* session doesn't exist.
*/ */
int struct session *
session_fd(uint32_t sid); session_find(uint32_t sid);
/**
* Check whether a session exists or not.
*/
static inline bool
session_exists(uint32_t sid)
{
return session_fd(sid) >= 0;
}
/** Set session auth token and user id. */ /** Set session auth token and user id. */
static inline void static inline void
......
...@@ -12,7 +12,11 @@ space:create_index('primary', { type = 'hash' }) ...@@ -12,7 +12,11 @@ space:create_index('primary', { type = 'hash' })
... ...
session.exists(session.id()) session.exists(session.id())
--- ---
- 0 - true
...
session.peer(session.id())
---
- null
... ...
session.exists() session.exists()
--- ---
...@@ -24,7 +28,7 @@ session.exists(1, 2, 3) ...@@ -24,7 +28,7 @@ session.exists(1, 2, 3)
... ...
session.exists(1234567890) session.exists(1234567890)
--- ---
- 0 - false
... ...
-- check session.id() -- check session.id()
session.id() > 0 session.id() > 0
......
...@@ -5,6 +5,7 @@ space = box.schema.create_space('tweedledum') ...@@ -5,6 +5,7 @@ space = box.schema.create_space('tweedledum')
space:create_index('primary', { type = 'hash' }) space:create_index('primary', { type = 'hash' })
session.exists(session.id()) session.exists(session.id())
session.peer(session.id())
session.exists() session.exists()
session.exists(1, 2, 3) session.exists(1, 2, 3)
session.exists(1234567890) session.exists(1234567890)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment