Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix JSONRPC connection issues on edge cases #234

Merged
merged 2 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/jsonrpc-remote-machine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,8 @@ static json jsonrpc_shutdown_handler(const json &j, mg_connection *con, http_han
jsonrpc_check_no_params(j);
con->is_draining = 1;
con->data[0] = 'X';
// Mark listen connection to be closed immediately so its port can be reused in subsequent rebind call
h->listen_connection->is_closing = 1;
return jsonrpc_response_ok(j);
}

Expand Down Expand Up @@ -1782,6 +1784,7 @@ static json jsonrpc_machine_verify_send_cmio_response_state_transition_handler(c
/// \param j JSON response object
void jsonrpc_http_reply(mg_connection *con, http_handler_data *h, const json &j) {
SLOG(trace) << h->server_address << " response is " << j.dump().data();
con->is_draining = 1;
return mg_http_reply(con, 200, "Access-Control-Allow-Origin: *\r\nContent-Type: application/json\r\n", "%s",
j.dump().data());
}
Expand All @@ -1790,6 +1793,7 @@ void jsonrpc_http_reply(mg_connection *con, http_handler_data *h, const json &j)
/// \param con Mongoose connection
void jsonrpc_send_empty_reply(mg_connection *con, http_handler_data *h) {
SLOG(trace) << h->server_address << " response is empty";
con->is_draining = 1;
return mg_http_reply(con, 200, "Access-Control-Allow-Origin: *\r\nContent-Type: application/json\r\n", "");
}

Expand Down Expand Up @@ -1895,6 +1899,7 @@ static void http_handler(mg_connection *con, int ev, void *ev_data, void *h_data
headers += "Access-Control-Allow-Methods: *\r\n";
headers += "Access-Control-Allow-Headers: *\r\n";
headers += "Access-Control-Max-Age: 0\r\n";
con->is_draining = 1;
mg_http_reply(con, 204, headers.c_str(), "");
return;
}
Expand All @@ -1903,6 +1908,7 @@ static void http_handler(mg_connection *con, int ev, void *ev_data, void *h_data
std::string headers;
headers += "Access-Control-Allow-Origin: *\r\n";
SLOG(trace) << h->server_address << " rejected unexpected \"" << method << "\" request";
con->is_draining = 1;
mg_http_reply(con, 405, headers.c_str(), "method not allowed");
return;
}
Expand All @@ -1912,6 +1918,7 @@ static void http_handler(mg_connection *con, int ev, void *ev_data, void *h_data
if (uri != "/") {
// anything else
SLOG(trace) << h->server_address << " rejected unexpected \"" << uri << "\" uri";
con->is_draining = 1;
mg_http_reply(con, 404, "Access-Control-Allow-Origin: *\r\n", "not found");
return;
}
Expand Down
2 changes: 0 additions & 2 deletions src/jsonrpc-virtual-machine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ struct http_request_data {

// Performs additional client socket configuration
static void setup_client_socket(struct mg_connection *c) {
#if defined(__APPLE__)
#if defined(SO_LINGER)
// Minimize socket close time by setting the linger time to 0.
// On macOS, it avoids accumulating socket in TIME_WAIT state, after rapid successive requests, which can consume
Expand All @@ -73,7 +72,6 @@ static void setup_client_socket(struct mg_connection *c) {
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
(void) setsockopt(socket, SOL_SOCKET, SO_LINGER, reinterpret_cast<char *>(&so_linger), sizeof(so_linger));
#endif
#endif
}

// Print HTTP response and signal that we're done
Expand Down