http.lua 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. -----------------------------------------------------------------------------
  2. -- HTTP/1.1 client support for the Lua language.
  3. -- LuaSocket toolkit.
  4. -- Author: Diego Nehab
  5. -----------------------------------------------------------------------------
  6. -----------------------------------------------------------------------------
  7. -- Declare module and import dependencies
  8. -------------------------------------------------------------------------------
  9. local socket = require("socket")
  10. local url = require("socket.url")
  11. local ltn12 = require("ltn12")
  12. local mime = require("mime")
  13. local string = require("string")
  14. local headers = require("socket.headers")
  15. local base = _G
  16. local table = require("table")
  17. socket.http = {}
  18. local _M = socket.http
  19. -----------------------------------------------------------------------------
  20. -- Program constants
  21. -----------------------------------------------------------------------------
  22. -- connection timeout in seconds
  23. _M.TIMEOUT = 60
  24. -- user agent field sent in request
  25. _M.USERAGENT = socket._VERSION
  26. -- supported schemes and their particulars
  27. local SCHEMES = {
  28. http = {
  29. port = 80
  30. , create = function(t)
  31. return socket.tcp end }
  32. , https = {
  33. port = 443
  34. , create = function(t)
  35. local https = assert(
  36. require("ssl.https"), 'LuaSocket: LuaSec not found')
  37. local tcp = assert(
  38. https.tcp, 'LuaSocket: Function tcp() not available from LuaSec')
  39. return tcp(t) end }}
  40. -- default scheme and port for document retrieval
  41. local SCHEME = 'http'
  42. local PORT = SCHEMES[SCHEME].port
  43. -----------------------------------------------------------------------------
  44. -- Reads MIME headers from a connection, unfolding where needed
  45. -----------------------------------------------------------------------------
  46. local function receiveheaders(sock, headers)
  47. local line, name, value, err
  48. headers = headers or {}
  49. -- get first line
  50. line, err = sock:receive()
  51. if err then return nil, err end
  52. -- headers go until a blank line is found
  53. while line ~= "" do
  54. -- get field-name and value
  55. name, value = socket.skip(2, string.find(line, "^(.-):%s*(.*)"))
  56. if not (name and value) then return nil, "malformed reponse headers" end
  57. name = string.lower(name)
  58. -- get next line (value might be folded)
  59. line, err = sock:receive()
  60. if err then return nil, err end
  61. -- unfold any folded values
  62. while string.find(line, "^%s") do
  63. value = value .. line
  64. line = sock:receive()
  65. if err then return nil, err end
  66. end
  67. -- save pair in table
  68. if headers[name] then headers[name] = headers[name] .. ", " .. value
  69. else headers[name] = value end
  70. end
  71. return headers
  72. end
  73. -----------------------------------------------------------------------------
  74. -- Extra sources and sinks
  75. -----------------------------------------------------------------------------
  76. socket.sourcet["http-chunked"] = function(sock, headers)
  77. return base.setmetatable({
  78. getfd = function() return sock:getfd() end,
  79. dirty = function() return sock:dirty() end
  80. }, {
  81. __call = function()
  82. -- get chunk size, skip extention
  83. local line, err = sock:receive()
  84. if err then return nil, err end
  85. local size = base.tonumber(string.gsub(line, ";.*", ""), 16)
  86. if not size then return nil, "invalid chunk size" end
  87. -- was it the last chunk?
  88. if size > 0 then
  89. -- if not, get chunk and skip terminating CRLF
  90. local chunk, err, part = sock:receive(size)
  91. if chunk then sock:receive() end
  92. return chunk, err
  93. else
  94. -- if it was, read trailers into headers table
  95. headers, err = receiveheaders(sock, headers)
  96. if not headers then return nil, err end
  97. end
  98. end
  99. })
  100. end
  101. socket.sinkt["http-chunked"] = function(sock)
  102. return base.setmetatable({
  103. getfd = function() return sock:getfd() end,
  104. dirty = function() return sock:dirty() end
  105. }, {
  106. __call = function(self, chunk, err)
  107. if not chunk then return sock:send("0\r\n\r\n") end
  108. local size = string.format("%X\r\n", string.len(chunk))
  109. return sock:send(size .. chunk .. "\r\n")
  110. end
  111. })
  112. end
  113. -----------------------------------------------------------------------------
  114. -- Low level HTTP API
  115. -----------------------------------------------------------------------------
  116. local metat = { __index = {} }
  117. function _M.open(host, port, create)
  118. -- create socket with user connect function, or with default
  119. local c = socket.try(create())
  120. local h = base.setmetatable({ c = c }, metat)
  121. -- create finalized try
  122. h.try = socket.newtry(function() h:close() end)
  123. -- set timeout before connecting
  124. h.try(c:settimeout(_M.TIMEOUT))
  125. h.try(c:connect(host, port))
  126. -- here everything worked
  127. return h
  128. end
  129. function metat.__index:sendrequestline(method, uri)
  130. local reqline = string.format("%s %s HTTP/1.1\r\n", method or "GET", uri)
  131. return self.try(self.c:send(reqline))
  132. end
  133. function metat.__index:sendheaders(tosend)
  134. local canonic = headers.canonic
  135. local h = "\r\n"
  136. for f, v in base.pairs(tosend) do
  137. h = (canonic[f] or f) .. ": " .. v .. "\r\n" .. h
  138. end
  139. self.try(self.c:send(h))
  140. return 1
  141. end
  142. function metat.__index:sendbody(headers, source, step)
  143. source = source or ltn12.source.empty()
  144. step = step or ltn12.pump.step
  145. -- if we don't know the size in advance, send chunked and hope for the best
  146. local mode = "http-chunked"
  147. if headers["content-length"] then mode = "keep-open" end
  148. return self.try(ltn12.pump.all(source, socket.sink(mode, self.c), step))
  149. end
  150. function metat.__index:receivestatusline()
  151. local status,ec = self.try(self.c:receive(5))
  152. -- identify HTTP/0.9 responses, which do not contain a status line
  153. -- this is just a heuristic, but is what the RFC recommends
  154. if status ~= "HTTP/" then
  155. if ec == "timeout" then
  156. return 408
  157. end
  158. return nil, status
  159. end
  160. -- otherwise proceed reading a status line
  161. status = self.try(self.c:receive("*l", status))
  162. local code = socket.skip(2, string.find(status, "HTTP/%d*%.%d* (%d%d%d)"))
  163. return self.try(base.tonumber(code), status)
  164. end
  165. function metat.__index:receiveheaders()
  166. return self.try(receiveheaders(self.c))
  167. end
  168. function metat.__index:receivebody(headers, sink, step)
  169. sink = sink or ltn12.sink.null()
  170. step = step or ltn12.pump.step
  171. local length = base.tonumber(headers["content-length"])
  172. local t = headers["transfer-encoding"] -- shortcut
  173. local mode = "default" -- connection close
  174. if t and t ~= "identity" then mode = "http-chunked"
  175. elseif base.tonumber(headers["content-length"]) then mode = "by-length" end
  176. return self.try(ltn12.pump.all(socket.source(mode, self.c, length),
  177. sink, step))
  178. end
  179. function metat.__index:receive09body(status, sink, step)
  180. local source = ltn12.source.rewind(socket.source("until-closed", self.c))
  181. source(status)
  182. return self.try(ltn12.pump.all(source, sink, step))
  183. end
  184. function metat.__index:close()
  185. return self.c:close()
  186. end
  187. -----------------------------------------------------------------------------
  188. -- High level HTTP API
  189. -----------------------------------------------------------------------------
  190. local function adjusturi(reqt)
  191. local u = reqt
  192. -- if there is a proxy, we need the full url. otherwise, just a part.
  193. if not reqt.proxy and not _M.PROXY then
  194. u = {
  195. path = socket.try(reqt.path, "invalid path 'nil'"),
  196. params = reqt.params,
  197. query = reqt.query,
  198. fragment = reqt.fragment
  199. }
  200. end
  201. return url.build(u)
  202. end
  203. local function adjustproxy(reqt)
  204. local proxy = reqt.proxy or _M.PROXY
  205. if proxy then
  206. proxy = url.parse(proxy)
  207. return proxy.host, proxy.port or 3128
  208. else
  209. return reqt.host, reqt.port
  210. end
  211. end
  212. local function adjustheaders(reqt)
  213. -- default headers
  214. local host = reqt.host
  215. local port = tostring(reqt.port)
  216. if port ~= tostring(SCHEMES[reqt.scheme].port) then
  217. host = host .. ':' .. port end
  218. local lower = {
  219. ["user-agent"] = _M.USERAGENT,
  220. ["host"] = host,
  221. ["connection"] = "close, TE",
  222. ["te"] = "trailers"
  223. }
  224. -- if we have authentication information, pass it along
  225. if reqt.user and reqt.password then
  226. lower["authorization"] =
  227. "Basic " .. (mime.b64(reqt.user .. ":" ..
  228. url.unescape(reqt.password)))
  229. end
  230. -- if we have proxy authentication information, pass it along
  231. local proxy = reqt.proxy or _M.PROXY
  232. if proxy then
  233. proxy = url.parse(proxy)
  234. if proxy.user and proxy.password then
  235. lower["proxy-authorization"] =
  236. "Basic " .. (mime.b64(proxy.user .. ":" .. proxy.password))
  237. end
  238. end
  239. -- override with user headers
  240. for i,v in base.pairs(reqt.headers or lower) do
  241. lower[string.lower(i)] = v
  242. end
  243. return lower
  244. end
  245. -- default url parts
  246. local default = {
  247. path ="/"
  248. , scheme = "http"
  249. }
  250. local function adjustrequest(reqt)
  251. -- parse url if provided
  252. local nreqt = reqt.url and url.parse(reqt.url, default) or {}
  253. -- explicit components override url
  254. for i,v in base.pairs(reqt) do nreqt[i] = v end
  255. -- default to scheme particulars
  256. local schemedefs, host, port, method
  257. = SCHEMES[nreqt.scheme], nreqt.host, nreqt.port, nreqt.method
  258. if not nreqt.create then nreqt.create = schemedefs.create(nreqt) end
  259. if not (port and port ~= '') then nreqt.port = schemedefs.port end
  260. if not (method and method ~= '') then nreqt.method = 'GET' end
  261. if not (host and host ~= "") then
  262. socket.try(nil, "invalid host '" .. base.tostring(nreqt.host) .. "'")
  263. end
  264. -- compute uri if user hasn't overriden
  265. nreqt.uri = reqt.uri or adjusturi(nreqt)
  266. -- adjust headers in request
  267. nreqt.headers = adjustheaders(nreqt)
  268. -- ajust host and port if there is a proxy
  269. nreqt.host, nreqt.port = adjustproxy(nreqt)
  270. return nreqt
  271. end
  272. local function shouldredirect(reqt, code, headers)
  273. local location = headers.location
  274. if not location then return false end
  275. location = string.gsub(location, "%s", "")
  276. if location == "" then return false end
  277. local scheme = url.parse(location).scheme
  278. if scheme and (not SCHEMES[scheme]) then return false end
  279. -- avoid https downgrades
  280. if ('https' == reqt.scheme) and ('https' ~= scheme) then return false end
  281. return (reqt.redirect ~= false) and
  282. (code == 301 or code == 302 or code == 303 or code == 307) and
  283. (not reqt.method or reqt.method == "GET" or reqt.method == "HEAD")
  284. and ((false == reqt.maxredirects)
  285. or ((reqt.nredirects or 0)
  286. < (reqt.maxredirects or 5)))
  287. end
  288. local function shouldreceivebody(reqt, code)
  289. if reqt.method == "HEAD" then return nil end
  290. if code == 204 or code == 304 then return nil end
  291. if code >= 100 and code < 200 then return nil end
  292. return 1
  293. end
  294. -- forward declarations
  295. local trequest, tredirect
  296. --[[local]] function tredirect(reqt, location)
  297. -- the RFC says the redirect URL has to be absolute, but some
  298. -- servers do not respect that
  299. local newurl = url.absolute(reqt.url, location)
  300. -- if switching schemes, reset port and create function
  301. if url.parse(newurl).scheme ~= reqt.scheme then
  302. reqt.port = nil
  303. reqt.create = nil end
  304. -- make new request
  305. local result, code, headers, status = trequest {
  306. url = newurl,
  307. source = reqt.source,
  308. sink = reqt.sink,
  309. headers = reqt.headers,
  310. proxy = reqt.proxy,
  311. maxredirects = reqt.maxredirects,
  312. nredirects = (reqt.nredirects or 0) + 1,
  313. create = reqt.create
  314. }
  315. -- pass location header back as a hint we redirected
  316. headers = headers or {}
  317. headers.location = headers.location or location
  318. return result, code, headers, status
  319. end
  320. --[[local]] function trequest(reqt)
  321. -- we loop until we get what we want, or
  322. -- until we are sure there is no way to get it
  323. local nreqt = adjustrequest(reqt)
  324. local h = _M.open(nreqt.host, nreqt.port, nreqt.create)
  325. -- send request line and headers
  326. h:sendrequestline(nreqt.method, nreqt.uri)
  327. h:sendheaders(nreqt.headers)
  328. -- if there is a body, send it
  329. if nreqt.source then
  330. h:sendbody(nreqt.headers, nreqt.source, nreqt.step)
  331. end
  332. local code, status = h:receivestatusline()
  333. -- if it is an HTTP/0.9 server, simply get the body and we are done
  334. if not code then
  335. h:receive09body(status, nreqt.sink, nreqt.step)
  336. return 1, 200
  337. elseif code == 408 then
  338. return 1, code
  339. end
  340. local headers
  341. -- ignore any 100-continue messages
  342. while code == 100 do
  343. headers = h:receiveheaders()
  344. code, status = h:receivestatusline()
  345. end
  346. headers = h:receiveheaders()
  347. -- at this point we should have a honest reply from the server
  348. -- we can't redirect if we already used the source, so we report the error
  349. if shouldredirect(nreqt, code, headers) and not nreqt.source then
  350. h:close()
  351. return tredirect(reqt, headers.location)
  352. end
  353. -- here we are finally done
  354. if shouldreceivebody(nreqt, code) then
  355. h:receivebody(headers, nreqt.sink, nreqt.step)
  356. end
  357. h:close()
  358. return 1, code, headers, status
  359. end
  360. -- turns an url and a body into a generic request
  361. local function genericform(u, b)
  362. local t = {}
  363. local reqt = {
  364. url = u,
  365. sink = ltn12.sink.table(t),
  366. target = t
  367. }
  368. if b then
  369. reqt.source = ltn12.source.string(b)
  370. reqt.headers = {
  371. ["content-length"] = string.len(b),
  372. ["content-type"] = "application/x-www-form-urlencoded"
  373. }
  374. reqt.method = "POST"
  375. end
  376. return reqt
  377. end
  378. _M.genericform = genericform
  379. local function srequest(u, b)
  380. local reqt = genericform(u, b)
  381. local _, code, headers, status = trequest(reqt)
  382. return table.concat(reqt.target), code, headers, status
  383. end
  384. _M.request = socket.protect(function(reqt, body)
  385. if base.type(reqt) == "string" then return srequest(reqt, body)
  386. else return trequest(reqt) end
  387. end)
  388. _M.schemes = SCHEMES
  389. return _M