blob: 1363d4f9674ce93435e499a4bafa085b34d6f663 [file] [log] [blame]
QUICHE teamfd50a402018-12-07 22:54:05 -05001// Copyright 2016 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "net/third_party/quiche/src/http2/decoder/payload_decoders/ping_payload_decoder.h"
6
QUICHE teamfd50a402018-12-07 22:54:05 -05007#include "net/third_party/quiche/src/http2/decoder/http2_frame_decoder_listener.h"
8#include "net/third_party/quiche/src/http2/http2_constants.h"
QUICHE team61940b42019-03-07 23:32:27 -05009#include "net/third_party/quiche/src/http2/platform/api/http2_logging.h"
QUICHE teamfd50a402018-12-07 22:54:05 -050010
11namespace http2 {
12namespace {
13constexpr auto kOpaqueSize = Http2PingFields::EncodedSize();
14}
15
16DecodeStatus PingPayloadDecoder::StartDecodingPayload(FrameDecoderState* state,
17 DecodeBuffer* db) {
18 const Http2FrameHeader& frame_header = state->frame_header();
19 const uint32_t total_length = frame_header.payload_length;
20
QUICHE team61940b42019-03-07 23:32:27 -050021 HTTP2_DVLOG(2) << "PingPayloadDecoder::StartDecodingPayload: "
22 << frame_header;
QUICHE teamfd50a402018-12-07 22:54:05 -050023 DCHECK_EQ(Http2FrameType::PING, frame_header.type);
24 DCHECK_LE(db->Remaining(), total_length);
25 DCHECK_EQ(0, frame_header.flags & ~(Http2FrameFlag::ACK));
26
27 // Is the payload entirely in the decode buffer and is it the correct size?
28 // Given the size of the header and payload (17 bytes total), this is most
29 // likely the case the vast majority of the time.
30 if (db->Remaining() == kOpaqueSize && total_length == kOpaqueSize) {
31 // Special case this situation as it allows us to avoid any copying;
32 // the other path makes two copies, first into the buffer in
33 // Http2StructureDecoder as it accumulates the 8 bytes of opaque data,
34 // and a second copy into the Http2PingFields member of in this class.
35 // This supports the claim that this decoder is (mostly) non-buffering.
36 static_assert(sizeof(Http2PingFields) == kOpaqueSize,
37 "If not, then can't enter this block!");
38 auto* ping = reinterpret_cast<const Http2PingFields*>(db->cursor());
39 if (frame_header.IsAck()) {
40 state->listener()->OnPingAck(frame_header, *ping);
41 } else {
42 state->listener()->OnPing(frame_header, *ping);
43 }
44 db->AdvanceCursor(kOpaqueSize);
45 return DecodeStatus::kDecodeDone;
46 }
47 state->InitializeRemainders();
48 return HandleStatus(
49 state, state->StartDecodingStructureInPayload(&ping_fields_, db));
50}
51
52DecodeStatus PingPayloadDecoder::ResumeDecodingPayload(FrameDecoderState* state,
53 DecodeBuffer* db) {
QUICHE team61940b42019-03-07 23:32:27 -050054 HTTP2_DVLOG(2) << "ResumeDecodingPayload: remaining_payload="
55 << state->remaining_payload();
QUICHE teamfd50a402018-12-07 22:54:05 -050056 DCHECK_EQ(Http2FrameType::PING, state->frame_header().type);
57 DCHECK_LE(db->Remaining(), state->frame_header().payload_length);
58 return HandleStatus(
59 state, state->ResumeDecodingStructureInPayload(&ping_fields_, db));
60}
61
62DecodeStatus PingPayloadDecoder::HandleStatus(FrameDecoderState* state,
63 DecodeStatus status) {
QUICHE team61940b42019-03-07 23:32:27 -050064 HTTP2_DVLOG(2) << "HandleStatus: status=" << status
65 << "; remaining_payload=" << state->remaining_payload();
QUICHE teamfd50a402018-12-07 22:54:05 -050066 if (status == DecodeStatus::kDecodeDone) {
67 if (state->remaining_payload() == 0) {
68 const Http2FrameHeader& frame_header = state->frame_header();
69 if (frame_header.IsAck()) {
70 state->listener()->OnPingAck(frame_header, ping_fields_);
71 } else {
72 state->listener()->OnPing(frame_header, ping_fields_);
73 }
74 return DecodeStatus::kDecodeDone;
75 }
76 // Payload is too long.
77 return state->ReportFrameSizeError();
78 }
79 // Not done decoding the structure. Either we've got more payload to decode,
80 // or we've run out because the payload is too short.
81 DCHECK(
82 (status == DecodeStatus::kDecodeInProgress &&
83 state->remaining_payload() > 0) ||
84 (status == DecodeStatus::kDecodeError && state->remaining_payload() == 0))
85 << "\n status=" << status
86 << "; remaining_payload=" << state->remaining_payload();
87 return status;
88}
89
90} // namespace http2