diff options
| author | E. Westbrook <github@westbrook.io> | 2018-08-21 08:03:51 -0600 |
|---|---|---|
| committer | E. Westbrook <github@westbrook.io> | 2018-08-21 08:03:51 -0600 |
| commit | c905b5d44f8cdfbc8110a9a7d1d62c08b5703ae3 (patch) | |
| tree | 67ac58e327ef5abc97b2c5bb957193a84a7a5946 /src | |
| parent | 17a95c126a178b17292637785c6ec09bb1180493 (diff) | |
| download | luasocket-c905b5d44f8cdfbc8110a9a7d1d62c08b5703ae3.tar.gz luasocket-c905b5d44f8cdfbc8110a9a7d1d62c08b5703ae3.tar.bz2 luasocket-c905b5d44f8cdfbc8110a9a7d1d62c08b5703ae3.zip | |
url.lua: separate remove_dot_components() from absolute_path(); also use in _M.absolute() even when not merging
Diffstat (limited to 'src')
| -rw-r--r-- | src/url.lua | 51 |
1 files changed, 34 insertions, 17 deletions
diff --git a/src/url.lua b/src/url.lua index 29b6734..a354ab5 100644 --- a/src/url.lua +++ b/src/url.lua | |||
| @@ -77,24 +77,19 @@ function _M.unescape(s) | |||
| 77 | end | 77 | end |
| 78 | 78 | ||
| 79 | ----------------------------------------------------------------------------- | 79 | ----------------------------------------------------------------------------- |
| 80 | -- Builds a path from a base path and a relative path | 80 | -- Removes '..' and '.' components appropriately from a path. |
| 81 | -- Input | 81 | -- Input |
| 82 | -- base_path | 82 | -- path |
| 83 | -- relative_path | ||
| 84 | -- Returns | 83 | -- Returns |
| 85 | -- corresponding absolute path | 84 | -- dot-normalized path |
| 86 | ----------------------------------------------------------------------------- | 85 | local function remove_dot_components(path) |
| 87 | local function absolute_path(base_path, relative_path) | ||
| 88 | if string.sub(relative_path, 1, 1) == "/" then return relative_path end | ||
| 89 | local path = string.gsub(base_path, "[^/]*$", "") | ||
| 90 | path = path .. relative_path | ||
| 91 | repeat | 86 | repeat |
| 92 | local was = path | 87 | local was = path |
| 93 | path = path:gsub('/%./', '/') | 88 | path = path:gsub('/%./', '/') |
| 94 | until path == was | 89 | until path == was |
| 95 | repeat | 90 | repeat |
| 96 | local was = path | 91 | local was = path |
| 97 | path = path:gsub('[^/]+/%.%./([^/]+)', '%1') | 92 | path = path:gsub('[^/]+/%.%./([^/]+)', '%1') |
| 98 | until path == was | 93 | until path == was |
| 99 | path = path:gsub('[^/]+/%.%./*$', '') | 94 | path = path:gsub('[^/]+/%.%./*$', '') |
| 100 | path = path:gsub('/%.%.$', '/') | 95 | path = path:gsub('/%.%.$', '/') |
| @@ -104,6 +99,23 @@ local function absolute_path(base_path, relative_path) | |||
| 104 | end | 99 | end |
| 105 | 100 | ||
| 106 | ----------------------------------------------------------------------------- | 101 | ----------------------------------------------------------------------------- |
| 102 | -- Builds a path from a base path and a relative path | ||
| 103 | -- Input | ||
| 104 | -- base_path | ||
| 105 | -- relative_path | ||
| 106 | -- Returns | ||
| 107 | -- corresponding absolute path | ||
| 108 | ----------------------------------------------------------------------------- | ||
| 109 | local function absolute_path(base_path, relative_path) | ||
| 110 | if string.sub(relative_path, 1, 1) == "/" then | ||
| 111 | return remove_dot_components(relative_path) end | ||
| 112 | base_path = base_path:gsub("[^/]*$", "") | ||
| 113 | local path = base_path .. relative_path | ||
| 114 | path = remove_dot_components(path) | ||
| 115 | return path | ||
| 116 | end | ||
| 117 | |||
| 118 | ----------------------------------------------------------------------------- | ||
| 107 | -- Parses a url and returns a table with all its parts according to RFC 2396 | 119 | -- Parses a url and returns a table with all its parts according to RFC 2396 |
| 108 | -- The following grammar describes the names given to the URL parts | 120 | -- The following grammar describes the names given to the URL parts |
| 109 | -- <url> ::= <scheme>://<authority>/<path>;<params>?<query>#<fragment> | 121 | -- <url> ::= <scheme>://<authority>/<path>;<params>?<query>#<fragment> |
| @@ -225,10 +237,14 @@ function _M.absolute(base_url, relative_url) | |||
| 225 | else | 237 | else |
| 226 | base_parsed = _M.parse(base_url) | 238 | base_parsed = _M.parse(base_url) |
| 227 | end | 239 | end |
| 240 | local result | ||
| 228 | local relative_parsed = _M.parse(relative_url) | 241 | local relative_parsed = _M.parse(relative_url) |
| 229 | if not base_parsed then return relative_url | 242 | if not base_parsed then |
| 230 | elseif not relative_parsed then return base_url | 243 | result = relative_url |
| 231 | elseif relative_parsed.scheme then return relative_url | 244 | elseif not relative_parsed then |
| 245 | result = base_url | ||
| 246 | elseif relative_parsed.scheme then | ||
| 247 | result = relative_url | ||
| 232 | else | 248 | else |
| 233 | relative_parsed.scheme = base_parsed.scheme | 249 | relative_parsed.scheme = base_parsed.scheme |
| 234 | if not relative_parsed.authority then | 250 | if not relative_parsed.authority then |
| @@ -246,8 +262,9 @@ function _M.absolute(base_url, relative_url) | |||
| 246 | relative_parsed.path) | 262 | relative_parsed.path) |
| 247 | end | 263 | end |
| 248 | end | 264 | end |
| 249 | return _M.build(relative_parsed) | 265 | result = _M.build(relative_parsed) |
| 250 | end | 266 | end |
| 267 | return remove_dot_components(result) | ||
| 251 | end | 268 | end |
| 252 | 269 | ||
| 253 | ----------------------------------------------------------------------------- | 270 | ----------------------------------------------------------------------------- |
