luat_http_client.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436
  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("http_ctrl->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->luatos_mode) {
  645. if (HTTP_STATE_IDLE == http_ctrl->state)
  646. {
  647. LLOGE("http state error %d", http_ctrl->state);
  648. return 0;
  649. }
  650. }
  651. if (http_ctrl->tcp_closed){
  652. LLOGD("http already closed %p", http_ctrl);
  653. return 0;
  654. }
  655. //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);
  656. LLOGD("luat_lib_http_callback %08X %d %p",event->ID - EV_NW_RESULT_BASE, event->Param1, http_ctrl);
  657. if (event->Param1){
  658. //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);
  659. 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);
  660. if (http_ctrl->tcp_closed == 0 && http_ctrl->error_code == 0) {
  661. http_ctrl->error_code = event->ID == EV_NW_RESULT_CONNECT ? HTTP_ERROR_CONNECT : HTTP_ERROR_CLOSE;
  662. }
  663. if (http_ctrl->tcp_closed == 0) {
  664. on_tcp_closed(http_ctrl);
  665. }
  666. return -1;
  667. }
  668. switch (event->ID)
  669. {
  670. case EV_NW_RESULT_EVENT:
  671. if (!http_ctrl->luatos_mode) {
  672. http_ctrl->new_data = 1;
  673. }
  674. if (http_ctrl->is_pause){
  675. LLOGD("rx pause");
  676. break;
  677. }
  678. uint32_t total_len = 0;
  679. uint32_t rx_len = 0;
  680. while (1) {
  681. int result = network_rx(http_ctrl->netc, NULL, 0, 0, NULL, NULL, &total_len);
  682. if (result) {
  683. if (http_ctrl->luatos_mode) {
  684. http_resp_error(http_ctrl, http_ctrl->http_body_is_finally ? HTTP_OK : HTTP_ERROR_RX);
  685. } else {
  686. http_ctrl->error_code = http_ctrl->http_body_is_finally ? HTTP_OK : HTTP_ERROR_RX;
  687. http_network_error(http_ctrl);
  688. }
  689. return -1;
  690. }
  691. if (0 == total_len)
  692. break;
  693. if (http_ctrl->resp_buff_offset + total_len > (HTTP_RESP_BUFF_SIZE - 1)) {
  694. total_len = HTTP_RESP_BUFF_SIZE - 1 - http_ctrl->resp_buff_offset;
  695. if (total_len < 1) {
  696. // 能到这里的就是片段太长了
  697. // 要么header太长, 要么chunked太长,拒绝吧
  698. if (http_ctrl->luatos_mode) {
  699. http_resp_error(http_ctrl, http_ctrl->http_body_is_finally ? HTTP_OK : HTTP_ERROR_RX);
  700. } else {
  701. http_ctrl->error_code = http_ctrl->http_body_is_finally ? HTTP_OK : HTTP_ERROR_RX;
  702. http_network_error(http_ctrl);
  703. }
  704. return -1;
  705. }
  706. }
  707. result = network_rx(http_ctrl->netc, (uint8_t*)http_ctrl->resp_buff+http_ctrl->resp_buff_offset, total_len, 0, NULL, NULL, &rx_len);
  708. LLOGD("result:%d rx_len:%d",result,rx_len);
  709. if (rx_len == 0||result!=0) {
  710. if (http_ctrl->luatos_mode) {
  711. http_resp_error(http_ctrl, http_ctrl->http_body_is_finally ? HTTP_OK : HTTP_ERROR_RX);
  712. } else {
  713. http_ctrl->error_code = http_ctrl->http_body_is_finally ? HTTP_OK : HTTP_ERROR_RX;
  714. http_network_error(http_ctrl);
  715. }
  716. return -1;
  717. }
  718. http_ctrl->resp_buff_offset += rx_len;
  719. //LLOGD("resp_buff_offset:%d resp_buff:%s",http_ctrl->resp_buff_offset,http_ctrl->resp_buff);
  720. // uint8_t *tmp = (uint8_t*)http_ctrl->resp_buff;
  721. //LLOGD("resp buff %.*s", http_ctrl->resp_buff_offset, http_ctrl->resp_buff);
  722. if (0 == http_ctrl->resp_headers_done) {
  723. LLOGD("search headers, buff len %d", http_ctrl->resp_buff_offset);
  724. if (http_ctrl->resp_buff_offset > 4) {
  725. uint8_t *tmp = (uint8_t*)http_ctrl->resp_buff;
  726. size_t search = http_ctrl->resp_buff_offset;
  727. for (size_t i = 0; i < search; i++)
  728. {
  729. // \\r\\n\\r\\n
  730. // \\n\\n
  731. // \\r\\r
  732. if ((0x0D == tmp[i] && 0x0A == tmp[i+1] && 0x0D == tmp[i+2] && 0x0A == tmp[i+3]) ||
  733. (0x0A == tmp[i] && 0x0A == tmp[i+1]) ||
  734. (0x0D == tmp[i] && 0x0D == tmp[i+1]) ){
  735. http_ctrl->resp_headers_done = 1;
  736. LLOGD("found headers end at %d", i);
  737. break;
  738. }
  739. }
  740. }
  741. }
  742. if (http_ctrl->resp_headers_done) {
  743. size_t nParseBytes = http_parser_execute(&http_ctrl->parser, &parser_settings, http_ctrl->resp_buff, http_ctrl->resp_buff_offset);
  744. LLOGD("nParseBytes %d resp_buff_offset %d", nParseBytes, http_ctrl->resp_buff_offset);
  745. if(http_ctrl->parser.http_errno) {
  746. LLOGW("http exit reason by errno: %d != HPE_OK!!!", http_ctrl->parser.http_errno);
  747. return 0;
  748. }
  749. if (http_ctrl->resp_buff_offset <= nParseBytes) {
  750. http_ctrl->resp_buff_offset = 0;
  751. }
  752. else {
  753. memmove(http_ctrl->resp_buff, http_ctrl->resp_buff + nParseBytes, http_ctrl->resp_buff_offset - nParseBytes);
  754. http_ctrl->resp_buff_offset -= nParseBytes;
  755. }
  756. }
  757. else {
  758. LLOGD("wait headers %.*s", http_ctrl->resp_buff_offset, http_ctrl->resp_buff);
  759. }
  760. if (http_ctrl->tcp_closed){
  761. return 0;
  762. }
  763. }
  764. break;
  765. case EV_NW_RESULT_TX:
  766. if (http_ctrl->luatos_mode) {
  767. if (http_ctrl->tx_offset){
  768. if (http_ctrl->req_body){
  769. if (http_ctrl->req_body_len-http_ctrl->tx_offset > HTTP_SEND_LEN_MAX){
  770. http_send(http_ctrl, (uint8_t*)http_ctrl->req_body+http_ctrl->tx_offset, HTTP_SEND_LEN_MAX);
  771. http_ctrl->tx_offset += HTTP_SEND_LEN_MAX;
  772. }else{
  773. http_send(http_ctrl, (uint8_t*)http_ctrl->req_body+http_ctrl->tx_offset, http_ctrl->req_body_len-http_ctrl->tx_offset);
  774. http_ctrl->tx_offset = 0;
  775. }
  776. }
  777. else if(http_ctrl->is_post==1 && http_ctrl->zbuff_body!=NULL){
  778. if (http_ctrl->zbuff_body->used-http_ctrl->tx_offset > HTTP_SEND_LEN_MAX){
  779. http_send(http_ctrl, http_ctrl->zbuff_body->addr+http_ctrl->tx_offset, HTTP_SEND_LEN_MAX);
  780. http_ctrl->tx_offset += HTTP_SEND_LEN_MAX;
  781. }else{
  782. http_send(http_ctrl, http_ctrl->zbuff_body->addr+http_ctrl->tx_offset, http_ctrl->zbuff_body->used-http_ctrl->tx_offset);
  783. http_ctrl->tx_offset = 0;
  784. }
  785. }
  786. }
  787. } else {
  788. if (http_ctrl->is_post){
  789. luat_http_cb http_cb = http_ctrl->http_cb;
  790. http_cb(HTTP_STATE_SEND_BODY, NULL, 0, http_ctrl->http_cb_userdata);
  791. }
  792. http_ctrl->state = HTTP_STATE_GET_HEAD;
  793. }
  794. return 0;
  795. case EV_NW_RESULT_CONNECT:
  796. http_ctrl->resp_buff_offset = 0; // 复位resp缓冲区
  797. http_ctrl->resp_headers_done = 0;
  798. http_parser_init(&http_ctrl->parser, HTTP_RESPONSE);
  799. http_ctrl->parser.data = http_ctrl;
  800. // TODO header 保持原始数据,在lua回调时才导出数据
  801. // if (http_ctrl->resp_headers) {
  802. // luat_heap_free(http_ctrl->resp_headers);
  803. // http_ctrl->resp_headers = NULL;
  804. // }
  805. http_send_message(http_ctrl);
  806. return 0;
  807. case EV_NW_RESULT_CLOSE:
  808. if (http_ctrl->luatos_mode) {
  809. if (http_ctrl->tcp_closed == 0) {
  810. on_tcp_closed(http_ctrl);
  811. }
  812. }
  813. else {
  814. if (http_ctrl->error_code && (http_ctrl->state != HTTP_STATE_DONE))
  815. {
  816. LLOGD("http network closed");
  817. http_network_error(http_ctrl);
  818. }
  819. else
  820. {
  821. http_ctrl->state = HTTP_STATE_IDLE;
  822. luat_http_cb http_cb = http_ctrl->http_cb;
  823. http_cb(http_ctrl->state, NULL, 0, http_ctrl->http_cb_userdata);
  824. }
  825. }
  826. return 0;
  827. case EV_NW_RESULT_LINK:
  828. return 0;
  829. default:
  830. break;
  831. }
  832. ret = network_wait_event(http_ctrl->netc, NULL, 0, NULL);
  833. if (ret < 0){
  834. LLOGE("network_wait_event %d", ret);
  835. if (http_ctrl->luatos_mode) {
  836. http_resp_error(http_ctrl, HTTP_ERROR_CLOSE);
  837. } else {
  838. http_ctrl->error_code = HTTP_ERROR_STATE;
  839. http_network_close(http_ctrl);
  840. }
  841. return -1;
  842. }
  843. return 0;
  844. }
  845. static void luat_http_dummy_cb(int status, void *data, uint32_t data_len, void *user_param) {;}
  846. luat_http_ctrl_t* luat_http_client_create(luat_http_cb cb, void *user_param, int adapter_index)
  847. {
  848. luat_http_ctrl_t *http_ctrl = luat_heap_malloc(sizeof(luat_http_ctrl_t));
  849. if (!http_ctrl) return NULL;
  850. memset(http_ctrl,0,sizeof(luat_http_ctrl_t));
  851. http_ctrl->timeout_timer = luat_create_rtos_timer(luat_http_timer_callback, http_ctrl, NULL);
  852. if (!http_ctrl->timeout_timer)
  853. {
  854. luat_heap_free(http_ctrl);
  855. LLOGE("no more timer");
  856. return NULL;
  857. }
  858. if (adapter_index >= 0)
  859. {
  860. http_ctrl->netc = network_alloc_ctrl(adapter_index);
  861. }
  862. else
  863. {
  864. http_ctrl->netc = network_alloc_ctrl(network_register_get_default());
  865. }
  866. if (!http_ctrl->netc)
  867. {
  868. luat_release_rtos_timer(http_ctrl->timeout_timer);
  869. luat_heap_free(http_ctrl);
  870. LLOGE("no more network ctrl");
  871. return NULL;
  872. }
  873. network_init_ctrl(http_ctrl->netc, NULL, luat_lib_http_callback, http_ctrl);
  874. network_set_base_mode(http_ctrl->netc, 1, 10000, 0, 0, 0, 0);
  875. network_set_local_port(http_ctrl->netc, 0);
  876. http_ctrl->http_cb = cb?cb:luat_http_dummy_cb;
  877. http_ctrl->http_cb_userdata = user_param;
  878. http_ctrl->timeout = 15000;
  879. http_ctrl->retry_cnt_max = 0;
  880. http_ctrl->state = HTTP_STATE_IDLE;
  881. http_ctrl->debug_onoff = 0;
  882. http_ctrl->netc->is_debug = 0;
  883. return http_ctrl;
  884. }
  885. int luat_http_client_base_config(luat_http_ctrl_t* http_ctrl, uint32_t timeout, uint8_t debug_onoff, uint8_t re_request_count)
  886. {
  887. if (!http_ctrl) return -ERROR_PARAM_INVALID;
  888. if (http_ctrl->state)
  889. {
  890. LLOGE("http running, please stop and set");
  891. return -ERROR_PERMISSION_DENIED;
  892. }
  893. http_ctrl->timeout = timeout;
  894. http_ctrl->debug_onoff = debug_onoff;
  895. http_ctrl->netc->is_debug = debug_onoff;
  896. http_ctrl->retry_cnt_max = re_request_count;
  897. // 如果retry_cnt_max大于0, 则警告将要废弃自动重试功能
  898. if (re_request_count > 0) {
  899. LLOGE("http client auto re-request is deprecated, please handle retry in your code!!!");
  900. }
  901. return 0;
  902. }
  903. int luat_http_client_ssl_config(luat_http_ctrl_t* http_ctrl, int mode, const char *server_cert, uint32_t server_cert_len,
  904. const char *client_cert, uint32_t client_cert_len,
  905. const char *client_cert_key, uint32_t client_cert_key_len,
  906. const char *client_cert_key_password, uint32_t client_cert_key_password_len)
  907. {
  908. if (!http_ctrl) return -ERROR_PARAM_INVALID;
  909. if (http_ctrl->state)
  910. {
  911. LLOGE("http running, please stop and set");
  912. return -ERROR_PERMISSION_DENIED;
  913. }
  914. if (mode < 0)
  915. {
  916. network_deinit_tls(http_ctrl->netc);
  917. return 0;
  918. }
  919. if (mode > 2)
  920. {
  921. return -ERROR_PARAM_INVALID;
  922. }
  923. int result;
  924. // network_init_tls(http_ctrl->netc, (server_cert || client_cert)?2:0);
  925. network_init_tls(http_ctrl->netc, 0);
  926. if (server_cert){
  927. result = network_set_server_cert(http_ctrl->netc, (const unsigned char *)server_cert, server_cert_len);
  928. if (result)
  929. {
  930. LLOGE("set server cert failed %d", result);
  931. return -ERROR_OPERATION_FAILED;
  932. }
  933. }
  934. if (client_cert){
  935. result = network_set_client_cert(http_ctrl->netc, (const unsigned char *)client_cert, client_cert_len,
  936. (const unsigned char *)client_cert_key, client_cert_key_len,
  937. (const unsigned char *)client_cert_key_password, client_cert_key_password_len);
  938. if (result)
  939. {
  940. LLOGE("set client cert failed %d", result);
  941. return -ERROR_OPERATION_FAILED;
  942. }
  943. }
  944. return 0;
  945. }
  946. int luat_http_client_clear(luat_http_ctrl_t *http_ctrl)
  947. {
  948. OS_DeInitBuffer(&http_ctrl->request_head_buffer);
  949. return 0;
  950. }
  951. int luat_http_client_set_user_head(luat_http_ctrl_t *http_ctrl, const char *name, const char *value)
  952. {
  953. if (!http_ctrl) return -ERROR_PARAM_INVALID;
  954. if (http_ctrl->state)
  955. {
  956. LLOGE("http running, please stop and set");
  957. return -ERROR_PERMISSION_DENIED;
  958. }
  959. if (!http_ctrl->request_head_buffer.Data)
  960. {
  961. OS_InitBuffer(&http_ctrl->request_head_buffer, HTTP_RESP_BUFF_SIZE);
  962. }
  963. int ret = sprintf_((char *)http_ctrl->request_head_buffer.Data + http_ctrl->request_head_buffer.Pos, "%s:%s\r\n", name, value);
  964. if (ret > 0)
  965. {
  966. http_ctrl->request_head_buffer.Pos += ret;
  967. if (!strcmp("Host", name) || !strcmp("host", name))
  968. {
  969. http_ctrl->custom_host = 1;
  970. }
  971. return 0;
  972. }
  973. else
  974. {
  975. return -ERROR_OPERATION_FAILED;
  976. }
  977. }
  978. int luat_http_client_close(luat_http_ctrl_t *http_ctrl)
  979. {
  980. if (!http_ctrl) return -ERROR_PARAM_INVALID;
  981. LLOGD("user close http!");
  982. http_ctrl->state = HTTP_STATE_WAIT_CLOSE;
  983. http_ctrl->re_request_count = http_ctrl->retry_cnt_max;
  984. network_force_close_socket(http_ctrl->netc);
  985. luat_rtos_timer_stop(http_ctrl->timeout_timer);
  986. http_ctrl->state = HTTP_STATE_IDLE;
  987. http_ctrl->offset = 0;
  988. return 0;
  989. }
  990. int luat_http_client_destroy(luat_http_ctrl_t **p_http_ctrl)
  991. {
  992. if (!p_http_ctrl) return -ERROR_PARAM_INVALID;
  993. luat_http_ctrl_t *http_ctrl = *p_http_ctrl;
  994. if (!http_ctrl) return -ERROR_PARAM_INVALID;
  995. LLOGD("user destroy http!");
  996. http_ctrl->state = HTTP_STATE_WAIT_CLOSE;
  997. OS_DeInitBuffer(&http_ctrl->request_head_buffer);
  998. OS_DeInitBuffer(&http_ctrl->response_head_buffer);
  999. http_close(http_ctrl);
  1000. *p_http_ctrl = NULL;
  1001. return 0;
  1002. }
  1003. int luat_http_client_get_status_code(luat_http_ctrl_t *http_ctrl)
  1004. {
  1005. if (!http_ctrl) return -ERROR_PARAM_INVALID;
  1006. return http_ctrl->parser.status_code;
  1007. }
  1008. int luat_http_client_set_get_offset(luat_http_ctrl_t *http_ctrl, uint32_t offset)
  1009. {
  1010. if (!http_ctrl) return -ERROR_PARAM_INVALID;
  1011. if (http_ctrl->state)
  1012. {
  1013. LLOGE("http running, stop and set!");
  1014. return -ERROR_PERMISSION_DENIED;
  1015. }
  1016. http_ctrl->offset = offset;
  1017. return 0;
  1018. }
  1019. int luat_http_client_pause(luat_http_ctrl_t *http_ctrl, uint8_t is_pause)
  1020. {
  1021. if (!http_ctrl) return -ERROR_PARAM_INVALID;
  1022. if (http_ctrl->state != HTTP_STATE_GET_BODY)
  1023. {
  1024. LLOGE("http not recv body data, no use!");
  1025. return -ERROR_PERMISSION_DENIED;
  1026. }
  1027. LLOGD("http pause state %d!", is_pause);
  1028. http_ctrl->is_pause = is_pause;
  1029. if (!http_ctrl->is_pause)
  1030. {
  1031. OS_EVENT event = {EV_NW_RESULT_EVENT, 0, 0, 0};
  1032. luat_lib_http_callback(&event, http_ctrl);
  1033. }
  1034. return 0;
  1035. }
  1036. int luat_http_client_get_context_len(luat_http_ctrl_t *http_ctrl, uint32_t *len)
  1037. {
  1038. if (http_ctrl->context_len_vaild)
  1039. {
  1040. *len = http_ctrl->context_len;
  1041. return 0;
  1042. }
  1043. else
  1044. {
  1045. return -1;
  1046. }
  1047. }
  1048. int luat_http_client_post_body(luat_http_ctrl_t *http_ctrl, void *data, uint32_t len)
  1049. {
  1050. if (!http_ctrl) return -ERROR_PARAM_INVALID;
  1051. if (http_ctrl->state != HTTP_STATE_GET_HEAD)
  1052. {
  1053. return -ERROR_PERMISSION_DENIED;
  1054. }
  1055. http_send(http_ctrl, data, len);
  1056. return 0;
  1057. }
  1058. int luat_http_client_start(luat_http_ctrl_t *http_ctrl, const char *url, uint8_t type, uint8_t ipv6, uint8_t data_mode)
  1059. {
  1060. if (!http_ctrl) return -ERROR_PARAM_INVALID;
  1061. if (http_ctrl->state)
  1062. {
  1063. LLOGE("http running, please stop and start");
  1064. return -ERROR_PERMISSION_DENIED;
  1065. }
  1066. switch(type)
  1067. {
  1068. case 0:
  1069. case 3:
  1070. http_ctrl->is_post = 0;
  1071. break;
  1072. case 1:
  1073. case 2:
  1074. http_ctrl->is_post = 1;
  1075. break;
  1076. default:
  1077. return -ERROR_PARAM_INVALID;
  1078. }
  1079. http_ctrl->luatos_mode = 0;
  1080. http_ctrl->tcp_closed = 0;
  1081. http_ctrl->data_mode = data_mode;
  1082. http_ctrl->re_request_count = 0;
  1083. http_ctrl->body_len = 0;
  1084. http_ctrl->remote_port = 0;
  1085. http_ctrl->parser.status_code = 0;
  1086. OS_ReInitBuffer(&http_ctrl->response_head_buffer, HTTP_HEADER_BASE_SIZE);
  1087. network_connect_ipv6_domain(http_ctrl->netc, ipv6);
  1088. if (http_ctrl->host)
  1089. {
  1090. luat_heap_free(http_ctrl->host);
  1091. http_ctrl->host = NULL;
  1092. }
  1093. if (http_ctrl->request_line)
  1094. {
  1095. luat_heap_free(http_ctrl->request_line);
  1096. http_ctrl->request_line = NULL;
  1097. }
  1098. char *tmp = (char *)url;
  1099. if (!strncmp("https://", url, strlen("https://"))) {
  1100. http_ctrl->is_tls = 1;
  1101. tmp += strlen("https://");
  1102. }
  1103. else if (!strncmp("http://", url, strlen("http://"))) {
  1104. http_ctrl->is_tls = 0;
  1105. tmp += strlen("http://");
  1106. }
  1107. else {
  1108. LLOGD("only http/https supported %s", url);
  1109. return -ERROR_PARAM_INVALID;
  1110. }
  1111. int tmplen = strlen(tmp);
  1112. if (tmplen < 5) {
  1113. LLOGD("url too short %s", url);
  1114. return -ERROR_PARAM_INVALID;
  1115. }
  1116. char tmphost[256] = {0};
  1117. char *tmpuri = NULL;
  1118. for (size_t i = 0; i < tmplen; i++){
  1119. if (tmp[i] == '/') {
  1120. if (i > 255) {
  1121. LLOGD("host too long %s", url);
  1122. return -ERROR_PARAM_INVALID;
  1123. }
  1124. tmpuri = tmp + i;
  1125. break;
  1126. }else if(i == tmplen-1){
  1127. tmphost[i+2] = '/';
  1128. tmpuri = tmp + i+1;
  1129. }
  1130. tmphost[i] = tmp[i];
  1131. }
  1132. if (strlen(tmphost) < 1) {
  1133. LLOGD("host not found %s", url);
  1134. return -ERROR_PARAM_INVALID;
  1135. }
  1136. if (strlen(tmpuri) == 0) {
  1137. tmpuri = "/";
  1138. }
  1139. // LLOGD("tmphost:%s",tmphost);
  1140. // LLOGD("tmpuri:%s",tmpuri);
  1141. for (size_t i = 1; i < strlen(tmphost); i++){
  1142. if (tmphost[i] == ':') {
  1143. tmphost[i] = '\0';
  1144. http_ctrl->remote_port = atoi(tmphost + i + 1);
  1145. break;
  1146. }
  1147. }
  1148. if (http_ctrl->remote_port <= 0) {
  1149. if (http_ctrl->is_tls)
  1150. http_ctrl->remote_port = 443;
  1151. else
  1152. http_ctrl->remote_port = 80;
  1153. }
  1154. http_ctrl->host = luat_heap_malloc(strlen(tmphost) + 1);
  1155. if (http_ctrl->host == NULL) {
  1156. LLOGD("out of memory when malloc host");
  1157. return -ERROR_NO_MEMORY;
  1158. }
  1159. memcpy(http_ctrl->host, tmphost, strlen(tmphost) + 1);
  1160. size_t linelen = strlen((char*)tmpuri) + 32;
  1161. http_ctrl->request_line = luat_heap_malloc(linelen);
  1162. if (http_ctrl->request_line == NULL) {
  1163. LLOGD("out of memory when malloc url/request_line");
  1164. return -ERROR_NO_MEMORY;
  1165. }
  1166. const char *me[4] = {
  1167. "GET","POST","PUT","DELETE"
  1168. };
  1169. snprintf_((char*)http_ctrl->request_line, 8192, "%s %s HTTP/1.1\r\n", me[type], tmpuri);
  1170. if (http_ctrl->timeout)
  1171. {
  1172. luat_start_rtos_timer(http_ctrl->timeout_timer, http_ctrl->timeout, 1);
  1173. }
  1174. else
  1175. {
  1176. luat_stop_rtos_timer(http_ctrl->timeout_timer);
  1177. }
  1178. http_ctrl->state = HTTP_STATE_CONNECT;
  1179. LLOGD("http connect %s:%d", http_ctrl->host, http_ctrl->remote_port);
  1180. http_ctrl->error_code = HTTP_ERROR_CONNECT;
  1181. http_ctrl->context_len_vaild = 0;
  1182. http_ctrl->context_len = 0;
  1183. if (network_connect(http_ctrl->netc, http_ctrl->host, strlen(http_ctrl->host), NULL, http_ctrl->remote_port, 0) < 0)
  1184. {
  1185. LLOGE("http can not connect!");
  1186. network_close(http_ctrl->netc, 0);
  1187. http_ctrl->state = HTTP_STATE_IDLE;
  1188. return -1;
  1189. }
  1190. return 0;
  1191. }
  1192. int http_set_url(luat_http_ctrl_t *http_ctrl, const char* url, const char* method) {
  1193. const char *tmp = url;
  1194. if (strcmp("POST", method) != 0 && strcmp("GET", method) != 0
  1195. && strcmp("PUT", method) != 0 && strcmp("DELETE", method) != 0
  1196. && strcmp("PATCH", method) != 0) {
  1197. LLOGE("NOT SUPPORT %s",method);
  1198. return -1;
  1199. }
  1200. if (!strncmp("https://", url, strlen("https://"))) {
  1201. http_ctrl->is_tls = 1;
  1202. tmp += strlen("https://");
  1203. }
  1204. else if (!strncmp("http://", url, strlen("http://"))) {
  1205. http_ctrl->is_tls = 0;
  1206. tmp += strlen("http://");
  1207. }
  1208. else {
  1209. LLOGE("only http/https supported %s", url);
  1210. return -1;
  1211. }
  1212. size_t tmplen = strlen(tmp);
  1213. if (tmplen < 5) {
  1214. LLOGE("url too short %s", url);
  1215. return -1;
  1216. }
  1217. #define HOST_MAX_LEN (256)
  1218. #define AUTH_MAX_LEN (128)
  1219. char tmphost[HOST_MAX_LEN] = {0};
  1220. char tmpauth[AUTH_MAX_LEN] = {0};
  1221. const char *tmpuri = NULL;
  1222. for (size_t i = 0; i < tmplen; i++){
  1223. if (tmp[i] == '/') {
  1224. if (i > 255) {
  1225. LLOGE("host too long %s", url);
  1226. return -1;
  1227. }
  1228. tmpuri = tmp + i;
  1229. break;
  1230. }else if(i == tmplen-1){
  1231. tmphost[i+2] = '/';
  1232. tmpuri = tmp + i+1;
  1233. }
  1234. tmphost[i] = tmp[i];
  1235. }
  1236. if (strlen(tmphost) < 1) {
  1237. LLOGE("host not found %s", url);
  1238. return -1;
  1239. }
  1240. if (strlen(tmpuri) == 0) {
  1241. tmpuri = "/";
  1242. }
  1243. // 先判断有无鉴权信息
  1244. for (size_t i = 1; i < AUTH_MAX_LEN; i++){
  1245. if (tmphost[i] == '@') {
  1246. memcpy(tmpauth, tmphost, i);
  1247. memmove(tmphost, tmphost + i + 1, strlen(tmphost) - i - 1);
  1248. tmphost[strlen(tmphost) - i - 1] = 0x00;
  1249. break;
  1250. }
  1251. }
  1252. // LLOGD("tmphost:%s",tmphost);
  1253. // LLOGD("tmpauth:%s", tmpauth);
  1254. // LLOGD("tmpuri:%s",tmpuri);
  1255. if(tmphost[0] != '[')
  1256. {
  1257. for (size_t i = 1; i < strlen(tmphost); i++){
  1258. if (tmphost[i] == ':') {
  1259. tmphost[i] = '\0';
  1260. http_ctrl->remote_port = atoi(tmphost + i + 1);
  1261. break;
  1262. }
  1263. }
  1264. }
  1265. else
  1266. {
  1267. // ipv6地址
  1268. uint16_t offset = 0;
  1269. for (size_t i = 1; i < strlen(tmphost); i++){
  1270. if (tmphost[i] == ']') {
  1271. offset = i;
  1272. break;
  1273. }
  1274. }
  1275. if (offset > 0) {
  1276. for (size_t i = offset; i < strlen(tmphost); i++){
  1277. if (tmphost[i] == ':') {
  1278. tmphost[i] = '\0';
  1279. http_ctrl->remote_port = atoi(tmphost + i + 1);
  1280. break;
  1281. }
  1282. }
  1283. memmove(tmphost, tmphost + 1, offset - 1);
  1284. tmphost[offset - 1] = 0x00;
  1285. }
  1286. }
  1287. if (http_ctrl->remote_port <= 0) {
  1288. if (http_ctrl->is_tls)
  1289. http_ctrl->remote_port = 443;
  1290. else
  1291. http_ctrl->remote_port = 80;
  1292. }
  1293. http_ctrl->host = luat_heap_malloc(strlen(tmphost) + 1);
  1294. if (http_ctrl->host == NULL) {
  1295. LLOGE("out of memory when malloc host");
  1296. return -1;
  1297. }
  1298. if (tmpauth[0]) {
  1299. size_t tmplen = 0;
  1300. http_ctrl->req_auth = luat_heap_malloc(strlen(tmpauth) * 2 + 64);
  1301. if (http_ctrl->req_auth == NULL) {
  1302. LLOGE("out of memory when malloc auth");
  1303. return -1;
  1304. }
  1305. memset(http_ctrl->req_auth, 0, strlen(tmpauth) * 2 + 64);
  1306. memcpy(http_ctrl->req_auth, "Authorization: Basic ", strlen("Authorization: Basic "));
  1307. luat_str_base64_encode((unsigned char *)http_ctrl->req_auth + strlen(http_ctrl->req_auth),
  1308. strlen(tmpauth) * 2, &tmplen, (const unsigned char *)tmpauth, strlen(tmpauth));
  1309. memcpy(http_ctrl->req_auth + strlen(http_ctrl->req_auth), "\r\n", 2);
  1310. }
  1311. memcpy(http_ctrl->host, tmphost, strlen(tmphost) + 1);
  1312. size_t linelen = strlen((char*)method) + strlen((char*)tmpuri) + 16;
  1313. http_ctrl->request_line = luat_heap_malloc(linelen);
  1314. if (http_ctrl->request_line == NULL) {
  1315. LLOGE("out of memory when malloc url/request_line");
  1316. return -1;
  1317. }
  1318. snprintf_((char*)http_ctrl->request_line, 8192, "%s %s HTTP/1.1\r\n", method, tmpuri);
  1319. return 0;
  1320. }
  1321. int luat_http_client_start_luatos(luat_http_ctrl_t* http_ctrl) {
  1322. http_ctrl->luatos_mode = 1;
  1323. if(http_ctrl->timeout){
  1324. http_ctrl->timeout_timer = luat_create_rtos_timer(luat_http_timer_callback, http_ctrl, NULL);
  1325. luat_start_rtos_timer(http_ctrl->timeout_timer, http_ctrl->timeout, 0);
  1326. }
  1327. if(network_connect(http_ctrl->netc, http_ctrl->host, strlen(http_ctrl->host), NULL, http_ctrl->remote_port, 0) < 0){
  1328. // network_close(http_ctrl->netc, 0);
  1329. return -1;
  1330. }
  1331. return 0;
  1332. }