luat_lib_http.c 12 KB

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