Clean up HttpEncoder. Remove static comments from .cc file in accordance with go/c-readability-advice#static-comments. Use make_unique instead of new. Move comments from separate line above to inline in tests to reduce number of lines and increase readability. PiperOrigin-RevId: 457048490
diff --git a/quiche/quic/core/http/http_encoder.cc b/quiche/quic/core/http/http_encoder.cc index 32c8d91..66de89e 100644 --- a/quiche/quic/core/http/http_encoder.cc +++ b/quiche/quic/core/http/http_encoder.cc
@@ -33,7 +33,6 @@ } // namespace -// static QuicByteCount HttpEncoder::GetDataFrameHeaderLength( QuicByteCount payload_length) { QUICHE_DCHECK_NE(0u, payload_length); @@ -42,7 +41,6 @@ static_cast<uint64_t>(HttpFrameType::DATA)); } -// static quiche::QuicheBuffer HttpEncoder::SerializeDataFrameHeader( QuicByteCount payload_length, quiche::QuicheBufferAllocator* allocator) { QUICHE_DCHECK_NE(0u, payload_length); @@ -59,7 +57,6 @@ return quiche::QuicheBuffer(); } -// static QuicByteCount HttpEncoder::SerializeHeadersFrameHeader( QuicByteCount payload_length, std::unique_ptr<char[]>* output) { QUICHE_DCHECK_NE(0u, payload_length); @@ -68,7 +65,7 @@ QuicDataWriter::GetVarInt62Len( static_cast<uint64_t>(HttpFrameType::HEADERS)); - output->reset(new char[header_length]); + *output = std::make_unique<char[]>(header_length); QuicDataWriter writer(header_length, output->get()); if (WriteFrameHeader(payload_length, HttpFrameType::HEADERS, &writer)) { @@ -80,7 +77,6 @@ return 0; } -// static QuicByteCount HttpEncoder::SerializeSettingsFrame( const SettingsFrame& settings, std::unique_ptr<char[]>* output) { QuicByteCount payload_length = 0; @@ -96,7 +92,7 @@ QuicByteCount total_length = GetTotalLength(payload_length, HttpFrameType::SETTINGS); - output->reset(new char[total_length]); + *output = std::make_unique<char[]>(total_length); QuicDataWriter writer(total_length, output->get()); if (!WriteFrameHeader(payload_length, HttpFrameType::SETTINGS, &writer)) { @@ -116,14 +112,13 @@ return total_length; } -// static QuicByteCount HttpEncoder::SerializeGoAwayFrame( const GoAwayFrame& goaway, std::unique_ptr<char[]>* output) { QuicByteCount payload_length = QuicDataWriter::GetVarInt62Len(goaway.id); QuicByteCount total_length = GetTotalLength(payload_length, HttpFrameType::GOAWAY); - output->reset(new char[total_length]); + *output = std::make_unique<char[]>(total_length); QuicDataWriter writer(total_length, output->get()); if (WriteFrameHeader(payload_length, HttpFrameType::GOAWAY, &writer) && @@ -135,7 +130,6 @@ return 0; } -// static QuicByteCount HttpEncoder::SerializePriorityUpdateFrame( const PriorityUpdateFrame& priority_update, std::unique_ptr<char[]>* output) { @@ -151,7 +145,7 @@ QuicByteCount total_length = GetTotalLength( payload_length, HttpFrameType::PRIORITY_UPDATE_REQUEST_STREAM); - output->reset(new char[total_length]); + *output = std::make_unique<char[]>(total_length); QuicDataWriter writer(total_length, output->get()); if (WriteFrameHeader(payload_length, @@ -168,7 +162,6 @@ return 0; } -// static QuicByteCount HttpEncoder::SerializeAcceptChFrame( const AcceptChFrame& accept_ch, std::unique_ptr<char[]>* output) { QuicByteCount payload_length = 0; @@ -182,7 +175,7 @@ QuicByteCount total_length = GetTotalLength(payload_length, HttpFrameType::ACCEPT_CH); - output->reset(new char[total_length]); + *output = std::make_unique<char[]>(total_length); QuicDataWriter writer(total_length, output->get()); if (!WriteFrameHeader(payload_length, HttpFrameType::ACCEPT_CH, &writer)) { @@ -203,7 +196,6 @@ return total_length; } -// static QuicByteCount HttpEncoder::SerializeGreasingFrame( std::unique_ptr<char[]>* output) { uint64_t frame_type; @@ -222,16 +214,15 @@ payload_length = result % 4; if (payload_length > 0) { - std::unique_ptr<char[]> buffer(new char[payload_length]); - QuicRandom::GetInstance()->RandBytes(buffer.get(), payload_length); - payload = std::string(buffer.get(), payload_length); + payload.resize(payload_length); + QuicRandom::GetInstance()->RandBytes(payload.data(), payload_length); } } QuicByteCount total_length = QuicDataWriter::GetVarInt62Len(frame_type) + QuicDataWriter::GetVarInt62Len(payload_length) + payload_length; - output->reset(new char[total_length]); + *output = std::make_unique<char[]>(total_length); QuicDataWriter writer(total_length, output->get()); bool success =
diff --git a/quiche/quic/core/http/http_encoder_test.cc b/quiche/quic/core/http/http_encoder_test.cc index 217d98a..b6d8844 100644 --- a/quiche/quic/core/http/http_encoder_test.cc +++ b/quiche/quic/core/http/http_encoder_test.cc
@@ -17,10 +17,8 @@ TEST(HttpEncoderTest, SerializeDataFrameHeader) { quiche::QuicheBuffer buffer = HttpEncoder::SerializeDataFrameHeader( /* payload_length = */ 5, quiche::SimpleBufferAllocator::Get()); - char output[] = {// type (DATA) - 0x00, - // length - 0x05}; + char output[] = {0x00, // type (DATA) + 0x05}; // length EXPECT_EQ(ABSL_ARRAYSIZE(output), buffer.size()); quiche::test::CompareCharArraysWithHexError( "DATA", buffer.data(), buffer.size(), output, ABSL_ARRAYSIZE(output)); @@ -30,10 +28,8 @@ std::unique_ptr<char[]> buffer; uint64_t length = HttpEncoder::SerializeHeadersFrameHeader( /* payload_length = */ 7, &buffer); - char output[] = {// type (HEADERS) - 0x01, - // length - 0x07}; + char output[] = {0x01, // type (HEADERS) + 0x07}; // length EXPECT_EQ(ABSL_ARRAYSIZE(output), length); quiche::test::CompareCharArraysWithHexError("HEADERS", buffer.get(), length, output, ABSL_ARRAYSIZE(output)); @@ -44,22 +40,14 @@ settings.values[1] = 2; settings.values[6] = 5; settings.values[256] = 4; - char output[] = {// type (SETTINGS) - 0x04, - // length - 0x07, - // identifier (SETTINGS_QPACK_MAX_TABLE_CAPACITY) - 0x01, - // content - 0x02, - // identifier (SETTINGS_MAX_HEADER_LIST_SIZE) - 0x06, - // content - 0x05, - // identifier (256 in variable length integer) - 0x40 + 0x01, 0x00, - // content - 0x04}; + char output[] = {0x04, // type (SETTINGS) + 0x07, // length + 0x01, // identifier (SETTINGS_QPACK_MAX_TABLE_CAPACITY) + 0x02, // content + 0x06, // identifier (SETTINGS_MAX_HEADER_LIST_SIZE) + 0x05, // content + 0x41, 0x00, // identifier 0x100, varint encoded + 0x04}; // content std::unique_ptr<char[]> buffer; uint64_t length = HttpEncoder::SerializeSettingsFrame(settings, &buffer); EXPECT_EQ(ABSL_ARRAYSIZE(output), length); @@ -70,12 +58,9 @@ TEST(HttpEncoderTest, SerializeGoAwayFrame) { GoAwayFrame goaway; goaway.id = 0x1; - char output[] = {// type (GOAWAY) - 0x07, - // length - 0x1, - // ID - 0x01}; + char output[] = {0x07, // type (GOAWAY) + 0x1, // length + 0x01}; // ID std::unique_ptr<char[]> buffer; uint64_t length = HttpEncoder::SerializeGoAwayFrame(goaway, &buffer); EXPECT_EQ(ABSL_ARRAYSIZE(output), length);