aboutsummaryrefslogtreecommitdiff
path: root/src/http.lua
diff options
context:
space:
mode:
authorDiego Nehab <diego@tecgraf.puc-rio.br>2005-06-12 22:02:21 +0000
committerDiego Nehab <diego@tecgraf.puc-rio.br>2005-06-12 22:02:21 +0000
commit8b114f3bf4ccea3b065551fa94649a9e45935b5b (patch)
tree06f2faa7e896e9434ba89ec68445ea56e6c8c6dc /src/http.lua
parentb22f6f3830515a57a8776e7489b3e2d434abd12f (diff)
downloadluasocket-8b114f3bf4ccea3b065551fa94649a9e45935b5b.tar.gz
luasocket-8b114f3bf4ccea3b065551fa94649a9e45935b5b.tar.bz2
luasocket-8b114f3bf4ccea3b065551fa94649a9e45935b5b.zip
Stupid bug in http.lua.
Diffstat (limited to 'src/http.lua')
-rw-r--r--src/http.lua53
1 files changed, 49 insertions, 4 deletions
diff --git a/src/http.lua b/src/http.lua
index 38b93e2..669f54d 100644
--- a/src/http.lua
+++ b/src/http.lua
@@ -28,6 +28,51 @@ PORT = 80
28USERAGENT = socket.VERSION 28USERAGENT = socket.VERSION
29 29
30----------------------------------------------------------------------------- 30-----------------------------------------------------------------------------
31-- Extra sources and sinks
32-----------------------------------------------------------------------------
33socket.sourcet["http-chunked"] = function(sock)
34 return base.setmetatable({
35 getfd = function() return sock:getfd() end,
36 dirty = function() return sock:dirty() end
37 }, {
38 __call = function()
39 -- get chunk size, skip extention
40 local line, err = sock:receive()
41 if err then return nil, err end
42 local size = base.tonumber(string.gsub(line, ";.*", ""), 16)
43 if not size then return nil, "invalid chunk size" end
44 -- was it the last chunk?
45 if size <= 0 then
46 -- skip trailer headers, if any
47 local line, err = sock:receive()
48 while not err and line ~= "" do
49 line, err = sock:receive()
50 end
51 return nil, err
52 else
53 -- get chunk and skip terminating CRLF
54 local chunk, err, part = sock:receive(size)
55 if chunk then sock:receive() end
56 return chunk, err
57 end
58 end
59 })
60end
61
62socket.sinkt["http-chunked"] = function(sock)
63 return base.setmetatable({
64 getfd = function() return sock:getfd() end,
65 dirty = function() return sock:dirty() end
66 }, {
67 __call = function(self, chunk, err)
68 if not chunk then return sock:send("0\r\n\r\n") end
69 local size = string.format("%X\r\n", string.len(chunk))
70 return sock:send(size .. chunk .. "\r\n")
71 end
72 })
73end
74
75-----------------------------------------------------------------------------
31-- Low level HTTP API 76-- Low level HTTP API
32----------------------------------------------------------------------------- 77-----------------------------------------------------------------------------
33local metat = { __index = {} } 78local metat = { __index = {} }
@@ -70,7 +115,7 @@ function metat.__index:sendheaders(headers)
70end 115end
71 116
72function metat.__index:sendbody(headers, source, step) 117function metat.__index:sendbody(headers, source, step)
73 source = source or ltn12.source.empty() 118 source = source or ltn12.source.empty()
74 step = step or ltn12.pump.step 119 step = step or ltn12.pump.step
75 -- if we don't know the size in advance, send chunked and hope for the best 120 -- if we don't know the size in advance, send chunked and hope for the best
76 local mode = "http-chunked" 121 local mode = "http-chunked"
@@ -155,7 +200,7 @@ end
155local function adjustheaders(headers, host) 200local function adjustheaders(headers, host)
156 local lower = {} 201 local lower = {}
157 -- override with user values 202 -- override with user values
158 for i,v in (headers or lower) do 203 for i,v in pairs(headers or lower) do
159 lower[string.lower(i)] = v 204 lower[string.lower(i)] = v
160 end 205 end
161 lower["user-agent"] = lower["user-agent"] or USERAGENT 206 lower["user-agent"] = lower["user-agent"] or USERAGENT
@@ -175,7 +220,7 @@ local function adjustrequest(reqt)
175 local nreqt = reqt.url and url.parse(reqt.url, default) or {} 220 local nreqt = reqt.url and url.parse(reqt.url, default) or {}
176 local t = url.parse(reqt.url, default) 221 local t = url.parse(reqt.url, default)
177 -- explicit components override url 222 -- explicit components override url
178 for i,v in reqt do nreqt[i] = reqt[i] end 223 for i,v in pairs(reqt) do nreqt[i] = v end
179 socket.try(nreqt.host, "invalid host '" .. base.tostring(nreqt.host) .. "'") 224 socket.try(nreqt.host, "invalid host '" .. base.tostring(nreqt.host) .. "'")
180 -- compute uri if user hasn't overriden 225 -- compute uri if user hasn't overriden
181 nreqt.uri = reqt.uri or adjusturi(nreqt) 226 nreqt.uri = reqt.uri or adjusturi(nreqt)
@@ -238,7 +283,7 @@ function trequest(reqt)
238 local h = open(reqt.host, reqt.port, reqt.connect) 283 local h = open(reqt.host, reqt.port, reqt.connect)
239 h:sendrequestline(reqt.method, reqt.uri) 284 h:sendrequestline(reqt.method, reqt.uri)
240 h:sendheaders(reqt.headers) 285 h:sendheaders(reqt.headers)
241 h:sendbody(reqt.headers, reqt.source, reqt.step) 286 if reqt.source then h:sendbody(reqt.headers, reqt.source, reqt.step) end
242 local code, headers, status 287 local code, headers, status
243 code, status = h:receivestatusline() 288 code, status = h:receivestatusline()
244 headers = h:receiveheaders() 289 headers = h:receiveheaders()