util-soc-imp-headers.lua /size: 5721 b    last modification: 2020-07-01 14:35
1-- original file : headers.lua
2-- for more into : see util-soc.lua
3
4local next = next
5local lower = string.lower
6local concat = table.concat
7
8local socket   = socket or require("socket")
9
10local headers  = { }
11socket.headers = headers
12
13local canonic = {
14    ["accept"]                    = "Accept",
15    ["accept-charset"]            = "Accept-Charset",
16    ["accept-encoding"]           = "Accept-Encoding",
17    ["accept-language"]           = "Accept-Language",
18    ["accept-ranges"]             = "Accept-Ranges",
19    ["action"]                    = "Action",
20    ["alternate-recipient"]       = "Alternate-Recipient",
21    ["age"]                       = "Age",
22    ["allow"]                     = "Allow",
23    ["arrival-date"]              = "Arrival-Date",
24    ["authorization"]             = "Authorization",
25    ["bcc"]                       = "Bcc",
26    ["cache-control"]             = "Cache-Control",
27    ["cc"]                        = "Cc",
28    ["comments"]                  = "Comments",
29    ["connection"]                = "Connection",
30    ["content-description"]       = "Content-Description",
31    ["content-disposition"]       = "Content-Disposition",
32    ["content-encoding"]          = "Content-Encoding",
33    ["content-id"]                = "Content-ID",
34    ["content-language"]          = "Content-Language",
35    ["content-length"]            = "Content-Length",
36    ["content-location"]          = "Content-Location",
37    ["content-md5"]               = "Content-MD5",
38    ["content-range"]             = "Content-Range",
39    ["content-transfer-encoding"] = "Content-Transfer-Encoding",
40    ["content-type"]              = "Content-Type",
41    ["cookie"]                    = "Cookie",
42    ["date"]                      = "Date",
43    ["diagnostic-code"]           = "Diagnostic-Code",
44    ["dsn-gateway"]               = "DSN-Gateway",
45    ["etag"]                      = "ETag",
46    ["expect"]                    = "Expect",
47    ["expires"]                   = "Expires",
48    ["final-log-id"]              = "Final-Log-ID",
49    ["final-recipient"]           = "Final-Recipient",
50    ["from"]                      = "From",
51    ["host"]                      = "Host",
52    ["if-match"]                  = "If-Match",
53    ["if-modified-since"]         = "If-Modified-Since",
54    ["if-none-match"]             = "If-None-Match",
55    ["if-range"]                  = "If-Range",
56    ["if-unmodified-since"]       = "If-Unmodified-Since",
57    ["in-reply-to"]               = "In-Reply-To",
58    ["keywords"]                  = "Keywords",
59    ["last-attempt-date"]         = "Last-Attempt-Date",
60    ["last-modified"]             = "Last-Modified",
61    ["location"]                  = "Location",
62    ["max-forwards"]              = "Max-Forwards",
63    ["message-id"]                = "Message-ID",
64    ["mime-version"]              = "MIME-Version",
65    ["original-envelope-id"]      = "Original-Envelope-ID",
66    ["original-recipient"]        = "Original-Recipient",
67    ["pragma"]                    = "Pragma",
68    ["proxy-authenticate"]        = "Proxy-Authenticate",
69    ["proxy-authorization"]       = "Proxy-Authorization",
70    ["range"]                     = "Range",
71    ["received"]                  = "Received",
72    ["received-from-mta"]         = "Received-From-MTA",
73    ["references"]                = "References",
74    ["referer"]                   = "Referer",
75    ["remote-mta"]                = "Remote-MTA",
76    ["reply-to"]                  = "Reply-To",
77    ["reporting-mta"]             = "Reporting-MTA",
78    ["resent-bcc"]                = "Resent-Bcc",
79    ["resent-cc"]                 = "Resent-Cc",
80    ["resent-date"]               = "Resent-Date",
81    ["resent-from"]               = "Resent-From",
82    ["resent-message-id"]         = "Resent-Message-ID",
83    ["resent-reply-to"]           = "Resent-Reply-To",
84    ["resent-sender"]             = "Resent-Sender",
85    ["resent-to"]                 = "Resent-To",
86    ["retry-after"]               = "Retry-After",
87    ["return-path"]               = "Return-Path",
88    ["sender"]                    = "Sender",
89    ["server"]                    = "Server",
90    ["smtp-remote-recipient"]     = "SMTP-Remote-Recipient",
91    ["status"]                    = "Status",
92    ["subject"]                   = "Subject",
93    ["te"]                        = "TE",
94    ["to"]                        = "To",
95    ["trailer"]                   = "Trailer",
96    ["transfer-encoding"]         = "Transfer-Encoding",
97    ["upgrade"]                   = "Upgrade",
98    ["user-agent"]                = "User-Agent",
99    ["vary"]                      = "Vary",
100    ["via"]                       = "Via",
101    ["warning"]                   = "Warning",
102    ["will-retry-until"]          = "Will-Retry-Until",
103    ["www-authenticate"]          = "WWW-Authenticate",
104    ["x-mailer"]                  = "X-Mailer",
105}
106
107headers.canonic = setmetatable(canonic, {
108    __index = function(t,k)
109        socket.report("invalid header: %s",k)
110        t[k] = k
111        return k
112    end
113})
114
115function headers.normalize(headers)
116    if not headers then
117        return { }
118    end
119    local normalized = { }
120    for k, v in next, headers do
121        normalized[#normalized+1] = canonic[k] .. ": " .. v
122    end
123    normalized[#normalized+1] = ""
124    normalized[#normalized+1] = ""
125    return concat(normalized,"\r\n")
126end
127
128function headers.lower(lowered,headers)
129    if not lowered then
130        return { }
131    end
132    if not headers then
133        lowered, headers = { }, lowered
134    end
135    for k, v in next, headers do
136        lowered[lower(k)] = v
137    end
138    return lowered
139end
140
141socket.headers = headers
142
143package.loaded["socket.headers"] = headers
144
145return headers
146