luat_http_client.c 39 KB

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