luat_lib_http.c 24 KB

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