luat_lib_http.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. /*
  2. @module http
  3. @summary http客户端
  4. @version 1.0
  5. @date 2022.09.05
  6. @demo http
  7. */
  8. #include "luat_base.h"
  9. #ifdef LUAT_USE_NETWORK
  10. #include "luat_network_adapter.h"
  11. #include "luat_rtos.h"
  12. #include "luat_msgbus.h"
  13. #include "luat_fs.h"
  14. #include "luat_malloc.h"
  15. #define LUAT_LOG_TAG "http"
  16. #include "luat_log.h"
  17. #define HTTP_REQUEST_BUF_LEN_MAX 2048
  18. typedef struct{
  19. network_ctrl_t *netc; // http netc
  20. luat_ip_addr_t ip_addr; // http ip
  21. uint8_t is_tls;
  22. const char *host; // http host
  23. uint16_t remote_port; // 远程端口号
  24. const char *url;
  25. const char *uri;
  26. const char *method;
  27. const char *header;
  28. const char *body;
  29. const char *dst;
  30. uint8_t is_download;
  31. uint8_t request_message[HTTP_REQUEST_BUF_LEN_MAX];
  32. uint8_t *reply_message;
  33. uint64_t* idp;
  34. uint16_t timeout;
  35. }luat_http_ctrl_t;
  36. static int http_close(luat_http_ctrl_t *http_ctrl){
  37. if (http_ctrl->netc){
  38. network_force_close_socket(http_ctrl->netc);
  39. network_release_ctrl(http_ctrl->netc);
  40. }
  41. if (http_ctrl->host){
  42. luat_heap_free(http_ctrl->host);
  43. }
  44. if (http_ctrl->url){
  45. luat_heap_free(http_ctrl->url);
  46. }
  47. if (http_ctrl->uri){
  48. luat_heap_free(http_ctrl->uri);
  49. }
  50. if (http_ctrl->method){
  51. luat_heap_free(http_ctrl->method);
  52. }
  53. if (http_ctrl->header){
  54. luat_heap_free(http_ctrl->header);
  55. }
  56. if (http_ctrl->body){
  57. luat_heap_free(http_ctrl->body);
  58. }
  59. if (http_ctrl->dst){
  60. luat_heap_free(http_ctrl->dst);
  61. }
  62. if (http_ctrl->reply_message){
  63. luat_heap_free(http_ctrl->reply_message);
  64. }
  65. if (http_ctrl->idp){
  66. luat_heap_free(http_ctrl->idp);
  67. }
  68. luat_heap_free(http_ctrl);
  69. }
  70. static int32_t l_http_callback(lua_State *L, void* ptr){
  71. rtos_msg_t* msg = (rtos_msg_t*)lua_topointer(L, -1);
  72. luat_http_ctrl_t *http_ctrl =(luat_http_ctrl_t *)msg->ptr;
  73. uint64_t* idp = (uint64_t*)http_ctrl->idp;
  74. if (!strncmp("HTTP/1.", http_ctrl->reply_message, strlen("HTTP/1."))){
  75. uint16_t code_offset = strlen("HTTP/1.x ");
  76. uint16_t code_len = 3;
  77. char *header = strstr(http_ctrl->reply_message,"\r\n")+2;
  78. char *body = strstr(header,"\r\n\r\n")+4;
  79. uint16_t header_len = strlen(header)-strlen(body)-4;
  80. lua_pushlstring(L, http_ctrl->reply_message+code_offset,code_len);
  81. lua_pushlstring(L, header,header_len);
  82. if (http_ctrl->is_download){
  83. luat_fs_remove(http_ctrl->dst);
  84. FILE *fd_out = luat_fs_fopen(http_ctrl->dst, "wb");
  85. if (!fd_out) {
  86. LLOGE("create file fail! %s", fd_out);
  87. }
  88. luat_fs_fwrite(body, 1, strlen(body), fd_out);
  89. luat_fs_fclose(fd_out);
  90. luat_cbcwait(L, *idp, 2);
  91. }else{
  92. lua_pushlstring(L, body,strlen(body));
  93. luat_cbcwait(L, *idp, 3);
  94. }
  95. http_close(http_ctrl);
  96. }
  97. return 0;
  98. }
  99. static int32_t luat_lib_http_callback(void *data, void *param){
  100. OS_EVENT *event = (OS_EVENT *)data;
  101. luat_http_ctrl_t *http_ctrl =(luat_http_ctrl_t *)param;
  102. int ret = 0;
  103. // LLOGD("LINK %d ON_LINE %d EVENT %d TX_OK %d CLOSED %d",EV_NW_RESULT_LINK & 0x0fffffff,EV_NW_RESULT_CONNECT & 0x0fffffff,EV_NW_RESULT_EVENT & 0x0fffffff,EV_NW_RESULT_TX & 0x0fffffff,EV_NW_RESULT_CLOSE & 0x0fffffff);
  104. // LLOGD("luat_lib_mqtt_callback %d %d",event->ID & 0x0fffffff,event->Param1);
  105. if (event->ID == EV_NW_RESULT_LINK){
  106. if(network_connect(http_ctrl->netc, http_ctrl->host, strlen(http_ctrl->host), http_ctrl->ip_addr.is_ipv6?NULL:&(http_ctrl->ip_addr), http_ctrl->remote_port, 0) < 0){
  107. network_close(http_ctrl->netc, 0);
  108. http_close(http_ctrl);
  109. return -1;
  110. }
  111. }else if(event->ID == EV_NW_RESULT_CONNECT){
  112. memset(http_ctrl->request_message, 0, HTTP_REQUEST_BUF_LEN_MAX);
  113. if (http_ctrl->header){
  114. snprintf(http_ctrl->request_message, HTTP_REQUEST_BUF_LEN_MAX,
  115. "%s %s HTTP/1.1\r\nHost: %s\r\n%s",
  116. http_ctrl->method,http_ctrl->uri,http_ctrl->host,http_ctrl->header);
  117. }else{
  118. snprintf(http_ctrl->request_message, HTTP_REQUEST_BUF_LEN_MAX,
  119. "%s %s HTTP/1.1\r\nHost: %s\r\n",
  120. http_ctrl->method,http_ctrl->uri,http_ctrl->host);
  121. }
  122. if (http_ctrl->body)
  123. {
  124. strncat(http_ctrl->request_message, "\r\n", 2);
  125. strncat(http_ctrl->request_message, http_ctrl->body, strlen(http_ctrl->body));
  126. }
  127. strncat(http_ctrl->request_message, "\r\n", 2);
  128. // LLOGD("http_ctrl->request_message:%s",http_ctrl->request_message);
  129. uint32_t tx_len = 0;
  130. network_tx(http_ctrl->netc, http_ctrl->request_message, strlen(http_ctrl->request_message), 0, http_ctrl->ip_addr.is_ipv6?NULL:&(http_ctrl->ip_addr), NULL, &tx_len, 0);
  131. }else if(event->ID == EV_NW_RESULT_EVENT){
  132. if (event->Param1==0){
  133. uint32_t total_len = 0;
  134. uint32_t rx_len = 0;
  135. int result = network_rx(http_ctrl->netc, NULL, 0, 0, NULL, NULL, &total_len);
  136. http_ctrl->reply_message = luat_heap_malloc(total_len + 1);
  137. result = network_rx(http_ctrl->netc, http_ctrl->reply_message, total_len, 0, NULL, NULL, &rx_len);
  138. if (rx_len == 0||result!=0) {
  139. http_close(http_ctrl);
  140. return -1;
  141. }
  142. // LLOGD("http_ctrl->reply_message:%s",http_ctrl->reply_message);
  143. rtos_msg_t msg = {0};
  144. msg.handler = l_http_callback;
  145. msg.ptr = http_ctrl;
  146. luat_msgbus_put(&msg, 0);
  147. }
  148. }else if(event->ID == EV_NW_RESULT_TX){
  149. }else if(event->ID == EV_NW_RESULT_CLOSE){
  150. }
  151. if (event->Param1){
  152. http_close(http_ctrl);
  153. return -1;
  154. }
  155. network_wait_event(http_ctrl->netc, NULL, 0, NULL);
  156. return 0;
  157. }
  158. static int http_add_header(luat_http_ctrl_t *http_ctrl, const char* name, const char* value){
  159. // LLOGD("http_add_header name:%s value:%s",name,value);
  160. if (http_ctrl->header){
  161. http_ctrl->header = luat_heap_realloc(http_ctrl->header, strlen(http_ctrl->header)+strlen(name)+strlen(value)+1);
  162. strncat(http_ctrl->header, name, strlen(name));
  163. strncat(http_ctrl->header, ":", 1);
  164. strncat(http_ctrl->header, value, strlen(value));
  165. strncat(http_ctrl->header, "\r\n", 2);
  166. }else{
  167. http_ctrl->header = luat_heap_malloc(strlen(name)+strlen(value)+1);
  168. memset(http_ctrl->header, 0, strlen(name)+strlen(value)+1);
  169. sprintf(http_ctrl->header, "%s:%s\r\n", name,value);
  170. }
  171. // LLOGD("http_ctrl->header:%s",http_ctrl->header);
  172. }
  173. static int http_set_url(luat_http_ctrl_t *http_ctrl) {
  174. char *tmp = http_ctrl->url;
  175. if (!strncmp("https://", http_ctrl->url, strlen("https://"))) {
  176. http_ctrl->is_tls = 1;
  177. tmp += strlen("https://");
  178. }
  179. else if (!strncmp("http://", http_ctrl->url, strlen("http://"))) {
  180. http_ctrl->is_tls = 0;
  181. tmp += strlen("http://");
  182. }
  183. else {
  184. LLOGI("only http/https supported %s", http_ctrl->url);
  185. return -1;
  186. }
  187. int tmplen = strlen(tmp);
  188. if (tmplen < 5) {
  189. LLOGI("url too short %s", http_ctrl->url);
  190. return -1;
  191. }
  192. char tmphost[256] = {0};
  193. char *tmpuri = NULL;
  194. for (size_t i = 0; i < tmplen; i++){
  195. if (tmp[i] == '/') {
  196. if (i > 255) {
  197. LLOGI("host too long %s", http_ctrl->url);
  198. return -1;
  199. }
  200. memcpy(tmphost, tmp, i);
  201. tmpuri = tmp + i;
  202. break;
  203. }
  204. }
  205. if (strlen(tmphost) < 1) {
  206. LLOGI("host not found %s", http_ctrl->url);
  207. return -1;
  208. }
  209. if (strlen(tmpuri) == 0) {
  210. tmpuri = "/";
  211. }
  212. for (size_t i = 1; i < strlen(tmphost); i++){
  213. if (tmp[i] == ":") {
  214. tmp[i] = 0x00;
  215. http_ctrl->remote_port = atoi(&tmp[i+1]);
  216. break;
  217. }
  218. }
  219. if (http_ctrl->remote_port <= 0) {
  220. if (http_ctrl->is_tls)
  221. http_ctrl->remote_port = 443;
  222. else
  223. http_ctrl->remote_port = 80;
  224. }
  225. // LLOGD("tmphost:%s",tmphost);
  226. // LLOGD("tmpuri:%s",tmpuri);
  227. http_ctrl->host = luat_heap_malloc(strlen(tmphost) + 1);
  228. if (http_ctrl->host == NULL) {
  229. LLOGE("out of memory when malloc host");
  230. return -1;
  231. }
  232. memcpy(http_ctrl->host, tmphost, strlen(tmphost) + 1);
  233. http_ctrl->uri = luat_heap_malloc(strlen(tmpuri) + 1);
  234. if (http_ctrl->uri == NULL) {
  235. LLOGE("out of memory when malloc url");
  236. return -1;
  237. }
  238. memcpy(http_ctrl->uri, tmpuri, strlen(tmpuri) + 1);
  239. // LLOGD("http_ctrl->uri:%s",http_ctrl->uri);
  240. // LLOGD("http_ctrl->host:%s",http_ctrl->host);
  241. // LLOGD("http_ctrl->port:%d",http_ctrl->remote_port);
  242. return 0;
  243. }
  244. /*
  245. http客户端
  246. @api http2.request(method,url,headers,body,opts,ca_file)
  247. @string 请求方法, 支持 GET/POST
  248. @string url地址
  249. @tabal 请求头 可选 例如{["Content-Type"] = "application/x-www-form-urlencoded"}
  250. @string body 可选
  251. @tabal 额外配置 可选 包含dst:下载路径,可选 adapter:选择使用网卡,可选
  252. @string 证书 可选
  253. @usage
  254. local code, headers, body = http2.request("GET","http://site0.cn/api/httptest/simple/time").wait()
  255. log.info("http2.get", code, headers, body)
  256. */
  257. static int l_http_request(lua_State *L) {
  258. size_t client_cert_len, client_key_len, client_password_len,len;
  259. const char *client_cert = NULL;
  260. const char *client_key = NULL;
  261. const char *client_password = NULL;
  262. int adapter_index;
  263. char body_len[6] = {0};
  264. luat_http_ctrl_t *http_ctrl = (luat_http_ctrl_t *)luat_heap_malloc(sizeof(luat_http_ctrl_t));
  265. if (!http_ctrl){
  266. goto error;
  267. }
  268. memset(http_ctrl, 0, sizeof(luat_http_ctrl_t));
  269. if (lua_istable(L, 5)){
  270. lua_pushstring(L, "adapter");
  271. if (LUA_TNUMBER == lua_gettable(L, 5)) {
  272. adapter_index = luaL_optinteger(L, -1, network_get_last_register_adapter());
  273. }else{
  274. adapter_index = network_get_last_register_adapter();
  275. }
  276. lua_pop(L, 1);
  277. lua_pushstring(L, "timeout");
  278. if (LUA_TNUMBER == lua_gettable(L, 5)) {
  279. http_ctrl->timeout = luaL_optinteger(L, -1, 0);
  280. }
  281. lua_pop(L, 1);
  282. lua_pushstring(L, "dst");
  283. if (LUA_TSTRING == lua_gettable(L, 5)) {
  284. const char *dst = luaL_checklstring(L, -1, &len);
  285. http_ctrl->dst = luat_heap_malloc(len + 1);
  286. memset(http_ctrl->dst, 0, len + 1);
  287. memcpy(http_ctrl->dst, dst, len);
  288. http_ctrl->is_download = 1;
  289. }
  290. lua_pop(L, 1);
  291. }else{
  292. adapter_index = network_get_last_register_adapter();
  293. }
  294. if (adapter_index < 0 || adapter_index >= NW_ADAPTER_QTY){
  295. goto error;
  296. }
  297. http_ctrl->netc = network_alloc_ctrl(adapter_index);
  298. if (!http_ctrl->netc){
  299. LLOGE("create fail");
  300. goto error;
  301. }
  302. network_init_ctrl(http_ctrl->netc, NULL, luat_lib_http_callback, http_ctrl);
  303. http_ctrl->netc->is_debug = 1;
  304. network_set_base_mode(http_ctrl->netc, 1, 10000, 0, 0, 0, 0);
  305. network_set_local_port(http_ctrl->netc, 0);
  306. const char *method = luaL_optlstring(L, 1, "GET", &len);
  307. http_ctrl->method = luat_heap_malloc(len + 1);
  308. memset(http_ctrl->method, 0, len + 1);
  309. memcpy(http_ctrl->method, method, len);
  310. // LLOGD("method:%s",http_ctrl->method);
  311. const char *url = luaL_checklstring(L, 2, &len);
  312. http_ctrl->url = luat_heap_malloc(len + 1);
  313. memset(http_ctrl->url, 0, len + 1);
  314. memcpy(http_ctrl->url, url, len);
  315. // LLOGD("http_ctrl->url:%s",http_ctrl->url);
  316. if (lua_istable(L, 3)) {
  317. lua_pushnil(L);
  318. while (lua_next(L, 3) != 0) {
  319. const char *name = lua_tostring(L, -2);
  320. const char *value = lua_tostring(L, -1);
  321. http_add_header(http_ctrl,name,value);
  322. lua_pop(L, 1);
  323. }
  324. }
  325. if (lua_isstring(L, 4)) {
  326. const char *body = luaL_checklstring(L, 4, &len);
  327. http_ctrl->body = luat_heap_malloc(len + 1);
  328. memset(http_ctrl->body, 0, len + 1);
  329. memcpy(http_ctrl->body, body, len);
  330. sprintf(body_len, "%d",len);
  331. http_add_header(http_ctrl,"Content-Length",body_len);
  332. }
  333. if (http_ctrl->is_tls){
  334. if (lua_isstring(L, 6)){
  335. client_cert = luaL_checklstring(L, 6, &client_cert_len);
  336. }
  337. if (lua_isstring(L, 7)){
  338. client_key = luaL_checklstring(L, 7, &client_key_len);
  339. }
  340. if (lua_isstring(L, 8)){
  341. client_password = luaL_checklstring(L, 8, &client_password_len);
  342. }
  343. network_init_tls(http_ctrl->netc, client_cert?2:0);
  344. if (client_cert){
  345. network_set_client_cert(http_ctrl->netc, client_cert, client_cert_len,
  346. client_key, client_key_len,
  347. client_password, client_password_len);
  348. }
  349. }else{
  350. network_deinit_tls(http_ctrl->netc);
  351. }
  352. int ret = http_set_url(http_ctrl);
  353. if (ret){
  354. goto error;
  355. }
  356. if (!strncmp("GET", http_ctrl->method, strlen("GET"))) {
  357. LLOGI("HTTP GET");
  358. }
  359. else if (!strncmp("POST", http_ctrl->method, strlen("POST"))) {
  360. LLOGI("HTTP POST");
  361. }else {
  362. LLOGI("only GET/POST supported %s", http_ctrl->method);
  363. goto error;
  364. }
  365. http_ctrl->ip_addr.is_ipv6 = 0xff;
  366. network_wait_link_up(http_ctrl->netc, 0);
  367. if (ret == 0){
  368. if(network_connect(http_ctrl->netc, http_ctrl->host, strlen(http_ctrl->host), http_ctrl->ip_addr.is_ipv6?NULL:&(http_ctrl->ip_addr), http_ctrl->remote_port, 0) < 0){
  369. network_close(http_ctrl->netc, 0);
  370. goto error;
  371. }
  372. }
  373. uint64_t id = luat_pushcwait(L);
  374. http_ctrl->idp = (uint64_t*)luat_heap_malloc(sizeof(uint64_t));
  375. memcpy(http_ctrl->idp, &id, sizeof(uint64_t));
  376. return 1;
  377. error:
  378. http_close(http_ctrl);
  379. return 0;
  380. }
  381. #include "rotable2.h"
  382. static const rotable_Reg_t reg_http2[] =
  383. {
  384. {"request", ROREG_FUNC(l_http_request)},
  385. { NULL, ROREG_INT(0)}
  386. };
  387. LUAMOD_API int luaopen_http2( lua_State *L ) {
  388. luat_newlib2(L, reg_http2);
  389. return 1;
  390. }
  391. #else
  392. #define LUAT_LOG_TAG "http2"
  393. #include "luat_log.h"
  394. #include "rotable2.h"
  395. static const rotable_Reg_t reg_http2[] =
  396. {
  397. { NULL, ROREG_INT(0)}
  398. };
  399. LUAMOD_API int luaopen_http2( lua_State *L ) {
  400. luat_newlib2(L, reg_http2);
  401. LLOGE("reg_http2 require network enable!!");
  402. return 1;
  403. }
  404. #endif