From 700ece0721b4b1b70da413f15c5df7dcbae0fe3b Mon Sep 17 00:00:00 2001 From: mpeterv Date: Thu, 11 Feb 2016 15:54:59 +0300 Subject: Fix base_parsed global in url module --- src/url.lua | 1 + 1 file changed, 1 insertion(+) (limited to 'src/url.lua') diff --git a/src/url.lua b/src/url.lua index 7809535..fbd93d1 100644 --- a/src/url.lua +++ b/src/url.lua @@ -219,6 +219,7 @@ end -- corresponding absolute url ----------------------------------------------------------------------------- function _M.absolute(base_url, relative_url) + local base_parsed if base.type(base_url) == "table" then base_parsed = base_url base_url = _M.build(base_parsed) -- cgit v1.2.3-55-g6feb From 9984741d94772be1484033d711c1a674da38217a Mon Sep 17 00:00:00 2001 From: Okash Khawaja Date: Tue, 12 Apr 2016 00:01:51 +0100 Subject: Update comments for url.unescape() function. --- src/url.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/url.lua') diff --git a/src/url.lua b/src/url.lua index fbd93d1..b59960a 100644 --- a/src/url.lua +++ b/src/url.lua @@ -64,11 +64,11 @@ local function protect_segment(s) end ----------------------------------------------------------------------------- --- Encodes a string into its escaped hexadecimal representation +-- Unencodes a escaped hexadecimal string into its binary representation -- Input --- s: binary string to be encoded +-- s: escaped hexadecimal string to be unencoded -- Returns --- escaped representation of string binary +-- unescaped binary representation of escaped hexadecimal binary ----------------------------------------------------------------------------- function _M.unescape(s) return (string.gsub(s, "%%(%x%x)", function(hex) -- cgit v1.2.3-55-g6feb From 860da0f4b4d9d6ca647ff0a3b15389b41557dec4 Mon Sep 17 00:00:00 2001 From: LordHelmchen Date: Thu, 16 Mar 2017 16:33:28 +0100 Subject: make protect_segment in url.lua rfc compliant percent-encode uppercase see https://tools.ietf.org/html/rfc3986#section-6.2.2 --- src/url.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/url.lua') diff --git a/src/url.lua b/src/url.lua index b59960a..3290ce7 100644 --- a/src/url.lua +++ b/src/url.lua @@ -59,7 +59,7 @@ local segment_set = make_set { local function protect_segment(s) return string.gsub(s, "([^A-Za-z0-9_])", function (c) if segment_set[c] then return c - else return string.format("%%%02x", string.byte(c)) end + else return string.format("%%%02X", string.byte(c)) end end) end -- cgit v1.2.3-55-g6feb From 16bb5487464be076136cc230e5b46754e03d559c Mon Sep 17 00:00:00 2001 From: François Perrad Date: Thu, 16 Mar 2017 16:57:17 +0100 Subject: fix for Lua 5.3 built without number / string conversion This kind of Lua could be built with this command: ``` hererocks --lua 5.3 --cflags="-DLUA_NOCVTN2S -DLUA_NOCVTS2N" ``` --- src/url.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/url.lua') diff --git a/src/url.lua b/src/url.lua index b59960a..eb094d5 100644 --- a/src/url.lua +++ b/src/url.lua @@ -193,7 +193,7 @@ function _M.build(parsed) if string.find(authority, ":") then -- IPv6? authority = "[" .. authority .. "]" end - if parsed.port then authority = authority .. ":" .. parsed.port end + if parsed.port then authority = authority .. ":" .. base.tostring(parsed.port) end local userinfo = parsed.userinfo if parsed.user then userinfo = parsed.user -- cgit v1.2.3-55-g6feb From 44fb9e911281a2ff2c032cd20959ad91a1f470b4 Mon Sep 17 00:00:00 2001 From: LordHelmchen Date: Thu, 16 Mar 2017 17:53:02 +0100 Subject: correct typo --- src/url.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/url.lua') diff --git a/src/url.lua b/src/url.lua index 3290ce7..fb333b1 100644 --- a/src/url.lua +++ b/src/url.lua @@ -49,7 +49,7 @@ local function make_set(t) return s end --- these are allowed withing a path segment, along with alphanum +-- these are allowed within a path segment, along with alphanum -- other characters must be escaped local segment_set = make_set { "-", "_", ".", "!", "~", "*", "'", "(", -- cgit v1.2.3-55-g6feb From 47e644031fed0ced1066ff843c9b42d45b54042d Mon Sep 17 00:00:00 2001 From: Diego Nehab Date: Mon, 20 Mar 2017 16:56:15 -0300 Subject: Preserve path when parsing urls. --- src/url.lua | 5 +++-- test/auth/.htaccess | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) (limited to 'src/url.lua') diff --git a/src/url.lua b/src/url.lua index eb094d5..1e5a3f6 100644 --- a/src/url.lua +++ b/src/url.lua @@ -183,8 +183,9 @@ end -- a stringing with the corresponding URL ----------------------------------------------------------------------------- function _M.build(parsed) - local ppath = _M.parse_path(parsed.path or "") - local url = _M.build_path(ppath) + --local ppath = _M.parse_path(parsed.path or "") + --local url = _M.build_path(ppath) + local url = parsed.path or "" if parsed.params then url = url .. ";" .. parsed.params end if parsed.query then url = url .. "?" .. parsed.query end local authority = parsed.authority diff --git a/test/auth/.htaccess b/test/auth/.htaccess index bb2794a..2509ae3 100644 --- a/test/auth/.htaccess +++ b/test/auth/.htaccess @@ -1,4 +1,4 @@ AuthName "test-auth" AuthType Basic - AuthUserFile /Users/diego/impa/luasocket/test/auth/.htpasswd + AuthUserFile /home/diego/impa/luasocket/test/auth/.htpasswd Require valid-user -- cgit v1.2.3-55-g6feb From 3ee89515a0ef4852f64b13133c22aa7d3a322cfd Mon Sep 17 00:00:00 2001 From: Herbert Leuwer Date: Sun, 19 Nov 2017 19:48:37 +0100 Subject: fixed URL parsing in url.lua: parse fragment after parsing username and password. --- src/url.lua | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'src/url.lua') diff --git a/src/url.lua b/src/url.lua index d61111e..6448853 100644 --- a/src/url.lua +++ b/src/url.lua @@ -131,11 +131,6 @@ function _M.parse(url, default) if not url or url == "" then return nil, "invalid url" end -- remove whitespace -- url = string.gsub(url, "%s", "") - -- get fragment - url = string.gsub(url, "#(.*)$", function(f) - parsed.fragment = f - return "" - end) -- get scheme url = string.gsub(url, "^([%w][%w%+%-%.]*)%:", function(s) parsed.scheme = s; return "" end) @@ -149,6 +144,11 @@ function _M.parse(url, default) parsed.query = q return "" end) + -- get fragment + url = string.gsub(url, "#(.*)$", function(f) + parsed.fragment = f + return "" + end) -- get params url = string.gsub(url, "%;(.*)", function(p) parsed.params = p -- cgit v1.2.3-55-g6feb From 2d6a0f7bda9241f827a3edbfa738603c024a423b Mon Sep 17 00:00:00 2001 From: Herbert Leuwer Date: Wed, 22 Nov 2017 09:30:12 +0100 Subject: fixed url parsing; postpone fragment parsing after authority parsing; added test cases to test/urltest.lua fixed reference patterns in check_protect() to upper case hex letters --- src/url.lua | 10 ++++---- test/urltest.lua | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 77 insertions(+), 8 deletions(-) (limited to 'src/url.lua') diff --git a/src/url.lua b/src/url.lua index 6448853..110ea94 100644 --- a/src/url.lua +++ b/src/url.lua @@ -139,16 +139,16 @@ function _M.parse(url, default) parsed.authority = n return "" end) - -- get query string - url = string.gsub(url, "%?(.*)", function(q) - parsed.query = q - return "" - end) -- get fragment url = string.gsub(url, "#(.*)$", function(f) parsed.fragment = f return "" end) + -- get query string + url = string.gsub(url, "%?(.*)", function(q) + parsed.query = q + return "" + end) -- get params url = string.gsub(url, "%;(.*)", function(p) parsed.params = p diff --git a/test/urltest.lua b/test/urltest.lua index 32cb348..1090a7e 100644 --- a/test/urltest.lua +++ b/test/urltest.lua @@ -90,6 +90,75 @@ local check_parse_url = function(gaba) end print("testing URL parsing") +check_parse_url{ + url = "scheme://user:pass$%?#wd@host:port/path;params?query#fragment", + scheme = "scheme", + authority = "user:pass$%?#wd@host:port", + host = "host", + port = "port", + userinfo = "user:pass$%?#wd", + password = "pass$%?#wd", + user = "user", + path = "/path", + params = "params", + query = "query", + fragment = "fragment" +} +check_parse_url{ + url = "scheme://user:pass?#wd@host:port/path;params?query#fragment", + scheme = "scheme", + authority = "user:pass?#wd@host:port", + host = "host", + port = "port", + userinfo = "user:pass?#wd", + password = "pass?#wd", + user = "user", + path = "/path", + params = "params", + query = "query", + fragment = "fragment" +} +check_parse_url{ + url = "scheme://user:pass-wd@host:port/path;params?query#fragment", + scheme = "scheme", + authority = "user:pass-wd@host:port", + host = "host", + port = "port", + userinfo = "user:pass-wd", + password = "pass-wd", + user = "user", + path = "/path", + params = "params", + query = "query", + fragment = "fragment" +} +check_parse_url{ + url = "scheme://user:pass#wd@host:port/path;params?query#fragment", + scheme = "scheme", + authority = "user:pass#wd@host:port", + host = "host", + port = "port", + userinfo = "user:pass#wd", + password = "pass#wd", + user = "user", + path = "/path", + params = "params", + query = "query", + fragment = "fragment" +} +check_parse_url{ + url = "scheme://user:pass#wd@host:port/path;params?query", + scheme = "scheme", + authority = "user:pass#wd@host:port", + host = "host", + port = "port", + userinfo = "user:pass#wd", + password = "pass#wd", + user = "user", + path = "/path", + params = "params", + query = "query", +} check_parse_url{ url = "scheme://userinfo@host:port/path;params?query#fragment", scheme = "scheme", @@ -608,9 +677,9 @@ check_parse_path("eu/tu", { "eu", "tu" }) print("testing path protection") check_protect({ "eu", "-_.!~*'():@&=+$,", "tu" }, "eu/-_.!~*'():@&=+$,/tu") check_protect({ "eu ", "~diego" }, "eu%20/~diego") -check_protect({ "/eu>", "", "", "/ Date: Sun, 19 Aug 2018 11:32:42 -0600 Subject: url.lua:absolute_path(): fix issue #254, simplify, add more test cases --- src/url.lua | 26 ++++++++++++-------------- test/urltest.lua | 25 ++++++++++++++++++++++++- 2 files changed, 36 insertions(+), 15 deletions(-) (limited to 'src/url.lua') diff --git a/src/url.lua b/src/url.lua index 110ea94..29b6734 100644 --- a/src/url.lua +++ b/src/url.lua @@ -88,20 +88,18 @@ local function absolute_path(base_path, relative_path) if string.sub(relative_path, 1, 1) == "/" then return relative_path end local path = string.gsub(base_path, "[^/]*$", "") path = path .. relative_path - path = string.gsub(path, "([^/]*%./)", function (s) - if s ~= "./" then return s else return "" end - end) - path = string.gsub(path, "/%.$", "/") - local reduced - while reduced ~= path do - reduced = path - path = string.gsub(reduced, "([^/]*/%.%./)", function (s) - if s ~= "../../" then return "" else return s end - end) - end - path = string.gsub(reduced, "([^/]*/%.%.)$", function (s) - if s ~= "../.." then return "" else return s end - end) + repeat + local was = path + path = path:gsub('/%./', '/') + until path == was + repeat + local was = path + path = path:gsub('[^/]+/%.%./([^/]+)', '%1') + until path == was + path = path:gsub('[^/]+/%.%./*$', '') + path = path:gsub('/%.%.$', '/') + path = path:gsub('/%.$', '/') + path = path:gsub('^/%.%.', '') return path end diff --git a/test/urltest.lua b/test/urltest.lua index 1090a7e..63a33ea 100644 --- a/test/urltest.lua +++ b/test/urltest.lua @@ -61,7 +61,7 @@ end local check_absolute_url = function(base, relative, absolute) local res = socket.url.absolute(base, relative) if res ~= absolute then - io.write("absolute: In test for '", relative, "' expected '", + io.write("absolute: In test for base='", base, "', rel='", relative, "' expected '", absolute, "' but got '", res, "'\n") os.exit() end @@ -637,6 +637,17 @@ check_absolute_url("http://a/b/c/d;p?q#f", "g;x", "http://a/b/c/g;x") check_absolute_url("http://a/b/c/d;p?q#f", "g;x?y#s", "http://a/b/c/g;x?y#s") check_absolute_url("http://a/b/c/d;p?q#f", ".", "http://a/b/c/") check_absolute_url("http://a/b/c/d;p?q#f", "./", "http://a/b/c/") +check_absolute_url("http://a/b/c/d;p?q#f", "./g", "http://a/b/c/g") +check_absolute_url("http://a/b/c/d;p?q#f", "./g/", "http://a/b/c/g/") +check_absolute_url("http://a/b/c/d;p?q#f", "././g", "http://a/b/c/g") +check_absolute_url("http://a/b/c/d;p?q#f", "././g/", "http://a/b/c/g/") +check_absolute_url("http://a/b/c/d;p?q#f", "g/.", "http://a/b/c/g/") +check_absolute_url("http://a/b/c/d;p?q#f", "g/./", "http://a/b/c/g/") +check_absolute_url("http://a/b/c/d;p?q#f", "g/./.", "http://a/b/c/g/") +check_absolute_url("http://a/b/c/d;p?q#f", "g/././", "http://a/b/c/g/") +check_absolute_url("http://a/b/c/d;p?q#f", "./.", "http://a/b/c/") +check_absolute_url("http://a/b/c/d;p?q#f", "././.", "http://a/b/c/") +check_absolute_url("http://a/b/c/d;p?q#f", "././g/./.", "http://a/b/c/g/") check_absolute_url("http://a/b/c/d;p?q#f", "..", "http://a/b/") check_absolute_url("http://a/b/c/d;p?q#f", "../", "http://a/b/") check_absolute_url("http://a/b/c/d;p?q#f", "../g", "http://a/b/g") @@ -655,6 +666,17 @@ check_absolute_url("http://a/b/c/d;p?q#f", "./g/.", "http://a/b/c/g/") check_absolute_url("http://a/b/c/d;p?q#f", "g/./h", "http://a/b/c/g/h") check_absolute_url("http://a/b/c/d;p?q#f", "g/../h", "http://a/b/c/h") +check_absolute_url("http://a/b/c/d:p?q#f/", "../g/", "http://a/b/g/") +check_absolute_url("http://a/b/c/d:p?q#f/", "../g", "http://a/b/g") +check_absolute_url("http://a/b/c/d:p?q#f/", "../.g/", "http://a/b/.g/") +check_absolute_url("http://a/b/c/d:p?q#f/", "../.g", "http://a/b/.g") +check_absolute_url("http://a/b/c/d:p?q#f/", "../.g.h/", "http://a/b/.g.h/") +check_absolute_url("http://a/b/c/d:p?q#f/", "../.g.h", "http://a/b/.g.h") + +check_absolute_url("http://a/b/c/d:p?q#f/", "g.h/", "http://a/b/c/g.h/") +check_absolute_url("http://a/b/c/d:p?q#f/", "../g.h/", "http://a/b/g.h/") +check_absolute_url("http://a/", "../g.h/", "http://a/g.h/") + -- extra tests check_absolute_url("//a/b/c/d;p?q#f", "d/e/f", "//a/b/c/d/e/f") check_absolute_url("/a/b/c/d;p?q#f", "d/e/f", "/a/b/c/d/e/f") @@ -662,6 +684,7 @@ check_absolute_url("a/b/c/d", "d/e/f", "a/b/c/d/e/f") check_absolute_url("a/b/c/d/../", "d/e/f", "a/b/c/d/e/f") check_absolute_url("http://velox.telemar.com.br", "/dashboard/index.html", "http://velox.telemar.com.br/dashboard/index.html") +check_absolute_url("http://example.com/", "../.badhost.com/", "http://example.com/.badhost.com/") print("testing path parsing and composition") check_parse_path("/eu/tu/ele", { "eu", "tu", "ele"; is_absolute = 1 }) -- cgit v1.2.3-55-g6feb From c905b5d44f8cdfbc8110a9a7d1d62c08b5703ae3 Mon Sep 17 00:00:00 2001 From: "E. Westbrook" Date: Tue, 21 Aug 2018 08:03:51 -0600 Subject: url.lua: separate remove_dot_components() from absolute_path(); also use in _M.absolute() even when not merging --- src/url.lua | 51 ++++++++++++++++++++++++++++++++++----------------- test/urltest.lua | 8 ++++---- 2 files changed, 38 insertions(+), 21 deletions(-) (limited to 'src/url.lua') diff --git a/src/url.lua b/src/url.lua index 29b6734..a354ab5 100644 --- a/src/url.lua +++ b/src/url.lua @@ -77,24 +77,19 @@ function _M.unescape(s) end ----------------------------------------------------------------------------- --- Builds a path from a base path and a relative path +-- Removes '..' and '.' components appropriately from a path. -- Input --- base_path --- relative_path +-- path -- Returns --- corresponding absolute path ------------------------------------------------------------------------------ -local function absolute_path(base_path, relative_path) - if string.sub(relative_path, 1, 1) == "/" then return relative_path end - local path = string.gsub(base_path, "[^/]*$", "") - path = path .. relative_path +-- dot-normalized path +local function remove_dot_components(path) repeat - local was = path - path = path:gsub('/%./', '/') + local was = path + path = path:gsub('/%./', '/') until path == was repeat - local was = path - path = path:gsub('[^/]+/%.%./([^/]+)', '%1') + local was = path + path = path:gsub('[^/]+/%.%./([^/]+)', '%1') until path == was path = path:gsub('[^/]+/%.%./*$', '') path = path:gsub('/%.%.$', '/') @@ -103,6 +98,23 @@ local function absolute_path(base_path, relative_path) return path end +----------------------------------------------------------------------------- +-- Builds a path from a base path and a relative path +-- Input +-- base_path +-- relative_path +-- Returns +-- corresponding absolute path +----------------------------------------------------------------------------- +local function absolute_path(base_path, relative_path) + if string.sub(relative_path, 1, 1) == "/" then + return remove_dot_components(relative_path) end + base_path = base_path:gsub("[^/]*$", "") + local path = base_path .. relative_path + path = remove_dot_components(path) + return path +end + ----------------------------------------------------------------------------- -- Parses a url and returns a table with all its parts according to RFC 2396 -- The following grammar describes the names given to the URL parts @@ -225,10 +237,14 @@ function _M.absolute(base_url, relative_url) else base_parsed = _M.parse(base_url) end + local result local relative_parsed = _M.parse(relative_url) - if not base_parsed then return relative_url - elseif not relative_parsed then return base_url - elseif relative_parsed.scheme then return relative_url + if not base_parsed then + result = relative_url + elseif not relative_parsed then + result = base_url + elseif relative_parsed.scheme then + result = relative_url else relative_parsed.scheme = base_parsed.scheme if not relative_parsed.authority then @@ -246,8 +262,9 @@ function _M.absolute(base_url, relative_url) relative_parsed.path) end end - return _M.build(relative_parsed) + result = _M.build(relative_parsed) end + return remove_dot_components(result) end ----------------------------------------------------------------------------- diff --git a/test/urltest.lua b/test/urltest.lua index 63a33ea..649be88 100644 --- a/test/urltest.lua +++ b/test/urltest.lua @@ -627,10 +627,10 @@ check_absolute_url("http://a/b/c/d;p?q#f", "/g", "http://a/g") check_absolute_url("http://a/b/c/d;p?q#f", "//g", "http://g") check_absolute_url("http://a/b/c/d;p?q#f", "?y", "http://a/b/c/d;p?y") check_absolute_url("http://a/b/c/d;p?q#f", "g?y", "http://a/b/c/g?y") -check_absolute_url("http://a/b/c/d;p?q#f", "g?y/./x", "http://a/b/c/g?y/./x") +check_absolute_url("http://a/b/c/d;p?q#f", "g?y/./x", "http://a/b/c/g?y/x") check_absolute_url("http://a/b/c/d;p?q#f", "#s", "http://a/b/c/d;p?q#s") check_absolute_url("http://a/b/c/d;p?q#f", "g#s", "http://a/b/c/g#s") -check_absolute_url("http://a/b/c/d;p?q#f", "g#s/./x", "http://a/b/c/g#s/./x") +check_absolute_url("http://a/b/c/d;p?q#f", "g#s/./x", "http://a/b/c/g#s/x") check_absolute_url("http://a/b/c/d;p?q#f", "g?y#s", "http://a/b/c/g?y#s") check_absolute_url("http://a/b/c/d;p?q#f", ";x", "http://a/b/c/d;x") check_absolute_url("http://a/b/c/d;p?q#f", "g;x", "http://a/b/c/g;x") @@ -655,8 +655,8 @@ check_absolute_url("http://a/b/c/d;p?q#f", "../..", "http://a/") check_absolute_url("http://a/b/c/d;p?q#f", "../../", "http://a/") check_absolute_url("http://a/b/c/d;p?q#f", "../../g", "http://a/g") check_absolute_url("http://a/b/c/d;p?q#f", "", "http://a/b/c/d;p?q#f") -check_absolute_url("http://a/b/c/d;p?q#f", "/./g", "http://a/./g") -check_absolute_url("http://a/b/c/d;p?q#f", "/../g", "http://a/../g") +check_absolute_url("http://a/b/c/d;p?q#f", "/./g", "http://a/g") +check_absolute_url("http://a/b/c/d;p?q#f", "/../g", "http://a/g") check_absolute_url("http://a/b/c/d;p?q#f", "g.", "http://a/b/c/g.") check_absolute_url("http://a/b/c/d;p?q#f", ".g", "http://a/b/c/.g") check_absolute_url("http://a/b/c/d;p?q#f", "g..", "http://a/b/c/g..") -- cgit v1.2.3-55-g6feb From c570a32c219c957fd405ed018f2500f06952c043 Mon Sep 17 00:00:00 2001 From: "E. Westbrook" Date: Tue, 21 Aug 2018 09:07:42 -0600 Subject: url.lua:remove_dot_components(): limit beginning-of-string double-dot corner case to prevent triple-dot activation and authority collision --- src/url.lua | 2 +- test/urltest.lua | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) (limited to 'src/url.lua') diff --git a/src/url.lua b/src/url.lua index a354ab5..0d88adb 100644 --- a/src/url.lua +++ b/src/url.lua @@ -94,7 +94,7 @@ local function remove_dot_components(path) path = path:gsub('[^/]+/%.%./*$', '') path = path:gsub('/%.%.$', '/') path = path:gsub('/%.$', '/') - path = path:gsub('^/%.%.', '') + path = path:gsub('^/%.%./', '/') return path end diff --git a/test/urltest.lua b/test/urltest.lua index 649be88..8664fa6 100644 --- a/test/urltest.lua +++ b/test/urltest.lua @@ -685,6 +685,7 @@ check_absolute_url("a/b/c/d/../", "d/e/f", "a/b/c/d/e/f") check_absolute_url("http://velox.telemar.com.br", "/dashboard/index.html", "http://velox.telemar.com.br/dashboard/index.html") check_absolute_url("http://example.com/", "../.badhost.com/", "http://example.com/.badhost.com/") +check_absolute_url("http://example.com/", "...badhost.com/", "http://example.com/...badhost.com/") print("testing path parsing and composition") check_parse_path("/eu/tu/ele", { "eu", "tu", "ele"; is_absolute = 1 }) -- cgit v1.2.3-55-g6feb From 7ccea58776b8084f29a48610cb44b17ca604e4b5 Mon Sep 17 00:00:00 2001 From: "E. Westbrook" Date: Tue, 21 Aug 2018 09:59:45 -0600 Subject: url.lua:remove_dot_components(): avoid overconsuming dot segments --- src/url.lua | 4 ++-- test/urltest.lua | 5 +++++ 2 files changed, 7 insertions(+), 2 deletions(-) (limited to 'src/url.lua') diff --git a/src/url.lua b/src/url.lua index 0d88adb..e1fa2bc 100644 --- a/src/url.lua +++ b/src/url.lua @@ -85,11 +85,11 @@ end local function remove_dot_components(path) repeat local was = path - path = path:gsub('/%./', '/') + path = path:gsub('/%./', '/', 1) until path == was repeat local was = path - path = path:gsub('[^/]+/%.%./([^/]+)', '%1') + path = path:gsub('[^/]+/%.%./([^/]+)', '%1', 1) until path == was path = path:gsub('[^/]+/%.%./*$', '') path = path:gsub('/%.%.$', '/') diff --git a/test/urltest.lua b/test/urltest.lua index 8664fa6..04b3c7f 100644 --- a/test/urltest.lua +++ b/test/urltest.lua @@ -654,6 +654,7 @@ check_absolute_url("http://a/b/c/d;p?q#f", "../g", "http://a/b/g") check_absolute_url("http://a/b/c/d;p?q#f", "../..", "http://a/") check_absolute_url("http://a/b/c/d;p?q#f", "../../", "http://a/") check_absolute_url("http://a/b/c/d;p?q#f", "../../g", "http://a/g") +check_absolute_url("http://a/b/c/d;p?q#f", "../../../g", "http://a/g") check_absolute_url("http://a/b/c/d;p?q#f", "", "http://a/b/c/d;p?q#f") check_absolute_url("http://a/b/c/d;p?q#f", "/./g", "http://a/g") check_absolute_url("http://a/b/c/d;p?q#f", "/../g", "http://a/g") @@ -686,6 +687,10 @@ check_absolute_url("http://velox.telemar.com.br", "/dashboard/index.html", "http://velox.telemar.com.br/dashboard/index.html") check_absolute_url("http://example.com/", "../.badhost.com/", "http://example.com/.badhost.com/") check_absolute_url("http://example.com/", "...badhost.com/", "http://example.com/...badhost.com/") +check_absolute_url("http://example.com/a/b/c/d/", "../q", "http://example.com/a/b/c/q") +check_absolute_url("http://example.com/a/b/c/d/", "../../q", "http://example.com/a/b/q") +check_absolute_url("http://example.com/a/b/c/d/", "../../../q", "http://example.com/a/q") +check_absolute_url("http://example.com/a/b/c/d/", "../../../../q", "http://example.com/q") print("testing path parsing and composition") check_parse_path("/eu/tu/ele", { "eu", "tu", "ele"; is_absolute = 1 }) -- cgit v1.2.3-55-g6feb From 5b862e6a3c79b8e336a0ac2f0d23ca69993b326d Mon Sep 17 00:00:00 2001 From: "E. Westbrook" Date: Tue, 21 Aug 2018 10:43:04 -0600 Subject: url.lua:absolute_path(): ensure a separator between base_path and relative_path --- src/url.lua | 1 + test/urltest.lua | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) (limited to 'src/url.lua') diff --git a/src/url.lua b/src/url.lua index e1fa2bc..243ee1c 100644 --- a/src/url.lua +++ b/src/url.lua @@ -110,6 +110,7 @@ local function absolute_path(base_path, relative_path) if string.sub(relative_path, 1, 1) == "/" then return remove_dot_components(relative_path) end base_path = base_path:gsub("[^/]*$", "") + if not base_path:find'/$' then base_path = base_path .. '/' end local path = base_path .. relative_path path = remove_dot_components(path) return path diff --git a/test/urltest.lua b/test/urltest.lua index 04b3c7f..b6ee299 100644 --- a/test/urltest.lua +++ b/test/urltest.lua @@ -690,7 +690,7 @@ check_absolute_url("http://example.com/", "...badhost.com/", "http://example.com check_absolute_url("http://example.com/a/b/c/d/", "../q", "http://example.com/a/b/c/q") check_absolute_url("http://example.com/a/b/c/d/", "../../q", "http://example.com/a/b/q") check_absolute_url("http://example.com/a/b/c/d/", "../../../q", "http://example.com/a/q") -check_absolute_url("http://example.com/a/b/c/d/", "../../../../q", "http://example.com/q") +check_absolute_url("http://example.com", ".badhost.com", "http://example.com/.badhost.com") print("testing path parsing and composition") check_parse_path("/eu/tu/ele", { "eu", "tu", "ele"; is_absolute = 1 }) -- cgit v1.2.3-55-g6feb From 38d936ec0ea05da9f85a5c582e5073e0d1b82209 Mon Sep 17 00:00:00 2001 From: "E. Westbrook" Date: Tue, 21 Aug 2018 11:27:42 -0600 Subject: url.lua:remove_dot_components(): empty path component double-dot corner case --- src/url.lua | 4 ++++ test/urltest.lua | 1 + 2 files changed, 5 insertions(+) (limited to 'src/url.lua') diff --git a/src/url.lua b/src/url.lua index 243ee1c..10a9d90 100644 --- a/src/url.lua +++ b/src/url.lua @@ -87,6 +87,10 @@ local function remove_dot_components(path) local was = path path = path:gsub('/%./', '/', 1) until path == was + repeat + local was = path + path = path:gsub('//%.%./([^/]+)', '/%1', 1) + until path == was repeat local was = path path = path:gsub('[^/]+/%.%./([^/]+)', '%1', 1) diff --git a/test/urltest.lua b/test/urltest.lua index b6ee299..13deb10 100644 --- a/test/urltest.lua +++ b/test/urltest.lua @@ -691,6 +691,7 @@ check_absolute_url("http://example.com/a/b/c/d/", "../q", "http://example.com/a/ check_absolute_url("http://example.com/a/b/c/d/", "../../q", "http://example.com/a/b/q") check_absolute_url("http://example.com/a/b/c/d/", "../../../q", "http://example.com/a/q") check_absolute_url("http://example.com", ".badhost.com", "http://example.com/.badhost.com") +check_absolute_url("http://example.com/a/b/c/d/", "..//../../../q", "http://example.com/a/q") print("testing path parsing and composition") check_parse_path("/eu/tu/ele", { "eu", "tu", "ele"; is_absolute = 1 }) -- cgit v1.2.3-55-g6feb From ca5398be098b571912dcbd93c83ab78151814f99 Mon Sep 17 00:00:00 2001 From: "E. Westbrook" Date: Tue, 21 Aug 2018 12:02:25 -0600 Subject: url.lua:remove_dot_components(): use temporary NUL marker to reduce empty-segment special-case code --- src/url.lua | 5 +++-- test/urltest.lua | 4 ++++ 2 files changed, 7 insertions(+), 2 deletions(-) (limited to 'src/url.lua') diff --git a/src/url.lua b/src/url.lua index 10a9d90..466d4fa 100644 --- a/src/url.lua +++ b/src/url.lua @@ -85,11 +85,11 @@ end local function remove_dot_components(path) repeat local was = path - path = path:gsub('/%./', '/', 1) + path = path:gsub('//', '/'..0x00..'/', 1) until path == was repeat local was = path - path = path:gsub('//%.%./([^/]+)', '/%1', 1) + path = path:gsub('/%./', '/', 1) until path == was repeat local was = path @@ -99,6 +99,7 @@ local function remove_dot_components(path) path = path:gsub('/%.%.$', '/') path = path:gsub('/%.$', '/') path = path:gsub('^/%.%./', '/') + path = path:gsub(0x00, '') return path end diff --git a/test/urltest.lua b/test/urltest.lua index 13deb10..ae8ba75 100644 --- a/test/urltest.lua +++ b/test/urltest.lua @@ -692,6 +692,10 @@ check_absolute_url("http://example.com/a/b/c/d/", "../../q", "http://example.com check_absolute_url("http://example.com/a/b/c/d/", "../../../q", "http://example.com/a/q") check_absolute_url("http://example.com", ".badhost.com", "http://example.com/.badhost.com") check_absolute_url("http://example.com/a/b/c/d/", "..//../../../q", "http://example.com/a/q") +check_absolute_url("http://example.com/a/b/c/d/", "..//a/../../../../q", "http://example.com/a/q") +check_absolute_url("http://example.com/a/b/c/d/", "..//a/..//../../../q", "http://example.com/a/b/q") +check_absolute_url("http://example.com/a/b/c/d/", "..//a/..///../../../../q", "http://example.com/a/b/q") +check_absolute_url("http://example.com/a/b/c/d/", "../x/a/../y/z/../../../../q", "http://example.com/a/b/q") print("testing path parsing and composition") check_parse_path("/eu/tu/ele", { "eu", "tu", "ele"; is_absolute = 1 }) -- cgit v1.2.3-55-g6feb From 043e99771352aff47680b99f09b66a32f0cc3ef5 Mon Sep 17 00:00:00 2001 From: "E. Westbrook" Date: Tue, 21 Aug 2018 12:42:26 -0600 Subject: url.lua:remove_dot_components(): avoid ambiguous numeric representation as empty-path-segment marker --- src/url.lua | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'src/url.lua') diff --git a/src/url.lua b/src/url.lua index 466d4fa..0a3a80a 100644 --- a/src/url.lua +++ b/src/url.lua @@ -83,9 +83,10 @@ end -- Returns -- dot-normalized path local function remove_dot_components(path) + local marker = string.char(1) repeat local was = path - path = path:gsub('//', '/'..0x00..'/', 1) + path = path:gsub('//', '/'..marker..'/', 1) until path == was repeat local was = path @@ -99,7 +100,7 @@ local function remove_dot_components(path) path = path:gsub('/%.%.$', '/') path = path:gsub('/%.$', '/') path = path:gsub('^/%.%./', '/') - path = path:gsub(0x00, '') + path = path:gsub(marker, '') return path end -- cgit v1.2.3-55-g6feb From 601ad8d59f11d7180015d0ecfb9d0a8d67f6f5c1 Mon Sep 17 00:00:00 2001 From: Thijs Schreijer Date: Fri, 18 Mar 2022 12:12:39 +0100 Subject: refactor: Address issues raised by linter --- etc/cookie.lua | 22 ++++----- etc/get.lua | 2 +- gem/ex11.lua | 4 +- gem/ex3.lua | 2 +- gem/ex4.lua | 2 +- samples/cddb.lua | 2 +- src/ftp.lua | 14 +++--- src/http.lua | 11 ++--- src/ltn12.lua | 3 +- src/mbox.lua | 13 +++--- src/mime.lua | 12 +---- src/url.lua | 6 +-- test/ltn12test.lua | 24 +++++----- test/mimetest.lua | 46 +++++++++--------- test/smtptest.lua | 20 ++++---- test/test_socket_error.lua | 2 +- test/testmesg.lua | 14 +++--- test/testsupport.lua | 2 +- test/urltest.lua | 114 ++++++++++++++++++++++----------------------- test/utestclnt.lua | 68 +++++++++++++-------------- test/utestsrvr.lua | 2 +- 21 files changed, 187 insertions(+), 198 deletions(-) (limited to 'src/url.lua') diff --git a/etc/cookie.lua b/etc/cookie.lua index 4adb403..fec10a1 100644 --- a/etc/cookie.lua +++ b/etc/cookie.lua @@ -5,7 +5,7 @@ local ltn12 = require"ltn12" local token_class = '[^%c%s%(%)%<%>%@%,%;%:%\\%"%/%[%]%?%=%{%}]' -local function unquote(t, quoted) +local function unquote(t, quoted) local n = string.match(t, "%$(%d+)$") if n then n = tonumber(n) end if quoted[n] then return quoted[n] @@ -14,19 +14,19 @@ end local function parse_set_cookie(c, quoted, cookie_table) c = c .. ";$last=last;" - local _, __, n, v, i = string.find(c, "(" .. token_class .. + local _, _, n, v, i = string.find(c, "(" .. token_class .. "+)%s*=%s*(.-)%s*;%s*()") local cookie = { - name = n, - value = unquote(v, quoted), + name = n, + value = unquote(v, quoted), attributes = {} } while 1 do - _, __, n, v, i = string.find(c, "(" .. token_class .. + _, _, n, v, i = string.find(c, "(" .. token_class .. "+)%s*=?%s*(.-)%s*;%s*()", i) if not n or n == "$last" then break end cookie.attributes[#cookie.attributes+1] = { - name = n, + name = n, value = unquote(v, quoted) } end @@ -46,8 +46,8 @@ local function split_set_cookie(s, cookie_table) -- split into individual cookies i = 1 while 1 do - local _, __, cookie, next_token - _, __, cookie, i, next_token = string.find(s, "(.-)%s*%,%s*()(" .. + local _, _, cookie, next_token + _, _, cookie, i, next_token = string.find(s, "(.-)%s*%,%s*()(" .. token_class .. "+)%s*=", i) if not next_token then break end parse_set_cookie(cookie, quoted, cookie_table) @@ -62,12 +62,12 @@ local function quote(s) end local _empty = {} -local function build_cookies(cookies) +local function build_cookies(cookies) s = "" for i,v in ipairs(cookies or _empty) do if v.name then s = s .. v.name - if v.value and v.value ~= "" then + if v.value and v.value ~= "" then s = s .. '=' .. quote(v.value) end end @@ -83,6 +83,6 @@ local function build_cookies(cookies) end if i < #cookies then s = s .. ", " end end - return s + return s end diff --git a/etc/get.lua b/etc/get.lua index 9edc235..d53c465 100644 --- a/etc/get.lua +++ b/etc/get.lua @@ -71,7 +71,7 @@ function stats(size) local current = socket.gettime() if chunk then -- total bytes received - got = got + string.len(chunk) + got = got + string.len(chunk) -- not enough time for estimate if current - last > 1 then io.stderr:write("\r", gauge(got, current - start, size)) diff --git a/gem/ex11.lua b/gem/ex11.lua index 1cbf01f..79c99af 100644 --- a/gem/ex11.lua +++ b/gem/ex11.lua @@ -1,7 +1,7 @@ local input = source.chain( - source.file(io.open("input.bin", "rb")), + source.file(io.open("input.bin", "rb")), encode("base64")) local output = sink.chain( - wrap(76), + wrap(76), sink.file(io.open("output.b64", "w"))) pump.all(input, output) diff --git a/gem/ex3.lua b/gem/ex3.lua index a43fefa..60b4423 100644 --- a/gem/ex3.lua +++ b/gem/ex3.lua @@ -7,7 +7,7 @@ local function chainpair(f1, f2) end function filter.chain(...) - local f = select(1, ...) + local f = select(1, ...) for i = 2, select('#', ...) do f = chainpair(f, select(i, ...)) end diff --git a/gem/ex4.lua b/gem/ex4.lua index c670e0e..c48b77e 100644 --- a/gem/ex4.lua +++ b/gem/ex4.lua @@ -1,4 +1,4 @@ -local qp = filter.chain(normalize(CRLF), encode("quoted-printable"), +local qp = filter.chain(normalize(CRLF), encode("quoted-printable"), wrap("quoted-printable")) local input = source.chain(source.file(io.stdin), qp) local output = sink.file(io.stdout) diff --git a/samples/cddb.lua b/samples/cddb.lua index 49a1871..59d5a44 100644 --- a/samples/cddb.lua +++ b/samples/cddb.lua @@ -26,7 +26,7 @@ function parse(body) data[key] = value end end - return data, code, message + return data, code, message end local host = socket.dns.gethostname() diff --git a/src/ftp.lua b/src/ftp.lua index bd528ca..0ebc508 100644 --- a/src/ftp.lua +++ b/src/ftp.lua @@ -56,7 +56,7 @@ end function metat.__index:login(user, password) self.try(self.tp:command("user", user or _M.USER)) - local code, reply = self.try(self.tp:check{"2..", 331}) + local code, _ = self.try(self.tp:check{"2..", 331}) if code == 331 then self.try(self.tp:command("pass", password or _M.PASSWORD)) self.try(self.tp:check("2..")) @@ -66,7 +66,7 @@ end function metat.__index:pasv() self.try(self.tp:command("pasv")) - local code, reply = self.try(self.tp:check("2..")) + local _, reply = self.try(self.tp:check("2..")) local pattern = "(%d+)%D(%d+)%D(%d+)%D(%d+)%D(%d+)%D(%d+)" local a, b, c, d, p1, p2 = socket.skip(2, string.find(reply, pattern)) self.try(a and b and c and d and p1 and p2, reply) @@ -83,9 +83,9 @@ end function metat.__index:epsv() self.try(self.tp:command("epsv")) - local code, reply = self.try(self.tp:check("229")) + local _, reply = self.try(self.tp:check("229")) local pattern = "%((.)(.-)%1(.-)%1(.-)%1%)" - local d, prt, address, port = string.match(reply, pattern) + local _, _, _, port = string.match(reply, pattern) self.try(port, "invalid epsv response") self.pasvt = { address = self.tp:getpeername(), @@ -102,7 +102,7 @@ end function metat.__index:port(address, port) self.pasvt = nil if not address then - address, port = self.try(self.tp:getsockname()) + address = self.try(self.tp:getsockname()) self.server = self.try(socket.bind(address, 0)) address, port = self.try(self.server:getsockname()) self.try(self.server:settimeout(_M.TIMEOUT)) @@ -118,7 +118,7 @@ end function metat.__index:eprt(family, address, port) self.pasvt = nil if not address then - address, port = self.try(self.tp:getsockname()) + address = self.try(self.tp:getsockname()) self.server = self.try(socket.bind(address, 0)) address, port = self.try(self.server:getsockname()) self.try(self.server:settimeout(_M.TIMEOUT)) @@ -142,7 +142,7 @@ function metat.__index:send(sendt) local command = sendt.command or "stor" -- send the transfer command and check the reply self.try(self.tp:command(command, argument)) - local code, reply = self.try(self.tp:check{"2..", "1.."}) + local code, _ = self.try(self.tp:check{"2..", "1.."}) -- if there is not a pasvt table, then there is a server -- and we already sent a PORT command if not self.pasvt then self:portconnect() end diff --git a/src/http.lua b/src/http.lua index 6a3416e..e3a1742 100644 --- a/src/http.lua +++ b/src/http.lua @@ -41,9 +41,6 @@ local SCHEMES = { https.tcp, 'LuaSocket: Function tcp() not available from LuaSec') return tcp(t) end }} --- default scheme and port for document retrieval -local SCHEME = 'http' -local PORT = SCHEMES[SCHEME].port ----------------------------------------------------------------------------- -- Reads MIME headers from a connection, unfolding where needed ----------------------------------------------------------------------------- @@ -92,7 +89,7 @@ socket.sourcet["http-chunked"] = function(sock, headers) -- was it the last chunk? if size > 0 then -- if not, get chunk and skip terminating CRLF - local chunk, err, part = sock:receive(size) + local chunk, err, _ = sock:receive(size) if chunk then sock:receive() end return chunk, err else @@ -166,8 +163,8 @@ function metat.__index:receivestatusline() if status ~= "HTTP/" then if ec == "timeout" then return 408 - end - return nil, status + end + return nil, status end -- otherwise proceed reading a status line status = self.try(self.c:receive("*l", status)) @@ -366,7 +363,7 @@ end local headers -- ignore any 100-continue messages while code == 100 do - headers = h:receiveheaders() + h:receiveheaders() code, status = h:receivestatusline() end headers = h:receiveheaders() diff --git a/src/ltn12.lua b/src/ltn12.lua index afa735d..f1e05e1 100644 --- a/src/ltn12.lua +++ b/src/ltn12.lua @@ -13,7 +13,7 @@ local unpack = unpack or table.unpack local base = _G local _M = {} if module then -- heuristic for exporting a global package table - ltn12 = _M + ltn12 = _M -- luacheck: ignore end local filter,source,sink,pump = {},{},{},{} @@ -23,7 +23,6 @@ _M.sink = sink _M.pump = pump local unpack = unpack or table.unpack -local select = base.select -- 2048 seems to be better in windows... _M.BLOCKSIZE = 2048 diff --git a/src/mbox.lua b/src/mbox.lua index ed9e781..12823b0 100644 --- a/src/mbox.lua +++ b/src/mbox.lua @@ -1,8 +1,8 @@ local _M = {} if module then - mbox = _M -end + mbox = _M -- luacheck: ignore +end function _M.split_message(message_s) local message = {} @@ -29,7 +29,7 @@ end function _M.parse_header(header_s) header_s = string.gsub(header_s, "\n[ ]+", " ") header_s = string.gsub(header_s, "\n+", "") - local _, __, name, value = string.find(header_s, "([^%s:]-):%s*(.*)") + local _, _, name, value = string.find(header_s, "([^%s:]-):%s*(.*)") return name, value end @@ -49,9 +49,9 @@ function _M.parse_headers(headers_s) end function _M.parse_from(from) - local _, __, name, address = string.find(from, "^%s*(.-)%s*%<(.-)%>") + local _, _, name, address = string.find(from, "^%s*(.-)%s*%<(.-)%>") if not address then - _, __, address = string.find(from, "%s*(.+)%s*") + _, _, address = string.find(from, "%s*(.+)%s*") end name = name or "" address = address or "" @@ -63,7 +63,8 @@ end function _M.split_mbox(mbox_s) local mbox = {} mbox_s = string.gsub(mbox_s, "\r\n", "\n") .."\n\nFrom \n" - local nj, i, j = 1, 1, 1 + local nj, i + local j = 1 while 1 do i, nj = string.find(mbox_s, "\n\nFrom .-\n", j) if not i then break end diff --git a/src/mime.lua b/src/mime.lua index d3abac5..93539de 100644 --- a/src/mime.lua +++ b/src/mime.lua @@ -10,7 +10,6 @@ local base = _G local ltn12 = require("ltn12") local mime = require("mime.core") -local string = require("string") local _M = mime -- encode, decode and wrap algorithm tables @@ -18,7 +17,7 @@ local encodet, decodet, wrapt = {},{},{} _M.encodet = encodet _M.decodet = decodet -_M.wrapt = wrapt +_M.wrapt = wrapt -- creates a function that chooses a filter by name from a given table local function choose(table) @@ -27,7 +26,7 @@ local function choose(table) name, opt1, opt2 = "default", name, opt1 end local f = table[name or "nil"] - if not f then + if not f then base.error("unknown key (" .. base.tostring(name) .. ")", 3) else return f(opt1, opt2) end end @@ -52,13 +51,6 @@ decodet['quoted-printable'] = function() return ltn12.filter.cycle(_M.unqp, "") end -local function format(chunk) - if chunk then - if chunk == "" then return "''" - else return string.len(chunk) end - else return "nil" end -end - -- define the line-wrap filters wrapt['text'] = function(length) length = length or 76 diff --git a/src/url.lua b/src/url.lua index 0a3a80a..8e0dc5c 100644 --- a/src/url.lua +++ b/src/url.lua @@ -179,9 +179,9 @@ function _M.parse(url, default) function(u) parsed.userinfo = u; return "" end) authority = string.gsub(authority, ":([^:%]]*)$", function(p) parsed.port = p; return "" end) - if authority ~= "" then + if authority ~= "" then -- IPv6? - parsed.host = string.match(authority, "^%[(.+)%]$") or authority + parsed.host = string.match(authority, "^%[(.+)%]$") or authority end local userinfo = parsed.userinfo if not userinfo then return parsed end @@ -264,7 +264,7 @@ function _M.absolute(base_url, relative_url) relative_parsed.query = base_parsed.query end end - else + else relative_parsed.path = absolute_path(base_parsed.path or "", relative_parsed.path) end diff --git a/test/ltn12test.lua b/test/ltn12test.lua index e7d368d..0cafbc9 100644 --- a/test/ltn12test.lua +++ b/test/ltn12test.lua @@ -38,7 +38,7 @@ local function named(f, name) end -------------------------------- -local function split(size) +local function split(size) local buffer = "" local last_out = "" local last_in = "" @@ -50,12 +50,12 @@ local function split(size) return last_out end return function(chunk, done) - if done then - return not last_in and not last_out + if done then + return not last_in and not last_out end -- check if argument is consistent with state if not chunk then - if last_in and last_in ~= "" and last_out ~= "" then + if last_in and last_in ~= "" and last_out ~= "" then error("nil chunk following data chunk", 2) end if not last_out then error("extra nil chunk", 2) end @@ -67,8 +67,8 @@ local function split(size) return output(chunk) else if not last_in then error("data chunk following nil chunk", 2) end - if last_in ~= "" and last_out ~= "" then - error("data chunk following data chunk", 2) + if last_in ~= "" and last_out ~= "" then + error("data chunk following data chunk", 2) end buffer = chunk return output(chunk) @@ -85,7 +85,7 @@ local function format(chunk) end -------------------------------- -local function merge(size) +local function merge(size) local buffer = "" local last_out = "" local last_in = "" @@ -102,12 +102,12 @@ local function merge(size) return last_out end return function(chunk, done) - if done then - return not last_in and not last_out + if done then + return not last_in and not last_out end -- check if argument is consistent with state if not chunk then - if last_in and last_in ~= "" and last_out ~= "" then + if last_in and last_in ~= "" and last_out ~= "" then error("nil chunk following data chunk", 2) end if not last_out then error("extra nil chunk", 2) end @@ -119,8 +119,8 @@ local function merge(size) return output(chunk) else if not last_in then error("data chunk following nil chunk", 2) end - if last_in ~= "" and last_out ~= "" then - error("data chunk following data chunk", 2) + if last_in ~= "" and last_out ~= "" then + error("data chunk following data chunk", 2) end buffer = buffer .. chunk return output(chunk) diff --git a/test/mimetest.lua b/test/mimetest.lua index f5b3747..a3c89ac 100644 --- a/test/mimetest.lua +++ b/test/mimetest.lua @@ -15,27 +15,27 @@ local eb64test = "b64test.bin2" local db64test = "b64test.bin3" --- from Machado de Assis, "A Mão e a Rosa" +-- from Machado de Assis, "A M�o e a Rosa" local mao = [[ - Cursavam estes dois moços a academia de S. Paulo, estando - Luís Alves no quarto ano e Estêvão no terceiro. - Conheceram-se na academia, e ficaram amigos íntimos, tanto - quanto podiam sê-lo dois espíritos diferentes, ou talvez por - isso mesmo que o eram. Estêvão, dotado de extrema - sensibilidade, e não menor fraqueza de ânimo, afetuoso e - bom, não daquela bondade varonil, que é apanágio de uma alma - forte, mas dessa outra bondade mole e de cera, que vai à - mercê de todas as circunstâncias, tinha, além de tudo isso, - o infortúnio de trazer ainda sobre o nariz os óculos - cor-de-rosa de suas virginais ilusões. Luís Alves via bem - com os olhos da cara. Não era mau rapaz, mas tinha o seu - grão de egoísmo, e se não era incapaz de afeições, sabia - regê-las, moderá-las, e sobretudo guiá-las ao seu próprio + Cursavam estes dois mo�os a academia de S. Paulo, estando + Lu�s Alves no quarto ano e Est�v�o no terceiro. + Conheceram-se na academia, e ficaram amigos �ntimos, tanto + quanto podiam s�-lo dois esp�ritos diferentes, ou talvez por + isso mesmo que o eram. Est�v�o, dotado de extrema + sensibilidade, e n�o menor fraqueza de �nimo, afetuoso e + bom, n�o daquela bondade varonil, que � apan�gio de uma alma + forte, mas dessa outra bondade mole e de cera, que vai � + merc� de todas as circunst�ncias, tinha, al�m de tudo isso, + o infort�nio de trazer ainda sobre o nariz os �culos + cor-de-rosa de suas virginais ilus�es. Lu�s Alves via bem + com os olhos da cara. N�o era mau rapaz, mas tinha o seu + gr�o de ego�smo, e se n�o era incapaz de afei��es, sabia + reg�-las, moder�-las, e sobretudo gui�-las ao seu pr�prio interesse. Entre estes dois homens travara-se amizade - íntima, nascida para um na simpatia, para outro no costume. + �ntima, nascida para um na simpatia, para outro no costume. Eram eles os naturais confidentes um do outro, com a - diferença que Luís Alves dava menos do que recebia, e, ainda - assim, nem tudo o que dava exprimia grande confiança. + diferen�a que Lu�s Alves dava menos do que recebia, e, ainda + assim, nem tudo o que dava exprimia grande confian�a. ]] local function random(handle, io_err) @@ -44,8 +44,8 @@ local function random(handle, io_err) if not handle then error("source is empty!", 2) end local len = math.random(0, 1024) local chunk = handle:read(len) - if not chunk then - handle:close() + if not chunk then + handle:close() handle = nil end return chunk @@ -62,7 +62,7 @@ local what = nil local function transform(input, output, filter) local source = random(io.open(input, "rb")) local sink = ltn12.sink.file(io.open(output, "wb")) - if what then + if what then sink = ltn12.sink.chain(filter, sink) else source = ltn12.source.chain(source, filter) @@ -147,7 +147,7 @@ local function create_qptest() f:write(' ',string.char(32)) end f:write("\r\n") - + f:close() end @@ -157,7 +157,7 @@ local function cleanup_qptest() os.remove(dqptest) end --- create test file +-- create test file local function create_b64test() local f = assert(io.open(b64test, "wb")) local t = {} diff --git a/test/smtptest.lua b/test/smtptest.lua index b5380ff..9d06054 100644 --- a/test/smtptest.lua +++ b/test/smtptest.lua @@ -27,8 +27,8 @@ local total = function() end local similar = function(s1, s2) - return - string.lower(string.gsub(s1, "%s", "")) == + return + string.lower(string.gsub(s1, "%s", "")) == string.lower(string.gsub(s2, "%s", "")) end @@ -40,9 +40,9 @@ end local readfile = function(name) local f = io.open(name, "r") - if not f then + if not f then fail("unable to open file!") - return nil + return nil end local s = f:read("*a") f:close() @@ -52,7 +52,7 @@ end local empty = function() for i,v in ipairs(files) do local f = io.open(v, "w") - if not f then + if not f then fail("unable to open file!") end f:close() @@ -116,8 +116,8 @@ local wait = function(sentinel, n) while 1 do local mbox = parse(get()) if n == #mbox then break end - if socket.time() - sentinel.time > 50 then - to = 1 + if socket.time() - sentinel.time > 50 then + to = 1 break end socket.sleep(1) @@ -132,7 +132,7 @@ local stuffed_body = [[ This message body needs to be stuffed because it has a dot . -by itself on a line. +by itself on a line. Otherwise the mailer would think that the dot . @@ -219,7 +219,7 @@ else print("ok") end io.write("testing invalid from: ") local ret, err = socket.smtp.mail{ - from = ' " " (( _ * ', + from = ' " " (( _ * ', rcpt = rcpt, } if ret or not err then fail("wrong error message") @@ -227,7 +227,7 @@ else print(err) end io.write("testing no rcpt: ") local ret, err = socket.smtp.mail{ - from = from, + from = from, } if ret or not err then fail("wrong error message") else print(err) end diff --git a/test/test_socket_error.lua b/test/test_socket_error.lua index bda6408..1b4b601 100644 --- a/test/test_socket_error.lua +++ b/test/test_socket_error.lua @@ -19,7 +19,7 @@ for i = 1, 10 do assert(ss == sock) else assert('timeout' == err, 'unexpected error :' .. tostring(err)) - end + end err = sock:getoption("error") -- i get 'connection refused' on WinXP if err then print("Passed! Error is '" .. err .. "'.") diff --git a/test/testmesg.lua b/test/testmesg.lua index 135a008..8c086d5 100644 --- a/test/testmesg.lua +++ b/test/testmesg.lua @@ -34,11 +34,11 @@ r, e = smtp.send{ print(r, e) --- creates a source to send a message with two parts. The first part is +-- creates a source to send a message with two parts. The first part is -- plain text, the second part is a PNG image, encoded as base64. source = smtp.message{ headers = { - -- Remember that headers are *ignored* by smtp.send. + -- Remember that headers are *ignored* by smtp.send. from = "Sicrano ", to = "Fulano ", subject = "Here is a message with attachments" @@ -49,18 +49,18 @@ source = smtp.message{ "Preamble might show up even in a MIME enabled client.", -- first part: No headers means plain text, us-ascii. -- The mime.eol low-level filter normalizes end-of-line markers. - [1] = { + [1] = { body = mime.eol(0, [[ - Lines in a message body should always end with CRLF. + Lines in a message body should always end with CRLF. The smtp module will *NOT* perform translation. It will perform necessary stuffing, though. ]]) }, - -- second part: Headers describe content the to be an image, + -- second part: Headers describe content the to be an image, -- sent under the base64 transfer content encoding. - -- Notice that nothing happens until the message is sent. Small + -- Notice that nothing happens until the message is sent. Small -- chunks are loaded into memory and translation happens on the fly. - [2] = { + [2] = { headers = { ["ConTenT-tYpE"] = 'image/png; name="luasocket.png"', ["content-disposition"] = 'attachment; filename="luasocket.png"', diff --git a/test/testsupport.lua b/test/testsupport.lua index b986088..4360b6b 100644 --- a/test/testsupport.lua +++ b/test/testsupport.lua @@ -7,7 +7,7 @@ function readfile(name) end function similar(s1, s2) - return string.lower(string.gsub(s1 or "", "%s", "")) == + return string.lower(string.gsub(s1 or "", "%s", "")) == string.lower(string.gsub(s2 or "", "%s", "")) end diff --git a/test/urltest.lua b/test/urltest.lua index ae8ba75..9a3c470 100644 --- a/test/urltest.lua +++ b/test/urltest.lua @@ -60,8 +60,8 @@ end local check_absolute_url = function(base, relative, absolute) local res = socket.url.absolute(base, relative) - if res ~= absolute then - io.write("absolute: In test for base='", base, "', rel='", relative, "' expected '", + if res ~= absolute then + io.write("absolute: In test for base='", base, "', rel='", relative, "' expected '", absolute, "' but got '", res, "'\n") os.exit() end @@ -73,7 +73,7 @@ local check_parse_url = function(gaba) local parsed = socket.url.parse(url) for i, v in pairs(gaba) do if v ~= parsed[i] then - io.write("parse: In test for '", url, "' expected ", i, " = '", + io.write("parse: In test for '", url, "' expected ", i, " = '", v, "' but got '", tostring(parsed[i]), "'\n") for i,v in pairs(parsed) do print(i,v) end os.exit() @@ -81,7 +81,7 @@ local check_parse_url = function(gaba) end for i, v in pairs(parsed) do if v ~= gaba[i] then - io.write("parse: In test for '", url, "' expected ", i, " = '", + io.write("parse: In test for '", url, "' expected ", i, " = '", tostring(gaba[i]), "' but got '", v, "'\n") for i,v in pairs(parsed) do print(i,v) end os.exit() @@ -92,8 +92,8 @@ end print("testing URL parsing") check_parse_url{ url = "scheme://user:pass$%?#wd@host:port/path;params?query#fragment", - scheme = "scheme", - authority = "user:pass$%?#wd@host:port", + scheme = "scheme", + authority = "user:pass$%?#wd@host:port", host = "host", port = "port", userinfo = "user:pass$%?#wd", @@ -106,8 +106,8 @@ check_parse_url{ } check_parse_url{ url = "scheme://user:pass?#wd@host:port/path;params?query#fragment", - scheme = "scheme", - authority = "user:pass?#wd@host:port", + scheme = "scheme", + authority = "user:pass?#wd@host:port", host = "host", port = "port", userinfo = "user:pass?#wd", @@ -120,8 +120,8 @@ check_parse_url{ } check_parse_url{ url = "scheme://user:pass-wd@host:port/path;params?query#fragment", - scheme = "scheme", - authority = "user:pass-wd@host:port", + scheme = "scheme", + authority = "user:pass-wd@host:port", host = "host", port = "port", userinfo = "user:pass-wd", @@ -134,8 +134,8 @@ check_parse_url{ } check_parse_url{ url = "scheme://user:pass#wd@host:port/path;params?query#fragment", - scheme = "scheme", - authority = "user:pass#wd@host:port", + scheme = "scheme", + authority = "user:pass#wd@host:port", host = "host", port = "port", userinfo = "user:pass#wd", @@ -148,8 +148,8 @@ check_parse_url{ } check_parse_url{ url = "scheme://user:pass#wd@host:port/path;params?query", - scheme = "scheme", - authority = "user:pass#wd@host:port", + scheme = "scheme", + authority = "user:pass#wd@host:port", host = "host", port = "port", userinfo = "user:pass#wd", @@ -161,8 +161,8 @@ check_parse_url{ } check_parse_url{ url = "scheme://userinfo@host:port/path;params?query#fragment", - scheme = "scheme", - authority = "userinfo@host:port", + scheme = "scheme", + authority = "userinfo@host:port", host = "host", port = "port", userinfo = "userinfo", @@ -175,8 +175,8 @@ check_parse_url{ check_parse_url{ url = "scheme://user:password@host:port/path;params?query#fragment", - scheme = "scheme", - authority = "user:password@host:port", + scheme = "scheme", + authority = "user:password@host:port", host = "host", port = "port", userinfo = "user:password", @@ -190,8 +190,8 @@ check_parse_url{ check_parse_url{ url = "scheme://userinfo@host:port/path;params?query#", - scheme = "scheme", - authority = "userinfo@host:port", + scheme = "scheme", + authority = "userinfo@host:port", host = "host", port = "port", userinfo = "userinfo", @@ -204,8 +204,8 @@ check_parse_url{ check_parse_url{ url = "scheme://userinfo@host:port/path;params?#fragment", - scheme = "scheme", - authority = "userinfo@host:port", + scheme = "scheme", + authority = "userinfo@host:port", host = "host", port = "port", userinfo = "userinfo", @@ -218,8 +218,8 @@ check_parse_url{ check_parse_url{ url = "scheme://userinfo@host:port/path;params#fragment", - scheme = "scheme", - authority = "userinfo@host:port", + scheme = "scheme", + authority = "userinfo@host:port", host = "host", port = "port", userinfo = "userinfo", @@ -231,8 +231,8 @@ check_parse_url{ check_parse_url{ url = "scheme://userinfo@host:port/path;?query#fragment", - scheme = "scheme", - authority = "userinfo@host:port", + scheme = "scheme", + authority = "userinfo@host:port", host = "host", port = "port", userinfo = "userinfo", @@ -245,8 +245,8 @@ check_parse_url{ check_parse_url{ url = "scheme://userinfo@host:port/path?query#fragment", - scheme = "scheme", - authority = "userinfo@host:port", + scheme = "scheme", + authority = "userinfo@host:port", host = "host", port = "port", userinfo = "userinfo", @@ -258,8 +258,8 @@ check_parse_url{ check_parse_url{ url = "scheme://userinfo@host:port/;params?query#fragment", - scheme = "scheme", - authority = "userinfo@host:port", + scheme = "scheme", + authority = "userinfo@host:port", host = "host", port = "port", userinfo = "userinfo", @@ -272,8 +272,8 @@ check_parse_url{ check_parse_url{ url = "scheme://userinfo@host:port", - scheme = "scheme", - authority = "userinfo@host:port", + scheme = "scheme", + authority = "userinfo@host:port", host = "host", port = "port", userinfo = "userinfo", @@ -282,7 +282,7 @@ check_parse_url{ check_parse_url{ url = "//userinfo@host:port/path;params?query#fragment", - authority = "userinfo@host:port", + authority = "userinfo@host:port", host = "host", port = "port", userinfo = "userinfo", @@ -295,7 +295,7 @@ check_parse_url{ check_parse_url{ url = "//userinfo@host:port/path", - authority = "userinfo@host:port", + authority = "userinfo@host:port", host = "host", port = "port", userinfo = "userinfo", @@ -305,7 +305,7 @@ check_parse_url{ check_parse_url{ url = "//userinfo@host/path", - authority = "userinfo@host", + authority = "userinfo@host", host = "host", userinfo = "userinfo", user = "userinfo", @@ -314,7 +314,7 @@ check_parse_url{ check_parse_url{ url = "//user:password@host/path", - authority = "user:password@host", + authority = "user:password@host", host = "host", userinfo = "user:password", password = "password", @@ -324,7 +324,7 @@ check_parse_url{ check_parse_url{ url = "//user:@host/path", - authority = "user:@host", + authority = "user:@host", host = "host", userinfo = "user:", password = "", @@ -334,7 +334,7 @@ check_parse_url{ check_parse_url{ url = "//user@host:port/path", - authority = "user@host:port", + authority = "user@host:port", host = "host", userinfo = "user", user = "user", @@ -344,7 +344,7 @@ check_parse_url{ check_parse_url{ url = "//host:port/path", - authority = "host:port", + authority = "host:port", port = "port", host = "host", path = "/path", @@ -352,14 +352,14 @@ check_parse_url{ check_parse_url{ url = "//host/path", - authority = "host", + authority = "host", host = "host", path = "/path", } check_parse_url{ url = "//host", - authority = "host", + authority = "host", host = "host", } @@ -433,7 +433,7 @@ check_parse_url{ check_parse_url{ url = "//userinfo@[::FFFF:129.144.52.38]:port/path;params?query#fragment", - authority = "userinfo@[::FFFF:129.144.52.38]:port", + authority = "userinfo@[::FFFF:129.144.52.38]:port", host = "::FFFF:129.144.52.38", port = "port", userinfo = "userinfo", @@ -447,7 +447,7 @@ check_parse_url{ check_parse_url{ url = "scheme://user:password@[::192.9.5.5]:port/path;params?query#fragment", scheme = "scheme", - authority = "user:password@[::192.9.5.5]:port", + authority = "user:password@[::192.9.5.5]:port", host = "::192.9.5.5", port = "port", userinfo = "user:password", @@ -462,7 +462,7 @@ check_parse_url{ print("testing URL building") check_build_url { url = "scheme://user:password@host:port/path;params?query#fragment", - scheme = "scheme", + scheme = "scheme", host = "host", port = "port", user = "user", @@ -499,7 +499,7 @@ check_build_url{ check_build_url { url = "scheme://user:password@host/path;params?query#fragment", - scheme = "scheme", + scheme = "scheme", host = "host", user = "user", password = "password", @@ -511,7 +511,7 @@ check_build_url { check_build_url { url = "scheme://user@host/path;params?query#fragment", - scheme = "scheme", + scheme = "scheme", host = "host", user = "user", path = "/path", @@ -522,7 +522,7 @@ check_build_url { check_build_url { url = "scheme://host/path;params?query#fragment", - scheme = "scheme", + scheme = "scheme", host = "host", path = "/path", params = "params", @@ -532,7 +532,7 @@ check_build_url { check_build_url { url = "scheme://host/path;params#fragment", - scheme = "scheme", + scheme = "scheme", host = "host", path = "/path", params = "params", @@ -541,7 +541,7 @@ check_build_url { check_build_url { url = "scheme://host/path#fragment", - scheme = "scheme", + scheme = "scheme", host = "host", path = "/path", fragment = "fragment" @@ -549,7 +549,7 @@ check_build_url { check_build_url { url = "scheme://host/path", - scheme = "scheme", + scheme = "scheme", host = "host", path = "/path", } @@ -567,7 +567,7 @@ check_build_url { check_build_url { url = "scheme://user:password@host:port/path;params?query#fragment", - scheme = "scheme", + scheme = "scheme", host = "host", port = "port", user = "user", @@ -581,7 +581,7 @@ check_build_url { check_build_url { url = "scheme://user:password@host:port/path;params?query#fragment", - scheme = "scheme", + scheme = "scheme", host = "host", port = "port", user = "user", @@ -596,7 +596,7 @@ check_build_url { check_build_url { url = "scheme://user:password@host:port/path;params?query#fragment", - scheme = "scheme", + scheme = "scheme", host = "host", port = "port", userinfo = "user:password", @@ -609,7 +609,7 @@ check_build_url { check_build_url { url = "scheme://user:password@host:port/path;params?query#fragment", - scheme = "scheme", + scheme = "scheme", authority = "user:password@host:port", path = "/path", params = "params", @@ -683,7 +683,7 @@ check_absolute_url("//a/b/c/d;p?q#f", "d/e/f", "//a/b/c/d/e/f") check_absolute_url("/a/b/c/d;p?q#f", "d/e/f", "/a/b/c/d/e/f") check_absolute_url("a/b/c/d", "d/e/f", "a/b/c/d/e/f") check_absolute_url("a/b/c/d/../", "d/e/f", "a/b/c/d/e/f") -check_absolute_url("http://velox.telemar.com.br", "/dashboard/index.html", +check_absolute_url("http://velox.telemar.com.br", "/dashboard/index.html", "http://velox.telemar.com.br/dashboard/index.html") check_absolute_url("http://example.com/", "../.badhost.com/", "http://example.com/.badhost.com/") check_absolute_url("http://example.com/", "...badhost.com/", "http://example.com/...badhost.com/") @@ -700,11 +700,11 @@ check_absolute_url("http://example.com/a/b/c/d/", "../x/a/../y/z/../../../../q", print("testing path parsing and composition") check_parse_path("/eu/tu/ele", { "eu", "tu", "ele"; is_absolute = 1 }) check_parse_path("/eu/", { "eu"; is_absolute = 1, is_directory = 1 }) -check_parse_path("eu/tu/ele/nos/vos/eles/", +check_parse_path("eu/tu/ele/nos/vos/eles/", { "eu", "tu", "ele", "nos", "vos", "eles"; is_directory = 1}) check_parse_path("/", { is_absolute = 1, is_directory = 1}) check_parse_path("", { }) -check_parse_path("eu%01/%02tu/e%03l%04e/nos/vos%05/e%12les/", +check_parse_path("eu%01/%02tu/e%03l%04e/nos/vos%05/e%12les/", { "eu\1", "\2tu", "e\3l\4e", "nos", "vos\5", "e\18les"; is_directory = 1}) check_parse_path("eu/tu", { "eu", "tu" }) diff --git a/test/utestclnt.lua b/test/utestclnt.lua index 34a0718..7f10643 100644 --- a/test/utestclnt.lua +++ b/test/utestclnt.lua @@ -54,30 +54,30 @@ function check_timeout(tm, sl, elapsed, err, opp, mode, alldone) if not err then warn("must be buffered") elseif err == "timeout" then pass("proper timeout") else fail("unexpected error '%s'", err) end - else - if err ~= "timeout" then fail("should have timed out") + else + if err ~= "timeout" then fail("should have timed out") else pass("proper timeout") end end else if mode == "total" then - if elapsed > tm then + if elapsed > tm then if err ~= "timeout" then fail("should have timed out") else pass("proper timeout") end elseif elapsed < tm then - if err then fail(err) + if err then fail(err) else pass("ok") end - else - if alldone then - if err then fail("unexpected error '%s'", err) + else + if alldone then + if err then fail("unexpected error '%s'", err) else pass("ok") end else - if err ~= "timeout" then fail(err) + if err ~= "timeout" then fail(err) else pass("proper timeoutk") end end end - else - if err then fail(err) - else pass("ok") end + else + if err then fail(err) + else pass("ok") end end end end @@ -104,7 +104,7 @@ function reconnect() print("done " .. i) ]] data, err = uconnect(host, port) - if not data then fail(err) + if not data then fail(err) else pass("connected!") end end @@ -116,8 +116,8 @@ else pass("connected!") end ------------------------------------------------------------------------ function test_methods(sock, methods) for _, v in pairs(methods) do - if type(sock[v]) ~= "function" then - fail(sock.class .. " method '" .. v .. "' not registered") + if type(sock[v]) ~= "function" then + fail(sock.class .. " method '" .. v .. "' not registered") end end pass(sock.class .. " methods are ok") @@ -132,7 +132,7 @@ function test_mixed(len) local p3 = "raw " .. string.rep("z", inter) .. "bytes" local p4 = "end" .. string.rep("w", inter) .. "bytes" local bp1, bp2, bp3, bp4 -remote (string.format("str = data:receive(%d)", +remote (string.format("str = data:receive(%d)", string.len(p1)+string.len(p2)+string.len(p3)+string.len(p4))) sent, err = data:send(p1..p2..p3..p4) if err then fail(err) end @@ -172,7 +172,7 @@ function test_rawline(len) reconnect() local str, str10, back, err str = string.rep(string.char(47), math.mod(len, 10)) - str10 = string.rep(string.char(120,21,77,4,5,0,7,36,44,100), + str10 = string.rep(string.char(120,21,77,4,5,0,7,36,44,100), math.floor(len/10)) str = str .. str10 remote "str = data:receive()" @@ -221,7 +221,7 @@ function test_totaltimeoutreceive(len, tm, sl) data:settimeout(tm, "total") local t = socket.gettime() str, err, partial, elapsed = data:receive(2*len) - check_timeout(tm, sl, elapsed, err, "receive", "total", + check_timeout(tm, sl, elapsed, err, "receive", "total", string.len(str or partial) == 2*len) end @@ -241,7 +241,7 @@ function test_totaltimeoutsend(len, tm, sl) data:settimeout(tm, "total") str = string.rep("a", 2*len) total, err, partial, elapsed = data:send(str) - check_timeout(tm, sl, elapsed, err, "send", "total", + check_timeout(tm, sl, elapsed, err, "send", "total", total == 2*len) end @@ -261,7 +261,7 @@ function test_blockingtimeoutreceive(len, tm, sl) ]], 2*tm, len, sl, sl)) data:settimeout(tm) str, err, partial, elapsed = data:receive(2*len) - check_timeout(tm, sl, elapsed, err, "receive", "blocking", + check_timeout(tm, sl, elapsed, err, "receive", "blocking", string.len(str or partial) == 2*len) end @@ -294,10 +294,10 @@ function empty_connect() data = server:accept() ]] data, err = socket.connect("", port) - if not data then + if not data then pass("ok") data = socket.connect(host, port) - else + else pass("gethostbyname returns localhost on empty string...") end end @@ -331,7 +331,7 @@ function test_closed() data:close() data = nil ]], str)) - -- try to get a line + -- try to get a line back, err, partial = data:receive() if not err then fail("should have gotten 'closed'.") elseif err ~= "closed" then fail("got '"..err.."' instead of 'closed'.") @@ -344,25 +344,25 @@ function test_closed() data = nil ]] total, err, partial = data:send(string.rep("ugauga", 100000)) - if not err then + if not err then pass("failed: output buffer is at least %d bytes long!", total) - elseif err ~= "closed" then + elseif err ~= "closed" then fail("got '"..err.."' instead of 'closed'.") - else - pass("graceful 'closed' received after %d bytes were sent", partial) + else + pass("graceful 'closed' received after %d bytes were sent", partial) end end ------------------------------------------------------------------------ function test_selectbugs() local r, s, e = socket.select(nil, nil, 0.1) - assert(type(r) == "table" and type(s) == "table" and + assert(type(r) == "table" and type(s) == "table" and (e == "timeout" or e == "error")) pass("both nil: ok") local udp = socket.udp() udp:close() r, s, e = socket.select({ udp }, { udp }, 0.1) - assert(type(r) == "table" and type(s) == "table" and + assert(type(r) == "table" and type(s) == "table" and (e == "timeout" or e == "error")) pass("closed sockets: ok") e = pcall(socket.select, "wrong", 1, 0.1) @@ -380,7 +380,7 @@ function accept_timeout() local t = socket.gettime() s:settimeout(1) local c, e = s:accept() - assert(not c, "should not accept") + assert(not c, "should not accept") assert(e == "timeout", string.format("wrong error message (%s)", e)) t = socket.gettime() - t assert(t < 2, string.format("took to long to give up (%gs)", t)) @@ -398,9 +398,9 @@ function connect_timeout() local t = socket.gettime() local r, e = c:connect("127.0.0.2", 80) assert(not r, "should not connect") - assert(socket.gettime() - t < 2, "took too long to give up.") + assert(socket.gettime() - t < 2, "took too long to give up.") c:close() - print("ok") + print("ok") end ------------------------------------------------------------------------ @@ -463,9 +463,9 @@ function getstats_test() data:receive(c) t = t + c local r, s, a = data:getstats() - assert(r == t, "received count failed" .. tostring(r) + assert(r == t, "received count failed" .. tostring(r) .. "/" .. tostring(t)) - assert(s == t, "sent count failed" .. tostring(s) + assert(s == t, "sent count failed" .. tostring(s) .. "/" .. tostring(t)) end print("ok") @@ -473,7 +473,7 @@ end ------------------------------------------------------------------------ -function test_nonblocking(size) +function test_nonblocking(size) reconnect() print("Testing " .. 2*size .. " bytes") remote(string.format([[ diff --git a/test/utestsrvr.lua b/test/utestsrvr.lua index a96b570..b6e4246 100644 --- a/test/utestsrvr.lua +++ b/test/utestsrvr.lua @@ -9,7 +9,7 @@ ack = "\n"; while 1 do print("server: waiting for client connection..."); control = assert(server:accept()); - while 1 do + while 1 do command = assert(control:receive()); assert(control:send(ack)); ((loadstring or load)(command))(); -- cgit v1.2.3-55-g6feb