File: unix.lua

package info (click to toggle)
lua-uri 0.1%2B20130926%2Bgit14fa255d-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, stretch
  • size: 408 kB
  • ctags: 332
  • sloc: makefile: 53
file content (27 lines) | stat: -rw-r--r-- 794 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
local M = { _NAME = "uri.file.unix" }
local URI = require "uri"
local Util = require "uri._util"

function M.filesystem_path (uri)
    if uri:host() ~= "" then
        error("a file URI with a host name can't be converted to a Unix path",
              2)
    end
    local path = uri:path()
    if path:find("%%00") or path:find("%%2F") then
        error("Unix paths cannot contain encoded null bytes or slashes", 2)
    end
    return Util.uri_decode(path)
end

function M.make_file_uri (path)
    if not path:find("^/") then
        error("Unix relative paths can't be converted to file URIs", 2)
    end
    path = path:gsub("//+", "/")
    path = Util.uri_encode(path, "^A-Za-z0-9%-._~!$&'()*+,;=:@/")
    return assert(URI:new("file://" .. path))
end

return M
-- vi:ts=4 sw=4 expandtab