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