Skip to content

Commit bf1718b

Browse files
committed
Fix tests [9] --filter=[core] --verbose
1 parent 9439b5c commit bf1718b

File tree

1 file changed

+11
-67
lines changed

1 file changed

+11
-67
lines changed

core-tests/src/protocol/http2.cpp

Lines changed: 11 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "swoole_http2.h"
2626

2727
#include <nghttp2/nghttp2.h>
28+
#include <nghttp2/nghttp2ver.h>
2829

2930
using namespace swoole;
3031
using namespace std;
@@ -136,7 +137,6 @@ struct Http2Session {
136137
Server *server;
137138
std::unordered_map<int32_t, std::string> stream_paths;
138139
std::unordered_map<int32_t, std::string> stream_data;
139-
std::unordered_map<int32_t, std::shared_ptr<File>> file_data_sources;
140140

141141
Http2Session(SessionId _fd, Server *_serv) : fd(_fd), session(nullptr), server(_serv) {}
142142
~Http2Session() {
@@ -147,7 +147,6 @@ struct Http2Session {
147147
}
148148
};
149149

150-
// 错误处理宏
151150
#define CHECK_NGHTTP2(expr, error_msg) \
152151
do { \
153152
int rv = (expr); \
@@ -171,9 +170,7 @@ static ssize_t send_callback(nghttp2_session *session, const uint8_t *data, size
171170
return length;
172171
}
173172

174-
// 处理流关闭回调
175173
static int on_stream_close_callback(nghttp2_session *session, int32_t stream_id, uint32_t error_code, void *user_data) {
176-
// 当 HTTP/2 流关闭时清理资源
177174
return 0;
178175
}
179176

@@ -217,29 +214,23 @@ static int on_frame_recv_callback(nghttp2_session *session, const nghttp2_frame
217214
if (frame->headers.cat == NGHTTP2_HCAT_REQUEST) {
218215
swoole_trace_log(SW_TRACE_HTTP2, "Received HEADERS frame for stream %d", frame->hd.stream_id);
219216

220-
// 如果头部帧有 END_STREAM 标志,表示请求已完成(没有请求体)
221217
if (frame->hd.flags & NGHTTP2_FLAG_END_STREAM) {
222-
// 请求已完全接收,可以生成响应
223218
handle_request(session, frame->hd.stream_id, http2_session);
224219
}
225220
}
226221
break;
227222
case NGHTTP2_DATA:
228223
swoole_trace_log(SW_TRACE_HTTP2, "Received DATA frame for stream %d", frame->hd.stream_id);
229224

230-
// 如果数据帧有 END_STREAM 标志,表示请求已完成
231225
if (frame->hd.flags & NGHTTP2_FLAG_END_STREAM) {
232-
// 请求已完全接收,可以生成响应
233226
handle_request(session, frame->hd.stream_id, http2_session);
234227
}
235228
break;
236-
// ... 其他 case 分支保持不变
237229
}
238230

239231
return 0;
240232
}
241233

242-
// 处理数据块回调
243234
static int on_data_chunk_recv_callback(
244235
nghttp2_session *session, uint8_t flags, int32_t stream_id, const uint8_t *data, size_t len, void *user_data) {
245236
auto http2_session = static_cast<Http2Session *>(user_data);
@@ -252,7 +243,6 @@ static int on_data_chunk_recv_callback(
252243
return 0;
253244
}
254245

255-
// 添加优先级回调
256246
static int on_frame_not_send_callback(nghttp2_session *session,
257247
const nghttp2_frame *frame,
258248
int lib_error_code,
@@ -262,7 +252,6 @@ static int on_frame_not_send_callback(nghttp2_session *session,
262252
return 0;
263253
}
264254

265-
// 添加窗口更新回调
266255
static int on_frame_send_callback(nghttp2_session *session, const nghttp2_frame *frame, void *user_data) {
267256
if (frame->hd.type == NGHTTP2_WINDOW_UPDATE) {
268257
DEBUG() << "Window update sent: stream=" << frame->hd.stream_id
@@ -271,7 +260,6 @@ static int on_frame_send_callback(nghttp2_session *session, const nghttp2_frame
271260
return 0;
272261
}
273262

274-
// 字符串数据回调
275263
static ssize_t string_read_callback(nghttp2_session *session,
276264
int32_t stream_id,
277265
uint8_t *buf,
@@ -313,21 +301,17 @@ static void handle_request(nghttp2_session *session, int32_t stream_id, Http2Ses
313301
path.c_str(),
314302
request_body.length());
315303

304+
auto header_server = "nghttp2-server/" NGHTTP2_VERSION;
316305
// 准备响应头
317306
nghttp2_nv hdrs[] = {{(uint8_t *) ":status", (uint8_t *) "200", 7, 3, NGHTTP2_NV_FLAG_NONE},
318307
{(uint8_t *) "content-type", (uint8_t *) "text/html", 12, 9, NGHTTP2_NV_FLAG_NONE},
319-
{(uint8_t *) "server", (uint8_t *) "nghttp2-custom/1.0", 6, 17, NGHTTP2_NV_FLAG_NONE}};
308+
{(uint8_t *) "server", (uint8_t *) header_server, 6, strlen(header_server), NGHTTP2_NV_FLAG_NONE}};
320309

321-
// 根据路径提供不同的响应
322310
if (path == "/" || path == "/index.html") {
323-
// 主页响应
324311
const char *body = "<html><body><h1>Welcome to HTTP/2 Server</h1>"
325312
"<p>This is a simple HTTP/2 server implementation.</p>"
326-
"<link rel='stylesheet' href='/style.css'>"
327-
"<script src='/script.js'></script>"
328313
"</body></html>";
329314

330-
// 创建数据提供者
331315
nghttp2_data_provider data_prd;
332316
data_prd.source.ptr = (void *) body;
333317
data_prd.read_callback = string_read_callback;
@@ -339,46 +323,12 @@ static void handle_request(nghttp2_session *session, int32_t stream_id, Http2Ses
339323
SW_LOG_ERROR, SW_ERROR_HTTP2_INTERNAL_ERROR, "Failed to submit response: %s", nghttp2_strerror(rv));
340324
return;
341325
}
342-
// 服务器推送逻辑保持不变...
343-
} else if (path == "/about") {
344-
// 关于页面
345-
const char *body = "<html><body><h1>About This Server</h1>"
346-
"<p>This is a custom HTTP/2 server built with nghttp2.</p>"
347-
"</body></html>";
348-
349-
nghttp2_data_provider data_prd;
350-
data_prd.source.ptr = (void *) body;
351-
data_prd.read_callback = string_read_callback;
352-
353-
nghttp2_submit_response(session, stream_id, hdrs, sizeof(hdrs) / sizeof(hdrs[0]), &data_prd);
354-
} else if (path == "/style.css" || path == "/script.js") {
355-
// 这些资源可能已经被推送,但客户端仍可能直接请求它们
356-
nghttp2_nv content_hdrs[] = {
357-
{(uint8_t *) ":status", (uint8_t *) "200", 7, 3, NGHTTP2_NV_FLAG_NONE},
358-
{(uint8_t *) "content-type",
359-
(uint8_t *) (path == "/style.css" ? "text/css" : "application/javascript"),
360-
12,
361-
(path == "/style.css" ? 8 : 22),
362-
NGHTTP2_NV_FLAG_NONE},
363-
{(uint8_t *) "server", (uint8_t *) "nghttp2-custom/1.0", 6, 17, NGHTTP2_NV_FLAG_NONE}};
364-
365-
const char *content =
366-
path == "/style.css"
367-
? "body { font-family: Arial; color: #333; max-width: 800px; margin: 0 auto; padding: 20px; }"
368-
: "console.log('HTTP/2 server push demo!');";
369-
370-
nghttp2_data_provider data_prd;
371-
data_prd.source.ptr = (void *) content;
372-
data_prd.read_callback = string_read_callback;
373-
374-
nghttp2_submit_response(
375-
session, stream_id, content_hdrs, sizeof(content_hdrs) / sizeof(content_hdrs[0]), &data_prd);
376326
} else {
377327
// 404 Not Found
378328
nghttp2_nv error_hdrs[] = {
379329
{(uint8_t *) ":status", (uint8_t *) "404", 7, 3, NGHTTP2_NV_FLAG_NONE},
380330
{(uint8_t *) "content-type", (uint8_t *) "text/html", 12, 9, NGHTTP2_NV_FLAG_NONE},
381-
{(uint8_t *) "server", (uint8_t *) "nghttp2-custom/1.0", 6, 17, NGHTTP2_NV_FLAG_NONE}};
331+
{(uint8_t *) "server", (uint8_t *) header_server, 6, 17, NGHTTP2_NV_FLAG_NONE}};
382332

383333
const char *body = "<html><body><h1>404 Not Found</h1>"
384334
"<p>The requested resource was not found on this server.</p>"
@@ -391,16 +341,14 @@ static void handle_request(nghttp2_session *session, int32_t stream_id, Http2Ses
391341
nghttp2_submit_response(session, stream_id, error_hdrs, sizeof(error_hdrs) / sizeof(error_hdrs[0]), &data_prd);
392342
}
393343

394-
// 触发发送
395344
nghttp2_session_send(session);
396345
}
397346

398347
static void http2_send_settings(Http2Session *session_data) {
399-
// 设置 HTTP/2 服务器连接设置
400348
nghttp2_settings_entry settings[] = {
401349
{NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS, 100},
402-
{NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE, 1048576}, // 1MB 初始窗口大小
403-
{NGHTTP2_SETTINGS_MAX_FRAME_SIZE, 16384} // 16KB 最大帧大小
350+
{NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE, 1048576},
351+
{NGHTTP2_SETTINGS_MAX_FRAME_SIZE, 16384}
404352
};
405353

406354
auto rv = nghttp2_submit_settings(
@@ -411,37 +359,29 @@ static void http2_send_settings(Http2Session *session_data) {
411359
return;
412360
}
413361

414-
// 发送初始 SETTINGS 帧
415362
nghttp2_session_send(session_data->session);
416363
}
417364

418365
static std::shared_ptr<Http2Session> create_http2_session(Server *serv, SessionId fd) {
419366
auto session_data = std::make_shared<Http2Session>(fd, serv);
420367

421-
// 创建回调
422368
nghttp2_session_callbacks *callbacks;
423369
int rv = nghttp2_session_callbacks_new(&callbacks);
424370
if (rv != 0) {
425371
swoole_warning("Failed to create nghttp2 callbacks: %s", nghttp2_strerror(rv));
426372
return nullptr;
427373
}
428374

429-
// 设置回调函数
430375
nghttp2_session_callbacks_set_send_callback(callbacks, send_callback);
431376
nghttp2_session_callbacks_set_on_frame_recv_callback(callbacks, on_frame_recv_callback);
432377
nghttp2_session_callbacks_set_on_stream_close_callback(callbacks, on_stream_close_callback);
433378
nghttp2_session_callbacks_set_on_header_callback(callbacks, on_header_callback);
434379
nghttp2_session_callbacks_set_on_begin_headers_callback(callbacks, on_begin_headers_callback);
435380
nghttp2_session_callbacks_set_on_data_chunk_recv_callback(callbacks, on_data_chunk_recv_callback);
436-
437-
// 在 create_http2_session 中添加此回调
438381
nghttp2_session_callbacks_set_on_frame_not_send_callback(callbacks, on_frame_not_send_callback);
439-
440-
// 可选:设置更多回调
441382
nghttp2_session_callbacks_set_on_frame_send_callback(callbacks, on_frame_send_callback);
442383
nghttp2_session_callbacks_set_on_frame_not_send_callback(callbacks, on_frame_not_send_callback);
443384

444-
// 创建服务器会话
445385
rv = nghttp2_session_server_new(&session_data->session, callbacks, session_data.get());
446386
nghttp2_session_callbacks_del(callbacks);
447387

@@ -451,9 +391,10 @@ static std::shared_ptr<Http2Session> create_http2_session(Server *serv, SessionI
451391
return nullptr;
452392
}
453393

454-
// 存储服务器对象以便在回调中使用
455394
nghttp2_session_set_user_data(session_data->session, session_data.get());
456395

396+
// http2_send_settings(session_data.get());
397+
457398
return session_data;
458399
}
459400

@@ -500,7 +441,10 @@ static void test_ssl_http2(Server::Mode mode) {
500441
break;
501442
}
502443

444+
int status;
445+
ASSERT_EQ(waitpid(pid, &status, 0), pid);
503446
close(_pipe);
447+
504448
usleep(10000);
505449

506450
DEBUG() << buf.to_std_string();

0 commit comments

Comments
 (0)