"Fossies" - the Fresh Open Source Software Archive

Member "wrk-4.2.0/src/wrk.lua" (7 Feb 2021, 1571 Bytes) of package /linux/www/wrk-4.2.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Lua source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 local wrk = {
    2    scheme  = "http",
    3    host    = "localhost",
    4    port    = nil,
    5    method  = "GET",
    6    path    = "/",
    7    headers = {},
    8    body    = nil,
    9    thread  = nil,
   10 }
   11 
   12 function wrk.resolve(host, service)
   13    local addrs = wrk.lookup(host, service)
   14    for i = #addrs, 1, -1 do
   15       if not wrk.connect(addrs[i]) then
   16          table.remove(addrs, i)
   17       end
   18    end
   19    wrk.addrs = addrs
   20 end
   21 
   22 function wrk.setup(thread)
   23    thread.addr = wrk.addrs[1]
   24    if type(setup) == "function" then
   25       setup(thread)
   26    end
   27 end
   28 
   29 function wrk.init(args)
   30    if not wrk.headers["Host"] then
   31       local host = wrk.host
   32       local port = wrk.port
   33 
   34       host = host:find(":") and ("[" .. host .. "]")  or host
   35       host = port           and (host .. ":" .. port) or host
   36 
   37       wrk.headers["Host"] = host
   38    end
   39 
   40    if type(init) == "function" then
   41       init(args)
   42    end
   43 
   44    local req = wrk.format()
   45    wrk.request = function()
   46       return req
   47    end
   48 end
   49 
   50 function wrk.format(method, path, headers, body)
   51    local method  = method  or wrk.method
   52    local path    = path    or wrk.path
   53    local headers = headers or wrk.headers
   54    local body    = body    or wrk.body
   55    local s       = {}
   56 
   57    if not headers["Host"] then
   58       headers["Host"] = wrk.headers["Host"]
   59    end
   60 
   61    headers["Content-Length"] = body and string.len(body)
   62 
   63    s[1] = string.format("%s %s HTTP/1.1", method, path)
   64    for name, value in pairs(headers) do
   65       s[#s+1] = string.format("%s: %s", name, value)
   66    end
   67 
   68    s[#s+1] = ""
   69    s[#s+1] = body or ""
   70 
   71    return table.concat(s, "\r\n")
   72 end
   73 
   74 return wrk