diff --git a/nse_nsock.cc b/nse_nsock.cc index 4cc06b5ed..25131bd64 100644 --- a/nse_nsock.cc +++ b/nse_nsock.cc @@ -635,17 +635,23 @@ static void receive_callback (nsock_pool nsp, nsock_event nse, void *udata) assert(nse_type(nse) == NSE_TYPE_READ); if (nse_status(nse) == NSE_STATUS_SUCCESS) { - assert(lua_status(L) == LUA_YIELD); int len; const char *str = nse_readbuf(nse, &len); trace(nse_iod(nse), hexify((const unsigned char *) str, len).c_str(), FROM); lua_pushboolean(L, true); lua_pushlstring(L, str, len); - nse_restore(L, 2); + // since r39036, read event can succeed immediately if there's pending SSL data + if(lua_status(L) == LUA_YIELD) + nse_restore(L, 2); + else + nu->action = NU_ACTION_IMMEDIATE; + return; } else if (lua_status(L) == LUA_OK && nse_status(nse) == NSE_STATUS_EOF) { // since r39028, read event can fail immediately if the socket is EOF. trace(nse_iod(nse), nu->action, "EOF"); + lua_pushnil(L); + lua_pushliteral(L, "EOF"); nu->action = NU_ACTION_IMMEDIATE; return; } @@ -658,10 +664,11 @@ static int l_receive (lua_State *L) nsock_pool nsp = get_pool(L); nse_nsock_udata *nu = check_nsock_udata(L, 1, true); NSOCK_UDATA_ENSURE_OPEN(L, nu); + int oldtop = lua_gettop(L); nsock_read(nsp, nu->nsiod, receive_callback, nu->timeout, nu); if (nu->action == NU_ACTION_IMMEDIATE) { // Immediate return - return nseU_safeerror(L, "EOF"); + return lua_gettop(L) - oldtop; } return yield(L, nu, "RECEIVE", FROM, 0, NULL); } @@ -671,11 +678,12 @@ static int l_receive_lines (lua_State *L) nsock_pool nsp = get_pool(L); nse_nsock_udata *nu = check_nsock_udata(L, 1, true); NSOCK_UDATA_ENSURE_OPEN(L, nu); + int oldtop = lua_gettop(L); nsock_readlines(nsp, nu->nsiod, receive_callback, nu->timeout, nu, luaL_checkinteger(L, 2)); if (nu->action == NU_ACTION_IMMEDIATE) { // Immediate return - return nseU_safeerror(L, "EOF"); + return lua_gettop(L) - oldtop; } return yield(L, nu, "RECEIVE LINES", FROM, 0, NULL); } @@ -685,11 +693,12 @@ static int l_receive_bytes (lua_State *L) nsock_pool nsp = get_pool(L); nse_nsock_udata *nu = check_nsock_udata(L, 1, true); NSOCK_UDATA_ENSURE_OPEN(L, nu); + int oldtop = lua_gettop(L); nsock_readbytes(nsp, nu->nsiod, receive_callback, nu->timeout, nu, luaL_checkinteger(L, 2)); if (nu->action == NU_ACTION_IMMEDIATE) { // Immediate return - return nseU_safeerror(L, "EOF"); + return lua_gettop(L) - oldtop; } return yield(L, nu, "RECEIVE BYTES", FROM, 0, NULL); } @@ -754,10 +763,11 @@ static int receive_buf (lua_State *L, int status, lua_KContext ctx) else { lua_pop(L, 2); /* pop 2 results */ + int oldtop = lua_gettop(L); nsock_read(nsp, nu->nsiod, receive_callback, nu->timeout, nu); if (nu->action == NU_ACTION_IMMEDIATE) { // Immediate return - return nseU_safeerror(L, "EOF"); + return lua_gettop(L) - oldtop; } return yield(L, nu, "RECEIVE BUF", FROM, 0, receive_buf); }