Something went wrong. Try again.
web crawler!
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692#!lua name=queue-- net/url.lua - a robust url parser and builder---- Bertrand Mansion, 2011-2021; License MIT-- @module net.url-- @alias M
local URL = {}URL.version = "1.1.0"
--- url options-- - `separator` is set to `&` by default but could be anything like `&` or `;`-- - `cumulative_parameters` is false by default. If true, query parameters with the same name will be stored in a table.-- - `legal_in_path` is a table of characters that will not be url encoded in path components-- - `legal_in_query` is a table of characters that will not be url encoded in query values. Query parameters only support a small set of legal characters (-_.).-- - `query_plus_is_space` is true by default, so a plus sign in a query value will be converted to %20 (space), not %2B (plus)-- @todo Add option to limit the size of the argument table-- @todo Add option to limit the depth of the argument table-- @todo Add option to process dots in parameter names, ie. `param.filter=1`URL.options = { separator = "&", cumulative_parameters = false, legal_in_path = { [":"] = true, ["-"] = true, ["_"] = true, ["."] = true, ["!"] = true, ["~"] = true, ["*"] = true, ["'"] = true, ["("] = true, [")"] = true, ["@"] = true, ["&"] = true, ["="] = true, ["$"] = true, [","] = true, [";"] = true, }, legal_in_query = { [":"] = true, ["-"] = true, ["_"] = true, ["."] = true, [","] = true, ["!"] = true, ["~"] = true, ["*"] = true, ["'"] = true, [";"] = true, ["("] = true, [")"] = true, ["@"] = true, ["$"] = true, }, query_plus_is_space = true,}
--- list of known and common scheme ports-- as documented in <a href="http://www.iana.org/assignments/uri-schemes.html">IANA URI scheme list</a>URL.services = { acap = 674, cap = 1026, dict = 2628, ftp = 21, gopher = 70, http = 80, https = 443, iax = 4569, icap = 1344, imap = 143, ipp = 631, ldap = 389, mtqp = 1038, mupdate = 3905, news = 2009, nfs = 2049, nntp = 119, rtsp = 554, sip = 5060, snmp = 161, telnet = 23, tftp = 69, vemmi = 575, afs = 1483, jms = 5673, rsync = 873, prospero = 191, videotex = 516,}
local function decode(str) return (str:gsub("%%(%x%x)", function(c) return string.char(tonumber(c, 16)) end))end
local function encode(str, legal) return ( str:gsub("([^%w])", function(v) if legal[v] then return v end return string.upper(string.format("%%%02x", string.byte(v))) end) )end
-- for query values, + can mean space if configured as suchlocal function decodeValue(str) if URL.options.query_plus_is_space then str = str:gsub("+", " ") end return decode(str)end
local function concat(a, b) if type(a) == "table" then return a:build() .. b else return a .. b:build() endend
function URL:addSegment(path) if type(path) == "string" then self.path = self.path .. "/" .. encode(path:gsub("^/+", ""), URL.options.legal_in_path) end return selfend
--- builds the url-- @return a string representing the built urlfunction URL:build() local url = "" if self.path then local path = self.path url = url .. tostring(path) end if self.query then local qstring = tostring(self.query) if qstring ~= "" then url = url .. "?" .. qstring end end if self.host then local authority = self.host if self.port and self.scheme and URL.services[self.scheme] ~= self.port then authority = authority .. ":" .. self.port end local userinfo if self.user and self.user ~= "" then userinfo = self.user if self.password then userinfo = userinfo .. ":" .. self.password end end if userinfo and userinfo ~= "" then authority = userinfo .. "@" .. authority end if authority then if url ~= "" then url = "//" .. authority .. "/" .. url:gsub("^/+", "") else url = "//" .. authority end end end if self.scheme then url = self.scheme .. ":" .. url end if self.fragment then url = url .. "#" .. self.fragment end return urlend
--- builds the querystring-- @param tab The key/value parameters-- @param sep The separator to use (optional)-- @param key The parent key if the value is multi-dimensional (optional)-- @return a string representing the built querystringfunction URL.buildQuery(tab, sep, key) local query = {} if not sep then sep = URL.options.separator or "&" end local keys = {} for k in pairs(tab) do keys[#keys + 1] = k end table.sort(keys, function(a, b) local function padnum(n, rest) return ("%03d" .. rest):format(tonumber(n)) end return tostring(a):gsub("(%d+)(%.)", padnum) < tostring(b):gsub("(%d+)(%.)", padnum) end) for _, name in ipairs(keys) do local value = tab[name] name = encode(tostring(name), { ["-"] = true, ["_"] = true, ["."] = true }) if key then if URL.options.cumulative_parameters and string.find(name, "^%d+$") then name = tostring(key) else name = string.format("%s[%s]", tostring(key), tostring(name)) end end if type(value) == "table" then query[#query + 1] = URL.buildQuery(value, sep, name) else local value = encode(tostring(value), URL.options.legal_in_query) if value ~= "" then query[#query + 1] = string.format("%s=%s", name, value) else query[#query + 1] = name end end end return table.concat(query, sep)end
--- Parses the querystring to a table-- This function can parse multidimensional pairs and is mostly compatible-- with PHP usage of brackets in key names like ?param[key]=value-- @param str The querystring to parse-- @param sep The separator between key/value pairs, defaults to `&`-- @todo limit the max number of parameters with URL.options.max_parameters-- @return a table representing the query key/value pairsfunction URL.parseQuery(str, sep) if not sep then sep = URL.options.separator or "&" end
local values = {} for key, val in str:gmatch(string.format("([^%q=]+)(=*[^%q=]*)", sep, sep)) do local key = decodeValue(key) local keys = {} key = key:gsub("%[([^%]]*)%]", function(v) -- extract keys between balanced brackets if string.find(v, "^-?%d+$") then v = tonumber(v) else v = decodeValue(v) end table.insert(keys, v) return "=" end) key = key:gsub("=+.*$", "") key = key:gsub("%s", "_") -- remove spaces in parameter name val = val:gsub("^=+", "")
if not values[key] then values[key] = {} end if #keys > 0 and type(values[key]) ~= "table" then values[key] = {} elseif #keys == 0 and type(values[key]) == "table" then values[key] = decodeValue(val) elseif URL.options.cumulative_parameters and type(values[key]) == "string" then values[key] = { values[key] } table.insert(values[key], decodeValue(val)) end
local t = values[key] for i, k in ipairs(keys) do if type(t) ~= "table" then t = {} end if k == "" then k = #t + 1 end if not t[k] then t[k] = {} end if i == #keys then t[k] = val end t = t[k] end end setmetatable(values, { __tostring = URL.buildQuery }) return valuesend
--- set the url query-- @param query Can be a string to parse or a table of key/value pairs-- @return a table representing the query key/value pairsfunction URL:setQuery(query) local query = query if type(query) == "table" then query = URL.buildQuery(query) end self.query = URL.parseQuery(query) return queryend
--- set the authority part of the url-- The authority is parsed to find the user, password, port and host if available.-- @param authority The string representing the authority-- @return a string with what remains after the authority was parsedfunction URL:setAuthority(authority) self.authority = authority self.port = nil self.host = nil self.userinfo = nil self.user = nil self.password = nil
authority = authority:gsub("^([^@]*)@", function(v) self.userinfo = v return "" end)
authority = authority:gsub(":(%d+)$", function(v) self.port = tonumber(v) return "" end)
local function getIP(str) -- ipv4 local chunks = { str:match("^(%d+)%.(%d+)%.(%d+)%.(%d+)$") } if #chunks == 4 then for _, v in pairs(chunks) do if tonumber(v) > 255 then return false end end return str end -- ipv6 local chunks = { str:match("^%[" .. (("([a-fA-F0-9]*):"):rep(8):gsub(":$", "%%]$"))) } if #chunks == 8 or #chunks < 8 and str:match("::") and not str:gsub("::", "", 1):match("::") then for _, v in pairs(chunks) do if #v > 0 and tonumber(v, 16) > 65535 then return false end end return str end return nil end
local ip = getIP(authority) if ip then self.host = ip elseif type(ip) == "nil" then -- domain if authority ~= "" and not self.host then local host = authority:lower() if string.match(host, "^[%d%a%-%.]+$") ~= nil and string.sub(host, 0, 1) ~= "." and string.sub(host, -1) ~= "." and string.find(host, "%.%.") == nil then self.host = host end end end
if self.userinfo then local userinfo = self.userinfo userinfo = userinfo:gsub(":([^:]*)$", function(v) self.password = v return "" end) if string.find(userinfo, "^[%w%+%.]+$") then self.user = userinfo else -- incorrect userinfo self.userinfo = nil self.user = nil self.password = nil end end
return authorityend
--- Parse the url into the designated parts.-- Depending on the url, the following parts can be available:-- scheme, userinfo, user, password, authority, host, port, path,-- query, fragment-- @param url Url string-- @return a table with the different parts and a few other functionsfunction URL.parse(url) local comp = {} URL.setAuthority(comp, "") URL.setQuery(comp, "")
local url = tostring(url or "") url = url:gsub("#(.*)$", function(v) comp.fragment = v return "" end) url = url:gsub("^([%w][%w%+%-%.]*)%:", function(v) comp.scheme = v:lower() return "" end) url = url:gsub("%?(.*)", function(v) URL.setQuery(comp, v) return "" end) url = url:gsub("^//([^/]*)", function(v) URL.setAuthority(comp, v) return "" end)
comp.path = url:gsub("([^/]+)", function(s) return encode(decode(s), URL.options.legal_in_path) end)
setmetatable(comp, { __index = URL, __tostring = URL.build, __concat = concat, __div = URL.addSegment, }) return compend
--- removes dots and slashes in urls when possible-- This function will also remove multiple slashes-- @param path The string representing the path to clean-- @return a string of the path without unnecessary dots and segmentsfunction URL.removeDotSegments(path) local fields = {} if string.len(path) == 0 then return "" end local startslash = false local endslash = false if string.sub(path, 1, 1) == "/" then startslash = true end if (string.len(path) > 1 or startslash == false) and string.sub(path, -1) == "/" then endslash = true end
path:gsub("[^/]+", function(c) table.insert(fields, c) end)
local new = {} local j = 0
for i, c in ipairs(fields) do if c == ".." then if j > 0 then j = j - 1 end elseif c ~= "." then j = j + 1 new[j] = c end end local ret = "" if #new > 0 and j > 0 then ret = table.concat(new, "/", 1, j) else ret = "" end if startslash then ret = "/" .. ret end if endslash then ret = ret .. "/" end return retend
local function reducePath(base_path, relative_path) if string.sub(relative_path, 1, 1) == "/" then return "/" .. string.gsub(relative_path, "^[%./]+", "") end local path = base_path local startslash = string.sub(path, 1, 1) ~= "/" if relative_path ~= "" then path = (startslash and "" or "/") .. path:gsub("[^/]*$", "") end path = path .. relative_path path = path:gsub("([^/]*%./)", 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(path, "([^/]*/%.%.?)$", function(s) if s ~= "../.." then return "" else return s end end) local reduced while reduced ~= path do reduced = path path = string.gsub(reduced, "^/?%.%./", "") end return (startslash and "" or "/") .. pathend
--- builds a new url by using the one given as parameter and resolving paths-- @param other A string or a table representing a url-- @return a new url tablefunction URL:resolve(other) if type(self) == "string" then self = URL.parse(self) end if type(other) == "string" then other = URL.parse(other) end if other.scheme then return other else other.scheme = self.scheme if not other.authority or other.authority == "" then other:setAuthority(self.authority) if not other.path or other.path == "" then other.path = self.path local query = other.query if not query or not next(query) then other.query = self.query end else other.path = reducePath(self.path, other.path) end end return other endend
--- normalize a url path following some common normalization rules-- described on <a href="http://en.wikipedia.org/wiki/URL_normalization">The URL normalization page of Wikipedia</a>-- @return the normalized pathfunction URL:normalize() if type(self) == "string" then self = URL.parse(self) end if self.path then local path = self.path path = reducePath(path, "") -- normalize multiple slashes path = string.gsub(path, "//+", "/") self.path = path end return selfend
local function countHosts(urls) local counts = {} for _, url in ipairs(urls) do counts[url.host] = (counts[url.host] or 0) + 1 end return countsend
local function mean(counts) local sum = 0 local count = 0 for k, v in pairs(counts) do sum = sum + v count = count + 1 end local meanCount = sum / count
return function(a, b) local distA = math.abs(counts[a] - meanCount) local distB = math.abs(counts[b] - meanCount)
return distA < distB endend
local function asc(counts) return function(a, b) return counts[a] < counts[b] endend
local function desc(counts) return function(a, b) return counts[a] > counts[b] endend
local function sortHosts(counts, method) local hosts = {} for host in pairs(counts) do table.insert(hosts, host) end table.sort(hosts, method(counts)) return hostsend
local function distributeUrls(urls, method) local counts = countHosts(urls) local sortedHosts = sortHosts(counts, method)
-- group urls by host local groupedUrls = {} for _, url in ipairs(urls) do if not groupedUrls[url.host] then groupedUrls[url.host] = {} end table.insert(groupedUrls[url.host], url) end
-- distribute urls local result = {} local index = 1 while #sortedHosts > 0 do local host = table.remove(sortedHosts, 1) while #groupedUrls[host] > 0 do table.insert(result, table.remove(groupedUrls[host], 1))
index = index + 1 if index > #sortedHosts then index = 1 end end end
return resultend
local function slice(tbl, first, last, step) local sliced = {}
for i = first or 1, last or #tbl, step or 1 do sliced[#sliced + 1] = tbl[i] end
return slicedend
local function map(tbl, f) local t = {} for k, v in pairs(tbl) do t[k] = f(v) end return tend
local function filter(tbl, f) local t = {} for k, v in pairs(tbl) do if f(v, k, t) then t[k] = v end end return tend
redis.register_function("QUEUESLICE", function(_, args) local queueSlice = redis.call("SRANDMEMBER", "queue", args[2])
local methods = { asc = asc, desc = desc, mean = mean, }
return map( slice( distributeUrls( filter(map(queueSlice, URL.parse), function(url) return url.host ~= nil end), methods[string.lower(args[1])] ), 0, args[3] ), URL.build )end)