2
0

httpclient.lua 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. -- Copyright 2009 Steven Barth <steven@midlink.org>
  2. -- Licensed to the public under the Apache License 2.0.
  3. require "nixio.util"
  4. local nixio = require "nixio"
  5. local ltn12 = require "luci.ltn12"
  6. local util = require "luci.util"
  7. local table = require "table"
  8. local http = require "luci.http.protocol"
  9. local date = require "luci.http.protocol.date"
  10. local type, pairs, ipairs, tonumber = type, pairs, ipairs, tonumber
  11. local unpack = unpack
  12. module "luci.httpclient"
  13. function chunksource(sock, buffer)
  14. buffer = buffer or ""
  15. return function()
  16. local output
  17. local _, endp, count = buffer:find("^([0-9a-fA-F]+);?.-\r\n")
  18. while not count and #buffer <= 1024 do
  19. local newblock, code = sock:recv(1024 - #buffer)
  20. if not newblock then
  21. return nil, code
  22. end
  23. buffer = buffer .. newblock
  24. _, endp, count = buffer:find("^([0-9a-fA-F]+);?.-\r\n")
  25. end
  26. count = tonumber(count, 16)
  27. if not count then
  28. return nil, -1, "invalid encoding"
  29. elseif count == 0 then
  30. return nil
  31. elseif count + 2 <= #buffer - endp then
  32. output = buffer:sub(endp+1, endp+count)
  33. buffer = buffer:sub(endp+count+3)
  34. return output
  35. else
  36. output = buffer:sub(endp+1, endp+count)
  37. buffer = ""
  38. if count - #output > 0 then
  39. local remain, code = sock:recvall(count-#output)
  40. if not remain then
  41. return nil, code
  42. end
  43. output = output .. remain
  44. count, code = sock:recvall(2)
  45. else
  46. count, code = sock:recvall(count+2-#buffer+endp)
  47. end
  48. if not count then
  49. return nil, code
  50. end
  51. return output
  52. end
  53. end
  54. end
  55. function request_to_buffer(uri, options)
  56. local source, code, msg = request_to_source(uri, options)
  57. local output = {}
  58. if not source then
  59. return nil, code, msg
  60. end
  61. source, code = ltn12.pump.all(source, (ltn12.sink.table(output)))
  62. if not source then
  63. return nil, code
  64. end
  65. return table.concat(output)
  66. end
  67. function request_to_source(uri, options)
  68. local status, response, buffer, sock = request_raw(uri, options)
  69. if not status then
  70. return status, response, buffer
  71. elseif status ~= 200 and status ~= 206 then
  72. return nil, status, buffer
  73. end
  74. if response.headers["Transfer-Encoding"] == "chunked" then
  75. return chunksource(sock, buffer)
  76. else
  77. return ltn12.source.cat(ltn12.source.string(buffer), sock:blocksource())
  78. end
  79. end
  80. --
  81. -- GET HTTP-resource
  82. --
  83. function request_raw(uri, options)
  84. options = options or {}
  85. local pr, auth, host, port, path
  86. if options.params then
  87. uri = uri .. '?' .. http.urlencode_params(options.params)
  88. end
  89. if uri:find("%[") then
  90. if uri:find("@") then
  91. pr, auth, host, port, path = uri:match("(%w+)://(.+)@(%b[]):?([0-9]*)(.*)")
  92. host = host:sub(2,-2)
  93. else
  94. pr, host, port, path = uri:match("(%w+)://(%b[]):?([0-9]*)(.*)")
  95. host = host:sub(2,-2)
  96. end
  97. else
  98. if uri:find("@") then
  99. pr, auth, host, port, path =
  100. uri:match("(%w+)://(.+)@([%w-.]+):?([0-9]*)(.*)")
  101. else
  102. pr, host, port, path = uri:match("(%w+)://([%w-.]+):?([0-9]*)(.*)")
  103. end
  104. end
  105. if not host then
  106. return nil, -1, "unable to parse URI"
  107. end
  108. if pr ~= "http" and pr ~= "https" then
  109. return nil, -2, "protocol not supported"
  110. end
  111. port = #port > 0 and port or (pr == "https" and 443 or 80)
  112. path = #path > 0 and path or "/"
  113. options.depth = options.depth or 10
  114. local headers = options.headers or {}
  115. local protocol = options.protocol or "HTTP/1.1"
  116. headers["User-Agent"] = headers["User-Agent"] or "LuCI httpclient 0.1"
  117. if headers.Connection == nil then
  118. headers.Connection = "close"
  119. end
  120. if auth and not headers.Authorization then
  121. headers.Authorization = "Basic " .. nixio.bin.b64encode(auth)
  122. end
  123. local sock, code, msg = nixio.connect(host, port)
  124. if not sock then
  125. return nil, code, msg
  126. end
  127. sock:setsockopt("socket", "sndtimeo", options.sndtimeo or 15)
  128. sock:setsockopt("socket", "rcvtimeo", options.rcvtimeo or 15)
  129. if pr == "https" then
  130. local tls = options.tls_context or nixio.tls()
  131. sock = tls:create(sock)
  132. local stat, code, error = sock:connect()
  133. if not stat then
  134. return stat, code, error
  135. end
  136. end
  137. -- Pre assemble fixes
  138. if protocol == "HTTP/1.1" then
  139. headers.Host = headers.Host or host
  140. end
  141. if type(options.body) == "table" then
  142. options.body = http.urlencode_params(options.body)
  143. end
  144. if type(options.body) == "string" then
  145. headers["Content-Length"] = headers["Content-Length"] or #options.body
  146. headers["Content-Type"] = headers["Content-Type"] or
  147. "application/x-www-form-urlencoded"
  148. options.method = options.method or "POST"
  149. end
  150. if type(options.body) == "function" then
  151. options.method = options.method or "POST"
  152. end
  153. if options.cookies then
  154. local cookiedata = {}
  155. for _, c in ipairs(options.cookies) do
  156. local cdo = c.flags.domain
  157. local cpa = c.flags.path
  158. if (cdo == host or cdo == "."..host or host:sub(-#cdo) == cdo)
  159. and (cpa == path or cpa == "/" or cpa .. "/" == path:sub(#cpa+1))
  160. and (not c.flags.secure or pr == "https")
  161. then
  162. cookiedata[#cookiedata+1] = c.key .. "=" .. c.value
  163. end
  164. end
  165. if headers["Cookie"] then
  166. headers["Cookie"] = headers["Cookie"] .. "; " .. table.concat(cookiedata, "; ")
  167. else
  168. headers["Cookie"] = table.concat(cookiedata, "; ")
  169. end
  170. end
  171. -- Assemble message
  172. local message = {(options.method or "GET") .. " " .. path .. " " .. protocol}
  173. for k, v in pairs(headers) do
  174. if type(v) == "string" or type(v) == "number" then
  175. message[#message+1] = k .. ": " .. v
  176. elseif type(v) == "table" then
  177. for i, j in ipairs(v) do
  178. message[#message+1] = k .. ": " .. j
  179. end
  180. end
  181. end
  182. message[#message+1] = ""
  183. message[#message+1] = ""
  184. -- Send request
  185. sock:sendall(table.concat(message, "\r\n"))
  186. if type(options.body) == "string" then
  187. sock:sendall(options.body)
  188. elseif type(options.body) == "function" then
  189. local res = {options.body(sock)}
  190. if not res[1] then
  191. sock:close()
  192. return unpack(res)
  193. end
  194. end
  195. -- Create source and fetch response
  196. local linesrc = sock:linesource()
  197. local line, code, error = linesrc()
  198. if not line then
  199. sock:close()
  200. return nil, code, error
  201. end
  202. local protocol, status, msg = line:match("^([%w./]+) ([0-9]+) (.*)")
  203. if not protocol then
  204. sock:close()
  205. return nil, -3, "invalid response magic: " .. line
  206. end
  207. local response = {
  208. status = line, headers = {}, code = 0, cookies = {}, uri = uri
  209. }
  210. line = linesrc()
  211. while line and line ~= "" do
  212. local key, val = line:match("^([%w-]+)%s?:%s?(.*)")
  213. if key and key ~= "Status" then
  214. if type(response.headers[key]) == "string" then
  215. response.headers[key] = {response.headers[key], val}
  216. elseif type(response.headers[key]) == "table" then
  217. response.headers[key][#response.headers[key]+1] = val
  218. else
  219. response.headers[key] = val
  220. end
  221. end
  222. line = linesrc()
  223. end
  224. if not line then
  225. sock:close()
  226. return nil, -4, "protocol error"
  227. end
  228. -- Parse cookies
  229. if response.headers["Set-Cookie"] then
  230. local cookies = response.headers["Set-Cookie"]
  231. for _, c in ipairs(type(cookies) == "table" and cookies or {cookies}) do
  232. local cobj = cookie_parse(c)
  233. cobj.flags.path = cobj.flags.path or path:match("(/.*)/?[^/]*")
  234. if not cobj.flags.domain or cobj.flags.domain == "" then
  235. cobj.flags.domain = host
  236. response.cookies[#response.cookies+1] = cobj
  237. else
  238. local hprt, cprt = {}, {}
  239. -- Split hostnames and save them in reverse order
  240. for part in host:gmatch("[^.]*") do
  241. table.insert(hprt, 1, part)
  242. end
  243. for part in cobj.flags.domain:gmatch("[^.]*") do
  244. table.insert(cprt, 1, part)
  245. end
  246. local valid = true
  247. for i, part in ipairs(cprt) do
  248. -- If parts are different and no wildcard
  249. if hprt[i] ~= part and #part ~= 0 then
  250. valid = false
  251. break
  252. -- Wildcard on invalid position
  253. elseif hprt[i] ~= part and #part == 0 then
  254. if i ~= #cprt or (#hprt ~= i and #hprt+1 ~= i) then
  255. valid = false
  256. break
  257. end
  258. end
  259. end
  260. -- No TLD cookies
  261. if valid and #cprt > 1 and #cprt[2] > 0 then
  262. response.cookies[#response.cookies+1] = cobj
  263. end
  264. end
  265. end
  266. end
  267. -- Follow
  268. response.code = tonumber(status)
  269. if response.code and options.depth > 0 then
  270. if (response.code == 301 or response.code == 302 or response.code == 307)
  271. and response.headers.Location then
  272. local nuri = response.headers.Location or response.headers.location
  273. if not nuri then
  274. return nil, -5, "invalid reference"
  275. end
  276. if not nuri:find("https?://") then
  277. nuri = pr .. "://" .. host .. ":" .. port .. nuri
  278. end
  279. options.depth = options.depth - 1
  280. if options.headers then
  281. options.headers.Host = nil
  282. end
  283. sock:close()
  284. return request_raw(nuri, options)
  285. end
  286. end
  287. return response.code, response, linesrc(true)..sock:readall(), sock
  288. end
  289. function cookie_parse(cookiestr)
  290. local key, val, flags = cookiestr:match("%s?([^=;]+)=?([^;]*)(.*)")
  291. if not key then
  292. return nil
  293. end
  294. local cookie = {key = key, value = val, flags = {}}
  295. for fkey, fval in flags:gmatch(";%s?([^=;]+)=?([^;]*)") do
  296. fkey = fkey:lower()
  297. if fkey == "expires" then
  298. fval = date.to_unix(fval:gsub("%-", " "))
  299. end
  300. cookie.flags[fkey] = fval
  301. end
  302. return cookie
  303. end
  304. function cookie_create(cookie)
  305. local cookiedata = {cookie.key .. "=" .. cookie.value}
  306. for k, v in pairs(cookie.flags) do
  307. if k == "expires" then
  308. v = date.to_http(v):gsub(", (%w+) (%w+) (%w+) ", ", %1-%2-%3 ")
  309. end
  310. cookiedata[#cookiedata+1] = k .. ((#v > 0) and ("=" .. v) or "")
  311. end
  312. return table.concat(cookiedata, "; ")
  313. end