Fix bug for HTTP write jsonp for srs-console.

pull/1568/head
winlin 5 years ago
parent 40f6ecaee2
commit a35a7f915e

@ -568,11 +568,11 @@ int srs_do_create_dir_recursively(string dir)
// create curren dir. // create curren dir.
// for srs-librtmp, @see https://github.com/ossrs/srs/issues/213 // 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; 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) { if (::mkdir(dir.c_str(), mode) < 0) {
#else
if (::_mkdir(dir.c_str()) < 0) {
#endif #endif
if (errno == EEXIST) { if (errno == EEXIST) {
return ERROR_SYSTEM_DIR_EXISTS; return ERROR_SYSTEM_DIR_EXISTS;

@ -187,7 +187,7 @@ int srs_hijack_io_set_recv_timeout(srs_hijack_io_t ctx, int64_t tm)
SrsBlockSyncSocket* skt = (SrsBlockSyncSocket*)ctx; SrsBlockSyncSocket* skt = (SrsBlockSyncSocket*)ctx;
#ifdef _WIN32 #ifdef _WIN32
DWORD tv = (DWORD)(timeout_us/1000); DWORD tv = (DWORD)(tm);
// To convert tv to const char* to make VS2015 happy. // To convert tv to const char* to make VS2015 happy.
if (setsockopt(skt->fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) == -1) { 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; SrsBlockSyncSocket* skt = (SrsBlockSyncSocket*)ctx;
#ifdef _WIN32 #ifdef _WIN32
DWORD tv = (DWORD)(timeout_us/1000); DWORD tv = (DWORD)(tm);
// To convert tv to const char* to make VS2015 happy. // To convert tv to const char* to make VS2015 happy.
if (setsockopt(skt->fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) == -1) { if (setsockopt(skt->fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) == -1) {

@ -160,7 +160,7 @@ public:
// w->header()->set_content_type("text/plain; charset=utf-8"); // w->header()->set_content_type("text/plain; charset=utf-8");
// w->header()->set_content_length(msg.length()); // w->header()->set_content_length(msg.length());
// w->write_header(SRS_CONSTS_HTTP_OK); // 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. // w->final_request(); // optional flush.
// Usage 2, response with HTTP code only, zero content length. // Usage 2, response with HTTP code only, zero content length.
// ISrsHttpResponseWriter* w; // create or get response. // ISrsHttpResponseWriter* w; // create or get response.

@ -699,7 +699,9 @@ srs_error_t SrsHttpResponseWriter::write(char* data, int size)
if (hdr->content_type().empty()) { if (hdr->content_type().empty()) {
hdr->set_content_type("text/plain; charset=utf-8"); 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); write_header(SRS_CONSTS_HTTP_OK);
} }

@ -192,12 +192,41 @@ VOID TEST(ProtocolHTTPTest, ResponseDetect)
VOID TEST(ProtocolHTTPTest, ResponseWriter) 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 directly write string, response with content-length.
if (true) { if (true) {
MockResponseWriter w; MockResponseWriter w;
char msg[] = "Hello, world!"; 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); __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_type("text/plain; charset=utf-8");
w.header()->set_content_length(sizeof(msg) - 1); w.header()->set_content_length(sizeof(msg) - 1);
w.write_header(SRS_CONSTS_HTTP_OK); w.write_header(SRS_CONSTS_HTTP_OK);
w.write((char*)msg, sizeof(msg) - 1); HELPER_EXPECT_SUCCESS(w.write((char*)msg, sizeof(msg) - 1));
w.final_request(); HELPER_ASSERT_SUCCESS(w.final_request());
__MOCK_HTTP_EXPECT_STREQ(200, "Hello, world!", w); __MOCK_HTTP_EXPECT_STREQ(200, "Hello, world!", w);
} }
@ -223,7 +252,7 @@ VOID TEST(ProtocolHTTPTest, ResponseWriter)
w.header()->set_content_length(0); w.header()->set_content_length(0);
w.write_header(SRS_CONSTS_HTTP_OK); w.write_header(SRS_CONSTS_HTTP_OK);
w.final_request(); HELPER_ASSERT_SUCCESS(w.final_request());
__MOCK_HTTP_EXPECT_STREQ(200, "", w); __MOCK_HTTP_EXPECT_STREQ(200, "", w);
} }
@ -234,7 +263,7 @@ VOID TEST(ProtocolHTTPTest, ResponseWriter)
w.header()->set_content_length(0); w.header()->set_content_length(0);
w.write_header(SRS_CONSTS_HTTP_OK); w.write_header(SRS_CONSTS_HTTP_OK);
w.write(NULL, 0); HELPER_EXPECT_SUCCESS(w.write(NULL, 0));
__MOCK_HTTP_EXPECT_STREQ(200, "", w); __MOCK_HTTP_EXPECT_STREQ(200, "", w);
} }
@ -245,9 +274,9 @@ VOID TEST(ProtocolHTTPTest, ResponseWriter)
w.header()->set_content_type("application/octet-stream"); w.header()->set_content_type("application/octet-stream");
w.write_header(SRS_CONSTS_HTTP_OK); w.write_header(SRS_CONSTS_HTTP_OK);
w.write((char*)"Hello", 5); HELPER_EXPECT_SUCCESS(w.write((char*)"Hello", 5));
w.write((char*)", world!", 8); HELPER_EXPECT_SUCCESS(w.write((char*)", world!", 8));
w.final_request(); 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); __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.header()->set_content_type("application/octet-stream");
w.write_header(SRS_CONSTS_HTTP_OK); w.write_header(SRS_CONSTS_HTTP_OK);
w.write((char*)"Hello, world!", 13); HELPER_EXPECT_SUCCESS(w.write((char*)"Hello, world!", 13));
w.final_request(); HELPER_ASSERT_SUCCESS(w.final_request());
__MOCK_HTTP_EXPECT_STREQ2(200, "d\r\nHello, world!\r\n0\r\n\r\n", w); __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.header()->set_content_type("application/octet-stream");
w.write_header(SRS_CONSTS_HTTP_OK); w.write_header(SRS_CONSTS_HTTP_OK);
w.write((char*)"Hello, world!", 13); HELPER_EXPECT_SUCCESS(w.write((char*)"Hello, world!", 13));
w.final_request(); HELPER_ASSERT_SUCCESS(w.final_request());
__MOCK_HTTP_EXPECT_STREQ2(200, "d\r\nHello, world!\r\n0\r\n\r\n", w); __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 directly write empty string, sent an empty response with content-length 0
if (true) { if (true) {
MockResponseWriter w; MockResponseWriter w;
w.write(NULL, 0); HELPER_EXPECT_SUCCESS(w.write(NULL, 0));
__MOCK_HTTP_EXPECT_STREQ(200, "", w); __MOCK_HTTP_EXPECT_STREQ(200, "", w);
} }
// If directly final request, response with EOF of chunked. // If directly final request, response with EOF of chunked.
if (true) { if (true) {
MockResponseWriter w; MockResponseWriter w;
w.final_request(); HELPER_ASSERT_SUCCESS(w.final_request());
__MOCK_HTTP_EXPECT_STREQ2(200, "0\r\n\r\n", w); __MOCK_HTTP_EXPECT_STREQ2(200, "0\r\n\r\n", w);
} }
} }

Loading…
Cancel
Save