luat_http_client.c 42 KB

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