Skip to content

Commit

Permalink
inspector: handle socket close before close frame
Browse files Browse the repository at this point in the history
This change handles clients that respond to close request with a TCP
close instead of close response.

PR-URL: #12937
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
  • Loading branch information
Eugene Ostroukhov committed May 12, 2017
1 parent 642bd4d commit 7c3a23b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/inspector_socket.cc
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -335,6 +335,10 @@ static void websockets_data_cb(uv_stream_t* stream, ssize_t nread,
if (!inspector->shutting_down && inspector->ws_state->read_cb) { if (!inspector->shutting_down && inspector->ws_state->read_cb) {
inspector->ws_state->read_cb(stream, nread, nullptr); inspector->ws_state->read_cb(stream, nread, nullptr);
} }
if (inspector->ws_state->close_sent &&
!inspector->ws_state->received_close) {
shutdown_complete(inspector); // invoke callback
}
} else { } else {
#if DUMP_READS #if DUMP_READS
printf("%s read %ld bytes\n", __FUNCTION__, nread); printf("%s read %ld bytes\n", __FUNCTION__, nread);
Expand Down
32 changes: 31 additions & 1 deletion test/cctest/test_inspector_socket.cc
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ static std::string last_path; // NOLINT(runtime/string)
static void (*handshake_delegate)(enum inspector_handshake_event state, static void (*handshake_delegate)(enum inspector_handshake_event state,
const std::string& path, const std::string& path,
bool* should_continue); bool* should_continue);
static const char SERVER_CLOSE_FRAME[] = {'\x88', '\x00'};



struct read_expects { struct read_expects {
const char* expected; const char* expected;
Expand Down Expand Up @@ -879,7 +881,6 @@ TEST_F(InspectorSocketTest, Send1Mb) {
// 3. Close // 3. Close
const char CLIENT_CLOSE_FRAME[] = {'\x88', '\x80', '\x2D', const char CLIENT_CLOSE_FRAME[] = {'\x88', '\x80', '\x2D',
'\x0E', '\x1E', '\xFA'}; '\x0E', '\x1E', '\xFA'};
const char SERVER_CLOSE_FRAME[] = {'\x88', '\x00'};
do_write(CLIENT_CLOSE_FRAME, sizeof(CLIENT_CLOSE_FRAME)); do_write(CLIENT_CLOSE_FRAME, sizeof(CLIENT_CLOSE_FRAME));
expect_on_client(SERVER_CLOSE_FRAME, sizeof(SERVER_CLOSE_FRAME)); expect_on_client(SERVER_CLOSE_FRAME, sizeof(SERVER_CLOSE_FRAME));
GTEST_ASSERT_EQ(0, uv_is_active( GTEST_ASSERT_EQ(0, uv_is_active(
Expand All @@ -906,4 +907,33 @@ TEST_F(InspectorSocketTest, ErrorCleansUpTheSocket) {
EXPECT_EQ(UV_EPROTO, err); EXPECT_EQ(UV_EPROTO, err);
} }


static void ServerClosedByClient_cb(InspectorSocket* socket, int code) {
*static_cast<bool*>(socket->data) = true;
}

TEST_F(InspectorSocketTest, NoCloseResponseFromClinet) {
ASSERT_TRUE(connected);
ASSERT_FALSE(inspector_ready);
do_write(const_cast<char*>(HANDSHAKE_REQ), sizeof(HANDSHAKE_REQ) - 1);
SPIN_WHILE(!inspector_ready);
expect_handshake();

// 2. Brief exchange
const char SERVER_MESSAGE[] = "abcd";
const char CLIENT_FRAME[] = {'\x81', '\x04', 'a', 'b', 'c', 'd'};
inspector_write(&inspector, SERVER_MESSAGE, sizeof(SERVER_MESSAGE) - 1);
expect_on_client(CLIENT_FRAME, sizeof(CLIENT_FRAME));

bool closed = false;

inspector.data = &closed;
inspector_close(&inspector, ServerClosedByClient_cb);
expect_on_client(SERVER_CLOSE_FRAME, sizeof(SERVER_CLOSE_FRAME));
uv_close(reinterpret_cast<uv_handle_t*>(&client_socket), nullptr);
SPIN_WHILE(!closed);
inspector.data = nullptr;
GTEST_ASSERT_EQ(0, uv_is_active(
reinterpret_cast<uv_handle_t*>(&client_socket)));
}

} // anonymous namespace } // anonymous namespace

0 comments on commit 7c3a23b

Please sign in to comment.