Skip to content

Commit aa46cfb

Browse files
Yang Chifacebook-github-bot
authored andcommitted
Add a priority header to HTTP/3 response if it doesn't already have one
Summary: This is to notify the client the priority that server uses when it sends out the response headers Reviewed By: afrind Differential Revision: D26753465 fbshipit-source-id: 4a50e91b6f2fd8fbcc06ed6eb07cf608577da55c
1 parent 8092a3a commit aa46cfb

File tree

7 files changed

+63
-5
lines changed

7 files changed

+63
-5
lines changed

proxygen/lib/http/HTTPMessage.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,16 @@ namespace {
2727
* approximately 1% of our total CPU time on temporary locale objects.)
2828
*/
2929
std::locale defaultLocale;
30+
} // namespace
31+
32+
namespace proxygen {
3033

3134
std::string httpPriorityToString(uint8_t urgency, bool incremental) {
3235
return folly::to<std::string>(
3336
"u=",
3437
std::min(static_cast<uint8_t>(proxygen::kMaxPriority), urgency),
3538
incremental ? ",i" : "");
3639
}
37-
} // namespace
38-
39-
namespace proxygen {
4040

4141
std::mutex HTTPMessage::mutexDump_;
4242

proxygen/lib/http/HTTPMessage.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ struct HTTPPriority {
5252
virtual ~HTTPPriority() = default;
5353
};
5454

55+
// Convert Priority to a string representation in the form of "u=urgency[,i]"
56+
std::string httpPriorityToString(uint8_t urgency, bool incremental);
57+
5558
class HTTPMessage;
5659

5760
folly::Optional<HTTPPriority> httpPriorityFromHTTPMessage(

proxygen/lib/http/session/HQDownstreamSession.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,4 +258,23 @@ hq::PushId HQDownstreamSession::createNewPushId() {
258258
return newPushId;
259259
}
260260

261+
folly::Optional<HTTPHeaders> HQDownstreamSession::getExtraHeaders(
262+
const HTTPMessage& headers, quic::StreamId streamId) {
263+
if (!sock_) {
264+
return folly::none;
265+
}
266+
if (headers.getHeaders().exists(HTTP_HEADER_PRIORITY)) {
267+
return folly::none;
268+
}
269+
auto priority = sock_->getStreamPriority(streamId);
270+
if (!priority) {
271+
return folly::none;
272+
}
273+
HTTPHeaders extraHeaders;
274+
extraHeaders.add(
275+
HTTP_HEADER_PRIORITY,
276+
httpPriorityToString(priority->level, priority->incremental));
277+
return extraHeaders;
278+
}
279+
261280
} // namespace proxygen

proxygen/lib/http/session/HQDownstreamSession.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ class HQDownstreamSession : public HQSession {
7575
return static_cast<uint32_t>(streams_.size());
7676
}
7777

78+
folly::Optional<HTTPHeaders> getExtraHeaders(
79+
const HTTPMessage& haeders, quic::StreamId streamId) override;
80+
7881
private:
7982
~HQDownstreamSession() override {
8083
CHECK_EQ(getNumStreams(), 0);

proxygen/lib/http/session/HQSession.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2872,8 +2872,12 @@ void HQSession::HQStreamTransportBase::sendHeaders(HTTPTransaction* txn,
28722872
<< headers.isRequest()
28732873
<< "; assocTxnId=" << txn->getAssocTxnId().value_or(-1)
28742874
<< "; txn=" << txn->getID();
2875-
codecFilterChain->generateHeader(
2876-
writeBuf_, *codecStreamId_, headers, includeEOM, size);
2875+
codecFilterChain->generateHeader(writeBuf_,
2876+
*codecStreamId_,
2877+
headers,
2878+
includeEOM,
2879+
size,
2880+
session_.getExtraHeaders(headers, streamId));
28772881

28782882
const uint64_t newOffset = streamWriteByteOffset();
28792883
egressHeadersStreamOffset_ = newOffset;

proxygen/lib/http/session/HQSession.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -773,6 +773,12 @@ class HQSession
773773
quic::StreamId id,
774774
HTTP3::ErrorCode err);
775775

776+
// Get extra HTTP headers we want to add to the HTTPMessage in sendHeaders.
777+
virtual folly::Optional<HTTPHeaders> getExtraHeaders(const HTTPMessage&,
778+
quic::StreamId) {
779+
return folly::none;
780+
}
781+
776782
proxygen::TransportDirection direction_;
777783
std::chrono::milliseconds transactionsTimeout_;
778784
TimePoint transportStart_;

proxygen/lib/http/session/test/HQDownstreamSessionTest.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,29 @@ TEST_P(HQDownstreamSessionTest, PriorityUpdateIntoTransport) {
356356
hqSession_->closeWhenIdle();
357357
}
358358

359+
TEST_P(HQDownstreamSessionTest, ReplyResponsePriority) {
360+
if (!IS_HQ) { // H1Q tests do not support priority
361+
hqSession_->closeWhenIdle();
362+
return;
363+
}
364+
auto request = getProgressiveGetRequest();
365+
sendRequest(request);
366+
auto handler = addSimpleStrictHandler();
367+
EXPECT_CALL(*socketDriver_->getSocket(), setStreamPriority(_, 1, true))
368+
.Times(1);
369+
handler->expectHeaders();
370+
handler->expectEOM([&]() {
371+
auto resp = makeResponse(200, 0);
372+
EXPECT_CALL(*socketDriver_->getSocket(), getStreamPriority(_))
373+
.Times(1)
374+
.WillOnce(Return(quic::Priority(1, true)));
375+
handler->sendRequest(*std::get<0>(resp));
376+
});
377+
handler->expectDetachTransaction();
378+
flushRequestsAndLoop();
379+
hqSession_->closeWhenIdle();
380+
}
381+
359382
TEST_P(HQDownstreamSessionTestHQPush, PushPriority) {
360383
sendRequest("/", 1);
361384
HTTPMessage promiseReq, parentResp;

0 commit comments

Comments
 (0)