Skip to content

Commit 85f7153

Browse files
authored
Strict session ID (swoole#3993)
* Strict session ID * optimize code
1 parent 36a1dc2 commit 85f7153

File tree

4 files changed

+26
-32
lines changed

4 files changed

+26
-32
lines changed

ext-src/swoole_http_response.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ extern "C" {
3939
#endif
4040

4141
using swoole::coroutine::Socket;
42+
using swoole::Server;
43+
using swoole::Connection;
4244
using swoole::substr_len;
4345
using http_response = swoole::http::Response;
4446
using http_context = swoole::http::Context;
@@ -843,8 +845,8 @@ void swoole_http_response_end(http_context *ctx, zval *zdata, zval *return_value
843845
_skip_copy:
844846
#endif
845847
if (ctx->upgrade && !ctx->co_socket) {
846-
swServer *serv = (swServer *) ctx->private_data;
847-
swConnection *conn = serv->get_connection_by_session_id(ctx->fd);
848+
Server *serv = (Server *) ctx->private_data;
849+
Connection *conn = serv->get_connection_verify(ctx->fd);
848850
if (conn && conn->websocket_status == WEBSOCKET_STATUS_HANDSHAKE) {
849851
if (ctx->response.status == 101) {
850852
conn->websocket_status = WEBSOCKET_STATUS_ACTIVE;

ext-src/swoole_server.cc

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3645,8 +3645,8 @@ static PHP_METHOD(swoole_server, bind) {
36453645
RETURN_FALSE;
36463646
}
36473647

3648-
Connection *conn = serv->get_connection_by_session_id(fd);
3649-
if (conn == nullptr || conn->active == 0) {
3648+
Connection *conn = serv->get_connection_verify(fd);
3649+
if (conn == nullptr) {
36503650
RETURN_FALSE;
36513651
}
36523652

@@ -3751,10 +3751,10 @@ static PHP_METHOD(swoole_server, getClientList) {
37513751
RETURN_FALSE;
37523752
}
37533753

3754-
zend_long start_fd = 0;
3754+
zend_long start_session_id = 0;
37553755
zend_long find_count = 10;
37563756

3757-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|ll", &start_fd, &find_count) == FAILURE) {
3757+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|ll", &start_session_id, &find_count) == FAILURE) {
37583758
RETURN_FALSE;
37593759
}
37603760

@@ -3766,12 +3766,13 @@ static PHP_METHOD(swoole_server, getClientList) {
37663766

37673767
// copy it out to avoid being overwritten by other processes
37683768
int serv_max_fd = serv->get_maxfd();
3769+
int start_fd;
37693770

3770-
if (start_fd == 0) {
3771+
if (start_session_id == 0) {
37713772
start_fd = serv->get_minfd();
37723773
} else {
3773-
Connection *conn = serv->get_connection_by_session_id(start_fd);
3774-
if (!conn) {
3774+
Connection *conn = serv->get_connection_verify(start_session_id);
3775+
if (!serv->is_valid_connection(conn)) {
37753776
RETURN_FALSE;
37763777
}
37773778
start_fd = conn->fd;
@@ -3786,10 +3787,10 @@ static PHP_METHOD(swoole_server, getClientList) {
37863787
Connection *conn;
37873788

37883789
for (; fd <= serv_max_fd; fd++) {
3789-
swTrace("maxfd=%d, fd=%d, find_count=%ld, start_fd=%ld", serv_max_fd, fd, find_count, start_fd);
3790+
swTrace("maxfd=%d, fd=%d, find_count=%ld, start_fd=%ld", serv_max_fd, fd, find_count, start_session_id);
37903791
conn = serv->get_connection(fd);
37913792

3792-
if (conn->active && !conn->closed) {
3793+
if (serv->is_valid_connection(conn)) {
37933794
#ifdef SW_USE_OPENSSL
37943795
if (conn->ssl && !conn->ssl_ready) {
37953796
continue;
@@ -3842,18 +3843,14 @@ static PHP_METHOD(swoole_server, exists) {
38423843
RETURN_FALSE;
38433844
}
38443845

3845-
zend_long fd;
3846+
zend_long session_id;
38463847

38473848
ZEND_PARSE_PARAMETERS_START(1, 1)
3848-
Z_PARAM_LONG(fd)
3849+
Z_PARAM_LONG(session_id)
38493850
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
38503851

3851-
Connection *conn = serv->get_connection_by_session_id(fd);
3852-
if (!conn) {
3853-
RETURN_FALSE;
3854-
}
3855-
// connection is closed
3856-
if (conn->active == 0 || conn->closed) {
3852+
Connection *conn = serv->get_connection_verify(session_id);
3853+
if (!conn || conn->closed) {
38573854
RETURN_FALSE;
38583855
} else {
38593856
RETURN_TRUE;
@@ -3867,19 +3864,15 @@ static PHP_METHOD(swoole_server, protect) {
38673864
RETURN_FALSE;
38683865
}
38693866

3870-
zend_long fd;
3867+
zend_long session_id;
38713868
zend_bool value = 1;
38723869

3873-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "l|b", &fd, &value) == FAILURE) {
3870+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "l|b", &session_id, &value) == FAILURE) {
38743871
RETURN_FALSE;
38753872
}
38763873

3877-
Connection *conn = serv->get_connection_by_session_id(fd);
3878-
if (!conn) {
3879-
RETURN_FALSE;
3880-
}
3881-
// connection is closed
3882-
if (conn->active == 0 || conn->closed) {
3874+
Connection *conn = serv->get_connection_verify(session_id);
3875+
if (!conn || conn->closed) {
38833876
RETURN_FALSE;
38843877
} else {
38853878
conn->protect = value;

ext-src/swoole_websocket_server.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -907,15 +907,15 @@ static PHP_METHOD(swoole_websocket_server, isEstablished) {
907907
RETURN_FALSE;
908908
}
909909

910-
zend_long fd;
910+
zend_long session_id;
911911

912-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &fd) == FAILURE) {
912+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &session_id) == FAILURE) {
913913
RETURN_FALSE;
914914
}
915915

916-
Connection *conn = serv->get_connection_by_session_id(fd);
916+
Connection *conn = serv->get_connection_verify(session_id);
917917
// not isEstablished
918-
if (!conn || conn->active == 0 || conn->closed || conn->websocket_status < WEBSOCKET_STATUS_ACTIVE) {
918+
if (!conn || conn->closed || conn->websocket_status < WEBSOCKET_STATUS_ACTIVE) {
919919
RETURN_FALSE;
920920
} else {
921921
RETURN_TRUE;

include/swoole_server.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1136,7 +1136,6 @@ class Server {
11361136
Connection *conn = get_connection_verify_no_ssl(session_id);
11371137
#ifdef SW_USE_OPENSSL
11381138
if (conn && conn->ssl && !conn->ssl_ready) {
1139-
swoole_error_log(SW_LOG_NOTICE, SW_ERROR_SSL_NOT_READY, "SSL not ready");
11401139
return nullptr;
11411140
}
11421141
#endif

0 commit comments

Comments
 (0)