summaryrefslogtreecommitdiff
path: root/server/src/3rd/lua-uri/uri/urn
diff options
context:
space:
mode:
Diffstat (limited to 'server/src/3rd/lua-uri/uri/urn')
-rw-r--r--server/src/3rd/lua-uri/uri/urn/isbn.lua67
-rw-r--r--server/src/3rd/lua-uri/uri/urn/issn.lua65
-rw-r--r--server/src/3rd/lua-uri/uri/urn/oid.lua62
3 files changed, 194 insertions, 0 deletions
diff --git a/server/src/3rd/lua-uri/uri/urn/isbn.lua b/server/src/3rd/lua-uri/uri/urn/isbn.lua
new file mode 100644
index 00000000..5f0bdb69
--- /dev/null
+++ b/server/src/3rd/lua-uri/uri/urn/isbn.lua
@@ -0,0 +1,67 @@
+local M = { _NAME = "uri.urn.isbn" }
+local Util = require "uri._util"
+local URN = require "uri.urn"
+Util.subclass_of(M, URN)
+
+-- This implements the 'isbn' NID defined in RFC 3187, and is consistent
+-- with the same NID suggested in RFC 2288.
+
+local function _valid_isbn (isbn)
+ if not isbn:find("^[-%d]+[%dXx]$") then return nil, "invalid character" end
+ local ISBN = Util.attempt_require("isbn")
+ if ISBN then return ISBN:new(isbn) end
+ return isbn
+end
+
+local function _normalize_isbn (isbn)
+ isbn = isbn:gsub("%-", ""):upper()
+ local ISBN = Util.attempt_require("isbn")
+ if ISBN then return tostring(ISBN:new(isbn)) end
+ return isbn
+end
+
+function M.init (self)
+ local nss = self:nss()
+ local ok, msg = _valid_isbn(nss)
+ if not ok then return nil, "invalid ISBN value (" .. msg .. ")" end
+ self:nss(_normalize_isbn(nss))
+ return self
+end
+
+function M.nss (self, new)
+ local old = M._SUPER.nss(self)
+
+ if new then
+ local ok, msg = _valid_isbn(new)
+ if not ok then
+ error("bad ISBN value '" .. new .. "' (" .. msg .. ")")
+ end
+ M._SUPER.nss(self, _normalize_isbn(new))
+ end
+
+ return old
+end
+
+function M.isbn_digits (self, new)
+ local old = self:nss():gsub("%-", "")
+
+ if new then
+ local ok, msg = _valid_isbn(new)
+ if not ok then
+ error("bad ISBN value '" .. new .. "' (" .. msg .. ")")
+ end
+ self._SUPER.nss(self, _normalize_isbn(new))
+ end
+
+ return old
+end
+
+function M.isbn (self, new)
+ local ISBN = require "isbn"
+ local old = ISBN:new(self:nss())
+ if new then self:nss(tostring(new)) end
+ return old
+end
+
+return M
+-- vi:ts=4 sw=4 expandtab
diff --git a/server/src/3rd/lua-uri/uri/urn/issn.lua b/server/src/3rd/lua-uri/uri/urn/issn.lua
new file mode 100644
index 00000000..c5f37f8c
--- /dev/null
+++ b/server/src/3rd/lua-uri/uri/urn/issn.lua
@@ -0,0 +1,65 @@
+local M = { _NAME = "uri.urn.issn" }
+local Util = require "uri._util"
+local URN = require "uri.urn"
+Util.subclass_of(M, URN)
+
+local function _parse_issn (issn)
+ local _, _, nums1, nums2, checksum
+ = issn:find("^(%d%d%d%d)-?(%d%d%d)([%dxX])$")
+ if checksum == "x" then checksum = "X" end
+ return nums1, nums2, checksum
+end
+
+local function _valid_issn (issn)
+ local nums1, nums2, actual_checksum = _parse_issn(issn)
+ if not nums1 then return nil, "invalid ISSN syntax" end
+ local nums = nums1 .. nums2
+
+ local expected_checksum = 0
+ for i = 1, 7 do
+ expected_checksum = expected_checksum + tonumber(nums:sub(i, i)) * (9 - i)
+ end
+ expected_checksum = (11 - expected_checksum % 11) % 11
+ expected_checksum = (expected_checksum == 10) and "X"
+ or tostring(expected_checksum)
+ if actual_checksum ~= expected_checksum then
+ return nil, "wrong checksum, expected " .. expected_checksum
+ end
+
+ return true
+end
+
+local function _normalize_issn (issn)
+ local nums1, nums2, checksum = _parse_issn(issn)
+ return nums1 .. "-" .. nums2 .. checksum
+end
+
+function M.init (self)
+ local nss = self:nss()
+ local ok, msg = _valid_issn(nss)
+ if not ok then return nil, "bad NSS value for ISSN URI (" .. msg .. ")" end
+ M._SUPER.nss(self, _normalize_issn(nss))
+ return self
+end
+
+function M.nss (self, new)
+ local old = M._SUPER.nss(self)
+
+ if new then
+ local ok, msg = _valid_issn(new)
+ if not ok then
+ error("bad ISSN value '" .. new .. "' (" .. msg .. ")")
+ end
+ M._SUPER.nss(self, _normalize_issn(new))
+ end
+
+ return old
+end
+
+function M.issn_digits (self, new)
+ local old = self:nss(new)
+ return old:sub(1, 4) .. old:sub(6, 9)
+end
+
+return M
+-- vi:ts=4 sw=4 expandtab
diff --git a/server/src/3rd/lua-uri/uri/urn/oid.lua b/server/src/3rd/lua-uri/uri/urn/oid.lua
new file mode 100644
index 00000000..37110cda
--- /dev/null
+++ b/server/src/3rd/lua-uri/uri/urn/oid.lua
@@ -0,0 +1,62 @@
+local M = { _NAME = "uri.urn.oid" }
+local Util = require "uri._util"
+local URN = require "uri.urn"
+Util.subclass_of(M, URN)
+
+-- This implements RFC 3061.
+
+local function _valid_oid (oid)
+ if oid == "" then return nil, "OID can't be zero-length" end
+ if not oid:find("^[.0-9]*$") then return nil, "bad character in OID" end
+ if oid:find("%.%.") then return nil, "missing number in OID" end
+ if oid:find("^0[^.]") or oid:find("%.0[^.]") then
+ return nil, "OID numbers shouldn't have leading zeros"
+ end
+ return true
+end
+
+function M.init (self)
+ local nss = self:nss()
+ local ok, msg = _valid_oid(nss)
+ if not ok then return nil, "bad NSS value for OID URI (" .. msg .. ")" end
+ return self
+end
+
+function M.nss (self, new)
+ local old = M._SUPER.nss(self)
+
+ if new then
+ local ok, msg = _valid_oid(new)
+ if not ok then
+ error("bad OID value '" .. new .. "' (" .. msg .. ")")
+ end
+ M._SUPER.nss(self, new)
+ end
+
+ return old
+end
+
+function M.oid_numbers (self, new)
+ local old = Util.split("%.", self:nss())
+ for i = 1, #old do old[i] = tonumber(old[i]) end
+
+ if new then
+ if type(new) ~= "table" then error("expected array of numbers") end
+ local nss = ""
+ for _, n in ipairs(new) do
+ if type(n) == "string" and n:find("^%d+$") then n = tonumber(n) end
+ if type(n) ~= "number" then error("bad type for number in OID") end
+ n = n - n % 1
+ if n < 0 then error("negative numbers not allowed in OID") end
+ if nss ~= "" then nss = nss .. "." end
+ nss = nss .. n
+ end
+ if nss == "" then error("no numbers in new OID value") end
+ self:nss(nss)
+ end
+
+ return old
+end
+
+return M
+-- vi:ts=4 sw=4 expandtab