aboutsummaryrefslogtreecommitdiff
path: root/src/url.lua
diff options
context:
space:
mode:
Diffstat (limited to 'src/url.lua')
-rw-r--r--src/url.lua50
1 files changed, 25 insertions, 25 deletions
diff --git a/src/url.lua b/src/url.lua
index 135d723..c700f90 100644
--- a/src/url.lua
+++ b/src/url.lua
@@ -20,7 +20,7 @@ _VERSION = "URL 1.0"
20 20
21----------------------------------------------------------------------------- 21-----------------------------------------------------------------------------
22-- Encodes a string into its escaped hexadecimal representation 22-- Encodes a string into its escaped hexadecimal representation
23-- Input 23-- Input
24-- s: binary string to be encoded 24-- s: binary string to be encoded
25-- Returns 25-- Returns
26-- escaped representation of string binary 26-- escaped representation of string binary
@@ -33,8 +33,8 @@ end
33 33
34----------------------------------------------------------------------------- 34-----------------------------------------------------------------------------
35-- Protects a path segment, to prevent it from interfering with the 35-- Protects a path segment, to prevent it from interfering with the
36-- url parsing. 36-- url parsing.
37-- Input 37-- Input
38-- s: binary string to be encoded 38-- s: binary string to be encoded
39-- Returns 39-- Returns
40-- escaped representation of string binary 40-- escaped representation of string binary
@@ -50,12 +50,12 @@ end
50-- these are allowed withing a path segment, along with alphanum 50-- these are allowed withing a path segment, along with alphanum
51-- other characters must be escaped 51-- other characters must be escaped
52local segment_set = make_set { 52local segment_set = make_set {
53 "-", "_", ".", "!", "~", "*", "'", "(", 53 "-", "_", ".", "!", "~", "*", "'", "(",
54 ")", ":", "@", "&", "=", "+", "$", ",", 54 ")", ":", "@", "&", "=", "+", "$", ",",
55} 55}
56 56
57local function protect_segment(s) 57local function protect_segment(s)
58 return string.gsub(s, "([^A-Za-z0-9_])", function (c) 58 return string.gsub(s, "([^A-Za-z0-9_])", function (c)
59 if segment_set[c] then return c 59 if segment_set[c] then return c
60 else return string.format("%%%02x", string.byte(c)) end 60 else return string.format("%%%02x", string.byte(c)) end
61 end) 61 end)
@@ -63,7 +63,7 @@ end
63 63
64----------------------------------------------------------------------------- 64-----------------------------------------------------------------------------
65-- Encodes a string into its escaped hexadecimal representation 65-- Encodes a string into its escaped hexadecimal representation
66-- Input 66-- Input
67-- s: binary string to be encoded 67-- s: binary string to be encoded
68-- Returns 68-- Returns
69-- escaped representation of string binary 69-- escaped representation of string binary
@@ -86,11 +86,11 @@ local function absolute_path(base_path, relative_path)
86 if string.sub(relative_path, 1, 1) == "/" then return relative_path end 86 if string.sub(relative_path, 1, 1) == "/" then return relative_path end
87 local path = string.gsub(base_path, "[^/]*$", "") 87 local path = string.gsub(base_path, "[^/]*$", "")
88 path = path .. relative_path 88 path = path .. relative_path
89 path = string.gsub(path, "([^/]*%./)", function (s) 89 path = string.gsub(path, "([^/]*%./)", function (s)
90 if s ~= "./" then return s else return "" end 90 if s ~= "./" then return s else return "" end
91 end) 91 end)
92 path = string.gsub(path, "/%.$", "/") 92 path = string.gsub(path, "/%.$", "/")
93 local reduced 93 local reduced
94 while reduced ~= path do 94 while reduced ~= path do
95 reduced = path 95 reduced = path
96 path = string.gsub(reduced, "([^/]*/%.%./)", function (s) 96 path = string.gsub(reduced, "([^/]*/%.%./)", function (s)
@@ -116,7 +116,7 @@ end
116-- Returns 116-- Returns
117-- table with the following fields, where RFC naming conventions have 117-- table with the following fields, where RFC naming conventions have
118-- been preserved: 118-- been preserved:
119-- scheme, authority, userinfo, user, password, host, port, 119-- scheme, authority, userinfo, user, password, host, port,
120-- path, params, query, fragment 120-- path, params, query, fragment
121-- Obs: 121-- Obs:
122-- the leading '/' in {/<path>} is considered part of <path> 122-- the leading '/' in {/<path>} is considered part of <path>
@@ -130,26 +130,26 @@ function parse(url, default)
130 -- remove whitespace 130 -- remove whitespace
131 -- url = string.gsub(url, "%s", "") 131 -- url = string.gsub(url, "%s", "")
132 -- get fragment 132 -- get fragment
133 url = string.gsub(url, "#(.*)$", function(f) 133 url = string.gsub(url, "#(.*)$", function(f)
134 parsed.fragment = f 134 parsed.fragment = f
135 return "" 135 return ""
136 end) 136 end)
137 -- get scheme 137 -- get scheme
138 url = string.gsub(url, "^([%w][%w%+%-%.]*)%:", 138 url = string.gsub(url, "^([%w][%w%+%-%.]*)%:",
139 function(s) parsed.scheme = s; return "" end) 139 function(s) parsed.scheme = s; return "" end)
140 -- get authority 140 -- get authority
141 url = string.gsub(url, "^//([^/]*)", function(n) 141 url = string.gsub(url, "^//([^/]*)", function(n)
142 parsed.authority = n 142 parsed.authority = n
143 return "" 143 return ""
144 end) 144 end)
145 -- get query stringing 145 -- get query stringing
146 url = string.gsub(url, "%?(.*)", function(q) 146 url = string.gsub(url, "%?(.*)", function(q)
147 parsed.query = q 147 parsed.query = q
148 return "" 148 return ""
149 end) 149 end)
150 -- get params 150 -- get params
151 url = string.gsub(url, "%;(.*)", function(p) 151 url = string.gsub(url, "%;(.*)", function(p)
152 parsed.params = p 152 parsed.params = p
153 return "" 153 return ""
154 end) 154 end)
155 -- path is whatever was left 155 -- path is whatever was left
@@ -158,14 +158,14 @@ function parse(url, default)
158 if not authority then return parsed end 158 if not authority then return parsed end
159 authority = string.gsub(authority,"^([^@]*)@", 159 authority = string.gsub(authority,"^([^@]*)@",
160 function(u) parsed.userinfo = u; return "" end) 160 function(u) parsed.userinfo = u; return "" end)
161 authority = string.gsub(authority, ":([^:]*)$", 161 authority = string.gsub(authority, ":([^:]*)$",
162 function(p) parsed.port = p; return "" end) 162 function(p) parsed.port = p; return "" end)
163 if authority ~= "" then parsed.host = authority end 163 if authority ~= "" then parsed.host = authority end
164 local userinfo = parsed.userinfo 164 local userinfo = parsed.userinfo
165 if not userinfo then return parsed end 165 if not userinfo then return parsed end
166 userinfo = string.gsub(userinfo, ":([^:]*)$", 166 userinfo = string.gsub(userinfo, ":([^:]*)$",
167 function(p) parsed.password = p; return "" end) 167 function(p) parsed.password = p; return "" end)
168 parsed.user = userinfo 168 parsed.user = userinfo
169 return parsed 169 return parsed
170end 170end
171 171
@@ -189,8 +189,8 @@ function build(parsed)
189 local userinfo = parsed.userinfo 189 local userinfo = parsed.userinfo
190 if parsed.user then 190 if parsed.user then
191 userinfo = parsed.user 191 userinfo = parsed.user
192 if parsed.password then 192 if parsed.password then
193 userinfo = userinfo .. ":" .. parsed.password 193 userinfo = userinfo .. ":" .. parsed.password
194 end 194 end
195 end 195 end
196 if userinfo then authority = userinfo .. "@" .. authority end 196 if userinfo then authority = userinfo .. "@" .. authority end
@@ -233,8 +233,8 @@ function absolute(base_url, relative_url)
233 relative_parsed.query = base_parsed.query 233 relative_parsed.query = base_parsed.query
234 end 234 end
235 end 235 end
236 else 236 else
237 relative_parsed.path = absolute_path(base_parsed.path or "", 237 relative_parsed.path = absolute_path(base_parsed.path or "",
238 relative_parsed.path) 238 relative_parsed.path)
239 end 239 end
240 end 240 end