webmcp

view framework/env/encode/mime/unstructured_header_line.lua @ 560:75204c64cc5f

Use CRLF instead of LF for terminating raw header
author jbe
date Tue Jun 09 13:27:58 2020 +0200 (2020-06-09)
parents 9fdfb27f8e67
children
line source
1 function encode.mime.unstructured_header_line(key, value)
2 if not value then
3 return ""
4 end
5 local charset = "UTF-8" -- TODO: support other charsets
6 local key_length = #key + #(": ")
7 if string.find(value, "^[\t -~]*$") then
8 local need_encoding = false
9 local parts = { key, ": " }
10 local line_length = key_length
11 local first_line = true
12 for spaced_word in string.gmatch(value, "[\t ]*[^\t ]*") do
13 if #spaced_word + line_length > 76 then
14 if first_line or #spaced_word > 76 then
15 need_encoding = true
16 break
17 end
18 parts[#parts+1] = "\r\n"
19 line_length = 0
20 end
21 parts[#parts+1] = spaced_word
22 line_length = line_length + #spaced_word
23 first_line = false
24 end
25 if not need_encoding then
26 parts[#parts+1] = "\r\n"
27 return table.concat(parts)
28 end
29 charset = "US-ASCII"
30 end
31 local parts = { key, ": " }
32 local line_length
33 local opening = "=?" .. charset .. "?Q?"
34 local closing = "?="
35 local indentation = ""
36 for i = 1, key_length do
37 indentation = indentation .. " "
38 end
39 local open = false
40 for char in string.gmatch(value, ".") do
41 local encoded_char
42 if string.find(char, "^[0-9A-Za-z%.%-]$") then
43 encoded_char = char
44 else
45 local byte = string.byte(char)
46 if byte == 32 then
47 encoded_char = "_"
48 else
49 encoded_char = string.format("=%02X", byte)
50 end
51 end
52 if open and line_length + #encoded_char > 76 then
53 parts[#parts+1] = closing
54 parts[#parts+1] = "\r\n"
55 parts[#parts+1] = indentation
56 open = false
57 end
58 if not open then
59 parts[#parts+1] = opening
60 line_length = key_length + #opening + #closing
61 open = true
62 end
63 parts[#parts+1] = encoded_char
64 line_length = line_length + #encoded_char
65 end
66 if open then
67 parts[#parts+1] = "?="
68 end
69 parts[#parts+1] = "\r\n"
70 return table.concat(parts)
71 end

Impressum / About Us