Skip to content

Commit e2fd804

Browse files
ossrs-aiwinlinvip
authored andcommitted
AI: API: Change pagination default count to 10, minimum 1. v7.0.128
1 parent a3a2fa5 commit e2fd804

File tree

5 files changed

+129
-9
lines changed

5 files changed

+129
-9
lines changed

trunk/doc/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ The changelog for SRS.
77
<a name="v7-changes"></a>
88

99
## SRS 7.0 Changelog
10+
* v7.0, 2025-11-18, AI: API: Change pagination default count to 10, minimum 1. v7.0.128
1011
* v7.0, 2025-11-14, AI: Fix race condition causing immediate deletion of new sources. v7.0.127 (#4449)
1112
* v7.0, 2025-11-11, AI: WebRTC: Support optional msid attribute per RFC 8830. v7.0.126 (#4570)
1213
* v7.0, 2025-11-11, AI: SRT: Stop TS parsing after codec detection. v7.0.125 (#4569)

trunk/src/app/srs_app_http_api.cpp

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,18 @@ srs_error_t srs_api_response_code(ISrsHttpResponseWriter *w, ISrsHttpMessage *r,
147147
return srs_api_response_jsonp_code(w, callback, code);
148148
}
149149

150+
void srs_api_parse_pagination(ISrsHttpMessage *r, int &start, int &count)
151+
{
152+
std::string rstart = r->query_get("start");
153+
std::string rcount = r->query_get("count");
154+
155+
// Parse start parameter, default to 0, minimum 0.
156+
start = srs_max(0, atoi(rstart.c_str()));
157+
158+
// Parse count parameter, default to 10, minimum 1.
159+
count = rcount.empty() ? 10 : srs_max(1, atoi(rcount.c_str()));
160+
}
161+
150162
// @remark we will free the code.
151163
srs_error_t srs_api_response_code(ISrsHttpResponseWriter *w, ISrsHttpMessage *r, srs_error_t code)
152164
{
@@ -794,10 +806,8 @@ srs_error_t SrsGoApiStreams::serve_http(ISrsHttpResponseWriter *w, ISrsHttpMessa
794806
SrsJsonArray *data = SrsJsonAny::array();
795807
obj->set("streams", data);
796808

797-
std::string rstart = r->query_get("start");
798-
std::string rcount = r->query_get("count");
799-
int start = srs_max(0, atoi(rstart.c_str()));
800-
int count = srs_max(1, atoi(rcount.c_str()));
809+
int start, count;
810+
srs_api_parse_pagination(r, start, count);
801811
if ((err = stat_->dumps_streams(data, start, count)) != srs_success) {
802812
int code = srs_error_code(err);
803813
srs_freep(err);
@@ -866,10 +876,8 @@ srs_error_t SrsGoApiClients::serve_http(ISrsHttpResponseWriter *w, ISrsHttpMessa
866876
SrsJsonArray *data = SrsJsonAny::array();
867877
obj->set("clients", data);
868878

869-
std::string rstart = r->query_get("start");
870-
std::string rcount = r->query_get("count");
871-
int start = srs_max(0, atoi(rstart.c_str()));
872-
int count = srs_max(1, atoi(rcount.c_str()));
879+
int start, count;
880+
srs_api_parse_pagination(r, start, count);
873881
if ((err = stat_->dumps_clients(data, start, count)) != srs_success) {
874882
int code = srs_error_code(err);
875883
srs_freep(err);

trunk/src/app/srs_app_http_api.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ extern srs_error_t srs_api_response(ISrsHttpResponseWriter *w, ISrsHttpMessage *
3535
extern srs_error_t srs_api_response_code(ISrsHttpResponseWriter *w, ISrsHttpMessage *r, int code);
3636
extern srs_error_t srs_api_response_code(ISrsHttpResponseWriter *w, ISrsHttpMessage *r, srs_error_t code);
3737

38+
// Parse pagination parameters from HTTP request query string.
39+
// @param r The HTTP request message.
40+
// @param start Output parameter for start index, defaults to 0, minimum 0.
41+
// @param count Output parameter for count, defaults to 10, minimum 1.
42+
extern void srs_api_parse_pagination(ISrsHttpMessage *r, int &start, int &count);
43+
3844
// For http root.
3945
class SrsGoApiRoot : public ISrsHttpHandler
4046
{

trunk/src/core/srs_core_version7.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99

1010
#define VERSION_MAJOR 7
1111
#define VERSION_MINOR 0
12-
#define VERSION_REVISION 127
12+
#define VERSION_REVISION 128
1313

1414
#endif

trunk/src/utest/srs_utest_ai24.cpp

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <srs_app_config.hpp>
99
#include <srs_app_fragment.hpp>
1010
#include <srs_app_hls.hpp>
11+
#include <srs_app_http_api.hpp>
1112
#include <srs_app_http_hooks.hpp>
1213
#include <srs_app_rtc_source.hpp>
1314
#include <srs_app_server.hpp>
@@ -21,6 +22,7 @@
2122
#include <srs_protocol_utility.hpp>
2223
#include <srs_utest_manual_kernel.hpp>
2324
#include <srs_utest_manual_mock.hpp>
25+
#include <srs_utest_ai16.hpp>
2426

2527
#ifdef SRS_FFMPEG_FIT
2628
#include <srs_app_rtc_codec.hpp>
@@ -1311,3 +1313,106 @@ VOID TEST(SdpTest, ParseLibdatachannelSdpFromIssue4570)
13111313
}
13121314
EXPECT_TRUE(found_audio_ssrc);
13131315
}
1316+
1317+
// Test: srs_api_parse_pagination with various input scenarios
1318+
VOID TEST(HttpApiPaginationTest, ParsePagination)
1319+
{
1320+
int start, count;
1321+
1322+
// Test 1: Default values (no query parameters)
1323+
if (true) {
1324+
MockHttpMessageForApiResponse mock_msg;
1325+
srs_api_parse_pagination(&mock_msg, start, count);
1326+
EXPECT_EQ(0, start);
1327+
EXPECT_EQ(10, count);
1328+
}
1329+
1330+
// Test 2: Valid start and count
1331+
if (true) {
1332+
MockHttpMessageForApiResponse mock_msg;
1333+
mock_msg.query_params_["start"] = "5";
1334+
mock_msg.query_params_["count"] = "20";
1335+
srs_api_parse_pagination(&mock_msg, start, count);
1336+
EXPECT_EQ(5, start);
1337+
EXPECT_EQ(20, count);
1338+
}
1339+
1340+
// Test 3: Zero count (should use minimum 1)
1341+
if (true) {
1342+
MockHttpMessageForApiResponse mock_msg;
1343+
mock_msg.query_params_["start"] = "0";
1344+
mock_msg.query_params_["count"] = "0";
1345+
srs_api_parse_pagination(&mock_msg, start, count);
1346+
EXPECT_EQ(0, start);
1347+
EXPECT_EQ(1, count);
1348+
}
1349+
1350+
// Test 4: Negative start (should use minimum 0)
1351+
if (true) {
1352+
MockHttpMessageForApiResponse mock_msg;
1353+
mock_msg.query_params_["start"] = "-10";
1354+
mock_msg.query_params_["count"] = "5";
1355+
srs_api_parse_pagination(&mock_msg, start, count);
1356+
EXPECT_EQ(0, start);
1357+
EXPECT_EQ(5, count);
1358+
}
1359+
1360+
// Test 5: Negative count (should use minimum 1)
1361+
if (true) {
1362+
MockHttpMessageForApiResponse mock_msg;
1363+
mock_msg.query_params_["start"] = "10";
1364+
mock_msg.query_params_["count"] = "-5";
1365+
srs_api_parse_pagination(&mock_msg, start, count);
1366+
EXPECT_EQ(10, start);
1367+
EXPECT_EQ(1, count);
1368+
}
1369+
1370+
// Test 6: Empty count string (should use default 10)
1371+
if (true) {
1372+
MockHttpMessageForApiResponse mock_msg;
1373+
mock_msg.query_params_["start"] = "5";
1374+
mock_msg.query_params_["count"] = "";
1375+
srs_api_parse_pagination(&mock_msg, start, count);
1376+
EXPECT_EQ(5, start);
1377+
EXPECT_EQ(10, count);
1378+
}
1379+
1380+
// Test 7: Only start parameter
1381+
if (true) {
1382+
MockHttpMessageForApiResponse mock_msg;
1383+
mock_msg.query_params_["start"] = "15";
1384+
srs_api_parse_pagination(&mock_msg, start, count);
1385+
EXPECT_EQ(15, start);
1386+
EXPECT_EQ(10, count);
1387+
}
1388+
1389+
// Test 8: Only count parameter
1390+
if (true) {
1391+
MockHttpMessageForApiResponse mock_msg;
1392+
mock_msg.query_params_["count"] = "25";
1393+
srs_api_parse_pagination(&mock_msg, start, count);
1394+
EXPECT_EQ(0, start);
1395+
EXPECT_EQ(25, count);
1396+
}
1397+
1398+
// Test 9: Invalid (non-numeric) values
1399+
if (true) {
1400+
MockHttpMessageForApiResponse mock_msg;
1401+
mock_msg.query_params_["start"] = "abc";
1402+
mock_msg.query_params_["count"] = "xyz";
1403+
srs_api_parse_pagination(&mock_msg, start, count);
1404+
// atoi returns 0 for invalid strings
1405+
EXPECT_EQ(0, start);
1406+
EXPECT_EQ(1, count); // minimum enforced since atoi returns 0
1407+
}
1408+
1409+
// Test 10: Large values
1410+
if (true) {
1411+
MockHttpMessageForApiResponse mock_msg;
1412+
mock_msg.query_params_["start"] = "1000000";
1413+
mock_msg.query_params_["count"] = "500";
1414+
srs_api_parse_pagination(&mock_msg, start, count);
1415+
EXPECT_EQ(1000000, start);
1416+
EXPECT_EQ(500, count);
1417+
}
1418+
}

0 commit comments

Comments
 (0)