diff options
author | Diego Nehab <diego@fresnel.local> | 2013-01-10 01:10:34 -0200 |
---|---|---|
committer | Diego Nehab <diego@fresnel.local> | 2013-01-10 01:10:34 -0200 |
commit | d548a78e5516bcc85d44f1d419cf53c71d6dcd79 (patch) | |
tree | 564578ecb9b86f41fb7e517c37fa76d08fc6fcaf | |
parent | 72a5347f97b3f158431d4181109db3086a8a2953 (diff) | |
download | luasocket-d548a78e5516bcc85d44f1d419cf53c71d6dcd79.tar.gz luasocket-d548a78e5516bcc85d44f1d419cf53c71d6dcd79.tar.bz2 luasocket-d548a78e5516bcc85d44f1d419cf53c71d6dcd79.zip |
Cookie modifications.
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | etc/cookie.lua | 88 | ||||
-rw-r--r-- | src/headers.lua | 1 |
3 files changed, 90 insertions, 0 deletions
@@ -3,4 +3,5 @@ | |||
3 | *.so.* | 3 | *.so.* |
4 | macosx.cmd | 4 | macosx.cmd |
5 | win32.cmd | 5 | win32.cmd |
6 | linux.cmd | ||
6 | 7 | ||
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 @@ | |||
1 | local socket = require"socket" | ||
2 | local http = require"socket.http" | ||
3 | local url = require"socket.url" | ||
4 | local ltn12 = require"ltn12" | ||
5 | |||
6 | local token_class = '[^%c%s%(%)%<%>%@%,%;%:%\\%"%/%[%]%?%=%{%}]' | ||
7 | |||
8 | local function unquote(t, quoted) | ||
9 | local n = string.match(t, "%$(%d+)$") | ||
10 | if n then n = tonumber(n) end | ||
11 | if quoted[n] then return quoted[n] | ||
12 | else return t end | ||
13 | end | ||
14 | |||
15 | local function parse_set_cookie(c, quoted, cookie_table) | ||
16 | c = c .. ";$last=last;" | ||
17 | local _, __, n, v, i = string.find(c, "(" .. token_class .. | ||
18 | "+)%s*=%s*(.-)%s*;%s*()") | ||
19 | local cookie = { | ||
20 | name = n, | ||
21 | value = unquote(v, quoted), | ||
22 | attributes = {} | ||
23 | } | ||
24 | while 1 do | ||
25 | _, __, n, v, i = string.find(c, "(" .. token_class .. | ||
26 | "+)%s*=?%s*(.-)%s*;%s*()", i) | ||
27 | if not n or n == "$last" then break end | ||
28 | cookie.attributes[#cookie.attributes+1] = { | ||
29 | name = n, | ||
30 | value = unquote(v, quoted) | ||
31 | } | ||
32 | end | ||
33 | cookie_table[#cookie_table+1] = cookie | ||
34 | end | ||
35 | |||
36 | local function split_set_cookie(s, cookie_table) | ||
37 | cookie_table = cookie_table or {} | ||
38 | -- remove quoted strings from cookie list | ||
39 | local quoted = {} | ||
40 | s = string.gsub(s, '"(.-)"', function(q) | ||
41 | quoted[#quoted+1] = q | ||
42 | return "$" .. #quoted | ||
43 | end) | ||
44 | -- add sentinel | ||
45 | s = s .. ",$last=" | ||
46 | -- split into individual cookies | ||
47 | i = 1 | ||
48 | while 1 do | ||
49 | local _, __, cookie, next_token | ||
50 | _, __, cookie, i, next_token = string.find(s, "(.-)%s*%,%s*()(" .. | ||
51 | token_class .. "+)%s*=", i) | ||
52 | if not next_token then break end | ||
53 | parse_set_cookie(cookie, quoted, cookie_table) | ||
54 | if next_token == "$last" then break end | ||
55 | end | ||
56 | return cookie_table | ||
57 | end | ||
58 | |||
59 | local function quote(s) | ||
60 | if string.find(s, "[ %,%;]") then return '"' .. s .. '"' | ||
61 | else return s end | ||
62 | end | ||
63 | |||
64 | local _empty = {} | ||
65 | local function build_cookies(cookies) | ||
66 | s = "" | ||
67 | for i,v in ipairs(cookies or _empty) do | ||
68 | if v.name then | ||
69 | s = s .. v.name | ||
70 | if v.value and v.value ~= "" then | ||
71 | s = s .. '=' .. quote(v.value) | ||
72 | end | ||
73 | end | ||
74 | if v.name and #(v.attributes or _empty) > 0 then s = s .. "; " end | ||
75 | for j,u in ipairs(v.attributes or _empty) do | ||
76 | if u.name then | ||
77 | s = s .. u.name | ||
78 | if u.value and u.value ~= "" then | ||
79 | s = s .. '=' .. quote(u.value) | ||
80 | end | ||
81 | end | ||
82 | if j < #v.attributes then s = s .. "; " end | ||
83 | end | ||
84 | if i < #cookies then s = s .. ", " end | ||
85 | end | ||
86 | return s | ||
87 | end | ||
88 | |||
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 = { | |||
33 | ["content-range"] = "Content-Range", | 33 | ["content-range"] = "Content-Range", |
34 | ["content-transfer-encoding"] = "Content-Transfer-Encoding", | 34 | ["content-transfer-encoding"] = "Content-Transfer-Encoding", |
35 | ["content-type"] = "Content-Type", | 35 | ["content-type"] = "Content-Type", |
36 | ["cookie"] = "Cookie", | ||
36 | ["date"] = "Date", | 37 | ["date"] = "Date", |
37 | ["diagnostic-code"] = "Diagnostic-Code", | 38 | ["diagnostic-code"] = "Diagnostic-Code", |
38 | ["dsn-gateway"] = "DSN-Gateway", | 39 | ["dsn-gateway"] = "DSN-Gateway", |