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: nodejs#12937
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
  • Loading branch information
Eugene Ostroukhov authored and Olivier Martin committed May 19, 2017
1 parent b4407ef commit 7d6a0fd
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 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) {
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 {
#if DUMP_READS
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 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,
const std::string& path,
bool* should_continue);
static const char SERVER_CLOSE_FRAME[] = {'\x88', '\x00'};


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

0 comments on commit 7d6a0fd

Please sign in to comment.