summaryrefslogtreecommitdiff
path: root/server/src/3rd/lua-uri/uri/file/win32.lua
blob: d4e40243af6eb59a40c777659e978ba711e26d4d (plain)
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
28
29
30
31
32
33
34
local M = { _NAME = "uri.file.win32" }
local URI = require "uri"
local Util = require "uri._util"

function M.filesystem_path (uri)
    local host = uri:host()
    local path = Util.uri_decode(uri:path())
    if host ~= "" then path = "//" .. host .. path end
    if path:find("^/[A-Za-z]|/") or path:find("^/[A-Za-z]|$") then
        path = path:gsub("|", ":", 1)
    end
    if path:find("^/[A-Za-z]:/") then
        path = path:sub(2)
    elseif path:find("^/[A-Za-z]:$") then
        path = path:sub(2) .. "/"
    end
    path = path:gsub("/", "\\")
    return path
end

function M.make_file_uri (path)
    path = path:gsub("\\", "/")
    if path:find("^[A-Za-z]:$") then path = path .. "/" end
    local _, _, host, hostpath = path:find("^//([A-Za-z0-9.]+)/(.*)$")
    host = host or ""
    hostpath = hostpath or path
    hostpath = hostpath:gsub("//+", "/")
    hostpath = Util.uri_encode(hostpath, "^A-Za-z0-9%-._~!$&'()*+,;=@/")
    if not hostpath:find("^/") then hostpath = "/" .. hostpath end
    return assert(URI:new("file://" .. host .. hostpath))
end

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