From d548a78e5516bcc85d44f1d419cf53c71d6dcd79 Mon Sep 17 00:00:00 2001 From: Diego Nehab Date: Thu, 10 Jan 2013 01:10:34 -0200 Subject: [PATCH] Cookie modifications. --- .gitignore | 1 + etc/cookie.lua | 88 +++++++++++++++++++++++++++++++++++++++++++++++++ src/headers.lua | 1 + 3 files changed, 90 insertions(+) create mode 100644 etc/cookie.lua diff --git a/.gitignore b/.gitignore index 2d37568..0e0b11f 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ *.so.* macosx.cmd win32.cmd +linux.cmd diff --git a/etc/cookie.lua b/etc/cookie.lua new file mode 100644 index 0000000..4adb403 --- /dev/null +++ b/etc/cookie.lua @@ -0,0 +1,88 @@ +local socket = require"socket" +local http = require"socket.http" +local url = require"socket.url" +local ltn12 = require"ltn12" + +local token_class = '[^%c%s%(%)%<%>%@%,%;%:%\\%"%/%[%]%?%=%{%}]' + +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] + else return t end +end + +local function parse_set_cookie(c, quoted, cookie_table) + c = c .. ";$last=last;" + local _, __, n, v, i = string.find(c, "(" .. token_class .. + "+)%s*=%s*(.-)%s*;%s*()") + local cookie = { + name = n, + value = unquote(v, quoted), + attributes = {} + } + while 1 do + _, __, 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, + value = unquote(v, quoted) + } + end + cookie_table[#cookie_table+1] = cookie +end + +local function split_set_cookie(s, cookie_table) + cookie_table = cookie_table or {} + -- remove quoted strings from cookie list + local quoted = {} + s = string.gsub(s, '"(.-)"', function(q) + quoted[#quoted+1] = q + return "$" .. #quoted + end) + -- add sentinel + s = s .. ",$last=" + -- split into individual cookies + i = 1 + while 1 do + 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) + if next_token == "$last" then break end + end + return cookie_table +end + +local function quote(s) + if string.find(s, "[ %,%;]") then return '"' .. s .. '"' + else return s end +end + +local _empty = {} +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 + s = s .. '=' .. quote(v.value) + end + end + if v.name and #(v.attributes or _empty) > 0 then s = s .. "; " end + for j,u in ipairs(v.attributes or _empty) do + if u.name then + s = s .. u.name + if u.value and u.value ~= "" then + s = s .. '=' .. quote(u.value) + end + end + if j < #v.attributes then s = s .. "; " end + end + if i < #cookies then s = s .. ", " end + end + return s +end + diff --git a/src/headers.lua b/src/headers.lua index 41794ba..a293329 100644 --- a/src/headers.lua +++ b/src/headers.lua @@ -33,6 +33,7 @@ canonic = { ["content-range"] = "Content-Range", ["content-transfer-encoding"] = "Content-Transfer-Encoding", ["content-type"] = "Content-Type", + ["cookie"] = "Cookie", ["date"] = "Date", ["diagnostic-code"] = "Diagnostic-Code", ["dsn-gateway"] = "DSN-Gateway",