From df9efb6486b0754f40ae15a849b45d8dddecb802 Mon Sep 17 00:00:00 2001 From: winlin Date: Thu, 2 Jul 2020 16:20:32 +0800 Subject: [PATCH] RTC: Infinite chunk handle read error as EOF. --- trunk/src/protocol/srs_service_http_conn.cpp | 10 +++++++- trunk/src/utest/srs_utest_service.cpp | 26 ++++++++++++++++++-- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/trunk/src/protocol/srs_service_http_conn.cpp b/trunk/src/protocol/srs_service_http_conn.cpp index dccca73a6..e161d167d 100644 --- a/trunk/src/protocol/srs_service_http_conn.cpp +++ b/trunk/src/protocol/srs_service_http_conn.cpp @@ -966,7 +966,15 @@ srs_error_t SrsHttpResponseReader::read(void* data, size_t nb_data, ssize_t* nb_ // Infinite chunked mode. // If not chunked encoding, and no content-length, it's infinite chunked. // In this mode, all body is data and never EOF util socket closed. - return read_specified(data, nb_data, nb_read); + if ((err = read_specified(data, nb_data, nb_read)) != srs_success) { + // For infinite chunked, the socket close event is EOF. + if (srs_error_code(err) == ERROR_SOCKET_READ) { + srs_freep(err); is_eof = true; + return err; + } + } + + return err; } srs_error_t SrsHttpResponseReader::read_chunked(void* data, size_t nb_data, ssize_t* nb_read) diff --git a/trunk/src/utest/srs_utest_service.cpp b/trunk/src/utest/srs_utest_service.cpp index 497c48829..b4e85c591 100644 --- a/trunk/src/utest/srs_utest_service.cpp +++ b/trunk/src/utest/srs_utest_service.cpp @@ -613,7 +613,6 @@ VOID TEST(HTTPServerTest, ContentLength) { srs_error_t err; - // For infinite chunked mode, all data is content. if (true) { MockBufferIO io; io.append("HTTP/1.1 200 OK\r\nContent-Length: 11\r\n\r\n"); @@ -642,7 +641,6 @@ VOID TEST(HTTPServerTest, HTTPChunked) { srs_error_t err; - // For infinite chunked mode, all data is content. if (true) { MockBufferIO io; io.append("HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n"); @@ -694,6 +692,30 @@ VOID TEST(HTTPServerTest, InfiniteChunked) HELPER_ASSERT_SUCCESS(r->read(buf, 8, &nread)); EXPECT_EQ(8, nread); EXPECT_STREQ("\r\nWorld!", buf); + + EXPECT_FALSE(r->eof()); + } + + // If read error, it's EOF. + if (true) { + MockBufferIO io; + io.append("HTTP/1.1 200 OK\r\n\r\n"); + + SrsHttpParser hp; HELPER_ASSERT_SUCCESS(hp.initialize(HTTP_RESPONSE, false)); + ISrsHttpMessage* msg = NULL; HELPER_ASSERT_SUCCESS(hp.parse_message(&io, &msg)); + + char buf[32]; ssize_t nread = 0; + ISrsHttpResponseReader* r = msg->body_reader(); + + io.append("Hello"); + HELPER_ARRAY_INIT(buf, sizeof(buf), 0); + HELPER_ASSERT_SUCCESS(r->read(buf, 10, &nread)); + EXPECT_EQ(5, nread); + EXPECT_STREQ("Hello", buf); + + io.in_err = srs_error_new(ERROR_SOCKET_READ, "EOF"); + HELPER_ASSERT_SUCCESS(r->read(buf, 10, &nread)); + EXPECT_TRUE(r->eof()); } }