diff options
author | Diego Nehab <diego@tecgraf.puc-rio.br> | 2001-09-26 20:38:47 +0000 |
---|---|---|
committer | Diego Nehab <diego@tecgraf.puc-rio.br> | 2001-09-26 20:38:47 +0000 |
commit | ef5322a92af5c193f04b599c01f0566ddc5ed5a2 (patch) | |
tree | ae234eff25f37d9bb570e21ea4817f044e7914a6 /src | |
parent | 6f02d1444bdf351f1008eb09546802985da49fdb (diff) | |
download | luasocket-ef5322a92af5c193f04b599c01f0566ddc5ed5a2.tar.gz luasocket-ef5322a92af5c193f04b599c01f0566ddc5ed5a2.tar.bz2 luasocket-ef5322a92af5c193f04b599c01f0566ddc5ed5a2.zip |
Changed build_url to compose from smaller parts
Changed build_path to add an option not to protect path components
Diffstat (limited to 'src')
-rw-r--r-- | src/url.lua | 44 |
1 files changed, 35 insertions, 9 deletions
diff --git a/src/url.lua b/src/url.lua index 8cafd9b..181a4bd 100644 --- a/src/url.lua +++ b/src/url.lua | |||
@@ -70,7 +70,20 @@ function Public.build_url(parsed) | |||
70 | local url = parsed.path or "" | 70 | local url = parsed.path or "" |
71 | if parsed.params then url = url .. ";" .. parsed.params end | 71 | if parsed.params then url = url .. ";" .. parsed.params end |
72 | if parsed.query then url = url .. "?" .. parsed.query end | 72 | if parsed.query then url = url .. "?" .. parsed.query end |
73 | if parsed.authority then url = "//" .. parsed.authority .. url end | 73 | local authority = parsed.authority |
74 | if parsed.host then | ||
75 | authority = parsed.host | ||
76 | if parsed.port then authority = authority .. ":" .. parsed.port end | ||
77 | local userinfo = parsed.userinfo | ||
78 | if parsed.user then | ||
79 | userinfo = parsed.user | ||
80 | if parsed.password then | ||
81 | userinfo = userinfo .. ":" .. parsed.password | ||
82 | end | ||
83 | end | ||
84 | if userinfo then authority = userinfo .. "@" .. authority end | ||
85 | end | ||
86 | if authority then url = "//" .. authority .. url end | ||
74 | if parsed.scheme then url = parsed.scheme .. ":" .. url end | 87 | if parsed.scheme then url = parsed.scheme .. ":" .. url end |
75 | if parsed.fragment then url = url .. "#" .. parsed.fragment end | 88 | if parsed.fragment then url = url .. "#" .. parsed.fragment end |
76 | return gsub(url, "%s", "") | 89 | return gsub(url, "%s", "") |
@@ -119,6 +132,7 @@ end | |||
119 | ----------------------------------------------------------------------------- | 132 | ----------------------------------------------------------------------------- |
120 | function Public.parse_path(path) | 133 | function Public.parse_path(path) |
121 | local parsed = {} | 134 | local parsed = {} |
135 | path = path or "" | ||
122 | path = gsub(path, "%s", "") | 136 | path = gsub(path, "%s", "") |
123 | gsub(path, "([^/]+)", function (s) tinsert(%parsed, s) end) | 137 | gsub(path, "([^/]+)", function (s) tinsert(%parsed, s) end) |
124 | for i = 1, getn(parsed) do | 138 | for i = 1, getn(parsed) do |
@@ -133,19 +147,31 @@ end | |||
133 | -- Builds a path component from its segments, escaping protected characters. | 147 | -- Builds a path component from its segments, escaping protected characters. |
134 | -- Input | 148 | -- Input |
135 | -- parsed: path segments | 149 | -- parsed: path segments |
150 | -- unsafe: if true, segments are not protected before path is built | ||
136 | -- Returns | 151 | -- Returns |
137 | -- path: correspondin path string | 152 | -- path: correspondin path string |
138 | ----------------------------------------------------------------------------- | 153 | ----------------------------------------------------------------------------- |
139 | function Public.build_path(parsed) | 154 | function Public.build_path(parsed, unsafe) |
140 | local path = "" | 155 | local path = "" |
141 | local n = getn(parsed) | 156 | local n = getn(parsed) |
142 | for i = 1, n-1 do | 157 | if unsafe then |
143 | path = path .. %Private.protect_segment(parsed[i]) | 158 | for i = 1, n-1 do |
144 | path = path .. "/" | 159 | path = path .. parsed[i] |
145 | end | 160 | path = path .. "/" |
146 | if n > 0 then | 161 | end |
147 | path = path .. %Private.protect_segment(parsed[n]) | 162 | if n > 0 then |
148 | if parsed.is_directory then path = path .. "/" end | 163 | path = path .. parsed[n] |
164 | if parsed.is_directory then path = path .. "/" end | ||
165 | end | ||
166 | else | ||
167 | for i = 1, n-1 do | ||
168 | path = path .. %Private.protect_segment(parsed[i]) | ||
169 | path = path .. "/" | ||
170 | end | ||
171 | if n > 0 then | ||
172 | path = path .. %Private.protect_segment(parsed[n]) | ||
173 | if parsed.is_directory then path = path .. "/" end | ||
174 | end | ||
149 | end | 175 | end |
150 | if parsed.is_absolute then path = "/" .. path end | 176 | if parsed.is_absolute then path = "/" .. path end |
151 | return path | 177 | return path |