瀏覽代碼

fix: http,修正chunked响应的时候总会超时,是重构之后引入的问题

Wendal Chen 2 月之前
父節點
當前提交
21f07e7453

+ 2 - 2
bsp/pc/test/004.http/main.lua

@@ -46,8 +46,8 @@ sys.taskInit(function()
     local code, headers, body = http.request("GET", "http://httpbin.air32.cn/range/1024", nil, nil, {debug=false}).wait()
     log.info("http3", code, json.encode(headers), body)
 
-    -- local code, resp = httpplus.request({url="http://quan.suning.com/getSysTime.do"})
-    -- log.info("http", code, resp.body:query())
+    local code, headers = http.request("GET", "http://httpbin.air32.cn/stream-bytes/20", nil, nil, {debug=true}).wait()
+    log.info("http4", code, headers, body)
 end)
 
 sys.run()

+ 10 - 0
bsp/pc/test/110.http_timeout/main.lua

@@ -0,0 +1,10 @@
+_G.sys = require("sys")
+
+sys.taskInit(function()
+    sys.wait(1000)
+    log.info("http_timeout", "start", "5s timeout test")
+    local code, headers, body = http.request("GET", "http://httpbin.air32.cn/delay/10", {timeout=5000}).wait()
+    log.info("http_timeout", code, body)
+end)
+
+sys.run()

+ 11 - 6
components/network/libhttp/luat_http_client.c

@@ -355,7 +355,7 @@ static int on_headers_complete(http_parser* parser){
 static int on_body(http_parser* parser, const char *at, size_t length){
 	luat_http_ctrl_t *http_ctrl =(luat_http_ctrl_t *)parser->data;
 	if (length > 128) {
-		LLOGD("on_body first 512byte:%.*s", 128, at);
+		LLOGD("on_body first 128byte:%.*s", 128, at);
 	} else {
 		LLOGD("on_body:%.*s",length,at);
 	}
@@ -435,7 +435,11 @@ static int on_body(http_parser* parser, const char *at, size_t length){
 	}
 	if (http_ctrl->resp_content_len > 0 && http_ctrl->body_len >= http_ctrl->resp_content_len) {
 		http_ctrl->http_body_is_finally = 1;
-		LLOGI("http body recv done by content_length");
+		LLOGD("http body recv done by content_length");
+		http_close_nw(http_ctrl);
+	}
+	else if (http_ctrl->http_body_is_finally) {
+		LLOGD("http body recv done by chunked end");
 		http_close_nw(http_ctrl);
 	}
     return 0;
@@ -500,10 +504,11 @@ static int on_message_complete(http_parser* parser){
 
 static int on_chunk_header(http_parser* parser){
 	luat_http_ctrl_t *http_ctrl =(luat_http_ctrl_t *)parser->data;
-	LLOGD("on_chunk_header");
-	LLOGD("content_length:%lld",parser->content_length);
-	// luat_http_ctrl_t *http_ctrl =(luat_http_ctrl_t *)parser->data;
-	// http_ctrl->is_chunk = 1;
+	LLOGD("on_chunk_header content_length:%lld",parser->content_length);
+	if (parser->content_length == 0){
+		http_ctrl->http_body_is_finally = 1;
+		http_close_nw(http_ctrl);
+	}
     return 0;
 }
 

+ 1 - 1
components/network/libhttp/luat_lib_http.c

@@ -483,7 +483,7 @@ exit:
 }
 
 void luat_http_client_onevent(luat_http_ctrl_t *http_ctrl, int error_code, int arg) {
-	LLOGI("luat_http_client_onevent %p %d", http_ctrl, error_code);
+	LLOGD("luat_http_client_onevent %p %d", http_ctrl, error_code);
 	if (!http_ctrl->luatos_mode) return;
 	if (http_ctrl->timeout_timer && error_code != HTTP_CALLBACK){
 		luat_stop_rtos_timer(http_ctrl->timeout_timer);