luat_lib_http.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /*
  2. @module http
  3. @summary http 客户端
  4. @version 1.0
  5. @date 2022.09.05
  6. @demo socket
  7. @tag LUAT_USE_NETWORK
  8. @usage
  9. -- 使用http库,需要引入sysplus库, 且需要在task内使用
  10. require "sys"
  11. require "sysplus"
  12. sys.taskInit(function()
  13. sys.wait(1000)
  14. local code,headers,body = http.request("GET", "http://www.example.com/abc").wait()
  15. log.info("http", code, body)
  16. end)
  17. */
  18. #include "luat_base.h"
  19. #include "luat_network_adapter.h"
  20. #include "luat_rtos.h"
  21. #include "luat_msgbus.h"
  22. #include "luat_fs.h"
  23. #include "luat_malloc.h"
  24. #include "http_parser.h"
  25. #include "luat_http.h"
  26. #define LUAT_LOG_TAG "http"
  27. #include "luat_log.h"
  28. #define HTTP_DEBUG 0
  29. #if HTTP_DEBUG == 0
  30. #undef LLOGD
  31. #define LLOGD(...)
  32. #endif
  33. int http_close(luat_http_ctrl_t *http_ctrl);
  34. int http_set_url(luat_http_ctrl_t *http_ctrl, const char* url, const char* method);
  35. static int http_add_header(luat_http_ctrl_t *http_ctrl, const char* name, const char* value){
  36. // LLOGD("http_add_header name:%s value:%s",name,value);
  37. // TODO 对value还需要进行urlencode
  38. sprintf_(http_ctrl->req_header + strlen((char*)http_ctrl->req_header), "%s:%s\r\n", name, value);
  39. // strncat(http_ctrl->req_header, name, strlen(name));
  40. // strncat(http_ctrl->req_header, ":", 1);
  41. // strncat(http_ctrl->req_header, value, strlen(value));
  42. // strncat(http_ctrl->req_header, "\r\n", 2);
  43. // LLOGD("http_ctrl->req_header:%s",http_ctrl->req_header);
  44. return 0;
  45. }
  46. /*
  47. http客户端
  48. @api http.request(method,url,headers,body,opts,ca_file)
  49. @string 请求方法, 支持 GET/POST
  50. @string url地址
  51. @tabal 请求头 可选 例如{["Content-Type"] = "application/x-www-form-urlencoded"}
  52. @string body 可选
  53. @table 额外配置 可选 包含 timeout:超时时间单位ms 可选,默认10分钟,写0即永久等待 dst:下载路径,可选 adapter:选择使用网卡,可选 debug:是否打开debug信息,可选,ipv6:是否为ipv6 默认不是,可选
  54. @string 服务器ca证书数据
  55. @string 客户端ca证书数据
  56. @string 客户端私钥加密数据
  57. @string 客户端私钥口令数据
  58. @return int code
  59. @return tabal headers
  60. @return string body
  61. @usage
  62. -- GET请求
  63. local code, headers, body = http.request("GET","http://site0.cn/api/httptest/simple/time").wait()
  64. log.info("http.get", code, headers, body)
  65. -- POST请求
  66. local code, headers, body = http.request("POST","http://httpbin.com/post", {}, "abc=123").wait()
  67. log.info("http.post", code, headers, body)
  68. -- GET请求,但下载到文件
  69. local code, headers, body = http.request("GET","http://httpbin.com/", {}, "", {dst="/data.bin"}).wait()
  70. log.info("http.get", code, headers, body)
  71. */
  72. static int l_http_request(lua_State *L) {
  73. size_t server_cert_len,client_cert_len, client_key_len, client_password_len,len;
  74. const char *server_cert = NULL;
  75. const char *client_cert = NULL;
  76. const char *client_key = NULL;
  77. const char *client_password = NULL;
  78. int adapter_index = -1;
  79. char body_len[6] = {0};
  80. // mbedtls_debug_set_threshold(4);
  81. luat_http_ctrl_t *http_ctrl = (luat_http_ctrl_t *)luat_heap_malloc(sizeof(luat_http_ctrl_t));
  82. if (!http_ctrl){
  83. LLOGE("out of memory when malloc http_ctrl");
  84. lua_pushinteger(L,HTTP_ERROR_CONNECT);
  85. luat_pushcwait_error(L,1);
  86. return 1;
  87. }
  88. memset(http_ctrl, 0, sizeof(luat_http_ctrl_t));
  89. http_ctrl->timeout = HTTP_TIMEOUT;
  90. int use_ipv6 = 0;
  91. if (lua_istable(L, 5)){
  92. lua_pushstring(L, "adapter");
  93. if (LUA_TNUMBER == lua_gettable(L, 5)) {
  94. adapter_index = luaL_optinteger(L, -1, network_get_last_register_adapter());
  95. }else{
  96. adapter_index = network_get_last_register_adapter();
  97. }
  98. lua_pop(L, 1);
  99. lua_pushstring(L, "timeout");
  100. if (LUA_TNUMBER == lua_gettable(L, 5)) {
  101. http_ctrl->timeout = luaL_optinteger(L, -1, HTTP_TIMEOUT);
  102. }
  103. lua_pop(L, 1);
  104. lua_pushstring(L, "dst");
  105. if (LUA_TSTRING == lua_gettable(L, 5)) {
  106. const char *dst = luaL_checklstring(L, -1, &len);
  107. http_ctrl->dst = luat_heap_malloc(len + 1);
  108. memset(http_ctrl->dst, 0, len + 1);
  109. memcpy(http_ctrl->dst, dst, len);
  110. http_ctrl->is_download = 1;
  111. }
  112. lua_pop(L, 1);
  113. lua_pushstring(L, "debug");
  114. if (LUA_TBOOLEAN == lua_gettable(L, 5)) {
  115. http_ctrl->netc->is_debug = lua_toboolean(L, -1);
  116. }
  117. lua_pop(L, 1);
  118. lua_pushstring(L, "ipv6");
  119. if (LUA_TBOOLEAN == lua_gettable(L, 5) && lua_toboolean(L, -1)) {
  120. use_ipv6 = 1;
  121. }
  122. lua_pop(L, 1);
  123. }else{
  124. adapter_index = network_get_last_register_adapter();
  125. }
  126. if (adapter_index < 0 || adapter_index >= NW_ADAPTER_QTY){
  127. LLOGD("bad network adapter index %d", adapter_index);
  128. goto error;
  129. }
  130. http_ctrl->netc = network_alloc_ctrl(adapter_index);
  131. if (!http_ctrl->netc){
  132. LLOGE("netc create fail");
  133. goto error;
  134. }
  135. luat_http_client_init(http_ctrl, use_ipv6);
  136. const char *method = luaL_optlstring(L, 1, "GET", &len);
  137. if (len > 11) {
  138. LLOGE("method is too long %s", method);
  139. goto error;
  140. }
  141. // memcpy(http_ctrl->method, method, len + 1);
  142. // LLOGD("method:%s",http_ctrl->method);
  143. const char *url = luaL_checklstring(L, 2, &len);
  144. // http_ctrl->url = luat_heap_malloc(len + 1);
  145. // memset(http_ctrl->url, 0, len + 1);
  146. // memcpy(http_ctrl->url, url, len);
  147. int ret = http_set_url(http_ctrl, url, method);
  148. if (ret){
  149. goto error;
  150. }
  151. // LLOGD("http_ctrl->url:%s",http_ctrl->url);
  152. http_ctrl->req_header = luat_heap_malloc(HTTP_RESP_HEADER_MAX_SIZE);
  153. memset(http_ctrl->req_header, 0, HTTP_RESP_HEADER_MAX_SIZE);
  154. if (lua_istable(L, 3)) {
  155. lua_pushnil(L);
  156. while (lua_next(L, 3) != 0) {
  157. const char *name = lua_tostring(L, -2);
  158. const char *value = lua_tostring(L, -1);
  159. if (!strcmp("Host", name) || !strcmp("host", name)) {
  160. http_ctrl->custom_host = 1;
  161. }
  162. if (strcmp("Content-Length", name)) {
  163. http_add_header(http_ctrl,name,value);
  164. }
  165. lua_pop(L, 1);
  166. }
  167. }
  168. if (lua_isstring(L, 4)) {
  169. const char *body = luaL_checklstring(L, 4, &(http_ctrl->req_body_len));
  170. http_ctrl->req_body = luat_heap_malloc((http_ctrl->req_body_len) + 1);
  171. memset(http_ctrl->req_body, 0, (http_ctrl->req_body_len) + 1);
  172. memcpy(http_ctrl->req_body, body, (http_ctrl->req_body_len));
  173. snprintf_(body_len, 6,"%d",(http_ctrl->req_body_len));
  174. http_add_header(http_ctrl,"Content-Length",body_len);
  175. }
  176. // TODO 对 req_header进行realloc
  177. if (http_ctrl->is_tls){
  178. if (lua_isstring(L, 6)){
  179. server_cert = luaL_checklstring(L, 6, &server_cert_len);
  180. }
  181. if (lua_isstring(L, 7)){
  182. client_cert = luaL_checklstring(L, 7, &client_cert_len);
  183. }
  184. if (lua_isstring(L, 8)){
  185. client_key = luaL_checklstring(L, 8, &client_key_len);
  186. }
  187. if (lua_isstring(L, 9)){
  188. client_password = luaL_checklstring(L, 9, &client_password_len);
  189. }
  190. network_init_tls(http_ctrl->netc, (server_cert || client_cert)?2:0);
  191. if (server_cert){
  192. network_set_server_cert(http_ctrl->netc, (const unsigned char *)server_cert, server_cert_len+1);
  193. }
  194. if (client_cert){
  195. network_set_client_cert(http_ctrl->netc, (const unsigned char *)client_cert, client_cert_len+1,
  196. (const unsigned char *)client_key, client_key_len+1,
  197. (const unsigned char *)client_password, client_password_len+1);
  198. }
  199. }else{
  200. network_deinit_tls(http_ctrl->netc);
  201. }
  202. #ifdef LUAT_USE_LWIP
  203. http_ctrl->ip_addr.type = 0xff;
  204. #else
  205. http_ctrl->ip_addr.is_ipv6 = 0xff;
  206. #endif
  207. http_ctrl->idp = luat_pushcwait(L);
  208. if (luat_http_client_start(http_ctrl)) {
  209. goto error;
  210. }
  211. return 1;
  212. error:
  213. if (http_ctrl->timeout_timer){
  214. luat_stop_rtos_timer(http_ctrl->timeout_timer);
  215. http_ctrl->timeout_timer = NULL;
  216. }
  217. http_close(http_ctrl);
  218. lua_pushinteger(L,HTTP_ERROR_CONNECT);
  219. luat_pushcwait_error(L,1);
  220. return 1;
  221. }
  222. #include "rotable2.h"
  223. const rotable_Reg_t reg_http[] =
  224. {
  225. {"request", ROREG_FUNC(l_http_request)},
  226. { NULL, ROREG_INT(0)}
  227. };
  228. const rotable_Reg_t reg_http_emtry[] =
  229. {
  230. { NULL, ROREG_INT(0)}
  231. };
  232. LUAMOD_API int luaopen_http( lua_State *L ) {
  233. #ifdef LUAT_USE_NETWORK
  234. luat_newlib2(L, reg_http);
  235. #else
  236. luat_newlib2(L, reg_http_emtry);
  237. LLOGE("reg_http require network enable!!");
  238. #endif
  239. lua_pushvalue(L, -1);
  240. lua_setglobal(L, "http2");
  241. return 1;
  242. }
  243. //------------------------------------------------------
  244. int32_t l_http_callback(lua_State *L, void* ptr){
  245. char* temp;
  246. char* header;
  247. char* value;
  248. uint16_t header_len = 0,value_len = 0;
  249. rtos_msg_t* msg = (rtos_msg_t*)lua_topointer(L, -1);
  250. luat_http_ctrl_t *http_ctrl =(luat_http_ctrl_t *)msg->ptr;
  251. uint64_t idp = http_ctrl->idp;
  252. if (http_ctrl->timeout_timer){
  253. luat_stop_rtos_timer(http_ctrl->timeout_timer);
  254. }
  255. LLOGD("l_http_callback arg1:%d is_download:%d idp:%d",msg->arg1,http_ctrl->is_download,idp);
  256. if (msg->arg1){
  257. lua_pushinteger(L, msg->arg1); // 把错误码返回去
  258. luat_cbcwait(L, idp, 1);
  259. goto exit;
  260. }
  261. lua_pushinteger(L, http_ctrl->parser.status_code);
  262. lua_newtable(L);
  263. // LLOGD("http_ctrl->headers:%.*s",http_ctrl->headers_len,http_ctrl->headers);
  264. header = http_ctrl->headers;
  265. while ( (http_ctrl->headers_len)>0 ){
  266. value = strstr(header,":")+1;
  267. if (value[1]==' '){
  268. value++;
  269. }
  270. temp = strstr(value,"\r\n")+2;
  271. header_len = value-header-1;
  272. value_len = temp-value-2;
  273. LLOGD("header:%.*s",header_len,header);
  274. LLOGD("value:%.*s",value_len,value);
  275. lua_pushlstring(L, header,header_len);
  276. lua_pushlstring(L, value,value_len);
  277. lua_settable(L, -3);
  278. http_ctrl->headers_len -= temp-header;
  279. header = temp;
  280. }
  281. LLOGD("http_ctrl->body:%.*s len:%d",http_ctrl->body_len,http_ctrl->body,http_ctrl->body_len);
  282. // 处理body, 需要区分下载模式和非下载模式
  283. if (http_ctrl->is_download) {
  284. // 下载模式
  285. if (http_ctrl->fd == NULL) {
  286. // 下载操作一切正常, 返回长度
  287. lua_pushinteger(L, http_ctrl->body_len);
  288. luat_cbcwait(L, idp, 3); // code, headers, body
  289. goto exit;
  290. }else if (http_ctrl->fd != NULL) {
  291. // 下载中断了!!
  292. luat_fs_fclose(http_ctrl->fd);
  293. luat_fs_remove(http_ctrl->dst); // 移除文件
  294. }
  295. // 下载失败, 返回错误码
  296. lua_pushinteger(L, -1);
  297. luat_cbcwait(L, idp, 3); // code, headers, body
  298. goto exit;
  299. } else {
  300. // 非下载模式
  301. lua_pushlstring(L, http_ctrl->body, http_ctrl->body_len);
  302. luat_cbcwait(L, idp, 3); // code, headers, body
  303. }
  304. exit:
  305. http_close(http_ctrl);
  306. return 0;
  307. }
  308. void luat_http_client_onevent(luat_http_ctrl_t *http_ctrl, int arg1, int arg2) {
  309. rtos_msg_t msg = {0};
  310. msg.handler = l_http_callback;
  311. msg.ptr = http_ctrl;
  312. msg.arg1 = arg1;
  313. msg.arg2 = arg2;
  314. luat_msgbus_put(&msg, 0);
  315. }