luat_http_client.c 38 KB

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