Created
June 18, 2014 02:39
-
-
Save leite/b085ccef34d8b3b082cb to your computer and use it in GitHub Desktop.
parsing accept header(s)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- @xxleite under [BeerWare] license | |
local string, io = require [[string]], require [[io]] | |
local gmatch, format, byte, write = string.gmatch, string.format, string.byte, io.write | |
-- common accept(s) content | |
local charsets = { | |
'*', 'utf-8', 'utf-8, ISO-8859-1', 'utf-8;q=0.8, ISO-8859-1', | |
'utf-8;q=0.8, ISO-8859-1', '*, utf-8;q=0', '*, utf-8' | |
} | |
local encodings = { | |
'gzip', 'gzip, compress', 'deflate', '*', 'gzip, compress', | |
'gzip;q=0.8, identity;q=0.5, *;q=0.3', 'gzip;q=0.8, compress', | |
'*, compress;q=0', 'gzip;q=0.8, compress', '*, compress' | |
} | |
local languages = { | |
'en', '*', 'en-US, en;q=0.8', 'en-US, en-GB', 'en;q=0.8, es', | |
'en-US;q=0.8, es', '*, en;q=0', 'en-US;q=0.8, es', '*, en' | |
} | |
local media = { | |
'text/html', '*/*', 'text/*', 'application/json, text/html', 'text/html;q=0.1', | |
'application/json, text/html', 'application/json;q=0.2, text/html', | |
'application/json;q=0.2, text/html', 'text/*, text/html;q=0', 'text/*, text/html;q=0.5', | |
'application/json, */*; q=0.01', 'application/vnd.example;attribute=value', | |
'application/vnd.example;attribute=other', 'text/html;level=1', 'text/html;level=1;foo=bar', | |
'text/html;level=2', 'text/html, text/html;level=1;q=0.1', | |
'text/*;q=0.3, text/html;q=0.7, text/html;level=1, text/html;level=2;q=0.4, */*;q=0.5', | |
'text/html, application/xhtml+xml, */*' | |
} | |
-- basic parsers | |
local function parse_one(str) | |
for a, b, c, d in gmatch(str, '%s*([^%s-=;,]+)%s*([-=,]?)%s*([^%s;,]*)%s*(,?)') do | |
write(format('\n [%s]%s[%s]%s (%d)(%d)', a, b, c, d, byte(b) or 0, byte(d) or 0)) | |
end | |
end | |
local function parse_two(str) | |
for a, b, c, d in gmatch(str, '%s*([^%s=;,]+)%s*([=,]?)%s*([^%s;,]*)%s*(,?)') do | |
write(format('\n [%s]%s[%s]%s (%d)(%d)', a, b, c, d, byte(b) or 0, byte(d) or 0)) | |
end | |
end | |
local function parse_three(str) | |
for a, b, c, d in gmatch(str, '%s*([^/=%s]+)%s*([/=])%s*([^;,%s]+)%s*([;,]?)') do | |
write(format('\n [%s]%s[%s]%s (%d)(%d)', a, b, c, d, byte(b) or 0, byte(d) or 0)) | |
end | |
end | |
-- test all | |
for i=1, #charsets do | |
write(charsets[i]) | |
parse_two(charsets[i]) | |
write('\n\n') | |
end | |
for i=1, #encodings do | |
write(encodings[i]) | |
parse_two(encodings[i]) | |
write('\n\n') | |
end | |
for i=1, #languages do | |
write(languages[i]) | |
parse_one(languages[i]) | |
write('\n\n') | |
end | |
for i=1, #media do | |
write(media[i]) | |
parse_three(media[i]) | |
write('\n\n') | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment