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 "quiche/http2/decoder/payload_decoders/continuation_payload_decoder.h"
6 
7 #include <stddef.h>
8 
9 #include "quiche/http2/decoder/decode_buffer.h"
10 #include "quiche/http2/decoder/http2_frame_decoder_listener.h"
11 #include "quiche/http2/http2_constants.h"
12 #include "quiche/http2/http2_structures.h"
13 #include "quiche/common/platform/api/quiche_logging.h"
14 
15 namespace http2 {
16 
StartDecodingPayload(FrameDecoderState * state,DecodeBuffer * db)17 DecodeStatus ContinuationPayloadDecoder::StartDecodingPayload(
18     FrameDecoderState* state, DecodeBuffer* db) {
19   const Http2FrameHeader& frame_header = state->frame_header();
20   const uint32_t total_length = frame_header.payload_length;
21 
22   QUICHE_DVLOG(2) << "ContinuationPayloadDecoder::StartDecodingPayload: "
23                   << frame_header;
24   QUICHE_DCHECK_EQ(Http2FrameType::CONTINUATION, frame_header.type);
25   QUICHE_DCHECK_LE(db->Remaining(), total_length);
26   QUICHE_DCHECK_EQ(0, frame_header.flags & ~(Http2FrameFlag::END_HEADERS));
27 
28   state->InitializeRemainders();
29   state->listener()->OnContinuationStart(frame_header);
30   return ResumeDecodingPayload(state, db);
31 }
32 
ResumeDecodingPayload(FrameDecoderState * state,DecodeBuffer * db)33 DecodeStatus ContinuationPayloadDecoder::ResumeDecodingPayload(
34     FrameDecoderState* state, DecodeBuffer* db) {
35   QUICHE_DVLOG(2) << "ContinuationPayloadDecoder::ResumeDecodingPayload"
36                   << "  remaining_payload=" << state->remaining_payload()
37                   << "  db->Remaining=" << db->Remaining();
38   QUICHE_DCHECK_EQ(Http2FrameType::CONTINUATION, state->frame_header().type);
39   QUICHE_DCHECK_LE(state->remaining_payload(),
40                    state->frame_header().payload_length);
41   QUICHE_DCHECK_LE(db->Remaining(), state->remaining_payload());
42 
43   size_t avail = db->Remaining();
44   QUICHE_DCHECK_LE(avail, state->remaining_payload());
45   if (avail > 0) {
46     state->listener()->OnHpackFragment(db->cursor(), avail);
47     db->AdvanceCursor(avail);
48     state->ConsumePayload(avail);
49   }
50   if (state->remaining_payload() == 0) {
51     state->listener()->OnContinuationEnd();
52     return DecodeStatus::kDecodeDone;
53   }
54   return DecodeStatus::kDecodeInProgress;
55 }
56 
57 }  // namespace http2
58