QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 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/third_party/quiche/src/http2/decoder/payload_decoders/push_promise_payload_decoder.h" |
| 6 | |
| 7 | #include <stddef.h> |
| 8 | |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 9 | #include "net/third_party/quiche/src/http2/decoder/decode_buffer.h" |
| 10 | #include "net/third_party/quiche/src/http2/decoder/http2_frame_decoder_listener.h" |
| 11 | #include "net/third_party/quiche/src/http2/http2_constants.h" |
| 12 | #include "net/third_party/quiche/src/http2/http2_structures.h" |
| 13 | #include "net/third_party/quiche/src/http2/platform/api/http2_bug_tracker.h" |
QUICHE team | 61940b4 | 2019-03-07 23:32:27 -0500 | [diff] [blame^] | 14 | #include "net/third_party/quiche/src/http2/platform/api/http2_logging.h" |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 15 | #include "net/third_party/quiche/src/http2/platform/api/http2_macros.h" |
| 16 | |
| 17 | namespace http2 { |
| 18 | |
| 19 | std::ostream& operator<<(std::ostream& out, |
| 20 | PushPromisePayloadDecoder::PayloadState v) { |
| 21 | switch (v) { |
| 22 | case PushPromisePayloadDecoder::PayloadState::kReadPadLength: |
| 23 | return out << "kReadPadLength"; |
| 24 | case PushPromisePayloadDecoder::PayloadState:: |
| 25 | kStartDecodingPushPromiseFields: |
| 26 | return out << "kStartDecodingPushPromiseFields"; |
| 27 | case PushPromisePayloadDecoder::PayloadState::kReadPayload: |
| 28 | return out << "kReadPayload"; |
| 29 | case PushPromisePayloadDecoder::PayloadState::kSkipPadding: |
| 30 | return out << "kSkipPadding"; |
| 31 | case PushPromisePayloadDecoder::PayloadState:: |
| 32 | kResumeDecodingPushPromiseFields: |
| 33 | return out << "kResumeDecodingPushPromiseFields"; |
| 34 | } |
| 35 | return out << static_cast<int>(v); |
| 36 | } |
| 37 | |
| 38 | DecodeStatus PushPromisePayloadDecoder::StartDecodingPayload( |
| 39 | FrameDecoderState* state, |
| 40 | DecodeBuffer* db) { |
| 41 | const Http2FrameHeader& frame_header = state->frame_header(); |
| 42 | const uint32_t total_length = frame_header.payload_length; |
| 43 | |
QUICHE team | 61940b4 | 2019-03-07 23:32:27 -0500 | [diff] [blame^] | 44 | HTTP2_DVLOG(2) << "PushPromisePayloadDecoder::StartDecodingPayload: " |
| 45 | << frame_header; |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 46 | |
| 47 | DCHECK_EQ(Http2FrameType::PUSH_PROMISE, frame_header.type); |
| 48 | DCHECK_LE(db->Remaining(), total_length); |
| 49 | DCHECK_EQ(0, frame_header.flags & |
| 50 | ~(Http2FrameFlag::END_HEADERS | Http2FrameFlag::PADDED)); |
| 51 | |
| 52 | if (!frame_header.IsPadded()) { |
| 53 | // If it turns out that PUSH_PROMISE frames without padding are sufficiently |
| 54 | // common, and that they are usually short enough that they fit entirely |
| 55 | // into one DecodeBuffer, we can detect that here and implement a special |
| 56 | // case, avoiding the state machine in ResumeDecodingPayload. |
| 57 | payload_state_ = PayloadState::kStartDecodingPushPromiseFields; |
| 58 | } else { |
| 59 | payload_state_ = PayloadState::kReadPadLength; |
| 60 | } |
| 61 | state->InitializeRemainders(); |
| 62 | return ResumeDecodingPayload(state, db); |
| 63 | } |
| 64 | |
| 65 | DecodeStatus PushPromisePayloadDecoder::ResumeDecodingPayload( |
| 66 | FrameDecoderState* state, |
| 67 | DecodeBuffer* db) { |
QUICHE team | 61940b4 | 2019-03-07 23:32:27 -0500 | [diff] [blame^] | 68 | HTTP2_DVLOG(2) << "UnknownPayloadDecoder::ResumeDecodingPayload" |
| 69 | << " remaining_payload=" << state->remaining_payload() |
| 70 | << " db->Remaining=" << db->Remaining(); |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 71 | |
| 72 | const Http2FrameHeader& frame_header = state->frame_header(); |
| 73 | DCHECK_EQ(Http2FrameType::PUSH_PROMISE, frame_header.type); |
| 74 | DCHECK_LE(state->remaining_payload(), frame_header.payload_length); |
| 75 | DCHECK_LE(db->Remaining(), frame_header.payload_length); |
| 76 | |
| 77 | DecodeStatus status; |
| 78 | while (true) { |
QUICHE team | 61940b4 | 2019-03-07 23:32:27 -0500 | [diff] [blame^] | 79 | HTTP2_DVLOG(2) |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 80 | << "PushPromisePayloadDecoder::ResumeDecodingPayload payload_state_=" |
| 81 | << payload_state_; |
| 82 | switch (payload_state_) { |
| 83 | case PayloadState::kReadPadLength: |
| 84 | DCHECK_EQ(state->remaining_payload(), frame_header.payload_length); |
| 85 | // ReadPadLength handles the OnPadLength callback, and updating the |
| 86 | // remaining_payload and remaining_padding fields. If the amount of |
| 87 | // padding is too large to fit in the frame's payload, ReadPadLength |
| 88 | // instead calls OnPaddingTooLong and returns kDecodeError. |
| 89 | // Suppress the call to OnPadLength because we haven't yet called |
| 90 | // OnPushPromiseStart, which needs to wait until we've decoded the |
| 91 | // Promised Stream ID. |
| 92 | status = state->ReadPadLength(db, /*report_pad_length*/ false); |
| 93 | if (status != DecodeStatus::kDecodeDone) { |
| 94 | payload_state_ = PayloadState::kReadPadLength; |
| 95 | return status; |
| 96 | } |
| 97 | HTTP2_FALLTHROUGH; |
| 98 | |
| 99 | case PayloadState::kStartDecodingPushPromiseFields: |
| 100 | status = |
| 101 | state->StartDecodingStructureInPayload(&push_promise_fields_, db); |
| 102 | if (status != DecodeStatus::kDecodeDone) { |
| 103 | payload_state_ = PayloadState::kResumeDecodingPushPromiseFields; |
| 104 | return status; |
| 105 | } |
| 106 | // Finished decoding the Promised Stream ID. Can now tell the listener |
| 107 | // that we're starting to decode a PUSH_PROMISE frame. |
| 108 | ReportPushPromise(state); |
| 109 | HTTP2_FALLTHROUGH; |
| 110 | |
| 111 | case PayloadState::kReadPayload: |
| 112 | DCHECK_LT(state->remaining_payload(), frame_header.payload_length); |
| 113 | DCHECK_LE(state->remaining_payload(), |
| 114 | frame_header.payload_length - |
| 115 | Http2PushPromiseFields::EncodedSize()); |
| 116 | DCHECK_LE( |
| 117 | state->remaining_payload(), |
| 118 | frame_header.payload_length - |
| 119 | Http2PushPromiseFields::EncodedSize() - |
| 120 | (frame_header.IsPadded() ? (1 + state->remaining_padding()) |
| 121 | : 0)); |
| 122 | { |
| 123 | size_t avail = state->AvailablePayload(db); |
| 124 | state->listener()->OnHpackFragment(db->cursor(), avail); |
| 125 | db->AdvanceCursor(avail); |
| 126 | state->ConsumePayload(avail); |
| 127 | } |
| 128 | if (state->remaining_payload() > 0) { |
| 129 | payload_state_ = PayloadState::kReadPayload; |
| 130 | return DecodeStatus::kDecodeInProgress; |
| 131 | } |
| 132 | HTTP2_FALLTHROUGH; |
| 133 | |
| 134 | case PayloadState::kSkipPadding: |
| 135 | // SkipPadding handles the OnPadding callback. |
| 136 | if (state->SkipPadding(db)) { |
| 137 | state->listener()->OnPushPromiseEnd(); |
| 138 | return DecodeStatus::kDecodeDone; |
| 139 | } |
| 140 | payload_state_ = PayloadState::kSkipPadding; |
| 141 | return DecodeStatus::kDecodeInProgress; |
| 142 | |
| 143 | case PayloadState::kResumeDecodingPushPromiseFields: |
| 144 | status = |
| 145 | state->ResumeDecodingStructureInPayload(&push_promise_fields_, db); |
| 146 | if (status == DecodeStatus::kDecodeDone) { |
| 147 | // Finished decoding the Promised Stream ID. Can now tell the listener |
| 148 | // that we're starting to decode a PUSH_PROMISE frame. |
| 149 | ReportPushPromise(state); |
| 150 | payload_state_ = PayloadState::kReadPayload; |
| 151 | continue; |
| 152 | } |
| 153 | payload_state_ = PayloadState::kResumeDecodingPushPromiseFields; |
| 154 | return status; |
| 155 | } |
| 156 | HTTP2_BUG << "PayloadState: " << payload_state_; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | void PushPromisePayloadDecoder::ReportPushPromise(FrameDecoderState* state) { |
| 161 | const Http2FrameHeader& frame_header = state->frame_header(); |
| 162 | if (frame_header.IsPadded()) { |
| 163 | state->listener()->OnPushPromiseStart(frame_header, push_promise_fields_, |
| 164 | 1 + state->remaining_padding()); |
| 165 | } else { |
| 166 | state->listener()->OnPushPromiseStart(frame_header, push_promise_fields_, |
| 167 | 0); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | } // namespace http2 |