luat_http_client.c 40 KB

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