diff --git a/trunk/src/kernel/srs_kernel_utility.cpp b/trunk/src/kernel/srs_kernel_utility.cpp index 4fab6985a..25a1683b8 100644 --- a/trunk/src/kernel/srs_kernel_utility.cpp +++ b/trunk/src/kernel/srs_kernel_utility.cpp @@ -568,11 +568,11 @@ int srs_do_create_dir_recursively(string dir) // create curren dir. // for srs-librtmp, @see https://github.com/ossrs/srs/issues/213 -#ifndef _WIN32 +#ifdef _WIN32 + if (::_mkdir(dir.c_str()) < 0) { +#else mode_t mode = S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IWGRP|S_IXGRP|S_IROTH|S_IXOTH; if (::mkdir(dir.c_str(), mode) < 0) { -#else - if (::_mkdir(dir.c_str()) < 0) { #endif if (errno == EEXIST) { return ERROR_SYSTEM_DIR_EXISTS; diff --git a/trunk/src/libs/srs_lib_simple_socket.cpp b/trunk/src/libs/srs_lib_simple_socket.cpp index fb3249cfc..127d99200 100644 --- a/trunk/src/libs/srs_lib_simple_socket.cpp +++ b/trunk/src/libs/srs_lib_simple_socket.cpp @@ -187,7 +187,7 @@ int srs_hijack_io_set_recv_timeout(srs_hijack_io_t ctx, int64_t tm) SrsBlockSyncSocket* skt = (SrsBlockSyncSocket*)ctx; #ifdef _WIN32 - DWORD tv = (DWORD)(timeout_us/1000); + DWORD tv = (DWORD)(tm); // To convert tv to const char* to make VS2015 happy. if (setsockopt(skt->fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) == -1) { @@ -229,7 +229,7 @@ int srs_hijack_io_set_send_timeout(srs_hijack_io_t ctx, int64_t tm) SrsBlockSyncSocket* skt = (SrsBlockSyncSocket*)ctx; #ifdef _WIN32 - DWORD tv = (DWORD)(timeout_us/1000); + DWORD tv = (DWORD)(tm); // To convert tv to const char* to make VS2015 happy. if (setsockopt(skt->fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) == -1) { diff --git a/trunk/src/protocol/srs_http_stack.hpp b/trunk/src/protocol/srs_http_stack.hpp index 9044f5549..b880e60ba 100644 --- a/trunk/src/protocol/srs_http_stack.hpp +++ b/trunk/src/protocol/srs_http_stack.hpp @@ -160,7 +160,7 @@ public: // w->header()->set_content_type("text/plain; charset=utf-8"); // w->header()->set_content_length(msg.length()); // w->write_header(SRS_CONSTS_HTTP_OK); -// w->write((char*)msg.data(), (int)msg.length()); +// w->write((char*)msg.data(), (int)msg.length()); // write N times, N>0 // w->final_request(); // optional flush. // Usage 2, response with HTTP code only, zero content length. // ISrsHttpResponseWriter* w; // create or get response. diff --git a/trunk/src/service/srs_service_http_conn.cpp b/trunk/src/service/srs_service_http_conn.cpp index 9503c19d0..3d97faa98 100644 --- a/trunk/src/service/srs_service_http_conn.cpp +++ b/trunk/src/service/srs_service_http_conn.cpp @@ -699,7 +699,9 @@ srs_error_t SrsHttpResponseWriter::write(char* data, int size) if (hdr->content_type().empty()) { hdr->set_content_type("text/plain; charset=utf-8"); } - hdr->set_content_length(size); + if (hdr->content_length() == -1) { + hdr->set_content_length(size); + } write_header(SRS_CONSTS_HTTP_OK); } diff --git a/trunk/src/utest/srs_utest_http.cpp b/trunk/src/utest/srs_utest_http.cpp index 778db7e63..425020adf 100644 --- a/trunk/src/utest/srs_utest_http.cpp +++ b/trunk/src/utest/srs_utest_http.cpp @@ -192,12 +192,41 @@ VOID TEST(ProtocolHTTPTest, ResponseDetect) VOID TEST(ProtocolHTTPTest, ResponseWriter) { + srs_error_t err; + + // Directly final_request, should work. + if (true) { + MockResponseWriter w; + w.header()->set_content_length(0); + HELPER_ASSERT_SUCCESS(w.final_request()); + } + + // Directly final_request, should work. + if (true) { + MockResponseWriter w; + HELPER_ASSERT_SUCCESS(w.final_request()); + } + + // When content-length is set, we could write multiple parts. + if (true) { + MockResponseWriter w; + + char msg[] = "Hello, world!"; + w.header()->set_content_length(sizeof(msg) - 1); + HELPER_EXPECT_SUCCESS(w.write((char*)msg, 5)); + HELPER_EXPECT_SUCCESS(w.write((char*)(msg+5), 2)); + HELPER_EXPECT_SUCCESS(w.write((char*)(msg+7), 5)); + HELPER_EXPECT_SUCCESS(w.write((char*)(msg+12), 1)); + + __MOCK_HTTP_EXPECT_STREQ(200, "Hello, world!", w); + } + // If directly write string, response with content-length. if (true) { MockResponseWriter w; char msg[] = "Hello, world!"; - w.write((char*)msg, sizeof(msg) - 1); + HELPER_EXPECT_SUCCESS(w.write((char*)msg, sizeof(msg) - 1)); __MOCK_HTTP_EXPECT_STREQ(200, "Hello, world!", w); } @@ -211,8 +240,8 @@ VOID TEST(ProtocolHTTPTest, ResponseWriter) w.header()->set_content_type("text/plain; charset=utf-8"); w.header()->set_content_length(sizeof(msg) - 1); w.write_header(SRS_CONSTS_HTTP_OK); - w.write((char*)msg, sizeof(msg) - 1); - w.final_request(); + HELPER_EXPECT_SUCCESS(w.write((char*)msg, sizeof(msg) - 1)); + HELPER_ASSERT_SUCCESS(w.final_request()); __MOCK_HTTP_EXPECT_STREQ(200, "Hello, world!", w); } @@ -223,7 +252,7 @@ VOID TEST(ProtocolHTTPTest, ResponseWriter) w.header()->set_content_length(0); w.write_header(SRS_CONSTS_HTTP_OK); - w.final_request(); + HELPER_ASSERT_SUCCESS(w.final_request()); __MOCK_HTTP_EXPECT_STREQ(200, "", w); } @@ -234,7 +263,7 @@ VOID TEST(ProtocolHTTPTest, ResponseWriter) w.header()->set_content_length(0); w.write_header(SRS_CONSTS_HTTP_OK); - w.write(NULL, 0); + HELPER_EXPECT_SUCCESS(w.write(NULL, 0)); __MOCK_HTTP_EXPECT_STREQ(200, "", w); } @@ -245,9 +274,9 @@ VOID TEST(ProtocolHTTPTest, ResponseWriter) w.header()->set_content_type("application/octet-stream"); w.write_header(SRS_CONSTS_HTTP_OK); - w.write((char*)"Hello", 5); - w.write((char*)", world!", 8); - w.final_request(); + HELPER_EXPECT_SUCCESS(w.write((char*)"Hello", 5)); + HELPER_EXPECT_SUCCESS(w.write((char*)", world!", 8)); + HELPER_ASSERT_SUCCESS(w.final_request()); __MOCK_HTTP_EXPECT_STREQ2(200, "5\r\nHello\r\n8\r\n, world!\r\n0\r\n\r\n", w); } @@ -256,8 +285,8 @@ VOID TEST(ProtocolHTTPTest, ResponseWriter) w.header()->set_content_type("application/octet-stream"); w.write_header(SRS_CONSTS_HTTP_OK); - w.write((char*)"Hello, world!", 13); - w.final_request(); + HELPER_EXPECT_SUCCESS(w.write((char*)"Hello, world!", 13)); + HELPER_ASSERT_SUCCESS(w.final_request()); __MOCK_HTTP_EXPECT_STREQ2(200, "d\r\nHello, world!\r\n0\r\n\r\n", w); } @@ -266,8 +295,8 @@ VOID TEST(ProtocolHTTPTest, ResponseWriter) w.header()->set_content_type("application/octet-stream"); w.write_header(SRS_CONSTS_HTTP_OK); - w.write((char*)"Hello, world!", 13); - w.final_request(); + HELPER_EXPECT_SUCCESS(w.write((char*)"Hello, world!", 13)); + HELPER_ASSERT_SUCCESS(w.final_request()); __MOCK_HTTP_EXPECT_STREQ2(200, "d\r\nHello, world!\r\n0\r\n\r\n", w); } @@ -275,14 +304,14 @@ VOID TEST(ProtocolHTTPTest, ResponseWriter) // If directly write empty string, sent an empty response with content-length 0 if (true) { MockResponseWriter w; - w.write(NULL, 0); + HELPER_EXPECT_SUCCESS(w.write(NULL, 0)); __MOCK_HTTP_EXPECT_STREQ(200, "", w); } // If directly final request, response with EOF of chunked. if (true) { MockResponseWriter w; - w.final_request(); + HELPER_ASSERT_SUCCESS(w.final_request()); __MOCK_HTTP_EXPECT_STREQ2(200, "0\r\n\r\n", w); } }