luat_http_client.c 36 KB

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