luat_lib_http.c 24 KB

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