Blame etc/cookie.lua

Packit b53373
local socket = require"socket"
Packit b53373
local http = require"socket.http"
Packit b53373
local url = require"socket.url"
Packit b53373
local ltn12 = require"ltn12"
Packit b53373
Packit b53373
local token_class =  '[^%c%s%(%)%<%>%@%,%;%:%\\%"%/%[%]%?%=%{%}]'
Packit b53373
Packit b53373
local function unquote(t, quoted) 
Packit b53373
    local n = string.match(t, "%$(%d+)$")
Packit b53373
    if n then n = tonumber(n) end
Packit b53373
    if quoted[n] then return quoted[n]
Packit b53373
    else return t end
Packit b53373
end
Packit b53373
Packit b53373
local function parse_set_cookie(c, quoted, cookie_table)
Packit b53373
    c = c .. ";$last=last;"
Packit b53373
    local _, __, n, v, i = string.find(c, "(" .. token_class .. 
Packit b53373
        "+)%s*=%s*(.-)%s*;%s*()")
Packit b53373
    local cookie = {
Packit b53373
        name = n, 
Packit b53373
        value = unquote(v, quoted), 
Packit b53373
        attributes = {}
Packit b53373
    }
Packit b53373
    while 1 do
Packit b53373
        _, __, n, v, i = string.find(c, "(" .. token_class .. 
Packit b53373
            "+)%s*=?%s*(.-)%s*;%s*()", i)
Packit b53373
        if not n or n == "$last" then break end
Packit b53373
        cookie.attributes[#cookie.attributes+1] = {
Packit b53373
            name = n, 
Packit b53373
            value = unquote(v, quoted)
Packit b53373
        }
Packit b53373
    end
Packit b53373
    cookie_table[#cookie_table+1] = cookie
Packit b53373
end
Packit b53373
Packit b53373
local function split_set_cookie(s, cookie_table)
Packit b53373
    cookie_table = cookie_table or {}
Packit b53373
    -- remove quoted strings from cookie list
Packit b53373
    local quoted = {}
Packit b53373
    s = string.gsub(s, '"(.-)"', function(q)
Packit b53373
        quoted[#quoted+1] = q
Packit b53373
        return "$" .. #quoted
Packit b53373
    end)
Packit b53373
    -- add sentinel
Packit b53373
    s = s .. ",$last="
Packit b53373
    -- split into individual cookies
Packit b53373
    i = 1
Packit b53373
    while 1 do
Packit b53373
        local _, __, cookie, next_token
Packit b53373
        _, __, cookie, i, next_token = string.find(s, "(.-)%s*%,%s*()(" .. 
Packit b53373
            token_class .. "+)%s*=", i)
Packit b53373
        if not next_token then break end
Packit b53373
        parse_set_cookie(cookie, quoted, cookie_table)
Packit b53373
        if next_token == "$last" then break end
Packit b53373
    end
Packit b53373
    return cookie_table
Packit b53373
end
Packit b53373
Packit b53373
local function quote(s)
Packit b53373
    if string.find(s, "[ %,%;]") then return '"' .. s .. '"'
Packit b53373
    else return s end
Packit b53373
end
Packit b53373
Packit b53373
local _empty = {}
Packit b53373
local function build_cookies(cookies) 
Packit b53373
    s = ""
Packit b53373
    for i,v in ipairs(cookies or _empty) do
Packit b53373
        if v.name then
Packit b53373
            s = s .. v.name
Packit b53373
            if v.value and v.value ~= "" then 
Packit b53373
                s = s .. '=' .. quote(v.value)
Packit b53373
            end
Packit b53373
        end
Packit b53373
        if v.name and #(v.attributes or _empty) > 0 then s = s .. "; "  end
Packit b53373
        for j,u in ipairs(v.attributes or _empty) do
Packit b53373
            if u.name then
Packit b53373
                s = s .. u.name
Packit b53373
                if u.value and u.value ~= "" then
Packit b53373
                    s = s .. '=' .. quote(u.value)
Packit b53373
                end
Packit b53373
            end
Packit b53373
            if j < #v.attributes then s = s .. "; "  end
Packit b53373
        end
Packit b53373
        if i < #cookies then s = s .. ", " end
Packit b53373
    end
Packit b53373
    return s 
Packit b53373
end
Packit b53373