From f628125a07b5fc9d2ae0e878f72c4c6e6496e384 Mon Sep 17 00:00:00 2001 From: brg-liuwei Date: Tue, 30 Aug 2016 23:33:00 +0800 Subject: [PATCH 1/4] ngx.socket.tcp receive bsd style --- src/ngx_stream_lua_socket_tcp.c | 35 ++++++++++++++ t/137-tcp-socket-receive-bsd-style.t | 72 ++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+) create mode 100644 t/137-tcp-socket-receive-bsd-style.t diff --git a/src/ngx_stream_lua_socket_tcp.c b/src/ngx_stream_lua_socket_tcp.c index 46808113..a06f6dd7 100644 --- a/src/ngx_stream_lua_socket_tcp.c +++ b/src/ngx_stream_lua_socket_tcp.c @@ -88,6 +88,7 @@ static int ngx_stream_lua_socket_write_error_retval_handler( ngx_stream_session_t *s, ngx_stream_lua_socket_tcp_upstream_t *u, lua_State *L); static ngx_int_t ngx_stream_lua_socket_read_all(void *data, ssize_t bytes); +static ngx_int_t ngx_stream_lua_socket_read_bsd(void *data, ssize_t bytes); static ngx_int_t ngx_stream_lua_socket_read_until(void *data, ssize_t bytes); static ngx_int_t ngx_stream_lua_socket_read_chunk(void *data, ssize_t bytes); static int ngx_stream_lua_socket_tcp_receiveuntil(lua_State *L); @@ -1736,6 +1737,10 @@ ngx_stream_lua_socket_tcp_receive(lua_State *L) u->input_filter = ngx_stream_lua_socket_read_all; break; + case 'b': + u->input_filter = ngx_stream_lua_socket_read_bsd; + break; + default: return luaL_argerror(L, 2, "bad pattern argument"); break; @@ -1990,6 +1995,36 @@ ngx_stream_lua_socket_read_line(void *data, ssize_t bytes) return NGX_AGAIN; } +static ngx_int_t +ngx_stream_lua_socket_read_bsd(void *data, ssize_t bytes) +{ + ngx_stream_lua_socket_tcp_upstream_t *u = data; + + ngx_buf_t *b; + u_char c; + + ngx_log_debug0(NGX_LOG_DEBUG_STREAM, u->request->connection->log, 0, + "stream lua tcp socket read bsd"); + + if (bytes == 0) { + u->ft_type |= NGX_STREAM_LUA_SOCKET_FT_CLOSED; + return NGX_ERROR; + } + + b = &u->buffer; + + dd("already read: %p: %.*s", u->buf_in, + (int) (u->buf_in->buf->last - u->buf_in->buf->pos), + u->buf_in->buf->pos); + + dd("data read: %.*s", (int) bytes, b->pos); + + u->buf_in->buf->last = ngx_cpymem(u->buf_in->buf->last, b->pos, bytes); + b->pos += bytes; + + return NGX_OK; +} + static ngx_int_t ngx_stream_lua_socket_tcp_read(ngx_stream_session_t *s, diff --git a/t/137-tcp-socket-receive-bsd-style.t b/t/137-tcp-socket-receive-bsd-style.t new file mode 100644 index 00000000..d1bbc0ae --- /dev/null +++ b/t/137-tcp-socket-receive-bsd-style.t @@ -0,0 +1,72 @@ + +use Test::Nginx::Socket::Lua::Stream 'no_plan'; + +repeat_each(10); + +run_tests(); + +__DATA__ + +=== TEST 1: *b pattern for receive +--- config + location = /t { + content_by_lua_block { + local sock = ngx.socket.tcp() + sock:settimeout(100) + assert(sock:connect("127.0.0.1", 5678)) + sock:send("10") + ngx.sleep(0.01) + sock:send("2") + ngx.sleep(0.01) + sock:send("4") + + local pow, _ = sock:receive('*l') + sock:close() + ngx.say(pow) + } + } +--- main_config + stream { + server { + listen 5678; + content_by_lua_block { + local function is_power_of_two(str) + local num = tonumber(str) + if num <= 0 then + return false, nil + end + local power = 0 + while num ~= 1 do + if math.fmod(num, 2) ~= 0 then + return false, nil + end + num = num / 2 + power = power + 1 + end + return true, power + end + + local sock = ngx.req.socket(true) + local msg = '' + local pow + while true do + local chunk, err = sock:receive('*b') + if not chunk then + break + end + msg = msg .. chunk + local ok, num = is_power_of_two(msg) + if ok then + pow = num + break + end + end + sock:send(tostring(pow) .. '\n') + } + } + } + +--- request +GET /t +--- response_body +10 From a5876476bdd38ece207ac1e51ba71aa15d043c56 Mon Sep 17 00:00:00 2001 From: brg-liuwei Date: Wed, 31 Aug 2016 13:20:00 +0800 Subject: [PATCH 2/4] remove unused variable --- src/ngx_stream_lua_socket_tcp.c | 1 - t/137-tcp-socket-receive-bsd-style.t | 4 ++++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/ngx_stream_lua_socket_tcp.c b/src/ngx_stream_lua_socket_tcp.c index a06f6dd7..5ea78199 100644 --- a/src/ngx_stream_lua_socket_tcp.c +++ b/src/ngx_stream_lua_socket_tcp.c @@ -2001,7 +2001,6 @@ ngx_stream_lua_socket_read_bsd(void *data, ssize_t bytes) ngx_stream_lua_socket_tcp_upstream_t *u = data; ngx_buf_t *b; - u_char c; ngx_log_debug0(NGX_LOG_DEBUG_STREAM, u->request->connection->log, 0, "stream lua tcp socket read bsd"); diff --git a/t/137-tcp-socket-receive-bsd-style.t b/t/137-tcp-socket-receive-bsd-style.t index d1bbc0ae..a2b615ee 100644 --- a/t/137-tcp-socket-receive-bsd-style.t +++ b/t/137-tcp-socket-receive-bsd-style.t @@ -70,3 +70,7 @@ __DATA__ GET /t --- response_body 10 +--- no_error_log +[error] +--- error_log +stream lua tcp socket read bsd From 0b74094517f543c3e1f97c4e05d12c0084d9fd1c Mon Sep 17 00:00:00 2001 From: brg-liuwei Date: Sat, 3 Sep 2016 02:11:19 +0800 Subject: [PATCH 3/4] add missing macro: NGX_DEBUG --- src/ngx_stream_lua_socket_tcp.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/ngx_stream_lua_socket_tcp.c b/src/ngx_stream_lua_socket_tcp.c index 5ea78199..55a61061 100644 --- a/src/ngx_stream_lua_socket_tcp.c +++ b/src/ngx_stream_lua_socket_tcp.c @@ -2002,8 +2002,10 @@ ngx_stream_lua_socket_read_bsd(void *data, ssize_t bytes) ngx_buf_t *b; +#if (NGX_DEBUG) ngx_log_debug0(NGX_LOG_DEBUG_STREAM, u->request->connection->log, 0, "stream lua tcp socket read bsd"); +#endif if (bytes == 0) { u->ft_type |= NGX_STREAM_LUA_SOCKET_FT_CLOSED; From 76a1b93e939ac50298487f288e4ae0fcedb3765d Mon Sep 17 00:00:00 2001 From: brg-liuwei Date: Sun, 4 Sep 2016 17:46:46 +0800 Subject: [PATCH 4/4] update test case --- src/ngx_stream_lua_socket_tcp.c | 1 + t/137-tcp-socket-receive-bsd-style.t | 81 +++++++++++++++------------- 2 files changed, 46 insertions(+), 36 deletions(-) diff --git a/src/ngx_stream_lua_socket_tcp.c b/src/ngx_stream_lua_socket_tcp.c index 55a61061..0e9080ff 100644 --- a/src/ngx_stream_lua_socket_tcp.c +++ b/src/ngx_stream_lua_socket_tcp.c @@ -1995,6 +1995,7 @@ ngx_stream_lua_socket_read_line(void *data, ssize_t bytes) return NGX_AGAIN; } + static ngx_int_t ngx_stream_lua_socket_read_bsd(void *data, ssize_t bytes) { diff --git a/t/137-tcp-socket-receive-bsd-style.t b/t/137-tcp-socket-receive-bsd-style.t index a2b615ee..d73f4de0 100644 --- a/t/137-tcp-socket-receive-bsd-style.t +++ b/t/137-tcp-socket-receive-bsd-style.t @@ -1,7 +1,10 @@ +# vim:set ft= ts=4 sw=4 et fdm=marker: use Test::Nginx::Socket::Lua::Stream 'no_plan'; -repeat_each(10); +repeat_each(2); + +log_level 'debug'; run_tests(); @@ -14,15 +17,27 @@ __DATA__ local sock = ngx.socket.tcp() sock:settimeout(100) assert(sock:connect("127.0.0.1", 5678)) - sock:send("10") + + sock:send("1") + ngx.sleep(0.01) + + sock:send("22") ngx.sleep(0.01) - sock:send("2") + + local t = { + 'test', + 'send', + 'table', + } + sock:send(t) ngx.sleep(0.01) - sock:send("4") - local pow, _ = sock:receive('*l') + sock:send("hello world") + + local data, _ = sock:receive('*a') + ngx.say(data) + sock:close() - ngx.say(pow) } } --- main_config @@ -30,38 +45,32 @@ __DATA__ server { listen 5678; content_by_lua_block { - local function is_power_of_two(str) - local num = tonumber(str) - if num <= 0 then - return false, nil - end - local power = 0 - while num ~= 1 do - if math.fmod(num, 2) ~= 0 then - return false, nil - end - num = num / 2 - power = power + 1 - end - return true, power + local sock = ngx.req.socket(true) + local data, _ = sock:receive('*b') + if data ~= '1' then + ngx.log(ngx.ERR, "unexcepted result of: ", data) + return end - local sock = ngx.req.socket(true) - local msg = '' - local pow - while true do - local chunk, err = sock:receive('*b') - if not chunk then - break - end - msg = msg .. chunk - local ok, num = is_power_of_two(msg) - if ok then - pow = num - break - end + data, _ = sock:receive('*b') + if data ~= '22' then + ngx.log(ngx.ERR, "unexcepted result of: ", data) + return + end + + data, _ = sock:receive('*b') + if data ~= 'testsendtable' then + ngx.log(ngx.ERR, "unexcepted result of: ", data) + return end - sock:send(tostring(pow) .. '\n') + + data, _ = sock:receive('*b') + if data ~= 'hello world' then + ngx.log(ngx.ERR, "unexcepted result of: ", data) + return + end + + sock:send('ok') } } } @@ -69,7 +78,7 @@ __DATA__ --- request GET /t --- response_body -10 +ok --- no_error_log [error] --- error_log