diff options
| author | Diego Nehab <diego@tecgraf.puc-rio.br> | 2004-06-16 22:51:04 +0000 |
|---|---|---|
| committer | Diego Nehab <diego@tecgraf.puc-rio.br> | 2004-06-16 22:51:04 +0000 |
| commit | 9fc682a106f13901b60a665619fc61d9dae49fb0 (patch) | |
| tree | 6efdd73fa4f2abce56b6fb710f7516d0a5c6c758 | |
| parent | 574708380f19b15bd19419bfd64ccbe422f2d924 (diff) | |
| download | luasocket-9fc682a106f13901b60a665619fc61d9dae49fb0.tar.gz luasocket-9fc682a106f13901b60a665619fc61d9dae49fb0.tar.bz2 luasocket-9fc682a106f13901b60a665619fc61d9dae49fb0.zip | |
HTTP now has only one function.
| -rw-r--r-- | etc/check-links.lua | 25 | ||||
| -rw-r--r-- | etc/get.lua | 10 | ||||
| -rw-r--r-- | src/http.lua | 19 | ||||
| -rw-r--r-- | src/url.lua | 3 |
4 files changed, 24 insertions, 33 deletions
diff --git a/etc/check-links.lua b/etc/check-links.lua index a4e9ef8..4487593 100644 --- a/etc/check-links.lua +++ b/etc/check-links.lua | |||
| @@ -8,8 +8,6 @@ local http = require("http") | |||
| 8 | local url = require("url") | 8 | local url = require("url") |
| 9 | http.TIMEOUT = 10 | 9 | http.TIMEOUT = 10 |
| 10 | 10 | ||
| 11 | cache = {} | ||
| 12 | |||
| 13 | function readfile(path) | 11 | function readfile(path) |
| 14 | path = url.unescape(path) | 12 | path = url.unescape(path) |
| 15 | local file, error = io.open(path, "r") | 13 | local file, error = io.open(path, "r") |
| @@ -22,22 +20,14 @@ end | |||
| 22 | 20 | ||
| 23 | function getstatus(u) | 21 | function getstatus(u) |
| 24 | local parsed = url.parse(u, {scheme = "file"}) | 22 | local parsed = url.parse(u, {scheme = "file"}) |
| 25 | if cache[u] then return cache[u] end | ||
| 26 | local res | ||
| 27 | if parsed.scheme == "http" then | 23 | if parsed.scheme == "http" then |
| 28 | local request = {url = u, method = "HEAD"} | 24 | local r, c, h, s = http.request{url = u, method = "HEAD"} |
| 29 | local response = http.request(request) | 25 | if c ~= 200 then return s or c end |
| 30 | if response.code == 200 then res = nil | ||
| 31 | else res = response.status or response.error end | ||
| 32 | elseif parsed.scheme == "file" then | 26 | elseif parsed.scheme == "file" then |
| 33 | local file, error = io.open(url.unescape(parsed.path), "r") | 27 | local file, error = io.open(url.unescape(parsed.path), "r") |
| 34 | if file then | 28 | if file then file:close() |
| 35 | file:close() | 29 | else return error end |
| 36 | res = nil | 30 | else return string.format("unhandled scheme '%s'", parsed.scheme) end |
| 37 | else res = error end | ||
| 38 | else res = string.format("unhandled scheme '%s'", parsed.scheme) end | ||
| 39 | cache[u] = res | ||
| 40 | return res | ||
| 41 | end | 31 | end |
| 42 | 32 | ||
| 43 | function retrieve(u) | 33 | function retrieve(u) |
| @@ -45,10 +35,13 @@ function retrieve(u) | |||
| 45 | local body, headers, code, error | 35 | local body, headers, code, error |
| 46 | local base = u | 36 | local base = u |
| 47 | if parsed.scheme == "http" then | 37 | if parsed.scheme == "http" then |
| 48 | body, headers, code, error = http.get(u) | 38 | body, code, headers = http.request(u) |
| 49 | if code == 200 then | 39 | if code == 200 then |
| 50 | base = base or headers.location | 40 | base = base or headers.location |
| 51 | end | 41 | end |
| 42 | if not body then | ||
| 43 | error = code | ||
| 44 | end | ||
| 52 | elseif parsed.scheme == "file" then | 45 | elseif parsed.scheme == "file" then |
| 53 | body, error = readfile(parsed.path) | 46 | body, error = readfile(parsed.path) |
| 54 | else error = string.format("unhandled scheme '%s'", parsed.scheme) end | 47 | else error = string.format("unhandled scheme '%s'", parsed.scheme) end |
diff --git a/etc/get.lua b/etc/get.lua index 0603ce5..c7e2a43 100644 --- a/etc/get.lua +++ b/etc/get.lua | |||
| @@ -87,9 +87,9 @@ end | |||
| 87 | 87 | ||
| 88 | -- determines the size of a http file | 88 | -- determines the size of a http file |
| 89 | function gethttpsize(u) | 89 | function gethttpsize(u) |
| 90 | local respt = http.request {method = "HEAD", url = u} | 90 | local r, c, h = http.request {method = "HEAD", url = u} |
| 91 | if respt.code == 200 then | 91 | if c == 200 then |
| 92 | return tonumber(respt.headers["content-length"]) | 92 | return tonumber(h["content-length"]) |
| 93 | end | 93 | end |
| 94 | end | 94 | end |
| 95 | 95 | ||
| @@ -98,8 +98,8 @@ function getbyhttp(u, file) | |||
| 98 | local save = ltn12.sink.file(file or io.stdout) | 98 | local save = ltn12.sink.file(file or io.stdout) |
| 99 | -- only print feedback if output is not stdout | 99 | -- only print feedback if output is not stdout |
| 100 | if file then save = ltn12.sink.chain(stats(gethttpsize(u)), save) end | 100 | if file then save = ltn12.sink.chain(stats(gethttpsize(u)), save) end |
| 101 | local respt = http.request {url = u, sink = save } | 101 | local r, c, h, s = http.request {url = u, sink = save } |
| 102 | if respt.code ~= 200 then print(respt.status or respt.error) end | 102 | if c ~= 200 then io.stderr:write(s or c, "\n") end |
| 103 | end | 103 | end |
| 104 | 104 | ||
| 105 | -- downloads a file using the ftp protocol | 105 | -- downloads a file using the ftp protocol |
diff --git a/src/http.lua b/src/http.lua index e0c4c27..b341ebb 100644 --- a/src/http.lua +++ b/src/http.lua | |||
| @@ -122,7 +122,7 @@ local function uri(reqt) | |||
| 122 | local u = reqt | 122 | local u = reqt |
| 123 | if not reqt.proxy and not PROXY then | 123 | if not reqt.proxy and not PROXY then |
| 124 | u = { | 124 | u = { |
| 125 | path = reqt.path, | 125 | path = socket.try(reqt.path, "invalid path 'nil'"), |
| 126 | params = reqt.params, | 126 | params = reqt.params, |
| 127 | query = reqt.query, | 127 | query = reqt.query, |
| 128 | fragment = reqt.fragment | 128 | fragment = reqt.fragment |
| @@ -152,18 +152,15 @@ local default = { | |||
| 152 | 152 | ||
| 153 | local function adjustrequest(reqt) | 153 | local function adjustrequest(reqt) |
| 154 | -- parse url if provided | 154 | -- parse url if provided |
| 155 | if reqt.url then | 155 | local nreqt = reqt.url and url.parse(reqt.url, default) or {} |
| 156 | local parsed = url.parse(reqt.url, default) | 156 | -- explicit components override url |
| 157 | -- explicit components override url | 157 | for i,v in reqt do nreqt[i] = reqt[i] end |
| 158 | for i,v in parsed do reqt[i] = reqt[i] or v end | 158 | socket.try(nreqt.host, "invalid host '" .. tostring(nreqt.host) .. "'") |
| 159 | end | ||
| 160 | socket.try(reqt.host, "invalid host '" .. tostring(reqt.host) .. "'") | ||
| 161 | socket.try(reqt.path, "invalid path '" .. tostring(reqt.path) .. "'") | ||
| 162 | -- compute uri if user hasn't overriden | 159 | -- compute uri if user hasn't overriden |
| 163 | reqt.uri = reqt.uri or uri(reqt) | 160 | nreqt.uri = nreqt.uri or uri(nreqt) |
| 164 | -- adjust headers in request | 161 | -- adjust headers in request |
| 165 | reqt.headers = adjustheaders(reqt.headers, reqt.host) | 162 | nreqt.headers = adjustheaders(nreqt.headers, nreqt.host) |
| 166 | return reqt | 163 | return nreqt |
| 167 | end | 164 | end |
| 168 | 165 | ||
| 169 | local function shouldredirect(reqt, code) | 166 | local function shouldredirect(reqt, code) |
diff --git a/src/url.lua b/src/url.lua index ec26e62..c708e19 100644 --- a/src/url.lua +++ b/src/url.lua | |||
| @@ -115,7 +115,8 @@ end | |||
| 115 | ----------------------------------------------------------------------------- | 115 | ----------------------------------------------------------------------------- |
| 116 | function parse(url, default) | 116 | function parse(url, default) |
| 117 | -- initialize default parameters | 117 | -- initialize default parameters |
| 118 | local parsed = default or {} | 118 | local parsed = {} |
| 119 | for i,v in (default or parsed) do parsed[i] = v end | ||
| 119 | -- empty url is parsed to nil | 120 | -- empty url is parsed to nil |
| 120 | if not url or url == "" then return nil, "invalid url" end | 121 | if not url or url == "" then return nil, "invalid url" end |
| 121 | -- remove whitespace | 122 | -- remove whitespace |
