luat_http_client.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440
  1. #include "luat_base.h"
  2. #include "luat_rtos.h"
  3. // #include "luat_msgbus.h"
  4. #include "luat_debug.h"
  5. #include "luat_mem.h"
  6. #include "http_parser.h"
  7. #include "luat_fota.h"
  8. #include "luat_spi.h"
  9. #include "luat_timer.h"
  10. #include "luat_str.h"
  11. #include "luat_fs.h"
  12. #include "luat_network_adapter.h"
  13. #include "luat_http.h"
  14. #define LUAT_LOG_TAG "http"
  15. #include "luat_log.h"
  16. extern void DBG_Printf(const char* format, ...);
  17. extern void luat_http_client_onevent(luat_http_ctrl_t *http_ctrl, int error_code, int arg);
  18. #undef LLOGD
  19. #ifdef __LUATOS__
  20. #define LLOGD(format, ...) do {if (http_ctrl->debug_onoff) {luat_log_log(LUAT_LOG_DEBUG, LUAT_LOG_TAG, format, ##__VA_ARGS__);}} while(0)
  21. #else
  22. #undef LLOGE
  23. #undef LLOGI
  24. #undef LLOGW
  25. #define LLOGI(format, ...)
  26. #define LLOGW(format, ...)
  27. #ifdef LUAT_LOG_NO_NEWLINE
  28. #define LLOGD(x,...) do {if (http_ctrl->debug_onoff) {DBG_Printf("%s %d:"x, __FUNCTION__,__LINE__,##__VA_ARGS__);}} while(0)
  29. #define LLOGE(x,...) DBG_Printf("%s %d:"x, __FUNCTION__,__LINE__,##__VA_ARGS__)
  30. #else
  31. #define LLOGD(x,...) do {if (http_ctrl->debug_onoff) {DBG_Printf("%s %d:"x"\r\n", __FUNCTION__,__LINE__,##__VA_ARGS__);}} while(0)
  32. #define LLOGE(x,...) DBG_Printf("%s %d:"x"\r\n", __FUNCTION__,__LINE__,##__VA_ARGS__)
  33. #endif
  34. #endif
  35. #ifdef LUAT_USE_NETDRV
  36. #include "luat_netdrv.h"
  37. #include "luat_netdrv_event.h"
  38. #endif
  39. static void http_send_message(luat_http_ctrl_t *http_ctrl);
  40. static int32_t luat_lib_http_callback(void *data, void *param);
  41. static void on_tcp_closed(luat_http_ctrl_t *http_ctrl);
  42. int strncasecmp(const char *string1, const char *string2, size_t count);
  43. static void http_close_nw(luat_http_ctrl_t *http_ctrl) {
  44. LLOGD("http close nw %p", http_ctrl);
  45. if (http_ctrl->netc){
  46. network_close(http_ctrl->netc, 0);
  47. }
  48. if (http_ctrl->timeout_timer){
  49. luat_stop_rtos_timer(http_ctrl->timeout_timer);
  50. }
  51. }
  52. int http_close(luat_http_ctrl_t *http_ctrl){
  53. LLOGI("http close %p", http_ctrl);
  54. if (http_ctrl->netc){
  55. network_close(http_ctrl->netc, 0);
  56. network_force_close_socket(http_ctrl->netc);
  57. network_release_ctrl(http_ctrl->netc);
  58. http_ctrl->netc = NULL;
  59. }
  60. if (http_ctrl->timeout_timer){
  61. luat_stop_rtos_timer(http_ctrl->timeout_timer);
  62. luat_release_rtos_timer(http_ctrl->timeout_timer);
  63. http_ctrl->timeout_timer = NULL;
  64. }
  65. if (http_ctrl->host){
  66. luat_heap_free(http_ctrl->host);
  67. http_ctrl->host = NULL;
  68. }
  69. if (http_ctrl->request_line){
  70. luat_heap_free(http_ctrl->request_line);
  71. http_ctrl->request_line = NULL;
  72. }
  73. if (http_ctrl->req_header){
  74. luat_heap_free(http_ctrl->req_header);
  75. http_ctrl->req_header = NULL;
  76. }
  77. if (http_ctrl->req_body){
  78. luat_heap_free(http_ctrl->req_body);
  79. http_ctrl->req_body = NULL;
  80. }
  81. if (http_ctrl->luatos_mode) {
  82. if (http_ctrl->dst){
  83. luat_heap_free(http_ctrl->dst);
  84. http_ctrl->dst = NULL;
  85. }
  86. if (http_ctrl->headers){
  87. luat_heap_free(http_ctrl->headers);
  88. http_ctrl->headers = NULL;
  89. }
  90. if (http_ctrl->body){
  91. luat_heap_free(http_ctrl->body);
  92. http_ctrl->body = NULL;
  93. }
  94. }
  95. if (http_ctrl->req_auth) {
  96. luat_heap_free(http_ctrl->req_auth);
  97. http_ctrl->req_auth = NULL;
  98. }
  99. memset(http_ctrl, 0, sizeof(luat_http_ctrl_t));
  100. luat_heap_free(http_ctrl);
  101. return 0;
  102. }
  103. #ifndef LUAT_COMPILER_NOWEAK
  104. LUAT_WEAK void luat_http_client_onevent(luat_http_ctrl_t *http_ctrl, int error_code, int arg){
  105. if (error_code == HTTP_OK){
  106. luat_http_cb http_cb = http_ctrl->http_cb;
  107. if (http_cb) {
  108. http_cb(HTTP_STATE_GET_BODY, NULL, 0, http_ctrl->http_cb_userdata); // 为了兼容老代码
  109. http_cb(HTTP_STATE_GET_BODY_DONE, (void *)((uint32_t)http_ctrl->parser.status_code), 0, http_ctrl->http_cb_userdata);
  110. }
  111. http_ctrl->error_code = 0;
  112. http_ctrl->state = HTTP_STATE_DONE;
  113. luat_rtos_timer_stop(http_ctrl->timeout_timer);
  114. }
  115. }
  116. #endif
  117. static void http_network_error(luat_http_ctrl_t *http_ctrl)
  118. {
  119. if (!http_ctrl->netc)
  120. {
  121. LLOGE("http is free!!!");
  122. return;
  123. }
  124. luat_http_cb http_cb = http_ctrl->http_cb;
  125. if (++(http_ctrl->re_request_count))
  126. {
  127. if (http_ctrl->re_request_count >= http_ctrl->retry_cnt_max)
  128. {
  129. if (http_ctrl->error_code > 0)
  130. {
  131. http_ctrl->error_code = HTTP_ERROR_STATE;
  132. }
  133. http_cb(http_ctrl->error_code, NULL, 0, http_ctrl->http_cb_userdata);
  134. return;
  135. }
  136. }
  137. LLOGD("retry %d", http_ctrl->re_request_count);
  138. http_ctrl->state = HTTP_STATE_CONNECT;
  139. if (http_ctrl->timeout)
  140. {
  141. luat_start_rtos_timer(http_ctrl->timeout_timer, http_ctrl->timeout, 1);
  142. }
  143. else
  144. {
  145. luat_stop_rtos_timer(http_ctrl->timeout_timer);
  146. }
  147. if (network_connect(http_ctrl->netc, http_ctrl->host, strlen(http_ctrl->host), NULL, http_ctrl->remote_port, 0) < 0)
  148. {
  149. LLOGW("can't connect! %s:%d", http_ctrl->host, http_ctrl->remote_port);
  150. http_ctrl->state = HTTP_STATE_IDLE;
  151. http_ctrl->error_code = HTTP_ERROR_CONNECT;
  152. network_close(http_ctrl->netc, 0);
  153. if (http_cb)
  154. {
  155. http_cb(http_ctrl->error_code, NULL, 0, http_ctrl->http_cb_userdata);
  156. }
  157. else
  158. {
  159. luat_debug_assert(__FUNCTION__, __LINE__, "http no cb");
  160. }
  161. }
  162. }
  163. static void http_network_close(luat_http_ctrl_t *http_ctrl)
  164. {
  165. http_ctrl->state = HTTP_STATE_WAIT_CLOSE;
  166. luat_rtos_timer_stop(http_ctrl->timeout_timer);
  167. if (!network_close(http_ctrl->netc, 0))
  168. {
  169. http_network_error(http_ctrl);
  170. }
  171. }
  172. static void http_resp_error(luat_http_ctrl_t *http_ctrl, int error_code) {
  173. LLOGD("report error(1) %d tcp_closed %d nw state %d",error_code, http_ctrl->tcp_closed, http_ctrl->netc->state);
  174. if (http_ctrl->fd) {
  175. LLOGW("http_resp_error: closing open fd due to error %d", error_code);
  176. luat_fs_fclose(http_ctrl->fd);
  177. http_ctrl->fd = NULL;
  178. if (http_ctrl->is_download && error_code != HTTP_OK && http_ctrl->dst) {
  179. luat_fs_remove(http_ctrl->dst);
  180. }
  181. }
  182. if (0 == http_ctrl->tcp_closed && NW_STATE_DISCONNECTING == http_ctrl->netc->state) {
  183. on_tcp_closed(http_ctrl);
  184. return;
  185. }
  186. LLOGD("report error(2) %d tcp_closed %d nw state %d",error_code, http_ctrl->tcp_closed, http_ctrl->netc->state);
  187. if (0 == http_ctrl->tcp_closed && NW_STATE_ONLINE == http_ctrl->netc->state) {
  188. if (0 == http_ctrl->error_code) {
  189. http_ctrl->error_code = error_code;
  190. }
  191. http_close_nw(http_ctrl);
  192. return;
  193. }
  194. if (http_ctrl->error_code) {
  195. error_code = http_ctrl->error_code;
  196. }
  197. #ifdef LUAT_USE_FOTA
  198. if (http_ctrl->isfota && error_code == HTTP_ERROR_FOTA){
  199. luat_fota_end(0);
  200. }
  201. #endif
  202. LLOGD("http_resp_error headers_complete:%d re_request_count:%d",http_ctrl->headers_complete,http_ctrl->re_request_count);
  203. if (http_ctrl->tcp_closed == 0 && http_ctrl->headers_complete==1 && http_ctrl->re_request_count < http_ctrl->retry_cnt_max){
  204. #ifdef LUAT_USE_NETDRV
  205. luat_netdrv_fire_socket_event_netctrl(EV_NW_TIMEOUT, http_ctrl->netc, 3);
  206. #endif
  207. http_ctrl->re_request_count++;
  208. network_close(http_ctrl->netc, 0);
  209. network_force_close_socket(http_ctrl->netc);
  210. if(network_connect(http_ctrl->netc, http_ctrl->host, strlen(http_ctrl->host), NULL, http_ctrl->remote_port, 0) < 0){
  211. LLOGE("http_resp_error network_connect error");
  212. goto error;
  213. }
  214. return;
  215. }
  216. error:
  217. #ifdef LUAT_USE_NETDRV
  218. luat_netdrv_fire_socket_event_netctrl(EV_NW_SOCKET_ERROR, http_ctrl->netc, 3);
  219. #endif
  220. luat_http_client_onevent(http_ctrl, error_code, 0);
  221. }
  222. // body接收回调
  223. static void luat_http_callback(luat_http_ctrl_t *http_ctrl){
  224. if (http_ctrl->http_cb && http_ctrl->luatos_mode){
  225. luat_http_client_onevent(http_ctrl, HTTP_CALLBACK, http_ctrl->body_len);
  226. LLOGD("luat_http_callback content_length:%ld body_len:%ld",http_ctrl->resp_content_len, http_ctrl->body_len);
  227. }
  228. }
  229. static int on_header_field(http_parser* parser, const char *at, size_t length){
  230. luat_http_ctrl_t *http_ctrl =(luat_http_ctrl_t *)parser->data;
  231. // LLOGD("on_header_field:%.*s",length,at);
  232. if (http_ctrl->headers_complete){
  233. return 0;
  234. }
  235. if (http_ctrl->luatos_mode) {
  236. if(!strncasecmp(at, "Content-Length: ", 16) && http_ctrl->resp_content_len == 0){
  237. http_ctrl->resp_content_len = -1;
  238. }
  239. if (!http_ctrl->headers){
  240. http_ctrl->headers = luat_heap_malloc(length+2);
  241. }else{
  242. http_ctrl->headers = luat_heap_realloc(http_ctrl->headers,http_ctrl->headers_len+length+2);
  243. }
  244. memcpy(http_ctrl->headers+http_ctrl->headers_len,at,length);
  245. memcpy(http_ctrl->headers+http_ctrl->headers_len+length, ":", 1);
  246. http_ctrl->headers_len += length+1;
  247. } else {
  248. char temp[16] = {':'};
  249. http_ctrl->response_head_buffer.Pos = 0;
  250. OS_BufferWrite(&http_ctrl->response_head_buffer, (void*)at, length);
  251. OS_BufferWrite(&http_ctrl->response_head_buffer, temp, 1);
  252. }
  253. return 0;
  254. }
  255. static int on_header_value(http_parser* parser, const char *at, size_t length){
  256. char tmp[16] = {0};
  257. luat_http_ctrl_t *http_ctrl =(luat_http_ctrl_t *)parser->data;
  258. // LLOGD("on_header_value:%.*s",length,at);
  259. if (http_ctrl->headers_complete){
  260. if (!http_ctrl->luatos_mode) {
  261. LLOGD("state %d", http_ctrl->state);
  262. }
  263. return 0;
  264. }
  265. if (http_ctrl->luatos_mode) {
  266. if(http_ctrl->resp_content_len == -1){
  267. memcpy(tmp, at, length);
  268. http_ctrl->resp_content_len = atoi(tmp);
  269. LLOGD("resp_content_len:%d",http_ctrl->resp_content_len);
  270. }
  271. http_ctrl->headers = luat_heap_realloc(http_ctrl->headers,http_ctrl->headers_len+length+3);
  272. memcpy(http_ctrl->headers+http_ctrl->headers_len,at,length);
  273. memcpy(http_ctrl->headers+http_ctrl->headers_len+length, "\r\n", 2);
  274. http_ctrl->headers_len += length+2;
  275. } else {
  276. OS_BufferWrite(&http_ctrl->response_head_buffer, (void *)at, length);
  277. OS_BufferWrite(&http_ctrl->response_head_buffer, tmp, 1);
  278. luat_http_cb http_cb = http_ctrl->http_cb;
  279. http_cb(HTTP_STATE_GET_HEAD, http_ctrl->response_head_buffer.Data, http_ctrl->response_head_buffer.Pos, http_ctrl->http_cb_userdata);
  280. }
  281. return 0;
  282. }
  283. static int on_headers_complete(http_parser* parser){
  284. luat_http_ctrl_t *http_ctrl =(luat_http_ctrl_t *)parser->data;
  285. LLOGD("on_headers_complete");
  286. if (http_ctrl->headers_complete){
  287. if (!http_ctrl->luatos_mode) {
  288. LLOGD("state %d", http_ctrl->state);
  289. }
  290. return 0;
  291. }
  292. if (http_ctrl->luatos_mode) {
  293. http_ctrl->headers[http_ctrl->headers_len] = 0x00;
  294. if (http_ctrl->is_download){
  295. luat_fs_remove(http_ctrl->dst);
  296. http_ctrl->fd = luat_fs_fopen(http_ctrl->dst, "w+");
  297. if (http_ctrl->fd == NULL) {
  298. LLOGE("open download file fail %s", http_ctrl->dst);
  299. }
  300. }
  301. #ifdef LUAT_USE_FOTA
  302. else if(http_ctrl->isfota){
  303. if (parser->status_code != 200 && parser->status_code != 206){
  304. LLOGE("fota http code error %d", parser->status_code);
  305. http_resp_error(http_ctrl, HTTP_ERROR_FOTA);
  306. return -1;
  307. }
  308. luat_fota_init(http_ctrl->address, http_ctrl->length, http_ctrl->spi_device, NULL, 0);
  309. }
  310. #endif
  311. http_ctrl->headers_complete = 1;
  312. // 如果头部解析出content_length为0,直接标记body接收完成
  313. if (http_ctrl->resp_content_len == 0){
  314. http_ctrl->http_body_is_finally = 1;
  315. }
  316. luat_http_callback(http_ctrl);
  317. } else {
  318. if (http_ctrl->state != HTTP_STATE_GET_HEAD){
  319. LLOGE("http state error %d", http_ctrl->state);
  320. return 0;
  321. }
  322. if (!http_ctrl->context_len_vaild)
  323. {
  324. if (http_ctrl->parser.content_length != -1)
  325. {
  326. http_ctrl->context_len = http_ctrl->parser.content_length;
  327. http_ctrl->context_len_vaild = 1;
  328. }
  329. else
  330. {
  331. LLOGD("no content length, maybe chuck!");
  332. }
  333. }
  334. luat_http_cb http_cb = http_ctrl->http_cb;
  335. http_cb(HTTP_STATE_GET_HEAD, NULL, 0, http_ctrl->http_cb_userdata); // 为了兼容老代码
  336. http_cb(HTTP_STATE_GET_HEAD_DONE, (void *)((uint32_t)parser->status_code), 0, http_ctrl->http_cb_userdata);
  337. http_ctrl->state = HTTP_STATE_GET_BODY;
  338. }
  339. return 0;
  340. }
  341. static int on_body(http_parser* parser, const char *at, size_t length){
  342. luat_http_ctrl_t *http_ctrl =(luat_http_ctrl_t *)parser->data;
  343. if (length > 128) {
  344. LLOGD("on_body first 128byte:%.*s", 128, at);
  345. } else {
  346. LLOGD("on_body:%.*s",length,at);
  347. }
  348. LLOGD("on_body length:%d http_ctrl->body_len:%d status_code:%d",length,http_ctrl->body_len+length,parser->status_code);
  349. if (http_ctrl->luatos_mode) {
  350. if (http_ctrl->is_download){
  351. if (http_ctrl->fd == NULL){
  352. luat_fs_remove(http_ctrl->dst);
  353. http_ctrl->fd = luat_fs_fopen(http_ctrl->dst, "w+");
  354. if (http_ctrl->fd == NULL) {
  355. LLOGE("open download file fail %s", http_ctrl->dst);
  356. http_resp_error(http_ctrl, HTTP_ERROR_DOWNLOAD);
  357. return -1;
  358. }
  359. }
  360. if (length != luat_fs_fwrite(at, length, 1, http_ctrl->fd)) {
  361. LLOGE("err when fwrite %s", http_ctrl->dst);
  362. http_resp_error(http_ctrl, HTTP_ERROR_DOWNLOAD);
  363. return -1;
  364. }
  365. }
  366. #ifdef LUAT_USE_FOTA
  367. else if(http_ctrl->isfota && (parser->status_code == 200 || parser->status_code == 206)){
  368. if (luat_fota_write((uint8_t*)at, length) < 0){
  369. http_resp_error(http_ctrl, HTTP_ERROR_FOTA);
  370. return -1;
  371. }
  372. }
  373. #endif
  374. else if(http_ctrl->is_post==0 && http_ctrl->zbuff_body!=NULL){
  375. if (http_ctrl->zbuff_body->len < http_ctrl->zbuff_body->used+length+1 ){
  376. void* tmpptr = luat_heap_realloc(http_ctrl->zbuff_body->addr,http_ctrl->zbuff_body->used+length+1);
  377. if (tmpptr == NULL) {
  378. LLOGE("out of memory when recv http body");
  379. http_resp_error(http_ctrl, HTTP_ERROR_DOWNLOAD);
  380. return -1;
  381. }
  382. http_ctrl->zbuff_body->addr = tmpptr;
  383. }
  384. memcpy(http_ctrl->zbuff_body->addr + http_ctrl->zbuff_body->used ,at,length);
  385. http_ctrl->zbuff_body->used += length;
  386. }
  387. else{
  388. if (!http_ctrl->body){
  389. http_ctrl->body = luat_heap_malloc(length+1);
  390. if (http_ctrl->body == NULL) {
  391. LLOGE("out of memory when recv http body");
  392. http_resp_error(http_ctrl, HTTP_ERROR_DOWNLOAD);
  393. return -1;
  394. }
  395. }else{
  396. void* tmpptr = luat_heap_realloc(http_ctrl->body,http_ctrl->body_len+length+1);
  397. if (tmpptr == NULL) {
  398. LLOGE("out of memory when recv http body");
  399. http_resp_error(http_ctrl, HTTP_ERROR_DOWNLOAD);
  400. return -1;
  401. }
  402. http_ctrl->body = tmpptr;
  403. }
  404. memcpy(http_ctrl->body+http_ctrl->body_len,at,length);
  405. }
  406. http_ctrl->body_len += length;
  407. luat_http_callback(http_ctrl);
  408. } else {
  409. if (http_ctrl->state != HTTP_STATE_GET_BODY){
  410. LLOGD("http state error %d", http_ctrl->state);
  411. return 0;
  412. }
  413. http_ctrl->body_len += length;
  414. luat_http_cb http_cb = http_ctrl->http_cb;
  415. if (at && length) {
  416. http_cb(HTTP_STATE_GET_BODY, (void *)at, length, http_ctrl->http_cb_userdata);
  417. }
  418. }
  419. if (http_ctrl->resp_content_len >= 0 && http_ctrl->body_len >= http_ctrl->resp_content_len) {
  420. http_ctrl->http_body_is_finally = 1;
  421. LLOGD("http body recv done by content_length");
  422. http_close_nw(http_ctrl);
  423. }
  424. else if (http_ctrl->http_body_is_finally) {
  425. LLOGD("http body recv done by chunked end");
  426. http_close_nw(http_ctrl);
  427. }
  428. return 0;
  429. }
  430. static int on_complete(http_parser* parser, luat_http_ctrl_t *http_ctrl){
  431. LLOGD("on_complete");
  432. // http_ctrl->body[http_ctrl->body_len] = 0x00;
  433. LLOGD("status_code:%d",parser->status_code);
  434. // LLOGD("content_length:%lld",parser->content_length);
  435. if (http_ctrl->luatos_mode) {
  436. if (http_ctrl->fd != NULL) {
  437. luat_fs_fclose(http_ctrl->fd);
  438. http_ctrl->fd = NULL;
  439. if (parser->status_code > 299 && http_ctrl->dst) {
  440. LLOGW("download fail, remove file %s", http_ctrl->dst);
  441. luat_fs_remove(http_ctrl->dst);
  442. }
  443. }
  444. #ifdef LUAT_USE_FOTA
  445. else if(http_ctrl->isfota){
  446. if (parser->status_code == 200 || parser->status_code == 206){
  447. parser->status_code = 200;
  448. int result = luat_fota_done();
  449. LLOGD("result1:%d",result);
  450. size_t wait_count = 0;
  451. while (result>0 && wait_count < 120){ // TODO 应该有超时机制
  452. luat_timer_mdelay(100);
  453. result = luat_fota_done();
  454. wait_count++;
  455. if (wait_count % 10 == 0){
  456. LLOGD("fota done wait %dms", wait_count*100);
  457. }
  458. }
  459. LLOGD("result2:%d",result);
  460. if (result==0){
  461. if (luat_fota_end(1)){
  462. LLOGE("fota finish error");
  463. http_resp_error(http_ctrl, HTTP_ERROR_FOTA);
  464. return -1;
  465. }
  466. }else{
  467. http_resp_error(http_ctrl, HTTP_ERROR_FOTA);
  468. return -1;
  469. }
  470. }else{
  471. http_resp_error(http_ctrl, HTTP_ERROR_FOTA);
  472. return -1;
  473. }
  474. }
  475. #endif
  476. }
  477. // network_close(http_ctrl->netc, 0);
  478. return 0;
  479. }
  480. static int on_message_complete(http_parser* parser){
  481. luat_http_ctrl_t *http_ctrl =(luat_http_ctrl_t *)parser->data;
  482. LLOGD("on_message_complete");
  483. return 0;
  484. }
  485. static int on_chunk_header(http_parser* parser){
  486. luat_http_ctrl_t *http_ctrl =(luat_http_ctrl_t *)parser->data;
  487. LLOGD("on_chunk_header content_length:%lld",parser->content_length);
  488. if (parser->content_length == 0){
  489. http_ctrl->http_body_is_finally = 1;
  490. http_close_nw(http_ctrl);
  491. }
  492. return 0;
  493. }
  494. static const http_parser_settings parser_settings = {
  495. .on_header_field = on_header_field,
  496. .on_header_value = on_header_value,
  497. .on_headers_complete = on_headers_complete,
  498. .on_body = on_body,
  499. .on_message_complete = on_message_complete,
  500. .on_chunk_header = on_chunk_header
  501. };
  502. int luat_http_client_init(luat_http_ctrl_t* http_ctrl, int use_ipv6) {
  503. network_init_ctrl(http_ctrl->netc, NULL, luat_lib_http_callback, http_ctrl);
  504. network_set_base_mode(http_ctrl->netc, 1, 10000, 0, 0, 0, 0);
  505. network_set_local_port(http_ctrl->netc, 0);
  506. if (use_ipv6) {
  507. LLOGI("enable ipv6 support for http request");
  508. network_connect_ipv6_domain(http_ctrl->netc, 1);
  509. }
  510. http_ctrl->luatos_mode = 1;
  511. return 0;
  512. }
  513. #define HTTP_SEND_LEN_MAX (4096)
  514. static uint32_t http_send(luat_http_ctrl_t *http_ctrl, void* data, size_t len) {
  515. if (len == 0)
  516. return 0;
  517. uint32_t tx_len = 0;
  518. // LLOGD("http_send data:%.*s",len,data);
  519. network_tx(http_ctrl->netc, (uint8_t *)data, len, 0, NULL, 0, &tx_len, 0);
  520. return tx_len;
  521. }
  522. static void http_send_message(luat_http_ctrl_t *http_ctrl){
  523. // 发送请求行, 主要,这里都借用了resp_buff,但这并不会与resp冲突
  524. int result;
  525. http_send(http_ctrl, (uint8_t *)http_ctrl->request_line, strlen((char*)http_ctrl->request_line));
  526. http_send(http_ctrl, (uint8_t*)"Connection: Close\r\n", strlen("Connection: Close\r\n"));
  527. // 判断自定义headers是否有host
  528. if (http_ctrl->custom_host == 0) {
  529. result = snprintf_(http_ctrl->resp_buff, HTTP_RESP_BUFF_SIZE, "Host: %s:%d\r\n", http_ctrl->host, http_ctrl->remote_port);
  530. http_send(http_ctrl, http_ctrl->resp_buff, result);
  531. }
  532. if (http_ctrl->luatos_mode) {
  533. if (http_ctrl->headers_complete){
  534. result = snprintf_(http_ctrl->resp_buff, HTTP_RESP_BUFF_SIZE, "Range: bytes=%lu-\r\n", http_ctrl->body_len);
  535. http_send(http_ctrl, http_ctrl->resp_buff, result);
  536. }
  537. if (http_ctrl->req_auth) {
  538. http_send(http_ctrl, (uint8_t*)http_ctrl->req_auth, strlen((char*)http_ctrl->req_auth));
  539. }
  540. // 发送自定义头部
  541. if (http_ctrl->req_header){
  542. http_send(http_ctrl, (uint8_t*)http_ctrl->req_header, strlen((char*)http_ctrl->req_header));
  543. }
  544. // 结束头部
  545. http_send(http_ctrl, (uint8_t*)"\r\n", 2);
  546. // 发送body
  547. if (http_ctrl->req_body){
  548. if (http_ctrl->req_body_len > HTTP_SEND_LEN_MAX){
  549. http_send(http_ctrl, (uint8_t*)http_ctrl->req_body, HTTP_SEND_LEN_MAX);
  550. http_ctrl->tx_offset = HTTP_SEND_LEN_MAX;
  551. }else{
  552. http_send(http_ctrl, (uint8_t*)http_ctrl->req_body, http_ctrl->req_body_len);
  553. http_ctrl->tx_offset = 0;
  554. }
  555. }
  556. else if(http_ctrl->is_post==1 && http_ctrl->zbuff_body!=NULL){
  557. if (http_ctrl->zbuff_body->used > HTTP_SEND_LEN_MAX){
  558. http_send(http_ctrl, http_ctrl->zbuff_body->addr, HTTP_SEND_LEN_MAX);
  559. http_ctrl->tx_offset = HTTP_SEND_LEN_MAX;
  560. }else{
  561. http_send(http_ctrl, http_ctrl->zbuff_body->addr, http_ctrl->zbuff_body->used);
  562. http_ctrl->tx_offset = 0;
  563. }
  564. }
  565. } else {
  566. const char line[] = "Accept: application/octet-stream\r\n";
  567. http_ctrl->state = HTTP_STATE_SEND_HEAD;
  568. if (http_ctrl->data_mode && (http_ctrl->offset || http_ctrl->body_len)){
  569. result = snprintf_(http_ctrl->resp_buff, 320, "Range: bytes=%lu-\r\n", (http_ctrl->offset + http_ctrl->body_len));
  570. LLOGD("get offset %u+%u", http_ctrl->offset, http_ctrl->body_len);
  571. http_send(http_ctrl, http_ctrl->resp_buff, result);
  572. }
  573. // 发送自定义头部
  574. if (http_ctrl->request_head_buffer.Data && http_ctrl->request_head_buffer.Pos){
  575. http_send(http_ctrl, http_ctrl->request_head_buffer.Data, http_ctrl->request_head_buffer.Pos);
  576. }
  577. if (http_ctrl->data_mode)
  578. {
  579. http_send(http_ctrl, (uint8_t *)line, sizeof(line) - 1);
  580. }
  581. // 结束头部
  582. http_send(http_ctrl, (uint8_t*)"\r\n", 2);
  583. // 发送body
  584. http_ctrl->state = HTTP_STATE_GET_HEAD;
  585. if (http_ctrl->is_post)
  586. {
  587. luat_http_cb http_cb = http_ctrl->http_cb;
  588. http_cb(HTTP_STATE_SEND_BODY_START, NULL, 0, http_ctrl->http_cb_userdata);
  589. }
  590. }
  591. }
  592. LUAT_RT_RET_TYPE luat_http_timer_callback(LUAT_RT_CB_PARAM){
  593. luat_http_ctrl_t * http_ctrl = (luat_http_ctrl_t *)param;
  594. if (http_ctrl->luatos_mode) {
  595. http_resp_error(http_ctrl, HTTP_ERROR_TIMEOUT);
  596. } else {
  597. if (http_ctrl->new_data)
  598. {
  599. http_ctrl->new_data = 0;
  600. }
  601. else
  602. {
  603. LLOGD("http timeout error!");
  604. http_ctrl->error_code = HTTP_ERROR_TIMEOUT;
  605. http_network_close(http_ctrl);
  606. }
  607. }
  608. }
  609. static void on_tcp_closed(luat_http_ctrl_t *http_ctrl) {
  610. LLOGD("on_tcp_closed %p body is done %d header is complete %d", http_ctrl, http_ctrl->http_body_is_finally, http_ctrl->headers_complete);
  611. int ret = 0;
  612. http_ctrl->tcp_closed = 1;
  613. if (http_ctrl->http_body_is_finally == 0) { // 当没有解析完成
  614. // 存在多种可能性
  615. // 1. 没有content_length的情况, 又没有chunked
  616. if (http_ctrl->resp_content_len >= 0 && http_ctrl->headers_complete) {
  617. http_ctrl->http_body_is_finally = 1;
  618. }
  619. }
  620. if (http_ctrl->http_body_is_finally) {
  621. ret = on_complete(&http_ctrl->parser, http_ctrl);
  622. if (ret) {
  623. return; // 结束,因为on_complete里面已经上报错误了
  624. }
  625. if (http_ctrl->error_code == -5) {
  626. http_ctrl->error_code = HTTP_OK;
  627. }
  628. }
  629. else {
  630. if (http_ctrl->error_code == 0) {
  631. http_ctrl->error_code = HTTP_ERROR_CLOSE;
  632. }
  633. }
  634. if (http_ctrl->luatos_mode) {
  635. http_resp_error(http_ctrl, HTTP_OK);
  636. } else {
  637. http_network_error(http_ctrl);
  638. }
  639. }
  640. int32_t luat_lib_http_callback(void *data, void *param){
  641. OS_EVENT *event = (OS_EVENT *)data;
  642. luat_http_ctrl_t *http_ctrl =(luat_http_ctrl_t *)param;
  643. int ret = 0;
  644. if (http_ctrl == NULL){
  645. LLOGE("http_ctrl is NULL");
  646. return -1;
  647. }
  648. if (!http_ctrl->luatos_mode) {
  649. if (HTTP_STATE_IDLE == http_ctrl->state)
  650. {
  651. LLOGE("http state error %d", http_ctrl->state);
  652. return 0;
  653. }
  654. }
  655. if (http_ctrl->tcp_closed){
  656. LLOGD("http already closed %p", http_ctrl);
  657. return 0;
  658. }
  659. //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);
  660. LLOGD("nw cb %08X %d %p", event->ID - EV_NW_RESULT_BASE, event->Param1, http_ctrl);
  661. if (event->Param1){
  662. //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);
  663. LLOGE("error event %08X %d host %s port %d",event->ID - EV_NW_RESULT_BASE, event->Param1, http_ctrl->netc->domain_name, http_ctrl->netc->remote_port);
  664. if (http_ctrl->tcp_closed == 0 && http_ctrl->error_code == 0) {
  665. http_ctrl->error_code = event->ID == EV_NW_RESULT_CONNECT ? HTTP_ERROR_CONNECT : HTTP_ERROR_CLOSE;
  666. }
  667. if (http_ctrl->tcp_closed == 0) {
  668. on_tcp_closed(http_ctrl);
  669. }
  670. return -1;
  671. }
  672. switch (event->ID)
  673. {
  674. case EV_NW_RESULT_EVENT:
  675. if (!http_ctrl->luatos_mode) {
  676. http_ctrl->new_data = 1;
  677. }
  678. if (http_ctrl->is_pause){
  679. LLOGD("rx pause");
  680. break;
  681. }
  682. uint32_t total_len = 0;
  683. uint32_t rx_len = 0;
  684. while (1) {
  685. int result = network_rx(http_ctrl->netc, NULL, 0, 0, NULL, NULL, &total_len);
  686. if (result) {
  687. if (http_ctrl->luatos_mode) {
  688. http_resp_error(http_ctrl, http_ctrl->http_body_is_finally ? HTTP_OK : HTTP_ERROR_RX);
  689. } else {
  690. http_ctrl->error_code = http_ctrl->http_body_is_finally ? HTTP_OK : HTTP_ERROR_RX;
  691. http_network_error(http_ctrl);
  692. }
  693. return -1;
  694. }
  695. if (0 == total_len)
  696. break;
  697. if (http_ctrl->resp_buff_offset + total_len > (HTTP_RESP_BUFF_SIZE - 1)) {
  698. total_len = HTTP_RESP_BUFF_SIZE - 1 - http_ctrl->resp_buff_offset;
  699. if (total_len < 1) {
  700. // 能到这里的就是片段太长了
  701. // 要么header太长, 要么chunked太长,拒绝吧
  702. if (http_ctrl->luatos_mode) {
  703. http_resp_error(http_ctrl, http_ctrl->http_body_is_finally ? HTTP_OK : HTTP_ERROR_RX);
  704. } else {
  705. http_ctrl->error_code = http_ctrl->http_body_is_finally ? HTTP_OK : HTTP_ERROR_RX;
  706. http_network_error(http_ctrl);
  707. }
  708. return -1;
  709. }
  710. }
  711. result = network_rx(http_ctrl->netc, (uint8_t*)http_ctrl->resp_buff+http_ctrl->resp_buff_offset, total_len, 0, NULL, NULL, &rx_len);
  712. LLOGD("result:%d rx_len:%d",result,rx_len);
  713. if (rx_len == 0||result!=0) {
  714. if (http_ctrl->luatos_mode) {
  715. http_resp_error(http_ctrl, http_ctrl->http_body_is_finally ? HTTP_OK : HTTP_ERROR_RX);
  716. } else {
  717. http_ctrl->error_code = http_ctrl->http_body_is_finally ? HTTP_OK : HTTP_ERROR_RX;
  718. http_network_error(http_ctrl);
  719. }
  720. return -1;
  721. }
  722. http_ctrl->resp_buff_offset += rx_len;
  723. //LLOGD("resp_buff_offset:%d resp_buff:%s",http_ctrl->resp_buff_offset,http_ctrl->resp_buff);
  724. // uint8_t *tmp = (uint8_t*)http_ctrl->resp_buff;
  725. //LLOGD("resp buff %.*s", http_ctrl->resp_buff_offset, http_ctrl->resp_buff);
  726. if (0 == http_ctrl->resp_headers_done) {
  727. LLOGD("search headers, buff len %d", http_ctrl->resp_buff_offset);
  728. if (http_ctrl->resp_buff_offset > 4) {
  729. uint8_t *tmp = (uint8_t*)http_ctrl->resp_buff;
  730. size_t search = http_ctrl->resp_buff_offset;
  731. for (size_t i = 0; i < search; i++)
  732. {
  733. // \\r\\n\\r\\n
  734. // \\n\\n
  735. // \\r\\r
  736. if ((0x0D == tmp[i] && 0x0A == tmp[i+1] && 0x0D == tmp[i+2] && 0x0A == tmp[i+3]) ||
  737. (0x0A == tmp[i] && 0x0A == tmp[i+1]) ||
  738. (0x0D == tmp[i] && 0x0D == tmp[i+1]) ){
  739. http_ctrl->resp_headers_done = 1;
  740. LLOGD("found headers end at %d", i);
  741. break;
  742. }
  743. }
  744. }
  745. }
  746. if (http_ctrl->resp_headers_done) {
  747. size_t nParseBytes = http_parser_execute(&http_ctrl->parser, &parser_settings, http_ctrl->resp_buff, http_ctrl->resp_buff_offset);
  748. LLOGD("nParseBytes %d resp_buff_offset %d", nParseBytes, http_ctrl->resp_buff_offset);
  749. if(http_ctrl->parser.http_errno) {
  750. LLOGW("http exit reason by errno: %d != HPE_OK!!!", http_ctrl->parser.http_errno);
  751. return 0;
  752. }
  753. if (http_ctrl->resp_buff_offset <= nParseBytes) {
  754. http_ctrl->resp_buff_offset = 0;
  755. }
  756. else {
  757. memmove(http_ctrl->resp_buff, http_ctrl->resp_buff + nParseBytes, http_ctrl->resp_buff_offset - nParseBytes);
  758. http_ctrl->resp_buff_offset -= nParseBytes;
  759. }
  760. }
  761. else {
  762. LLOGD("wait headers %.*s", http_ctrl->resp_buff_offset, http_ctrl->resp_buff);
  763. }
  764. if (http_ctrl->tcp_closed){
  765. return 0;
  766. }
  767. }
  768. break;
  769. case EV_NW_RESULT_TX:
  770. if (http_ctrl->luatos_mode) {
  771. if (http_ctrl->tx_offset){
  772. if (http_ctrl->req_body){
  773. if (http_ctrl->req_body_len-http_ctrl->tx_offset > HTTP_SEND_LEN_MAX){
  774. http_send(http_ctrl, (uint8_t*)http_ctrl->req_body+http_ctrl->tx_offset, HTTP_SEND_LEN_MAX);
  775. http_ctrl->tx_offset += HTTP_SEND_LEN_MAX;
  776. }else{
  777. http_send(http_ctrl, (uint8_t*)http_ctrl->req_body+http_ctrl->tx_offset, http_ctrl->req_body_len-http_ctrl->tx_offset);
  778. http_ctrl->tx_offset = 0;
  779. }
  780. }
  781. else if(http_ctrl->is_post==1 && http_ctrl->zbuff_body!=NULL){
  782. if (http_ctrl->zbuff_body->used-http_ctrl->tx_offset > HTTP_SEND_LEN_MAX){
  783. http_send(http_ctrl, http_ctrl->zbuff_body->addr+http_ctrl->tx_offset, HTTP_SEND_LEN_MAX);
  784. http_ctrl->tx_offset += HTTP_SEND_LEN_MAX;
  785. }else{
  786. http_send(http_ctrl, http_ctrl->zbuff_body->addr+http_ctrl->tx_offset, http_ctrl->zbuff_body->used-http_ctrl->tx_offset);
  787. http_ctrl->tx_offset = 0;
  788. }
  789. }
  790. }
  791. } else {
  792. if (http_ctrl->is_post){
  793. luat_http_cb http_cb = http_ctrl->http_cb;
  794. http_cb(HTTP_STATE_SEND_BODY, NULL, 0, http_ctrl->http_cb_userdata);
  795. }
  796. http_ctrl->state = HTTP_STATE_GET_HEAD;
  797. }
  798. return 0;
  799. case EV_NW_RESULT_CONNECT:
  800. http_ctrl->resp_buff_offset = 0; // 复位resp缓冲区
  801. http_ctrl->resp_headers_done = 0;
  802. http_parser_init(&http_ctrl->parser, HTTP_RESPONSE);
  803. http_ctrl->parser.data = http_ctrl;
  804. // TODO header 保持原始数据,在lua回调时才导出数据
  805. // if (http_ctrl->resp_headers) {
  806. // luat_heap_free(http_ctrl->resp_headers);
  807. // http_ctrl->resp_headers = NULL;
  808. // }
  809. http_send_message(http_ctrl);
  810. return 0;
  811. case EV_NW_RESULT_CLOSE:
  812. if (http_ctrl->luatos_mode) {
  813. if (http_ctrl->tcp_closed == 0) {
  814. on_tcp_closed(http_ctrl);
  815. }
  816. }
  817. else {
  818. if (http_ctrl->error_code && (http_ctrl->state != HTTP_STATE_DONE))
  819. {
  820. LLOGD("http network closed");
  821. http_network_error(http_ctrl);
  822. }
  823. else
  824. {
  825. http_ctrl->state = HTTP_STATE_IDLE;
  826. luat_http_cb http_cb = http_ctrl->http_cb;
  827. http_cb(http_ctrl->state, NULL, 0, http_ctrl->http_cb_userdata);
  828. }
  829. }
  830. return 0;
  831. case EV_NW_RESULT_LINK:
  832. return 0;
  833. default:
  834. break;
  835. }
  836. ret = network_wait_event(http_ctrl->netc, NULL, 0, NULL);
  837. if (ret < 0){
  838. LLOGE("network_wait_event %d", ret);
  839. if (http_ctrl->luatos_mode) {
  840. http_resp_error(http_ctrl, HTTP_ERROR_CLOSE);
  841. } else {
  842. http_ctrl->error_code = HTTP_ERROR_STATE;
  843. http_network_close(http_ctrl);
  844. }
  845. return -1;
  846. }
  847. return 0;
  848. }
  849. static void luat_http_dummy_cb(int status, void *data, uint32_t data_len, void *user_param) {;}
  850. luat_http_ctrl_t* luat_http_client_create(luat_http_cb cb, void *user_param, int adapter_index)
  851. {
  852. luat_http_ctrl_t *http_ctrl = luat_heap_malloc(sizeof(luat_http_ctrl_t));
  853. if (!http_ctrl) return NULL;
  854. memset(http_ctrl,0,sizeof(luat_http_ctrl_t));
  855. http_ctrl->timeout_timer = luat_create_rtos_timer(luat_http_timer_callback, http_ctrl, NULL);
  856. if (!http_ctrl->timeout_timer)
  857. {
  858. luat_heap_free(http_ctrl);
  859. LLOGE("no more timer");
  860. return NULL;
  861. }
  862. if (adapter_index >= 0)
  863. {
  864. http_ctrl->netc = network_alloc_ctrl(adapter_index);
  865. }
  866. else
  867. {
  868. http_ctrl->netc = network_alloc_ctrl(network_register_get_default());
  869. }
  870. if (!http_ctrl->netc)
  871. {
  872. luat_release_rtos_timer(http_ctrl->timeout_timer);
  873. luat_heap_free(http_ctrl);
  874. LLOGE("no more network ctrl");
  875. return NULL;
  876. }
  877. network_init_ctrl(http_ctrl->netc, NULL, luat_lib_http_callback, http_ctrl);
  878. network_set_base_mode(http_ctrl->netc, 1, 10000, 0, 0, 0, 0);
  879. network_set_local_port(http_ctrl->netc, 0);
  880. http_ctrl->http_cb = cb?cb:luat_http_dummy_cb;
  881. http_ctrl->http_cb_userdata = user_param;
  882. http_ctrl->timeout = 15000;
  883. http_ctrl->retry_cnt_max = 0;
  884. http_ctrl->state = HTTP_STATE_IDLE;
  885. http_ctrl->debug_onoff = 0;
  886. http_ctrl->netc->is_debug = 0;
  887. return http_ctrl;
  888. }
  889. int luat_http_client_base_config(luat_http_ctrl_t* http_ctrl, uint32_t timeout, uint8_t debug_onoff, uint8_t re_request_count)
  890. {
  891. if (!http_ctrl) return -ERROR_PARAM_INVALID;
  892. if (http_ctrl->state)
  893. {
  894. LLOGE("http running, please stop and set");
  895. return -ERROR_PERMISSION_DENIED;
  896. }
  897. http_ctrl->timeout = timeout;
  898. http_ctrl->debug_onoff = debug_onoff;
  899. http_ctrl->netc->is_debug = debug_onoff;
  900. http_ctrl->retry_cnt_max = re_request_count;
  901. // 如果retry_cnt_max大于0, 则警告将要废弃自动重试功能
  902. if (re_request_count > 0) {
  903. LLOGE("http client auto re-request is deprecated, please handle retry in your code!!!");
  904. }
  905. return 0;
  906. }
  907. int luat_http_client_ssl_config(luat_http_ctrl_t* http_ctrl, int mode, const char *server_cert, uint32_t server_cert_len,
  908. const char *client_cert, uint32_t client_cert_len,
  909. const char *client_cert_key, uint32_t client_cert_key_len,
  910. const char *client_cert_key_password, uint32_t client_cert_key_password_len)
  911. {
  912. if (!http_ctrl) return -ERROR_PARAM_INVALID;
  913. if (http_ctrl->state)
  914. {
  915. LLOGE("http running, please stop and set");
  916. return -ERROR_PERMISSION_DENIED;
  917. }
  918. if (mode < 0)
  919. {
  920. network_deinit_tls(http_ctrl->netc);
  921. return 0;
  922. }
  923. if (mode > 2)
  924. {
  925. return -ERROR_PARAM_INVALID;
  926. }
  927. int result;
  928. // network_init_tls(http_ctrl->netc, (server_cert || client_cert)?2:0);
  929. network_init_tls(http_ctrl->netc, 0);
  930. if (server_cert){
  931. result = network_set_server_cert(http_ctrl->netc, (const unsigned char *)server_cert, server_cert_len);
  932. if (result)
  933. {
  934. LLOGE("set server cert failed %d", result);
  935. return -ERROR_OPERATION_FAILED;
  936. }
  937. }
  938. if (client_cert){
  939. result = network_set_client_cert(http_ctrl->netc, (const unsigned char *)client_cert, client_cert_len,
  940. (const unsigned char *)client_cert_key, client_cert_key_len,
  941. (const unsigned char *)client_cert_key_password, client_cert_key_password_len);
  942. if (result)
  943. {
  944. LLOGE("set client cert failed %d", result);
  945. return -ERROR_OPERATION_FAILED;
  946. }
  947. }
  948. return 0;
  949. }
  950. int luat_http_client_clear(luat_http_ctrl_t *http_ctrl)
  951. {
  952. OS_DeInitBuffer(&http_ctrl->request_head_buffer);
  953. return 0;
  954. }
  955. int luat_http_client_set_user_head(luat_http_ctrl_t *http_ctrl, const char *name, const char *value)
  956. {
  957. if (!http_ctrl) return -ERROR_PARAM_INVALID;
  958. if (http_ctrl->state)
  959. {
  960. LLOGE("http running, please stop and set");
  961. return -ERROR_PERMISSION_DENIED;
  962. }
  963. if (!http_ctrl->request_head_buffer.Data)
  964. {
  965. OS_InitBuffer(&http_ctrl->request_head_buffer, HTTP_RESP_BUFF_SIZE);
  966. }
  967. int ret = sprintf_((char *)http_ctrl->request_head_buffer.Data + http_ctrl->request_head_buffer.Pos, "%s:%s\r\n", name, value);
  968. if (ret > 0)
  969. {
  970. http_ctrl->request_head_buffer.Pos += ret;
  971. if (!strcmp("Host", name) || !strcmp("host", name))
  972. {
  973. http_ctrl->custom_host = 1;
  974. }
  975. return 0;
  976. }
  977. else
  978. {
  979. return -ERROR_OPERATION_FAILED;
  980. }
  981. }
  982. int luat_http_client_close(luat_http_ctrl_t *http_ctrl)
  983. {
  984. if (!http_ctrl) return -ERROR_PARAM_INVALID;
  985. LLOGD("user close http!");
  986. http_ctrl->state = HTTP_STATE_WAIT_CLOSE;
  987. http_ctrl->re_request_count = http_ctrl->retry_cnt_max;
  988. network_force_close_socket(http_ctrl->netc);
  989. luat_rtos_timer_stop(http_ctrl->timeout_timer);
  990. http_ctrl->state = HTTP_STATE_IDLE;
  991. http_ctrl->offset = 0;
  992. return 0;
  993. }
  994. int luat_http_client_destroy(luat_http_ctrl_t **p_http_ctrl)
  995. {
  996. if (!p_http_ctrl) return -ERROR_PARAM_INVALID;
  997. luat_http_ctrl_t *http_ctrl = *p_http_ctrl;
  998. if (!http_ctrl) return -ERROR_PARAM_INVALID;
  999. LLOGD("user destroy http!");
  1000. http_ctrl->state = HTTP_STATE_WAIT_CLOSE;
  1001. OS_DeInitBuffer(&http_ctrl->request_head_buffer);
  1002. OS_DeInitBuffer(&http_ctrl->response_head_buffer);
  1003. http_close(http_ctrl);
  1004. *p_http_ctrl = NULL;
  1005. return 0;
  1006. }
  1007. int luat_http_client_get_status_code(luat_http_ctrl_t *http_ctrl)
  1008. {
  1009. if (!http_ctrl) return -ERROR_PARAM_INVALID;
  1010. return http_ctrl->parser.status_code;
  1011. }
  1012. int luat_http_client_set_get_offset(luat_http_ctrl_t *http_ctrl, uint32_t offset)
  1013. {
  1014. if (!http_ctrl) return -ERROR_PARAM_INVALID;
  1015. if (http_ctrl->state)
  1016. {
  1017. LLOGE("http running, stop and set!");
  1018. return -ERROR_PERMISSION_DENIED;
  1019. }
  1020. http_ctrl->offset = offset;
  1021. return 0;
  1022. }
  1023. int luat_http_client_pause(luat_http_ctrl_t *http_ctrl, uint8_t is_pause)
  1024. {
  1025. if (!http_ctrl) return -ERROR_PARAM_INVALID;
  1026. if (http_ctrl->state != HTTP_STATE_GET_BODY)
  1027. {
  1028. LLOGE("http not recv body data, no use!");
  1029. return -ERROR_PERMISSION_DENIED;
  1030. }
  1031. LLOGD("http pause state %d!", is_pause);
  1032. http_ctrl->is_pause = is_pause;
  1033. if (!http_ctrl->is_pause)
  1034. {
  1035. OS_EVENT event = {EV_NW_RESULT_EVENT, 0, 0, 0};
  1036. luat_lib_http_callback(&event, http_ctrl);
  1037. }
  1038. return 0;
  1039. }
  1040. int luat_http_client_get_context_len(luat_http_ctrl_t *http_ctrl, uint32_t *len)
  1041. {
  1042. if (http_ctrl->context_len_vaild)
  1043. {
  1044. *len = http_ctrl->context_len;
  1045. return 0;
  1046. }
  1047. else
  1048. {
  1049. return -1;
  1050. }
  1051. }
  1052. int luat_http_client_post_body(luat_http_ctrl_t *http_ctrl, void *data, uint32_t len)
  1053. {
  1054. if (!http_ctrl) return -ERROR_PARAM_INVALID;
  1055. if (http_ctrl->state != HTTP_STATE_GET_HEAD)
  1056. {
  1057. return -ERROR_PERMISSION_DENIED;
  1058. }
  1059. http_send(http_ctrl, data, len);
  1060. return 0;
  1061. }
  1062. int luat_http_client_start(luat_http_ctrl_t *http_ctrl, const char *url, uint8_t type, uint8_t ipv6, uint8_t data_mode)
  1063. {
  1064. if (!http_ctrl) return -ERROR_PARAM_INVALID;
  1065. if (http_ctrl->state)
  1066. {
  1067. LLOGE("http running, please stop and start");
  1068. return -ERROR_PERMISSION_DENIED;
  1069. }
  1070. switch(type)
  1071. {
  1072. case 0:
  1073. case 3:
  1074. http_ctrl->is_post = 0;
  1075. break;
  1076. case 1:
  1077. case 2:
  1078. http_ctrl->is_post = 1;
  1079. break;
  1080. default:
  1081. return -ERROR_PARAM_INVALID;
  1082. }
  1083. http_ctrl->luatos_mode = 0;
  1084. http_ctrl->tcp_closed = 0;
  1085. http_ctrl->data_mode = data_mode;
  1086. http_ctrl->re_request_count = 0;
  1087. http_ctrl->body_len = 0;
  1088. http_ctrl->remote_port = 0;
  1089. http_ctrl->parser.status_code = 0;
  1090. OS_ReInitBuffer(&http_ctrl->response_head_buffer, HTTP_HEADER_BASE_SIZE);
  1091. network_connect_ipv6_domain(http_ctrl->netc, ipv6);
  1092. if (http_ctrl->host)
  1093. {
  1094. luat_heap_free(http_ctrl->host);
  1095. http_ctrl->host = NULL;
  1096. }
  1097. if (http_ctrl->request_line)
  1098. {
  1099. luat_heap_free(http_ctrl->request_line);
  1100. http_ctrl->request_line = NULL;
  1101. }
  1102. char *tmp = (char *)url;
  1103. if (!strncmp("https://", url, strlen("https://"))) {
  1104. http_ctrl->is_tls = 1;
  1105. tmp += strlen("https://");
  1106. }
  1107. else if (!strncmp("http://", url, strlen("http://"))) {
  1108. http_ctrl->is_tls = 0;
  1109. tmp += strlen("http://");
  1110. }
  1111. else {
  1112. LLOGD("only http/https supported %s", url);
  1113. return -ERROR_PARAM_INVALID;
  1114. }
  1115. int tmplen = strlen(tmp);
  1116. if (tmplen < 5) {
  1117. LLOGD("url too short %s", url);
  1118. return -ERROR_PARAM_INVALID;
  1119. }
  1120. char tmphost[256] = {0};
  1121. char *tmpuri = NULL;
  1122. for (size_t i = 0; i < tmplen; i++){
  1123. if (tmp[i] == '/') {
  1124. if (i > 255) {
  1125. LLOGD("host too long %s", url);
  1126. return -ERROR_PARAM_INVALID;
  1127. }
  1128. tmpuri = tmp + i;
  1129. break;
  1130. }else if(i == tmplen-1){
  1131. tmphost[i+2] = '/';
  1132. tmpuri = tmp + i+1;
  1133. }
  1134. tmphost[i] = tmp[i];
  1135. }
  1136. if (strlen(tmphost) < 1) {
  1137. LLOGD("host not found %s", url);
  1138. return -ERROR_PARAM_INVALID;
  1139. }
  1140. if (strlen(tmpuri) == 0) {
  1141. tmpuri = "/";
  1142. }
  1143. // LLOGD("tmphost:%s",tmphost);
  1144. // LLOGD("tmpuri:%s",tmpuri);
  1145. for (size_t i = 1; i < strlen(tmphost); i++){
  1146. if (tmphost[i] == ':') {
  1147. tmphost[i] = '\0';
  1148. http_ctrl->remote_port = atoi(tmphost + i + 1);
  1149. break;
  1150. }
  1151. }
  1152. if (http_ctrl->remote_port <= 0) {
  1153. if (http_ctrl->is_tls)
  1154. http_ctrl->remote_port = 443;
  1155. else
  1156. http_ctrl->remote_port = 80;
  1157. }
  1158. http_ctrl->host = luat_heap_malloc(strlen(tmphost) + 1);
  1159. if (http_ctrl->host == NULL) {
  1160. LLOGD("out of memory when malloc host");
  1161. return -ERROR_NO_MEMORY;
  1162. }
  1163. memcpy(http_ctrl->host, tmphost, strlen(tmphost) + 1);
  1164. size_t linelen = strlen((char*)tmpuri) + 32;
  1165. http_ctrl->request_line = luat_heap_malloc(linelen);
  1166. if (http_ctrl->request_line == NULL) {
  1167. LLOGD("out of memory when malloc url/request_line");
  1168. return -ERROR_NO_MEMORY;
  1169. }
  1170. const char *me[4] = {
  1171. "GET","POST","PUT","DELETE"
  1172. };
  1173. snprintf_((char*)http_ctrl->request_line, 8192, "%s %s HTTP/1.1\r\n", me[type], tmpuri);
  1174. if (http_ctrl->timeout)
  1175. {
  1176. luat_start_rtos_timer(http_ctrl->timeout_timer, http_ctrl->timeout, 1);
  1177. }
  1178. else
  1179. {
  1180. luat_stop_rtos_timer(http_ctrl->timeout_timer);
  1181. }
  1182. http_ctrl->state = HTTP_STATE_CONNECT;
  1183. LLOGD("http connect %s:%d", http_ctrl->host, http_ctrl->remote_port);
  1184. http_ctrl->error_code = HTTP_ERROR_CONNECT;
  1185. http_ctrl->context_len_vaild = 0;
  1186. http_ctrl->context_len = 0;
  1187. if (network_connect(http_ctrl->netc, http_ctrl->host, strlen(http_ctrl->host), NULL, http_ctrl->remote_port, 0) < 0)
  1188. {
  1189. LLOGE("http can not connect!");
  1190. network_close(http_ctrl->netc, 0);
  1191. http_ctrl->state = HTTP_STATE_IDLE;
  1192. return -1;
  1193. }
  1194. return 0;
  1195. }
  1196. int http_set_url(luat_http_ctrl_t *http_ctrl, const char* url, const char* method) {
  1197. const char *tmp = url;
  1198. if (strcmp("POST", method) != 0 && strcmp("GET", method) != 0
  1199. && strcmp("PUT", method) != 0 && strcmp("DELETE", method) != 0
  1200. && strcmp("PATCH", method) != 0) {
  1201. LLOGE("NOT SUPPORT %s",method);
  1202. return -1;
  1203. }
  1204. if (!strncmp("https://", url, strlen("https://"))) {
  1205. http_ctrl->is_tls = 1;
  1206. tmp += strlen("https://");
  1207. }
  1208. else if (!strncmp("http://", url, strlen("http://"))) {
  1209. http_ctrl->is_tls = 0;
  1210. tmp += strlen("http://");
  1211. }
  1212. else {
  1213. LLOGE("only http/https supported %s", url);
  1214. return -1;
  1215. }
  1216. size_t tmplen = strlen(tmp);
  1217. if (tmplen < 5) {
  1218. LLOGE("url too short %s", url);
  1219. return -1;
  1220. }
  1221. #define HOST_MAX_LEN (256)
  1222. #define AUTH_MAX_LEN (128)
  1223. char tmphost[HOST_MAX_LEN] = {0};
  1224. char tmpauth[AUTH_MAX_LEN] = {0};
  1225. const char *tmpuri = NULL;
  1226. for (size_t i = 0; i < tmplen; i++){
  1227. if (tmp[i] == '/') {
  1228. if (i > 255) {
  1229. LLOGE("host too long %s", url);
  1230. return -1;
  1231. }
  1232. tmpuri = tmp + i;
  1233. break;
  1234. }else if(i == tmplen-1){
  1235. tmphost[i+2] = '/';
  1236. tmpuri = tmp + i+1;
  1237. }
  1238. tmphost[i] = tmp[i];
  1239. }
  1240. if (strlen(tmphost) < 1) {
  1241. LLOGE("host not found %s", url);
  1242. return -1;
  1243. }
  1244. if (strlen(tmpuri) == 0) {
  1245. tmpuri = "/";
  1246. }
  1247. // 先判断有无鉴权信息
  1248. for (size_t i = 1; i < AUTH_MAX_LEN; i++){
  1249. if (tmphost[i] == '@') {
  1250. memcpy(tmpauth, tmphost, i);
  1251. memmove(tmphost, tmphost + i + 1, strlen(tmphost) - i - 1);
  1252. tmphost[strlen(tmphost) - i - 1] = 0x00;
  1253. break;
  1254. }
  1255. }
  1256. // LLOGD("tmphost:%s",tmphost);
  1257. // LLOGD("tmpauth:%s", tmpauth);
  1258. // LLOGD("tmpuri:%s",tmpuri);
  1259. if(tmphost[0] != '[')
  1260. {
  1261. for (size_t i = 1; i < strlen(tmphost); i++){
  1262. if (tmphost[i] == ':') {
  1263. tmphost[i] = '\0';
  1264. http_ctrl->remote_port = atoi(tmphost + i + 1);
  1265. break;
  1266. }
  1267. }
  1268. }
  1269. else
  1270. {
  1271. // ipv6地址
  1272. uint16_t offset = 0;
  1273. for (size_t i = 1; i < strlen(tmphost); i++){
  1274. if (tmphost[i] == ']') {
  1275. offset = i;
  1276. break;
  1277. }
  1278. }
  1279. if (offset > 0) {
  1280. for (size_t i = offset; i < strlen(tmphost); i++){
  1281. if (tmphost[i] == ':') {
  1282. tmphost[i] = '\0';
  1283. http_ctrl->remote_port = atoi(tmphost + i + 1);
  1284. break;
  1285. }
  1286. }
  1287. memmove(tmphost, tmphost + 1, offset - 1);
  1288. tmphost[offset - 1] = 0x00;
  1289. }
  1290. }
  1291. if (http_ctrl->remote_port <= 0) {
  1292. if (http_ctrl->is_tls)
  1293. http_ctrl->remote_port = 443;
  1294. else
  1295. http_ctrl->remote_port = 80;
  1296. }
  1297. http_ctrl->host = luat_heap_malloc(strlen(tmphost) + 1);
  1298. if (http_ctrl->host == NULL) {
  1299. LLOGE("out of memory when malloc host");
  1300. return -1;
  1301. }
  1302. if (tmpauth[0]) {
  1303. size_t tmplen = 0;
  1304. http_ctrl->req_auth = luat_heap_malloc(strlen(tmpauth) * 2 + 64);
  1305. if (http_ctrl->req_auth == NULL) {
  1306. LLOGE("out of memory when malloc auth");
  1307. return -1;
  1308. }
  1309. memset(http_ctrl->req_auth, 0, strlen(tmpauth) * 2 + 64);
  1310. memcpy(http_ctrl->req_auth, "Authorization: Basic ", strlen("Authorization: Basic "));
  1311. luat_str_base64_encode((unsigned char *)http_ctrl->req_auth + strlen(http_ctrl->req_auth),
  1312. strlen(tmpauth) * 2, &tmplen, (const unsigned char *)tmpauth, strlen(tmpauth));
  1313. memcpy(http_ctrl->req_auth + strlen(http_ctrl->req_auth), "\r\n", 2);
  1314. }
  1315. memcpy(http_ctrl->host, tmphost, strlen(tmphost) + 1);
  1316. size_t linelen = strlen((char*)method) + strlen((char*)tmpuri) + 16;
  1317. http_ctrl->request_line = luat_heap_malloc(linelen);
  1318. if (http_ctrl->request_line == NULL) {
  1319. LLOGE("out of memory when malloc url/request_line");
  1320. return -1;
  1321. }
  1322. snprintf_((char*)http_ctrl->request_line, 8192, "%s %s HTTP/1.1\r\n", method, tmpuri);
  1323. return 0;
  1324. }
  1325. int luat_http_client_start_luatos(luat_http_ctrl_t* http_ctrl) {
  1326. http_ctrl->luatos_mode = 1;
  1327. if(http_ctrl->timeout){
  1328. http_ctrl->timeout_timer = luat_create_rtos_timer(luat_http_timer_callback, http_ctrl, NULL);
  1329. luat_start_rtos_timer(http_ctrl->timeout_timer, http_ctrl->timeout, 0);
  1330. }
  1331. if(network_connect(http_ctrl->netc, http_ctrl->host, strlen(http_ctrl->host), NULL, http_ctrl->remote_port, 0) < 0){
  1332. // network_close(http_ctrl->netc, 0);
  1333. return -1;
  1334. }
  1335. return 0;
  1336. }