luat_lib_http.c 22 KB

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