From a85a11c1d9ce51cb216ad68e7273d6b037e31d20 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Thu, 9 Jan 2014 23:30:45 +0900 Subject: [PATCH] Update priority for the stream to get response only --- lib/nghttp2_session.c | 28 ++++++++++++++--- lib/nghttp2_submit.c | 4 --- tests/nghttp2_session_test.c | 59 +++++++++++++++++++++++++++++++++--- 3 files changed, 78 insertions(+), 13 deletions(-) diff --git a/lib/nghttp2_session.c b/lib/nghttp2_session.c index 6b44a4d4..e90e262d 100644 --- a/lib/nghttp2_session.c +++ b/lib/nghttp2_session.c @@ -1501,9 +1501,24 @@ static int nghttp2_session_after_frame_sent(nghttp2_session *session) } break; } - case NGHTTP2_PRIORITY: - /* nothing to do */ + case NGHTTP2_PRIORITY: { + nghttp2_stream *stream; + stream = nghttp2_session_get_stream(session, frame->hd.stream_id); + if(!stream) { + break; + } + /* Only update priority of the stream, only if it is not pushed + stream and is initiated by local peer, or it is pushed stream + and is initiated by remote peer */ + if(((stream->flags & NGHTTP2_STREAM_FLAG_PUSH) == 0 && + nghttp2_session_is_my_stream_id(session, frame->hd.stream_id)) || + ((stream->flags & NGHTTP2_STREAM_FLAG_PUSH) && + !nghttp2_session_is_my_stream_id(session, frame->hd.stream_id))) { + nghttp2_session_reprioritize_stream(session, stream, + frame->priority.pri); + } break; + } case NGHTTP2_RST_STREAM: r = nghttp2_session_close_stream(session, frame->hd.stream_id, frame->rst_stream.error_code); @@ -2157,8 +2172,13 @@ int nghttp2_session_on_priority_received(nghttp2_session *session, return nghttp2_session_handle_invalid_connection(session, frame, NGHTTP2_PROTOCOL_ERROR); } - /* Only update priority on server side for now */ - if(session->server) { + /* Only update priority of the stream, only if it is not pushed + stream and is initiated by remote peer, or it is pushed stream + and is initiated by local peer */ + if(((stream->flags & NGHTTP2_STREAM_FLAG_PUSH) == 0 && + !nghttp2_session_is_my_stream_id(session, frame->hd.stream_id)) || + ((stream->flags & NGHTTP2_STREAM_FLAG_PUSH) && + nghttp2_session_is_my_stream_id(session, frame->hd.stream_id))) { nghttp2_session_reprioritize_stream(session, stream, frame->priority.pri); } diff --git a/lib/nghttp2_submit.c b/lib/nghttp2_submit.c index cc42c990..68c92e34 100644 --- a/lib/nghttp2_submit.c +++ b/lib/nghttp2_submit.c @@ -162,10 +162,6 @@ int nghttp2_submit_priority(nghttp2_session *session, uint8_t flags, free(frame); return r; } - /* Only update priority if the sender is client for now */ - if(!session->server) { - nghttp2_session_reprioritize_stream(session, stream, pri); - } return 0; } diff --git a/tests/nghttp2_session_test.c b/tests/nghttp2_session_test.c index 842509f1..8c8e9cb3 100644 --- a/tests/nghttp2_session_test.c +++ b/tests/nghttp2_session_test.c @@ -1009,19 +1009,42 @@ void test_nghttp2_session_on_priority_received(void) nghttp2_session_callbacks callbacks; my_user_data user_data; nghttp2_frame frame; + nghttp2_stream *stream; + memset(&callbacks, 0, sizeof(nghttp2_session_callbacks)); callbacks.on_frame_recv_callback = on_frame_recv_callback; callbacks.on_invalid_frame_recv_callback = on_invalid_frame_recv_callback; nghttp2_session_server_new(&session, &callbacks, &user_data); - nghttp2_session_open_stream(session, 1, NGHTTP2_STREAM_FLAG_NONE, - NGHTTP2_PRI_DEFAULT, - NGHTTP2_STREAM_OPENING, NULL); + stream = nghttp2_session_open_stream(session, 1, NGHTTP2_STREAM_FLAG_NONE, + NGHTTP2_PRI_DEFAULT, + NGHTTP2_STREAM_OPENING, NULL); nghttp2_frame_priority_init(&frame.priority, 1, 1000000007); + /* non-push and initiated by remote peer */ CU_ASSERT(0 == nghttp2_session_on_priority_received(session, &frame)); - CU_ASSERT(1000000007 == nghttp2_session_get_stream(session, 1)->pri); + CU_ASSERT(1000000007 == stream->pri); + + /* push and initiated by remote peer: no update */ + stream->flags = NGHTTP2_STREAM_FLAG_PUSH; + stream->pri = NGHTTP2_PRI_DEFAULT; + CU_ASSERT(0 == nghttp2_session_on_priority_received(session, &frame)); + CU_ASSERT(NGHTTP2_PRI_DEFAULT == stream->pri); + + stream = nghttp2_session_open_stream(session, 2, NGHTTP2_STREAM_FLAG_NONE, + NGHTTP2_PRI_DEFAULT, + NGHTTP2_STREAM_OPENING, NULL); + + frame.hd.stream_id = 2; + /* non-push and initiated by local peer: no update */ + CU_ASSERT(0 == nghttp2_session_on_priority_received(session, &frame)); + CU_ASSERT(NGHTTP2_PRI_DEFAULT == stream->pri); + + /* push and initiated by local peer */ + stream->flags = NGHTTP2_STREAM_FLAG_PUSH; + CU_ASSERT(0 == nghttp2_session_on_priority_received(session, &frame)); + CU_ASSERT(1000000007 == stream->pri); nghttp2_frame_priority_free(&frame.priority); nghttp2_session_del(session); @@ -2231,11 +2254,37 @@ void test_nghttp2_submit_priority(void) CU_ASSERT(NGHTTP2_ERR_INVALID_ARGUMENT == nghttp2_submit_priority(session, NGHTTP2_FLAG_NONE, 1, -1)); - + /* non-push stream and initiated by local peer */ CU_ASSERT(0 == nghttp2_submit_priority(session, NGHTTP2_FLAG_NONE, 1, 1000000007)); + CU_ASSERT(0 == nghttp2_session_send(session)); CU_ASSERT(1000000007 == stream->pri); + /* push stream and initiated by local peer: no update */ + stream->flags = NGHTTP2_STREAM_FLAG_PUSH; + stream->pri = NGHTTP2_PRI_DEFAULT; + CU_ASSERT(0 == nghttp2_submit_priority(session, NGHTTP2_FLAG_NONE, 1, + 1000000007)); + CU_ASSERT(0 == nghttp2_session_send(session)); + CU_ASSERT(NGHTTP2_PRI_DEFAULT == stream->pri); + + /* non-push stream and initiated by remote peer: no update */ + stream = nghttp2_session_open_stream(session, 2, NGHTTP2_STREAM_FLAG_NONE, + NGHTTP2_PRI_DEFAULT, + NGHTTP2_STREAM_OPENING, NULL); + CU_ASSERT(0 == nghttp2_submit_priority(session, NGHTTP2_FLAG_NONE, 2, + 1000000007)); + CU_ASSERT(0 == nghttp2_session_send(session)); + CU_ASSERT(NGHTTP2_PRI_DEFAULT == stream->pri); + + /* push stream and initiated by remote peer */ + stream->flags = NGHTTP2_STREAM_FLAG_PUSH; + CU_ASSERT(0 == nghttp2_submit_priority(session, NGHTTP2_FLAG_NONE, 2, + 1000000007)); + CU_ASSERT(0 == nghttp2_session_send(session)); + CU_ASSERT(1000000007 == stream->pri); + + nghttp2_session_del(session); /* Check that transmission of PRIORITY in reserved(local) is