diff options
author | Diego Nehab <diego@tecgraf.puc-rio.br> | 2004-03-26 00:18:41 +0000 |
---|---|---|
committer | Diego Nehab <diego@tecgraf.puc-rio.br> | 2004-03-26 00:18:41 +0000 |
commit | e77f1792005088f55353c6c85fe9335e60772754 (patch) | |
tree | 097b71fc87bae4e1b3a8dd4e0a7869e6976fd9c1 | |
parent | 5b279bac9eea05d764e9cd14c89a699da8898126 (diff) | |
download | luasocket-e77f1792005088f55353c6c85fe9335e60772754.tar.gz luasocket-e77f1792005088f55353c6c85fe9335e60772754.tar.bz2 luasocket-e77f1792005088f55353c6c85fe9335e60772754.zip |
Adjusted some of the broken examples.
-rw-r--r-- | etc/check-links.lua | 41 | ||||
-rw-r--r-- | etc/get.lua | 3 | ||||
-rw-r--r-- | src/inet.c | 1 | ||||
-rw-r--r-- | src/url.lua | 12 | ||||
-rw-r--r-- | test/urltest.lua | 6 |
5 files changed, 28 insertions, 35 deletions
diff --git a/etc/check-links.lua b/etc/check-links.lua index c45131c..03ca6de 100644 --- a/etc/check-links.lua +++ b/etc/check-links.lua | |||
@@ -9,11 +9,11 @@ socket.http.TIMEOUT = 10 | |||
9 | cache = {} | 9 | cache = {} |
10 | 10 | ||
11 | function readfile(path) | 11 | function readfile(path) |
12 | path = socket.code.unescape(path) | 12 | path = socket.url.unescape(path) |
13 | local file, error = openfile(path, "r") | 13 | local file, error = io.open(path, "r") |
14 | if file then | 14 | if file then |
15 | local body = read(file, "*a") | 15 | local body = file:read("*a") |
16 | closefile(file) | 16 | file:close() |
17 | return body | 17 | return body |
18 | else return nil, error end | 18 | else return nil, error end |
19 | end | 19 | end |
@@ -23,20 +23,14 @@ function getstatus(url) | |||
23 | if cache[url] then return cache[url] end | 23 | if cache[url] then return cache[url] end |
24 | local res | 24 | local res |
25 | if parsed.scheme == "http" then | 25 | if parsed.scheme == "http" then |
26 | local request = { url = url } | 26 | local request = { url = url, method = "HEAD" } |
27 | local response = { body_cb = function(chunk, err) | 27 | local response = socket.http.request(request) |
28 | return nil | ||
29 | end } | ||
30 | local blocksize = socket.http.BLOCKSIZE | ||
31 | socket.http.BLOCKSIZE = 1 | ||
32 | response = socket.http.request_cb(request, response) | ||
33 | socket.http.BLOCKSIZE = blocksize | ||
34 | if response.code == 200 then res = nil | 28 | if response.code == 200 then res = nil |
35 | else res = response.status or response.error end | 29 | else res = response.status or response.error end |
36 | elseif parsed.scheme == "file" then | 30 | elseif parsed.scheme == "file" then |
37 | local file, error = openfile(Code.unescape(parsed.path), "r") | 31 | local file, error = io.open(socket.url.unescape(parsed.path), "r") |
38 | if file then | 32 | if file then |
39 | closefile(file) | 33 | file:close() |
40 | res = nil | 34 | res = nil |
41 | else res = error end | 35 | else res = error end |
42 | else res = string.format("unhandled scheme '%s'", parsed.scheme) end | 36 | else res = string.format("unhandled scheme '%s'", parsed.scheme) end |
@@ -46,15 +40,12 @@ end | |||
46 | 40 | ||
47 | function retrieve(url) | 41 | function retrieve(url) |
48 | local parsed = socket.url.parse(url, { scheme = "file" }) | 42 | local parsed = socket.url.parse(url, { scheme = "file" }) |
49 | local base, body, error | 43 | local body, headers, code, error |
50 | base = url | 44 | local base = url |
51 | if parsed.scheme == "http" then | 45 | if parsed.scheme == "http" then |
52 | local response = socket.http.request{url = url} | 46 | body, headers, code, error = socket.http.get(url) |
53 | if response.code ~= 200 then | 47 | if code == 200 then |
54 | error = response.status or response.error | 48 | base = base or headers.location |
55 | else | ||
56 | base = response.headers.location or url | ||
57 | body = response.body | ||
58 | end | 49 | end |
59 | elseif parsed.scheme == "file" then | 50 | elseif parsed.scheme == "file" then |
60 | body, error = readfile(parsed.path) | 51 | body, error = readfile(parsed.path) |
@@ -67,13 +58,13 @@ function getlinks(body, base) | |||
67 | body = string.gsub(body, "%<%!%-%-.-%-%-%>", "") | 58 | body = string.gsub(body, "%<%!%-%-.-%-%-%>", "") |
68 | local links = {} | 59 | local links = {} |
69 | -- extract links | 60 | -- extract links |
70 | string.gsub(body, '[Hh][Rr][Ee][Ff]%s*=%s*"([^"]*)"', function(href) | 61 | body = string.gsub(body, '[Hh][Rr][Ee][Ff]%s*=%s*"([^"]*)"', function(href) |
71 | table.insert(links, socket.url.absolute(base, href)) | 62 | table.insert(links, socket.url.absolute(base, href)) |
72 | end) | 63 | end) |
73 | string.gsub(body, "[Hh][Rr][Ee][Ff]%s*=%s*'([^']*)'", function(href) | 64 | body = string.gsub(body, "[Hh][Rr][Ee][Ff]%s*=%s*'([^']*)'", function(href) |
74 | table.insert(links, socket.url.absolute(base, href)) | 65 | table.insert(links, socket.url.absolute(base, href)) |
75 | end) | 66 | end) |
76 | string.gsub(body, "[Hh][Rr][Ee][Ff]%s*=%s*(%a+)", function(href) | 67 | string.gsub(body, "[Hh][Rr][Ee][Ff]%s*=%s*(.-)>", function(href) |
77 | table.insert(links, socket.url.absolute(base, href)) | 68 | table.insert(links, socket.url.absolute(base, href)) |
78 | end) | 69 | end) |
79 | return links | 70 | return links |
diff --git a/etc/get.lua b/etc/get.lua index eafebda..aa29ec2 100644 --- a/etc/get.lua +++ b/etc/get.lua | |||
@@ -71,12 +71,11 @@ function stats(size) | |||
71 | io.stderr:write("\r", gauge(got, delta, size)) | 71 | io.stderr:write("\r", gauge(got, delta, size)) |
72 | io.stderr:flush() | 72 | io.stderr:flush() |
73 | end | 73 | end |
74 | return chunk | ||
75 | else | 74 | else |
76 | -- close up | 75 | -- close up |
77 | io.stderr:write("\r", gauge(got, delta), "\n") | 76 | io.stderr:write("\r", gauge(got, delta), "\n") |
78 | return "" | ||
79 | end | 77 | end |
78 | return chunk | ||
80 | end | 79 | end |
81 | end | 80 | end |
82 | 81 | ||
@@ -44,6 +44,7 @@ int inet_open(lua_State *L) | |||
44 | luaL_openlib(L, NULL, func, 0); | 44 | luaL_openlib(L, NULL, func, 0); |
45 | lua_settable(L, -3); | 45 | lua_settable(L, -3); |
46 | lua_pop(L, 1); | 46 | lua_pop(L, 1); |
47 | return 0; | ||
47 | } | 48 | } |
48 | 49 | ||
49 | /*=========================================================================*\ | 50 | /*=========================================================================*\ |
diff --git a/src/url.lua b/src/url.lua index de0474b..2b9e4dc 100644 --- a/src/url.lua +++ b/src/url.lua | |||
@@ -127,7 +127,7 @@ function parse(url, default) | |||
127 | -- empty url is parsed to nil | 127 | -- empty url is parsed to nil |
128 | if not url or url == "" then return nil end | 128 | if not url or url == "" then return nil end |
129 | -- remove whitespace | 129 | -- remove whitespace |
130 | url = string.gsub(url, "%s", "") | 130 | -- url = string.gsub(url, "%s", "") |
131 | -- get fragment | 131 | -- get fragment |
132 | url = string.gsub(url, "#(.*)$", function(f) parsed.fragment = f end) | 132 | url = string.gsub(url, "#(.*)$", function(f) parsed.fragment = f end) |
133 | -- get scheme | 133 | -- get scheme |
@@ -139,6 +139,7 @@ function parse(url, default) | |||
139 | url = string.gsub(url, "%?(.*)", function(q) parsed.query = q end) | 139 | url = string.gsub(url, "%?(.*)", function(q) parsed.query = q end) |
140 | -- get params | 140 | -- get params |
141 | url = string.gsub(url, "%;(.*)", function(p) parsed.params = p end) | 141 | url = string.gsub(url, "%;(.*)", function(p) parsed.params = p end) |
142 | -- path is whatever was left | ||
142 | if url ~= "" then parsed.path = url end | 143 | if url ~= "" then parsed.path = url end |
143 | local authority = parsed.authority | 144 | local authority = parsed.authority |
144 | if not authority then return parsed end | 145 | if not authority then return parsed end |
@@ -164,7 +165,8 @@ end | |||
164 | -- a stringing with the corresponding URL | 165 | -- a stringing with the corresponding URL |
165 | ----------------------------------------------------------------------------- | 166 | ----------------------------------------------------------------------------- |
166 | function build(parsed) | 167 | function build(parsed) |
167 | local url = parsed.path or "" | 168 | local ppath = parse_path(parsed.path or "") |
169 | local url = build_path(ppath) | ||
168 | if parsed.params then url = url .. ";" .. parsed.params end | 170 | if parsed.params then url = url .. ";" .. parsed.params end |
169 | if parsed.query then url = url .. "?" .. parsed.query end | 171 | if parsed.query then url = url .. "?" .. parsed.query end |
170 | local authority = parsed.authority | 172 | local authority = parsed.authority |
@@ -183,7 +185,7 @@ function build(parsed) | |||
183 | if authority then url = "//" .. authority .. url end | 185 | if authority then url = "//" .. authority .. url end |
184 | if parsed.scheme then url = parsed.scheme .. ":" .. url end | 186 | if parsed.scheme then url = parsed.scheme .. ":" .. url end |
185 | if parsed.fragment then url = url .. "#" .. parsed.fragment end | 187 | if parsed.fragment then url = url .. "#" .. parsed.fragment end |
186 | url = string.gsub(url, "%s", "") | 188 | -- url = string.gsub(url, "%s", "") |
187 | return url | 189 | return url |
188 | end | 190 | end |
189 | 191 | ||
@@ -214,7 +216,7 @@ function absolute(base_url, relative_url) | |||
214 | end | 216 | end |
215 | end | 217 | end |
216 | else | 218 | else |
217 | relative.path = absolute_path(base.path,relative.path) | 219 | relative.path = absolute_path(base.path or "", relative.path) |
218 | end | 220 | end |
219 | end | 221 | end |
220 | return build(relative) | 222 | return build(relative) |
@@ -231,7 +233,7 @@ end | |||
231 | function parse_path(path) | 233 | function parse_path(path) |
232 | local parsed = {} | 234 | local parsed = {} |
233 | path = path or "" | 235 | path = path or "" |
234 | path = string.gsub(path, "%s", "") | 236 | --path = string.gsub(path, "%s", "") |
235 | string.gsub(path, "([^/]+)", function (s) table.insert(parsed, s) end) | 237 | string.gsub(path, "([^/]+)", function (s) table.insert(parsed, s) end) |
236 | for i = 1, table.getn(parsed) do | 238 | for i = 1, table.getn(parsed) do |
237 | parsed[i] = unescape(parsed[i]) | 239 | parsed[i] = unescape(parsed[i]) |
diff --git a/test/urltest.lua b/test/urltest.lua index 7e0e73f..02b7077 100644 --- a/test/urltest.lua +++ b/test/urltest.lua | |||
@@ -59,7 +59,7 @@ end | |||
59 | local check_absolute_url = function(base, relative, absolute) | 59 | local check_absolute_url = function(base, relative, absolute) |
60 | local res = socket.url.absolute(base, relative) | 60 | local res = socket.url.absolute(base, relative) |
61 | if res ~= absolute then | 61 | if res ~= absolute then |
62 | write("absolute: In test for '", relative, "' expected '", | 62 | io.write("absolute: In test for '", relative, "' expected '", |
63 | absolute, "' but got '", res, "'\n") | 63 | absolute, "' but got '", res, "'\n") |
64 | exit() | 64 | exit() |
65 | end | 65 | end |
@@ -71,7 +71,7 @@ local check_parse_url = function(gaba) | |||
71 | local parsed = socket.url.parse(url) | 71 | local parsed = socket.url.parse(url) |
72 | for i, v in gaba do | 72 | for i, v in gaba do |
73 | if v ~= parsed[i] then | 73 | if v ~= parsed[i] then |
74 | write("parse: In test for '", url, "' expected ", i, " = '", | 74 | io.write("parse: In test for '", url, "' expected ", i, " = '", |
75 | v, "' but got '", tostring(parsed[i]), "'\n") | 75 | v, "' but got '", tostring(parsed[i]), "'\n") |
76 | for i,v in parsed do print(i,v) end | 76 | for i,v in parsed do print(i,v) end |
77 | exit() | 77 | exit() |
@@ -79,7 +79,7 @@ local check_parse_url = function(gaba) | |||
79 | end | 79 | end |
80 | for i, v in parsed do | 80 | for i, v in parsed do |
81 | if v ~= gaba[i] then | 81 | if v ~= gaba[i] then |
82 | write("parse: In test for '", url, "' expected ", i, " = '", | 82 | io.write("parse: In test for '", url, "' expected ", i, " = '", |
83 | tostring(gaba[i]), "' but got '", v, "'\n") | 83 | tostring(gaba[i]), "' but got '", v, "'\n") |
84 | for i,v in parsed do print(i,v) end | 84 | for i,v in parsed do print(i,v) end |
85 | exit() | 85 | exit() |