Mercurial > prosody-hg
comparison tools/http-status-codes.lua @ 9166:cce55767004a
tools: Add a tool to generate net.http.codes from IANA registry
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Thu, 08 Feb 2018 17:35:42 +0100 |
| parents | |
| children | f31bb79f51d7 |
comparison
equal
deleted
inserted
replaced
| 9165:2aaa9f3bb950 | 9166:cce55767004a |
|---|---|
| 1 -- Generate net/http/codes.lua from IANA HTTP status code registry | |
| 2 | |
| 3 local xml = require "util.xml"; | |
| 4 local registry = xml.parse(io.read("*a")); | |
| 5 | |
| 6 io.write([[ | |
| 7 | |
| 8 local response_codes = { | |
| 9 -- Source: http://www.iana.org/assignments/http-status-codes | |
| 10 ]]); | |
| 11 | |
| 12 for record in registry:get_child("registry"):childtags("record") do | |
| 13 -- Extract values | |
| 14 local value = record:get_child_text("value"); | |
| 15 local description = record:get_child_text("description"); | |
| 16 local ref = record:get_child_text("xref"); | |
| 17 local code = tonumber(value); | |
| 18 | |
| 19 -- Space between major groups | |
| 20 if code and code % 100 == 0 then | |
| 21 io.write("\n"); | |
| 22 end | |
| 23 | |
| 24 -- Reserved and Unassigned entries should be not be included | |
| 25 if description == "Reserved" or description == "Unassigned" or description == "(Unused)" then | |
| 26 code = nil; | |
| 27 end | |
| 28 | |
| 29 -- Non-empty references become comments | |
| 30 if ref and ref:find("%S") then | |
| 31 ref = " -- " .. ref; | |
| 32 else | |
| 33 ref = ""; | |
| 34 end | |
| 35 | |
| 36 io.write((code and "\t[%d] = %q;%s\n" or "\t-- [%s] = %q;%s\n"):format(code or value, description, ref)); | |
| 37 end | |
| 38 | |
| 39 io.write([[}; | |
| 40 | |
| 41 for k,v in pairs(response_codes) do response_codes[k] = k.." "..v; end | |
| 42 return setmetatable(response_codes, { __index = function(_, k) return k.." Unassigned"; end }) | |
| 43 ]]); |
