luat_lib_http.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  1. /*
  2. @module http2
  3. @summary http2客户端
  4. @version 1.0
  5. @date 2022.09.05
  6. @demo network
  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 1024
  18. #define HTTP_RESP_HEADER_MAX_SIZE (4096)
  19. typedef struct{
  20. network_ctrl_t *netc; // http netc
  21. luat_ip_addr_t ip_addr; // http ip
  22. uint8_t is_tls; // 是否SSL
  23. const char *host; // http host
  24. uint16_t remote_port; // 远程端口号
  25. const char *url;
  26. const char *uri;
  27. const char *method;
  28. const char *req_header;
  29. const char *req_body;
  30. size_t req_body_len;
  31. const char *dst;
  32. uint8_t is_download;
  33. uint8_t request_message[HTTP_REQUEST_BUF_LEN_MAX];
  34. // 响应相关
  35. uint8_t resp_header_parsed;
  36. char* resp_headers;
  37. char *resp_buff;
  38. uint32_t resp_buff_len;
  39. uint32_t resp_content_len;
  40. FILE* fd;
  41. uint32_t fd_writed;
  42. uint8_t fd_ok;
  43. uint64_t idp;
  44. uint16_t timeout;
  45. }luat_http_ctrl_t;
  46. static int http_close(luat_http_ctrl_t *http_ctrl){
  47. if (http_ctrl->netc){
  48. network_force_close_socket(http_ctrl->netc);
  49. network_release_ctrl(http_ctrl->netc);
  50. }
  51. if (http_ctrl->host){
  52. luat_heap_free(http_ctrl->host);
  53. }
  54. if (http_ctrl->url){
  55. luat_heap_free(http_ctrl->url);
  56. }
  57. if (http_ctrl->uri){
  58. luat_heap_free(http_ctrl->uri);
  59. }
  60. if (http_ctrl->method){
  61. luat_heap_free(http_ctrl->method);
  62. }
  63. if (http_ctrl->req_header){
  64. luat_heap_free(http_ctrl->req_header);
  65. }
  66. if (http_ctrl->req_body){
  67. luat_heap_free(http_ctrl->req_body);
  68. }
  69. if (http_ctrl->dst){
  70. luat_heap_free(http_ctrl->dst);
  71. }
  72. if (http_ctrl->resp_buff){
  73. luat_heap_free(http_ctrl->resp_buff);
  74. }
  75. if (http_ctrl->resp_headers){
  76. luat_heap_free(http_ctrl->resp_headers);
  77. }
  78. luat_heap_free(http_ctrl);
  79. return 0;
  80. }
  81. static int32_t l_http_callback(lua_State *L, void* ptr){
  82. char code[6] = {0};
  83. rtos_msg_t* msg = (rtos_msg_t*)lua_topointer(L, -1);
  84. uint64_t idp = msg->arg2;
  85. luat_http_ctrl_t *http_ctrl =(luat_http_ctrl_t *)msg->ptr;
  86. // LLOGD("l_http_callback arg1:%d arg2:%d is_download:%d idp:%d",msg->arg1,msg->arg2,http_ctrl->is_download,idp);
  87. if (msg->arg1){
  88. http_close(http_ctrl);
  89. lua_pushinteger(L, msg->arg1); // 把错误码返回去
  90. luat_cbcwait(L, idp, 1);
  91. return 0;
  92. }
  93. // 解析status code
  94. uint16_t code_offset = strlen("HTTP/1.x ");
  95. uint16_t code_len = 3;
  96. strncpy(code, http_ctrl->resp_headers+code_offset,code_len);
  97. lua_pushinteger(L, atoi(code));
  98. // 解析出header
  99. char *body_rec = http_ctrl->resp_headers + strlen(http_ctrl->resp_headers);
  100. lua_newtable(L);
  101. char* temp;
  102. char *header;
  103. char *value;
  104. uint16_t header_len,value_len;
  105. temp = strstr(http_ctrl->resp_headers,"\r\n")+2;
  106. while ( temp < body_rec){
  107. header = temp;
  108. value = strstr(header,":")+1;
  109. if (value[1]==' '){
  110. value++;
  111. }
  112. temp = strstr(value,"\r\n")+2;
  113. header_len = value-header-1;
  114. value_len = temp-value-2;
  115. // LLOGD("header:%.*s",header_len,header);
  116. // LLOGD("value:%.*s",value_len,value);
  117. lua_pushlstring(L, header,header_len);
  118. lua_pushlstring(L, value,value_len);
  119. lua_settable(L, -3);
  120. }
  121. // 处理body, 需要区分下载模式和非下载模式
  122. if (http_ctrl->is_download) {
  123. // 下载模式
  124. if (http_ctrl->fd_ok) {
  125. // 下载操作一切正常, 返回长度
  126. lua_pushinteger(L, http_ctrl->resp_content_len);
  127. luat_cbcwait(L, idp, 3); // code, headers, body
  128. return 0;
  129. }
  130. if (http_ctrl->fd != NULL) {
  131. // 下载中断了!!
  132. luat_fs_fclose(http_ctrl->fd);
  133. luat_fs_remove(http_ctrl->dst); // 移除文件
  134. }
  135. // 下载失败, 返回错误码
  136. lua_pushinteger(L, -1);
  137. luat_cbcwait(L, idp, 3); // code, headers, body
  138. return 0;
  139. } else {
  140. // 非下载模式
  141. lua_pushlstring(L, http_ctrl->resp_buff, http_ctrl->resp_content_len);
  142. luat_cbcwait(L, idp, 3); // code, headers, body
  143. }
  144. return 0;
  145. }
  146. static void http_resp_error(luat_http_ctrl_t *http_ctrl, int error_code) {
  147. if (http_ctrl->netc){
  148. network_close(http_ctrl->netc, 0);
  149. }
  150. rtos_msg_t msg = {0};
  151. msg.handler = l_http_callback;
  152. msg.ptr = http_ctrl;
  153. msg.arg1 = error_code;
  154. msg.arg2 = http_ctrl->idp;
  155. luat_msgbus_put(&msg, 0);
  156. }
  157. static void http_parse_resp_content_length(luat_http_ctrl_t *http_ctrl) {
  158. // 开始找Content-Length
  159. char* cl_start;
  160. cl_start = strstr(http_ctrl->resp_buff, "Content-Length: ");
  161. if (cl_start == NULL) {
  162. cl_start = strstr(http_ctrl->resp_buff, "CONTENT-LENGTH: ");
  163. if (cl_start == NULL) {
  164. // 当前必须有Content-Length, 否则一律将其设置为0
  165. LLOGD("resp Content-Length not found, set 0");
  166. http_ctrl->resp_content_len = 0;
  167. }
  168. }
  169. else {
  170. char* cl_end = strstr(cl_start, "\r\n");
  171. if (cl_end == NULL) {
  172. // 当前必须有Content-Length, 否则一律将其设置为0
  173. LLOGD("resp Content-Length NOT ok, set 0");
  174. http_ctrl->resp_content_len = 0;
  175. }
  176. else {
  177. char tmp[16] = {0};
  178. cl_start += strlen("Content-Length: ");
  179. memcpy(tmp, cl_start, cl_end - cl_start); // TODO 还需要判断一下长度
  180. http_ctrl->resp_content_len = atoi(tmp);
  181. if (http_ctrl->resp_content_len < 0) {
  182. LLOGD("resp Content-Length not good, %s", tmp);
  183. http_ctrl->resp_content_len = 0;
  184. }
  185. }
  186. }
  187. }
  188. static int http_resp_parse_header(luat_http_ctrl_t *http_ctrl) {
  189. if (strncmp("HTTP/1.", http_ctrl->resp_buff, strlen("HTTP/1."))){
  190. // 开头几个字节不是HTTP/1 ? 可以断开连接了
  191. LLOGW("resp NOT startwith HTTP/1.");
  192. http_resp_error(http_ctrl, -1); // 非法响应
  193. // http_close(http_ctrl);
  194. return -1;
  195. }
  196. else {
  197. char* header_end = strstr(http_ctrl->resp_buff, "\r\n\r\n");
  198. if (header_end != NULL) {
  199. http_ctrl->resp_header_parsed = 1; // 肯定收到头部了, 可以处理了
  200. // 把body的位置先保存起来
  201. char* body_start = header_end + 4;
  202. // 分隔header与body
  203. header_end[2] = 0x00; // 将第二个\r设置为0, 预防Content-Length就是最后一个header
  204. http_parse_resp_content_length(http_ctrl);
  205. http_ctrl->resp_headers = http_ctrl->resp_buff; // 留着解析全部header
  206. if (http_ctrl->resp_content_len > 0) {
  207. // 还有数据
  208. uint32_t header_size = (size_t)(body_start - http_ctrl->resp_headers);
  209. if (http_ctrl->resp_buff_len > header_size) {
  210. // 已经有部分/全部body数据了
  211. http_ctrl->resp_buff = luat_heap_malloc(http_ctrl->resp_buff_len - header_size);
  212. if (http_ctrl->resp_buff == NULL) {
  213. LLOGE("out of memory when malloc buff for http resp");
  214. http_resp_error(http_ctrl, -4); // 炸了
  215. http_close(http_ctrl);
  216. return -1;
  217. }
  218. http_ctrl->resp_buff_len = http_ctrl->resp_buff_len - header_size;
  219. mempcpy(http_ctrl->resp_buff, header_end + 4, http_ctrl->resp_buff_len);
  220. // TODO 把http_ctrl->resp_headers进行realloc会不会好一些
  221. }
  222. else {
  223. http_ctrl->resp_buff = NULL;
  224. http_ctrl->resp_buff_len = 0;
  225. }
  226. }
  227. else {
  228. // 没有数据了,直接结束吧
  229. // 首先, 把headers指向当前buff, 为后续header table做保留
  230. // http_ctrl->resp_headers = http_ctrl->resp_buff;
  231. // 重置 resp 缓冲区, 因为没有数据,直接NULL就行
  232. http_ctrl->resp_buff = NULL;
  233. http_ctrl->resp_buff_len = 0;
  234. }
  235. return 0;
  236. }
  237. else {
  238. // 防御一下太大的header
  239. if (http_ctrl->resp_buff_len > HTTP_RESP_HEADER_MAX_SIZE) {
  240. LLOGW("http resp header too big!!!");
  241. http_resp_error(http_ctrl, -2); // 非法响应
  242. // http_close(http_ctrl);
  243. return 0; // 是返回0还是-1的?
  244. }
  245. else {
  246. // 数据不够, 头部也不齐, 等下一波的数据.
  247. // 后续还需要根据ticks判断一下timeout, 或者timer?
  248. return 0;
  249. }
  250. }
  251. }
  252. return 0;
  253. }
  254. static int http_read_packet(luat_http_ctrl_t *http_ctrl){
  255. if (http_ctrl->resp_header_parsed == 0) {
  256. if (http_ctrl->resp_buff_len < 10) {
  257. return 0; // 继续等头部. TODO 超时判断
  258. }
  259. int ret = http_resp_parse_header(http_ctrl);
  260. if (ret < 0) {
  261. LLOGE("http_resp_parse_header ret:%d",ret);
  262. return ret; // 出错啦
  263. }
  264. // 能到这里, 头部已经解析完成了
  265. // 如果是下载模式, 打开文件, 开始写
  266. if (http_ctrl->is_download) {
  267. luat_fs_remove(http_ctrl->dst);
  268. http_ctrl->fd = luat_fs_fopen(http_ctrl->dst, "w+");
  269. if (http_ctrl->fd == NULL) {
  270. LLOGE("open download file fail %s", http_ctrl->dst);
  271. }
  272. }
  273. }
  274. // 下面是body的处理了
  275. rtos_msg_t msg = {0};
  276. msg.handler = l_http_callback;
  277. msg.ptr = http_ctrl;
  278. msg.arg1 = 0;
  279. msg.arg2 = http_ctrl->idp;
  280. if (http_ctrl->is_download) {
  281. // 写数据
  282. if (http_ctrl->resp_buff_len > 0) {
  283. if (http_ctrl->fd != NULL) {
  284. luat_fs_fwrite(http_ctrl->resp_buff, http_ctrl->resp_buff_len, 1, http_ctrl->fd);
  285. }
  286. http_ctrl->fd_writed += http_ctrl->resp_buff_len;
  287. // 释放buff, 等待下一波数据
  288. http_ctrl->resp_buff_len = 0;
  289. luat_heap_free(http_ctrl->resp_buff);
  290. http_ctrl->resp_buff = NULL;
  291. }
  292. if (http_ctrl->fd_writed == http_ctrl->resp_content_len) {
  293. if (http_ctrl->fd != NULL) {
  294. luat_fs_fclose(http_ctrl->fd);
  295. http_ctrl->fd = NULL;
  296. http_ctrl->fd_ok = 1; // 标记成功
  297. }
  298. // 读完写完, 完结散花
  299. luat_msgbus_put(&msg, 0);
  300. return 0;
  301. }
  302. }
  303. else { // 非下载模式, 等数据齐了就结束
  304. // LLOGD("resp_buff_len:%d resp_content_len:%d",http_ctrl->resp_buff_len,http_ctrl->resp_content_len);
  305. if (http_ctrl->resp_buff_len == http_ctrl->resp_content_len) {
  306. luat_msgbus_put(&msg, 0);
  307. return 0;
  308. }
  309. }
  310. // 其他情况就是继续等数据, 后续还得判断timeout
  311. return 0;
  312. }
  313. static uint32_t http_send(luat_http_ctrl_t *http_ctrl, uint8_t* data, size_t len) {
  314. if (len == 0)
  315. return 0;
  316. uint32_t tx_len = 0;
  317. // LLOGD("http_send data:%.*s",len,data);
  318. network_tx(http_ctrl->netc, data, len, 0, http_ctrl->ip_addr.is_ipv6?NULL:&(http_ctrl->ip_addr), NULL, &tx_len, 0);
  319. return tx_len;
  320. }
  321. static int32_t luat_lib_http_callback(void *data, void *param){
  322. OS_EVENT *event = (OS_EVENT *)data;
  323. luat_http_ctrl_t *http_ctrl =(luat_http_ctrl_t *)param;
  324. int ret = 0;
  325. // 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);
  326. // LLOGD("luat_lib_http_callback %d %d",event->ID & 0x0fffffff,event->Param1);
  327. if (event->ID == EV_NW_RESULT_LINK){
  328. 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){
  329. // network_close(http_ctrl->netc, 0);
  330. http_resp_error(http_ctrl, -5);
  331. // http_close(http_ctrl);
  332. return -1;
  333. }
  334. }else if(event->ID == EV_NW_RESULT_CONNECT){
  335. //memset(http_ctrl->request_message, 0, HTTP_REQUEST_BUF_LEN_MAX);
  336. uint32_t tx_len = 0;
  337. // 发送请求行
  338. snprintf_(http_ctrl->request_message, HTTP_REQUEST_BUF_LEN_MAX, "%s %s HTTP/1.0\r\n", http_ctrl->method, http_ctrl->uri);
  339. http_send(http_ctrl, http_ctrl->request_message, strlen(http_ctrl->request_message));
  340. // 强制添加host. TODO 判断自定义headers是否有host
  341. snprintf_(http_ctrl->request_message, HTTP_REQUEST_BUF_LEN_MAX, "Host: %s\r\n", http_ctrl->host);
  342. http_send(http_ctrl, http_ctrl->request_message, strlen(http_ctrl->request_message));
  343. // 发送自定义头部
  344. if (http_ctrl->req_header){
  345. http_send(http_ctrl, http_ctrl->req_header, strlen(http_ctrl->req_header));
  346. }
  347. // 结束头部
  348. http_send(http_ctrl, "\r\n", 2);
  349. // 发送body
  350. if (http_ctrl->req_body){
  351. http_send(http_ctrl, http_ctrl->req_body, http_ctrl->req_body_len);
  352. }
  353. //--------------------------------------------
  354. // 清理资源
  355. if (http_ctrl->host){
  356. luat_heap_free(http_ctrl->host);
  357. http_ctrl->host = NULL;
  358. }
  359. if (http_ctrl->url){
  360. luat_heap_free(http_ctrl->url);
  361. http_ctrl->url = NULL;
  362. }
  363. if (http_ctrl->uri){
  364. luat_heap_free(http_ctrl->uri);
  365. http_ctrl->uri = NULL;
  366. }
  367. if (http_ctrl->method){
  368. luat_heap_free(http_ctrl->method);
  369. http_ctrl->method = NULL;
  370. }
  371. if (http_ctrl->req_header){
  372. luat_heap_free(http_ctrl->req_header);
  373. http_ctrl->req_header = NULL;
  374. }
  375. if (http_ctrl->req_body){
  376. luat_heap_free(http_ctrl->req_body);
  377. http_ctrl->req_body = NULL;
  378. http_ctrl->req_body_len = 0;
  379. }
  380. //------------------------------
  381. }else if(event->ID == EV_NW_RESULT_EVENT){
  382. if (event->Param1==0){
  383. uint32_t total_len = 0;
  384. uint32_t rx_len = 0;
  385. int result = network_rx(http_ctrl->netc, NULL, 0, 0, NULL, NULL, &total_len);
  386. // LLOGD("result:%d total_len:%d",result,total_len);
  387. if (0 == result){
  388. if (total_len>0){
  389. if (0 == http_ctrl->resp_buff_len){
  390. http_ctrl->resp_buff = luat_heap_malloc(total_len + 1);
  391. http_ctrl->resp_buff[total_len] = 0x00;
  392. }else{
  393. http_ctrl->resp_buff = luat_heap_realloc(http_ctrl->resp_buff,http_ctrl->resp_buff_len+total_len+1);
  394. http_ctrl->resp_buff[http_ctrl->resp_buff_len+total_len] = 0x00;
  395. }
  396. next:
  397. result = network_rx(http_ctrl->netc, http_ctrl->resp_buff+(http_ctrl->resp_buff_len), total_len, 0, NULL, NULL, &rx_len);
  398. // LLOGD("result:%d rx_len:%d",result,rx_len);
  399. if (result)
  400. goto next;
  401. if (rx_len == 0||result!=0) {
  402. http_resp_error(http_ctrl, -3);
  403. // http_close(http_ctrl);
  404. return -1;
  405. }
  406. http_ctrl->resp_buff_len += total_len;
  407. // LLOGD("http_ctrl->resp_buff:%.*s len:%d",http_ctrl->resp_buff_len,http_ctrl->resp_buff,http_ctrl->resp_buff_len);
  408. http_read_packet(http_ctrl);
  409. }
  410. }else{
  411. http_resp_error(http_ctrl, -3);
  412. // http_close(http_ctrl);
  413. return -1;
  414. }
  415. }
  416. }else if(event->ID == EV_NW_RESULT_TX){
  417. }else if(event->ID == EV_NW_RESULT_CLOSE){
  418. }
  419. if (event->Param1){
  420. LLOGD("luat_lib_http_callback http_ctrl close %d %d",event->ID & 0x0fffffff,event->Param1);
  421. http_resp_error(http_ctrl, -1);
  422. http_close(http_ctrl);
  423. return -1;
  424. }
  425. network_wait_event(http_ctrl->netc, NULL, 0, NULL);
  426. return 0;
  427. }
  428. static int http_add_header(luat_http_ctrl_t *http_ctrl, const char* name, const char* value){
  429. // LLOGD("http_add_header name:%s value:%s",name,value);
  430. // TODO 对value还需要进行urlencode
  431. if (http_ctrl->req_header){
  432. http_ctrl->req_header = luat_heap_realloc(http_ctrl->req_header, strlen(http_ctrl->req_header)+strlen(name)+strlen(value)+1);
  433. strncat(http_ctrl->req_header, name, strlen(name));
  434. strncat(http_ctrl->req_header, ":", 1);
  435. strncat(http_ctrl->req_header, value, strlen(value));
  436. strncat(http_ctrl->req_header, "\r\n", 2);
  437. }else{
  438. http_ctrl->req_header = luat_heap_malloc(strlen(name)+strlen(value)+1);
  439. memset(http_ctrl->req_header, 0, strlen(name)+strlen(value)+1);
  440. sprintf(http_ctrl->req_header, "%s:%s\r\n", name,value);
  441. }
  442. // LLOGD("http_ctrl->req_header:%s",http_ctrl->req_header);
  443. }
  444. static int http_set_url(luat_http_ctrl_t *http_ctrl) {
  445. char *tmp = http_ctrl->url;
  446. if (!strncmp("https://", http_ctrl->url, strlen("https://"))) {
  447. http_ctrl->is_tls = 1;
  448. tmp += strlen("https://");
  449. }
  450. else if (!strncmp("http://", http_ctrl->url, strlen("http://"))) {
  451. http_ctrl->is_tls = 0;
  452. tmp += strlen("http://");
  453. }
  454. else {
  455. LLOGI("only http/https supported %s", http_ctrl->url);
  456. return -1;
  457. }
  458. int tmplen = strlen(tmp);
  459. if (tmplen < 5) {
  460. LLOGI("url too short %s", http_ctrl->url);
  461. return -1;
  462. }
  463. char tmphost[256] = {0};
  464. char *tmpuri = NULL;
  465. for (size_t i = 0; i < tmplen; i++){
  466. if (tmp[i] == '/') {
  467. if (i > 255) {
  468. LLOGI("host too long %s", http_ctrl->url);
  469. return -1;
  470. }
  471. tmpuri = tmp + i;
  472. break;
  473. }else if(i == tmplen-1){
  474. tmphost[i+2] = '/';
  475. tmpuri = tmp + i+1;
  476. }
  477. tmphost[i] = tmp[i];
  478. }
  479. if (strlen(tmphost) < 1) {
  480. LLOGI("host not found %s", http_ctrl->url);
  481. return -1;
  482. }
  483. if (strlen(tmpuri) == 0) {
  484. tmpuri = "/";
  485. }
  486. // LLOGD("tmphost:%s",tmphost);
  487. // LLOGD("tmpuri:%s",tmpuri);
  488. for (size_t i = 1; i < strlen(tmphost); i++){
  489. if (tmp[i] == ":") {
  490. tmp[i] = 0x00;
  491. http_ctrl->remote_port = atoi(&tmp[i+1]);
  492. break;
  493. }
  494. }
  495. if (http_ctrl->remote_port <= 0) {
  496. if (http_ctrl->is_tls)
  497. http_ctrl->remote_port = 443;
  498. else
  499. http_ctrl->remote_port = 80;
  500. }
  501. http_ctrl->host = luat_heap_malloc(strlen(tmphost) + 1);
  502. if (http_ctrl->host == NULL) {
  503. LLOGE("out of memory when malloc host");
  504. return -1;
  505. }
  506. memcpy(http_ctrl->host, tmphost, strlen(tmphost) + 1);
  507. http_ctrl->uri = luat_heap_malloc(strlen(tmpuri) + 1);
  508. if (http_ctrl->uri == NULL) {
  509. LLOGE("out of memory when malloc url");
  510. return -1;
  511. }
  512. memcpy(http_ctrl->uri, tmpuri, strlen(tmpuri) + 1);
  513. // LLOGD("http_ctrl->uri:%s",http_ctrl->uri);
  514. // LLOGD("http_ctrl->host:%s",http_ctrl->host);
  515. // LLOGD("http_ctrl->port:%d",http_ctrl->remote_port);
  516. return 0;
  517. }
  518. /*
  519. http2客户端
  520. @api http2.request(method,url,headers,body,opts,ca_file)
  521. @string 请求方法, 支持 GET/POST
  522. @string url地址
  523. @tabal 请求头 可选 例如{["Content-Type"] = "application/x-www-form-urlencoded"}
  524. @string body 可选
  525. @tabal 额外配置 可选 包含dst:下载路径,可选 adapter:选择使用网卡,可选
  526. @string 证书 可选
  527. @return int code
  528. @return tabal headers
  529. @return string body
  530. @usage
  531. local code, headers, body = http2.request("GET","http://site0.cn/api/httptest/simple/time").wait()
  532. log.info("http2.get", code, headers, body)
  533. */
  534. static int l_http_request(lua_State *L) {
  535. size_t client_cert_len, client_key_len, client_password_len,len;
  536. const char *client_cert = NULL;
  537. const char *client_key = NULL;
  538. const char *client_password = NULL;
  539. int adapter_index;
  540. char body_len[6] = {0};
  541. // mbedtls_debug_set_threshold(4);
  542. luat_http_ctrl_t *http_ctrl = (luat_http_ctrl_t *)luat_heap_malloc(sizeof(luat_http_ctrl_t));
  543. if (!http_ctrl){
  544. goto error;
  545. }
  546. memset(http_ctrl, 0, sizeof(luat_http_ctrl_t));
  547. if (lua_istable(L, 5)){
  548. lua_pushstring(L, "adapter");
  549. if (LUA_TNUMBER == lua_gettable(L, 5)) {
  550. adapter_index = luaL_optinteger(L, -1, network_get_last_register_adapter());
  551. }else{
  552. adapter_index = network_get_last_register_adapter();
  553. }
  554. lua_pop(L, 1);
  555. lua_pushstring(L, "timeout");
  556. if (LUA_TNUMBER == lua_gettable(L, 5)) {
  557. http_ctrl->timeout = luaL_optinteger(L, -1, 0);
  558. }
  559. lua_pop(L, 1);
  560. lua_pushstring(L, "dst");
  561. if (LUA_TSTRING == lua_gettable(L, 5)) {
  562. const char *dst = luaL_checklstring(L, -1, &len);
  563. http_ctrl->dst = luat_heap_malloc(len + 1);
  564. memset(http_ctrl->dst, 0, len + 1);
  565. memcpy(http_ctrl->dst, dst, len);
  566. http_ctrl->is_download = 1;
  567. }
  568. lua_pop(L, 1);
  569. }else{
  570. adapter_index = network_get_last_register_adapter();
  571. }
  572. if (adapter_index < 0 || adapter_index >= NW_ADAPTER_QTY){
  573. goto error;
  574. }
  575. http_ctrl->netc = network_alloc_ctrl(adapter_index);
  576. if (!http_ctrl->netc){
  577. LLOGE("create fail");
  578. goto error;
  579. }
  580. network_init_ctrl(http_ctrl->netc, NULL, luat_lib_http_callback, http_ctrl);
  581. http_ctrl->netc->is_debug = 1;
  582. network_set_base_mode(http_ctrl->netc, 1, 10000, 0, 0, 0, 0);
  583. network_set_local_port(http_ctrl->netc, 0);
  584. const char *method = luaL_optlstring(L, 1, "GET", &len);
  585. http_ctrl->method = luat_heap_malloc(len + 1);
  586. memset(http_ctrl->method, 0, len + 1);
  587. memcpy(http_ctrl->method, method, len);
  588. // LLOGD("method:%s",http_ctrl->method);
  589. const char *url = luaL_checklstring(L, 2, &len);
  590. http_ctrl->url = luat_heap_malloc(len + 1);
  591. memset(http_ctrl->url, 0, len + 1);
  592. memcpy(http_ctrl->url, url, len);
  593. // LLOGD("http_ctrl->url:%s",http_ctrl->url);
  594. if (lua_istable(L, 3)) {
  595. lua_pushnil(L);
  596. while (lua_next(L, 3) != 0) {
  597. const char *name = lua_tostring(L, -2);
  598. const char *value = lua_tostring(L, -1);
  599. http_add_header(http_ctrl,name,value);
  600. lua_pop(L, 1);
  601. }
  602. }
  603. if (lua_isstring(L, 4)) {
  604. const char *body = luaL_checklstring(L, 4, &(http_ctrl->req_body_len));
  605. http_ctrl->req_body = luat_heap_malloc((http_ctrl->req_body_len) + 1);
  606. memset(http_ctrl->req_body, 0, (http_ctrl->req_body_len) + 1);
  607. memcpy(http_ctrl->req_body, body, (http_ctrl->req_body_len));
  608. sprintf(body_len, "%d",(http_ctrl->req_body_len));
  609. http_add_header(http_ctrl,"Content-Length",body_len);
  610. }
  611. // else{
  612. // http_add_header(http_ctrl,"Content-Length","0");
  613. // }
  614. int ret = http_set_url(http_ctrl);
  615. if (ret){
  616. goto error;
  617. }
  618. if (http_ctrl->is_tls){
  619. if (lua_isstring(L, 6)){
  620. client_cert = luaL_checklstring(L, 6, &client_cert_len);
  621. }
  622. if (lua_isstring(L, 7)){
  623. client_key = luaL_checklstring(L, 7, &client_key_len);
  624. }
  625. if (lua_isstring(L, 8)){
  626. client_password = luaL_checklstring(L, 8, &client_password_len);
  627. }
  628. network_init_tls(http_ctrl->netc, client_cert?2:0);
  629. if (client_cert){
  630. network_set_client_cert(http_ctrl->netc, client_cert, client_cert_len,
  631. client_key, client_key_len,
  632. client_password, client_password_len);
  633. }
  634. }else{
  635. network_deinit_tls(http_ctrl->netc);
  636. }
  637. if (!strncmp("GET", http_ctrl->method, strlen("GET"))) {
  638. LLOGI("HTTP GET");
  639. }
  640. else if (!strncmp("POST", http_ctrl->method, strlen("POST"))) {
  641. LLOGI("HTTP POST");
  642. }else {
  643. LLOGI("only GET/POST supported %s", http_ctrl->method);
  644. goto error;
  645. }
  646. http_ctrl->ip_addr.is_ipv6 = 0xff;
  647. network_wait_link_up(http_ctrl->netc, 0);
  648. if (ret == 0){
  649. 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){
  650. // network_close(http_ctrl->netc, 0);
  651. goto error;
  652. }
  653. }
  654. http_ctrl->idp = luat_pushcwait(L);
  655. return 1;
  656. error:
  657. http_resp_error(http_ctrl, -5);
  658. // http_close(http_ctrl);
  659. return 0;
  660. }
  661. #include "rotable2.h"
  662. static const rotable_Reg_t reg_http[] =
  663. {
  664. {"request", ROREG_FUNC(l_http_request)},
  665. { NULL, ROREG_INT(0)}
  666. };
  667. LUAMOD_API int luaopen_http( lua_State *L ) {
  668. luat_newlib2(L, reg_http);
  669. return 1;
  670. }
  671. #else
  672. #define LUAT_LOG_TAG "http2"
  673. #include "luat_log.h"
  674. #include "rotable2.h"
  675. static const rotable_Reg_t reg_http[] =
  676. {
  677. { NULL, ROREG_INT(0)}
  678. };
  679. LUAMOD_API int luaopen_http( lua_State *L ) {
  680. luat_newlib2(L, reg_http);
  681. LLOGE("reg_http2 require network enable!!");
  682. return 1;
  683. }
  684. #endif