From d80bb0d82ba105c8fdb27e6174c267965d06ffb0 Mon Sep 17 00:00:00 2001 From: Diego Nehab Date: Tue, 15 Jul 2014 14:49:20 -0300 Subject: [PATCH 1/5] Fix Host: header according to RFC7230 --- src/http.lua | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/http.lua b/src/http.lua index 1d0eb50..d5457f6 100644 --- a/src/http.lua +++ b/src/http.lua @@ -209,8 +209,7 @@ end local function adjustheaders(reqt) -- default headers - local host = reqt.host - if reqt.port then host = host .. ":" .. reqt.port end + local host = string.gsub(reqt.authority, "^.-@", "") local lower = { ["user-agent"] = _M.USERAGENT, ["host"] = host, @@ -353,4 +352,4 @@ _M.request = socket.protect(function(reqt, body) else return trequest(reqt) end end) -return _M \ No newline at end of file +return _M From e602c2b271c4efdd0f9138863fb16f5b790fb824 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Mon, 27 Oct 2014 11:10:48 -0400 Subject: [PATCH 2/5] src/usocket: Do not setblocking on destroy; This results in unexpected behaviour if the socket has been `dup()`d, as O_NONBLOCK is shared. Close is always 'blocking' anyway See https://github.com/wahern/cqueues/issues/13 for an example use case --- src/usocket.c | 1 - 1 file changed, 1 deletion(-) diff --git a/src/usocket.c b/src/usocket.c index 096ecd0..ca483f4 100644 --- a/src/usocket.c +++ b/src/usocket.c @@ -92,7 +92,6 @@ int socket_close(void) { \*-------------------------------------------------------------------------*/ void socket_destroy(p_socket ps) { if (*ps != SOCKET_INVALID) { - socket_setblocking(ps); close(*ps); *ps = SOCKET_INVALID; } From 4f122e60b147107a0a5ca7a8dff698a47794e30c Mon Sep 17 00:00:00 2001 From: daurnimator Date: Mon, 27 Oct 2014 14:03:34 -0400 Subject: [PATCH 3/5] src/usocket: Don't unset/set O_NONBLOCK around listen() or shutdown() calls. It doesn't effect them. Not true on windows --- src/usocket.c | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/usocket.c b/src/usocket.c index ca483f4..89f774d 100644 --- a/src/usocket.c +++ b/src/usocket.c @@ -139,9 +139,7 @@ int socket_bind(p_socket ps, SA *addr, socklen_t len) { \*-------------------------------------------------------------------------*/ int socket_listen(p_socket ps, int backlog) { int err = IO_DONE; - socket_setblocking(ps); if (listen(*ps, backlog)) err = errno; - socket_setnonblocking(ps); return err; } @@ -149,9 +147,7 @@ int socket_listen(p_socket ps, int backlog) { * \*-------------------------------------------------------------------------*/ void socket_shutdown(p_socket ps, int how) { - socket_setblocking(ps); shutdown(*ps, how); - socket_setnonblocking(ps); } /*-------------------------------------------------------------------------*\ From 7006ae120da2f8ec74323422541cd585c91832d6 Mon Sep 17 00:00:00 2001 From: Philipp Janda Date: Mon, 10 Nov 2014 18:17:10 +0100 Subject: [PATCH 4/5] fixed yieldable socket.protect in etc/dispatch.lua --- etc/dispatch.lua | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/etc/dispatch.lua b/etc/dispatch.lua index cab7f59..2485415 100644 --- a/etc/dispatch.lua +++ b/etc/dispatch.lua @@ -5,6 +5,7 @@ ----------------------------------------------------------------------------- local base = _G local table = require("table") +local string = require("string") local socket = require("socket") local coroutine = require("coroutine") module("dispatch") @@ -43,26 +44,32 @@ end ----------------------------------------------------------------------------- -- Mega hack. Don't try to do this at home. ----------------------------------------------------------------------------- --- we can't yield across calls to protect, so we rewrite it with coxpcall +-- we can't yield across calls to protect on Lua 5.1, so we rewrite it with +-- coroutines -- make sure you don't require any module that uses socket.protect before -- loading our hack -function socket.protect(f) - return function(...) - local co = coroutine.create(f) - while true do - local results = {coroutine.resume(co, ...)} - local status = table.remove(results, 1) - if not status then - if base.type(results[1]) == 'table' then - return nil, results[1][1] - else base.error(results[1]) end - end - if coroutine.status(co) == "suspended" then - arg = {coroutine.yield(base.unpack(results))} +if string.sub(base._VERSION, -3) == "5.1" then + local function _protect(co, status, ...) + if not status then + local msg = ... + if base.type(msg) == 'table' then + return nil, msg[1] else - return base.unpack(results) + base.error(msg, 0) end end + if coroutine.status(co) == "suspended" then + return _protect(co, coroutine.resume(co, coroutine.yield(...))) + else + return ... + end + end + + function socket.protect(f) + return function(...) + local co = coroutine.create(f) + return _protect(co, coroutine.resume(co, ...)) + end end end From 0b03eec16be0b3a5efe71bcb8887719d1ea87d60 Mon Sep 17 00:00:00 2001 From: Philipp Janda Date: Mon, 10 Nov 2014 18:49:40 +0100 Subject: [PATCH 5/5] make socket.protect yieldable on Lua 5.2/5.3 --- src/except.c | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/src/except.c b/src/except.c index 002e701..4faa208 100644 --- a/src/except.c +++ b/src/except.c @@ -9,6 +9,15 @@ #include "except.h" +#if LUA_VERSION_NUM < 502 +#define lua_pcallk(L, na, nr, err, ctx, cont) \ + ((void)ctx,(void)cont,lua_pcall(L, na, nr, err)) +#endif + +#if LUA_VERSION_NUM < 503 +typedef int lua_KContext; +#endif + /*=========================================================================*\ * Internal function prototypes. \*=========================================================================*/ @@ -73,14 +82,30 @@ static int unwrap(lua_State *L) { } else return 0; } +static int protected_finish(lua_State *L, int status, lua_KContext ctx) { + (void)ctx; + if (status != 0 && status != LUA_YIELD) { + if (unwrap(L)) return 2; + else return lua_error(L); + } else return lua_gettop(L); +} + +#if LUA_VERSION_NUM == 502 +static int protected_cont(lua_State *L) { + int ctx = 0; + int status = lua_getctx(L, &ctx); + return protected_finish(L, status, ctx); +} +#else +#define protected_cont protected_finish +#endif + static int protected_(lua_State *L) { + int status; lua_pushvalue(L, lua_upvalueindex(1)); lua_insert(L, 1); - if (lua_pcall(L, lua_gettop(L) - 1, LUA_MULTRET, 0) != 0) { - if (unwrap(L)) return 2; - else lua_error(L); - return 0; - } else return lua_gettop(L); + status = lua_pcallk(L, lua_gettop(L) - 1, LUA_MULTRET, 0, 0, protected_cont); + return protected_finish(L, status, 0); } static int global_protect(lua_State *L) {