OLD | NEW |
(Empty) | |
| 1 // 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/http2/decoder/payload_decoders/ping_payload_decoder.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "net/http2/decoder/http2_frame_decoder_listener.h" |
| 9 #include "net/http2/http2_constants.h" |
| 10 |
| 11 namespace net { |
| 12 namespace { |
| 13 constexpr auto kOpaqueSize = Http2PingFields::EncodedSize(); |
| 14 } |
| 15 |
| 16 DecodeStatus 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 |
| 21 DVLOG(2) << "PingPayloadDecoder::StartDecodingPayload: " << frame_header; |
| 22 DCHECK_EQ(Http2FrameType::PING, frame_header.type); |
| 23 DCHECK_LE(db->Remaining(), total_length); |
| 24 DCHECK_EQ(0, frame_header.flags & ~(Http2FrameFlag::FLAG_ACK)); |
| 25 |
| 26 // Is the payload entirely in the decode buffer and is it the correct size? |
| 27 // Given the size of the header and payload (17 bytes total), this is most |
| 28 // likely the case the vast majority of the time. |
| 29 if (db->Remaining() == kOpaqueSize && total_length == kOpaqueSize) { |
| 30 // Special case this situation as it allows us to avoid any copying; |
| 31 // the other path makes two copies, first into the buffer in |
| 32 // Http2StructureDecoder as it accumulates the 8 bytes of opaque data, |
| 33 // and a second copy into the Http2PingFields member of in this class. |
| 34 // This supports the claim that this decoder is (mostly) non-buffering. |
| 35 static_assert(sizeof(Http2PingFields) == kOpaqueSize, |
| 36 "If not, then can't enter this block!"); |
| 37 auto ping = reinterpret_cast<const Http2PingFields*>(db->cursor()); |
| 38 if (frame_header.IsAck()) { |
| 39 state->listener()->OnPingAck(frame_header, *ping); |
| 40 } else { |
| 41 state->listener()->OnPing(frame_header, *ping); |
| 42 } |
| 43 db->AdvanceCursor(kOpaqueSize); |
| 44 return DecodeStatus::kDecodeDone; |
| 45 } |
| 46 state->InitializeRemainders(); |
| 47 return HandleStatus( |
| 48 state, state->StartDecodingStructureInPayload(&ping_fields_, db)); |
| 49 } |
| 50 |
| 51 DecodeStatus PingPayloadDecoder::ResumeDecodingPayload(FrameDecoderState* state, |
| 52 DecodeBuffer* db) { |
| 53 DVLOG(2) << "ResumeDecodingPayload: remaining_payload=" |
| 54 << state->remaining_payload(); |
| 55 DCHECK_EQ(Http2FrameType::PING, state->frame_header().type); |
| 56 DCHECK_LE(db->Remaining(), state->frame_header().payload_length); |
| 57 return HandleStatus( |
| 58 state, state->ResumeDecodingStructureInPayload(&ping_fields_, db)); |
| 59 } |
| 60 |
| 61 DecodeStatus PingPayloadDecoder::HandleStatus(FrameDecoderState* state, |
| 62 DecodeStatus status) { |
| 63 DVLOG(2) << "HandleStatus: status=" << status |
| 64 << "; remaining_payload=" << state->remaining_payload(); |
| 65 if (status == DecodeStatus::kDecodeDone) { |
| 66 if (state->remaining_payload() == 0) { |
| 67 const Http2FrameHeader& frame_header = state->frame_header(); |
| 68 if (frame_header.IsAck()) { |
| 69 state->listener()->OnPingAck(frame_header, ping_fields_); |
| 70 } else { |
| 71 state->listener()->OnPing(frame_header, ping_fields_); |
| 72 } |
| 73 return DecodeStatus::kDecodeDone; |
| 74 } |
| 75 // Payload is too long. |
| 76 return state->ReportFrameSizeError(); |
| 77 } |
| 78 // Not done decoding the structure. Either we've got more payload to decode, |
| 79 // or we've run out because the payload is too short. |
| 80 DCHECK( |
| 81 (status == DecodeStatus::kDecodeInProgress && |
| 82 state->remaining_payload() > 0) || |
| 83 (status == DecodeStatus::kDecodeError && state->remaining_payload() == 0)) |
| 84 << "\n status=" << status |
| 85 << "; remaining_payload=" << state->remaining_payload(); |
| 86 return status; |
| 87 } |
| 88 |
| 89 } // namespace net |
OLD | NEW |