httpplus.lua 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. --[[
  2. @module httpplus
  3. @summary http库的补充
  4. @version 1.0
  5. @date 2023.11.23
  6. @author wendal
  7. @demo httpplus
  8. @tag LUAT_USE_NETWORK
  9. @usage
  10. -- 本库支持的功能有:
  11. -- 1. 大文件上传的问题,不限大小
  12. -- 2. 任意长度的header设置
  13. -- 3. 任意长度的body设置
  14. -- 4. 鉴权URL自动识别
  15. -- 5. body使用zbuff返回,可直接传输给uart等库
  16. -- 与http库的差异
  17. -- 1. 不支持文件下载
  18. -- 2. 不支持fota
  19. ]]
  20. local httpplus = {}
  21. local TAG = "httpplus"
  22. local function http_opts_parse(opts)
  23. if not opts then
  24. log.error(TAG, "opts不能为nil")
  25. return -100, "opts不能为nil"
  26. end
  27. if not opts.url or #opts.url < 5 then
  28. log.error(TAG, "URL不存在或者太短了", url)
  29. return -100, "URL不存在或者太短了"
  30. end
  31. if not opts.headers then
  32. opts.headers = {}
  33. end
  34. if opts.debug or httpplus.debug then
  35. if not opts.log then
  36. opts.log = log.debug
  37. end
  38. else
  39. opts.log = function()
  40. -- log.info(TAG, "无日志")
  41. end
  42. end
  43. -- 解析url
  44. -- 先判断协议是否加密
  45. local is_ssl = false
  46. local tmp = ""
  47. if opts.url:startsWith("https://") then
  48. is_ssl = true
  49. tmp = opts.url:sub(9)
  50. elseif opts.url:startsWith("http://") then
  51. tmp = opts.url:sub(8)
  52. else
  53. tmp = opts.url
  54. end
  55. -- log.info("http分解阶段1", is_ssl, tmp)
  56. -- 然后判断host段
  57. local uri = ""
  58. local host = ""
  59. local port = 0
  60. if tmp:find("/") then
  61. uri = tmp:sub((tmp:find("/"))) -- 注意find会返回多个值
  62. tmp = tmp:sub(1, tmp:find("/") - 1)
  63. else
  64. uri = "/"
  65. end
  66. -- log.info("http分解阶段2", is_ssl, tmp, uri)
  67. if tmp == nil or #tmp == 0 then
  68. log.error(TAG, "非法的URL", url)
  69. return -101, "非法的URL"
  70. end
  71. -- 有无鉴权信息
  72. if tmp:find("@") then
  73. local auth = tmp:sub(1, tmp:find("@") - 1)
  74. if not opts.headers["Authorization"] then
  75. opts.headers["Authorization"] = "Basic " .. auth:toBase64()
  76. end
  77. -- log.info("http鉴权信息", auth, opts.headers["Authorization"])
  78. tmp = tmp:sub(tmp:find("@") + 1)
  79. end
  80. -- 解析端口
  81. if tmp:find(":") then
  82. host = tmp:sub(1, tmp:find(":") - 1)
  83. port = tmp:sub(tmp:find(":") + 1)
  84. port = tonumber(port)
  85. else
  86. host = tmp
  87. end
  88. if not port or port < 1 then
  89. if is_ssl then
  90. port = 443
  91. else
  92. port = 80
  93. end
  94. end
  95. -- 收尾工作
  96. if not opts.headers["Host"] then
  97. opts.headers["Host"] = string.format("%s:%d", host, port)
  98. end
  99. -- Connection 必须关闭
  100. opts.headers["Connection"] = "Close"
  101. -- 复位一些变量,免得判断出错
  102. opts.is_closed = nil
  103. opts.body_len = 0
  104. -- multipart需要boundary
  105. local boundary = "------------------------16ef6e68ef" .. tostring(os.time())
  106. opts.boundary = boundary
  107. opts.mp = {}
  108. if opts.files then
  109. -- 强制设置为true
  110. opts.multipart = true
  111. end
  112. -- 表单数据
  113. if opts.forms then
  114. if opts.multipart then
  115. for kk, vv in pairs(opts.forms) do
  116. local tmp = string.format("--%s\r\nContent-Disposition: form-data; name=\"%s\"\r\n\r\n", boundary, kk)
  117. table.insert(opts.mp, {vv, tmp, "form"})
  118. opts.body_len = opts.body_len + #tmp + #vv + 2
  119. -- log.info("当前body长度", opts.body_len, "数据长度", #vv)
  120. end
  121. else
  122. if not opts.headers["Content-Type"] then
  123. opts.headers["Content-Type"] = "application/x-www-form-urlencoded;charset=UTF-8"
  124. end
  125. local buff = zbuff.create(120)
  126. for kk, vv in pairs(opts.forms) do
  127. buff:copy(nil, kk)
  128. buff:copy(nil, "=")
  129. buff:copy(nil, string.urlEncode(tostring(vv)))
  130. buff:copy(nil, "&")
  131. end
  132. if buff:used() > 0 then
  133. buff:del(-1, 1)
  134. opts.body = buff
  135. opts.body_len = buff:used()
  136. opts.log(TAG, "普通表单", opts.body)
  137. end
  138. end
  139. end
  140. if opts.files then
  141. -- 强制设置为true
  142. opts.multipart = true
  143. local contentType =
  144. {
  145. txt = "text/plain", -- 文本
  146. jpg = "image/jpeg", -- JPG 格式图片
  147. jpeg = "image/jpeg", -- JPEG 格式图片
  148. png = "image/png", -- PNG 格式图片
  149. gif = "image/gif", -- GIF 格式图片
  150. html = "image/html", -- HTML
  151. json = "application/json" -- JSON
  152. }
  153. for kk, vv in pairs(opts.files) do
  154. local ct = contentType[vv:match("%.(%w+)$")] or "application/octet-stream"
  155. local fname = vv:match("[^%/]+%w$")
  156. local tmp = string.format("--%s\r\nContent-Disposition: form-data; name=\"%s\"; filename=\"%s\"\r\nContent-Type: %s\r\n\r\n", boundary, kk, fname, ct)
  157. -- log.info("文件传输头", tmp)
  158. table.insert(opts.mp, {vv, tmp, "file"})
  159. opts.body_len = opts.body_len + #tmp + io.fileSize(vv) + 2
  160. -- log.info("当前body长度", opts.body_len, "文件长度", io.fileSize(vv), fname, ct)
  161. end
  162. end
  163. -- 如果multipart模式
  164. if opts.multipart then
  165. -- 如果没主动设置body, 那么补个结尾
  166. if not opts.body then
  167. opts.body_len = opts.body_len + #boundary + 2 + 2 + 2
  168. end
  169. -- Content-Type没设置? 那就设置一下
  170. if not opts.headers["Content-Type"] then
  171. opts.headers["Content-Type"] = "multipart/form-data; boundary="..boundary
  172. end
  173. end
  174. -- 直接设置bodyfile
  175. if opts.bodyfile then
  176. local fd = io.open(opts.bodyfile, "rb")
  177. if not fd then
  178. log.error("httpplus", "bodyfile失败,文件不存在", opts.bodyfile)
  179. return -104, "bodyfile失败,文件不存在"
  180. end
  181. fd:close()
  182. opts.body_len = io.fileSize(opts.bodyfile)
  183. end
  184. -- 有设置body, 而且没设置长度
  185. if opts.body and (not opts.body_len or opts.body_len == 0) then
  186. -- body是zbuff的情况
  187. if type(opts.body) == "userdata" then
  188. opts.body_len = opts.body:used()
  189. -- body是json的情况
  190. elseif type(opts.body) == "table" then
  191. opts.body = json.encode(opts.body, "7f")
  192. if opts.body then
  193. opts.body_len = #opts.body
  194. if not opts.headers["Content-Type"] then
  195. opts.headers["Content-Type"] = "application/json;charset=UTF-8"
  196. opts.log(TAG, "JSON", opts.body)
  197. end
  198. end
  199. -- 其他情况就只能当文本了
  200. else
  201. opts.body = tostring(opts.body)
  202. opts.body_len = #opts.body
  203. end
  204. end
  205. -- 一定要设置Content-Length,而且强制覆盖客户自定义的值
  206. -- opts.body_len = opts.body_len or 0
  207. opts.headers["Content-Length"] = tostring(opts.body_len or 0)
  208. -- 如果没设置method, 自动补齐
  209. if not opts.method or #opts.method == 0 then
  210. if opts.body_len > 0 then
  211. opts.method = "POST"
  212. else
  213. opts.method = "GET"
  214. end
  215. else
  216. -- 确保一定是大写字母
  217. opts.method = opts.method:upper()
  218. end
  219. if opts.debug then
  220. opts.log(TAG, is_ssl, host, port, uri, json.encode(opts.headers))
  221. end
  222. -- 把剩余的属性设置好
  223. opts.host = host
  224. opts.port = port
  225. opts.uri = uri
  226. opts.is_ssl = is_ssl
  227. if not opts.timeout or opts.timeout == 0 then
  228. opts.timeout = 30
  229. end
  230. return -- 成功完成,不需要返回值
  231. end
  232. local function zbuff_find(buff, str)
  233. -- log.info("zbuff查找", buff:used(), #str)
  234. if buff:used() < #str then
  235. return
  236. end
  237. local maxoff = buff:used()
  238. maxoff = maxoff - #str
  239. local tmp = zbuff.create(#str)
  240. tmp:write(str)
  241. -- log.info("tmp数据", tmp:query():toHex())
  242. for i = 0, maxoff, 1 do
  243. local flag = true
  244. for j = 0, #str - 1, 1 do
  245. -- log.info("对比", i, j, string.char(buff[i+j]):toHex(), string.char(tmp[j]):toHex(), buff[i+j] ~= tmp[j])
  246. if buff[i+j] ~= tmp[j] then
  247. flag = false
  248. break
  249. end
  250. end
  251. if flag then
  252. return i
  253. end
  254. end
  255. end
  256. local function resp_parse(opts)
  257. -- log.info("这里--------")
  258. local header_offset = zbuff_find(opts.rx_buff, "\r\n\r\n")
  259. -- log.info("头部偏移量", header_offset)
  260. if not header_offset then
  261. log.warn(TAG, "没有检测到http响应头部,非法响应")
  262. opts.resp_code = -198
  263. return
  264. end
  265. local state_line_offset = zbuff_find(opts.rx_buff, "\r\n")
  266. local state_line = opts.rx_buff:query(0, state_line_offset)
  267. local tmp = state_line:split(" ")
  268. if not tmp or #tmp < 2 then
  269. log.warn(TAG, "非法的响应行", state_line)
  270. opts.resp_code = -197
  271. return
  272. end
  273. local code = tonumber(tmp[2])
  274. if not code then
  275. log.warn(TAG, "非法的响应码", tmp[2])
  276. opts.resp_code = -196
  277. return
  278. end
  279. opts.resp_code = code
  280. opts.resp = {
  281. headers = {}
  282. }
  283. opts.log(TAG, "state code", code)
  284. -- TODO 解析header和body
  285. opts.rx_buff:del(0, state_line_offset + 2)
  286. -- opts.log(TAG, "剩余的响应体", opts.rx_buff:query())
  287. -- 解析headers
  288. while 1 do
  289. local offset = zbuff_find(opts.rx_buff, "\r\n")
  290. if not offset then
  291. log.warn(TAG, "不合法的剩余headers", opts.rx_buff:query())
  292. break
  293. end
  294. if offset == 0 then
  295. -- header的最后一个空行
  296. opts.rx_buff:del(0, 2)
  297. break
  298. end
  299. local line = opts.rx_buff:query(0, offset)
  300. opts.rx_buff:del(0, offset + 2)
  301. local tmp2 = line:split(":")
  302. opts.log(TAG, tmp2[1]:trim(), tmp2[2]:trim())
  303. opts.resp.headers[tmp2[1]:trim()] = tmp2[2]:trim()
  304. end
  305. -- if opts.resp_code < 299 then
  306. -- 解析body
  307. -- 有Content-Length就好办
  308. if opts.resp.headers["Content-Length"] then
  309. opts.log(TAG, "有长度, 标准的咯")
  310. opts.resp.body = opts.rx_buff
  311. elseif opts.resp.headers["Transfer-Encoding"] == "chunked" then
  312. -- log.info(TAG, "数据是chunked编码", opts.rx_buff[0], opts.rx_buff[1])
  313. -- log.info(TAG, "数据是chunked编码", opts.rx_buff:query(0, 4):toHex())
  314. local coffset = 0
  315. local crun = true
  316. while crun and coffset < opts.rx_buff:used() do
  317. -- 从当前offset读取长度, 长度总不会超过8字节吧?
  318. local flag = true
  319. -- local coffset = zbuff_find(opts.rx_buff, "\r\n")
  320. -- if not coffset then
  321. -- end
  322. for i = 1, 8, 1 do
  323. if opts.rx_buff[coffset+i] == 0x0D and opts.rx_buff[coffset+i+1] == 0x0A then
  324. local ctmp = opts.rx_buff:query(coffset, i)
  325. -- opts.log(TAG, "chunked分片长度", ctmp, ctmp:toHex())
  326. local clen = tonumber(ctmp, 16)
  327. -- opts.log(TAG, "chunked分片长度2", clen)
  328. if clen == 0 then
  329. -- 末尾了
  330. opts.rx_buff:resize(coffset)
  331. crun = false
  332. else
  333. -- 先删除chunked块
  334. opts.rx_buff:del(coffset, i+2)
  335. coffset = coffset + clen
  336. end
  337. flag = false
  338. break
  339. end
  340. end
  341. -- 肯定能搜到chunked
  342. if flag then
  343. log.error("非法的chunked块")
  344. break
  345. end
  346. end
  347. opts.resp.body = opts.rx_buff
  348. end
  349. -- end
  350. -- 清空rx_buff
  351. opts.rx_buff = nil
  352. -- 完结散花
  353. end
  354. -- socket 回调函数
  355. local function http_socket_cb(opts, event)
  356. opts.log(TAG, "tcp.event", event)
  357. if event == socket.ON_LINE then
  358. -- TCP链接已建立, 那就可以上行了
  359. -- opts.state = "ON_LINE"
  360. sys.publish(opts.topic)
  361. elseif event == socket.TX_OK then
  362. -- 数据传输完成, 如果是文件上传就需要这个消息
  363. -- opts.state = "TX_OK"
  364. sys.publish(opts.topic)
  365. elseif event == socket.EVENT then
  366. -- 收到数据或者链接断开了, 这里总需要读取一次才知道
  367. local succ, data_len = socket.rx(opts.netc, opts.rx_buff)
  368. if succ and data_len > 0 then
  369. opts.log(TAG, "收到数据", data_len, "总长", #opts.rx_buff)
  370. -- opts.log(TAG, "数据", opts.rx_buff:query())
  371. else
  372. if not opts.is_closed then
  373. opts.log(TAG, "服务器已经断开了连接或接收出错")
  374. opts.is_closed = true
  375. sys.publish(opts.topic)
  376. end
  377. end
  378. elseif event == socket.CLOSED then
  379. log.info(TAG, "连接已关闭")
  380. opts.is_closed = true
  381. sys.publish(opts.topic)
  382. end
  383. end
  384. local function http_exec(opts)
  385. local netc = socket.create(opts.adapter, function(sc, event)
  386. if opts.netc then
  387. return http_socket_cb(opts, event)
  388. end
  389. end)
  390. if not netc then
  391. log.error(TAG, "创建socket失败了!!")
  392. return -102
  393. end
  394. opts.netc = netc
  395. opts.rx_buff = zbuff.create(1024)
  396. opts.topic = tostring(netc)
  397. socket.config(netc, nil,nil, opts.is_ssl)
  398. if opts.debug or httpplus.debug then
  399. socket.debug(netc)
  400. end
  401. if not socket.connect(netc, opts.host, opts.port, opts.try_ipv6) then
  402. log.warn(TAG, "调用socket.connect返回错误了")
  403. return -103, "调用socket.connect返回错误了"
  404. end
  405. local ret = sys.waitUntil(opts.topic, 5000)
  406. if ret == false then
  407. log.warn(TAG, "建立连接超时了!!!")
  408. return -104, "建立连接超时了!!!"
  409. end
  410. -- 首先是头部
  411. local line = string.format("%s %s HTTP/1.1\r\n", opts.method:upper(), opts.uri)
  412. -- opts.log(TAG, line)
  413. socket.tx(netc, line)
  414. for k, v in pairs(opts.headers) do
  415. line = string.format("%s: %s\r\n", k, v)
  416. socket.tx(netc, line)
  417. end
  418. line = "\r\n"
  419. socket.tx(netc, line)
  420. -- 然后是body
  421. local rbody = ""
  422. local write_counter = 0
  423. if opts.mp and #opts.mp > 0 then
  424. opts.log(TAG, "执行mulitpart上传模式")
  425. for k, v in pairs(opts.mp) do
  426. socket.tx(netc, v[2])
  427. write_counter = write_counter + #v[2]
  428. if v[3] == "file" then
  429. -- log.info("写入文件数据头", v[2])
  430. local fd = io.open(v[1], "rb")
  431. -- log.info("写入文件数据", v[1])
  432. if fd then
  433. while not opts.is_closed do
  434. local fdata = fd:read(1400)
  435. if not fdata or #fdata == 0 then
  436. break
  437. end
  438. -- log.info("写入文件数据", "长度", #fdata)
  439. socket.tx(netc, fdata)
  440. write_counter = write_counter + #fdata
  441. -- 注意, 这里要等待TX_OK事件
  442. sys.waitUntil(opts.topic, 300)
  443. end
  444. fd:close()
  445. end
  446. else
  447. socket.tx(netc, v[1])
  448. write_counter = write_counter + #v[1]
  449. end
  450. socket.tx(netc, "\r\n")
  451. write_counter = write_counter + 2
  452. end
  453. -- rbody = rbody .. "--" .. opts.boundary .. "--\r\n"
  454. socket.tx(netc, "--")
  455. socket.tx(netc, opts.boundary)
  456. socket.tx(netc, "--\r\n")
  457. write_counter = write_counter + #opts.boundary + 2 + 2 + 2
  458. elseif opts.bodyfile then
  459. local fd = io.open(opts.bodyfile, "rb")
  460. -- log.info("写入文件数据", v[1])
  461. if fd then
  462. while not opts.is_closed do
  463. local fdata = fd:read(1400)
  464. if not fdata or #fdata == 0 then
  465. break
  466. end
  467. -- log.info("写入文件数据", "长度", #fdata)
  468. socket.tx(netc, fdata)
  469. write_counter = write_counter + #fdata
  470. -- 注意, 这里要等待TX_OK事件
  471. sys.waitUntil(opts.topic, 300)
  472. end
  473. fd:close()
  474. end
  475. elseif opts.body then
  476. if type(opts.body) == "string" and #opts.body > 0 then
  477. socket.tx(netc, opts.body)
  478. write_counter = write_counter + #opts.body
  479. elseif type(opts.body) == "userdata" then
  480. write_counter = write_counter + opts.body:used()
  481. if opts.body:used() < 4*1024 then
  482. socket.tx(netc, opts.body)
  483. else
  484. local offset = 0
  485. local tmpbuff = opts.body
  486. local tsize = tmpbuff:used()
  487. while offset < tsize do
  488. opts.log(TAG, "body(zbuff)分段写入", offset, tsize)
  489. if tsize - offset > 4096 then
  490. socket.tx(netc, tmpbuff:toStr(offset, 4096))
  491. offset = offset + 4096
  492. sys.waitUntil(opts.topic, 300)
  493. else
  494. socket.tx(netc, tmpbuff:toStr(offset, tsize - offset))
  495. break
  496. end
  497. end
  498. end
  499. end
  500. end
  501. -- log.info("写入长度", "期望", opts.body_len, "实际", write_counter)
  502. -- log.info("hex", rbody)
  503. -- 处理响应信息
  504. while not opts.is_closed and opts.timeout > 0 do
  505. log.info(TAG, "等待服务器完成响应")
  506. sys.waitUntil(opts.topic, 1000)
  507. opts.timeout = opts.timeout - 1
  508. end
  509. log.info(TAG, "服务器已完成响应,开始解析响应")
  510. resp_parse(opts)
  511. -- log.info("执行完成", "返回结果")
  512. end
  513. --[[
  514. 执行HTTP请求
  515. @api httpplus.request(opts)
  516. @table 请求参数,是一个table,最起码得有url属性
  517. @return int 响应码,服务器返回的状态码>=100, 若本地检测到错误,会返回<0的值
  518. @return 服务器正常响应时返回结果, 否则是错误信息或者nil
  519. @usage
  520. -- 请求参数介绍
  521. local opts = {
  522. url = "https://httpbin.air32.cn/abc", -- 必选, 目标URL
  523. method = "POST", -- 可选,默认GET, 如果有body,files,forms参数,会设置成POST
  524. headers = {}, -- 可选,自定义的额外header
  525. files = {}, -- 可选,键值对的形式,文件上传,若存在本参数,会强制以multipart/form-data形式上传
  526. forms = {}, -- 可选,键值对的形式,表单参数,若存在本参数,如果不存在files,按application/x-www-form-urlencoded上传
  527. body = "abc=123",-- 可选,自定义body参数, 字符串/zbuff/table均可, 但不能与files和forms同时存在
  528. debug = false, -- 可选,打开调试日志,默认false
  529. try_ipv6 = false, -- 可选,是否优先尝试ipv6地址,默认是false
  530. adapter = nil, -- 可选,网络适配器编号, 默认是自动选
  531. timeout = 30, -- 可选,读取服务器响应的超时时间,单位秒,默认30
  532. bodyfile = "xxx" -- 可选,直接把文件内容作为body上传, 优先级高于body参数
  533. }
  534. local code, resp = httpplus.request({url="https://httpbin.air32.cn/get"})
  535. log.info("http", code)
  536. -- 返回值resp的说明
  537. -- 情况1, code >= 100 时, resp会是个table, 包含2个元素
  538. if code >= 100 then
  539. -- headers, 是个table
  540. log.info("http", "headers", json.encode(resp.headers))
  541. -- body, 是个zbuff
  542. -- 通过query函数可以转为lua的string
  543. log.info("http", "headers", resp.body:query())
  544. -- 也可以通过uart.tx等支持zbuff的函数转发出去
  545. -- uart.tx(1, resp.body)
  546. end
  547. ]]
  548. function httpplus.request(opts)
  549. -- 参数解析
  550. local ret = http_opts_parse(opts)
  551. if ret then
  552. return ret
  553. end
  554. -- 执行请求
  555. local ret, msg = pcall(http_exec, opts)
  556. if opts.netc then
  557. -- 清理连接
  558. if not opts.is_closed then
  559. socket.close(opts.netc)
  560. end
  561. socket.release(opts.netc)
  562. opts.netc = nil
  563. end
  564. -- 处理响应或错误
  565. if not ret then
  566. log.error(TAG, msg)
  567. return -199, msg
  568. end
  569. return opts.resp_code, opts.resp
  570. end
  571. return httpplus