| 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/data_payload_decoder.h" | |
| 6 | |
| 7 #include <stddef.h> | |
| 8 | |
| 9 #include "base/logging.h" | |
| 10 #include "base/macros.h" | |
| 11 #include "net/http2/decoder/decode_buffer.h" | |
| 12 #include "net/http2/decoder/http2_frame_decoder_listener.h" | |
| 13 #include "net/http2/http2_constants.h" | |
| 14 #include "net/http2/http2_structures.h" | |
| 15 #include "net/http2/tools/http2_bug_tracker.h" | |
| 16 | |
| 17 namespace net { | |
| 18 | |
| 19 std::ostream& operator<<(std::ostream& out, | |
| 20 DataPayloadDecoder::PayloadState v) { | |
| 21 switch (v) { | |
| 22 case DataPayloadDecoder::PayloadState::kReadPadLength: | |
| 23 return out << "kReadPadLength"; | |
| 24 case DataPayloadDecoder::PayloadState::kReadPayload: | |
| 25 return out << "kReadPayload"; | |
| 26 case DataPayloadDecoder::PayloadState::kSkipPadding: | |
| 27 return out << "kSkipPadding"; | |
| 28 } | |
| 29 return out << static_cast<int>(v); | |
| 30 } | |
| 31 | |
| 32 DecodeStatus DataPayloadDecoder::StartDecodingPayload(FrameDecoderState* state, | |
| 33 DecodeBuffer* db) { | |
| 34 const Http2FrameHeader& frame_header = state->frame_header(); | |
| 35 const uint32_t total_length = frame_header.payload_length; | |
| 36 | |
| 37 DVLOG(2) << "DataPayloadDecoder::StartDecodingPayload: " << frame_header; | |
| 38 DCHECK_EQ(Http2FrameType::DATA, frame_header.type); | |
| 39 DCHECK_LE(db->Remaining(), total_length); | |
| 40 DCHECK_EQ( | |
| 41 0, frame_header.flags & | |
| 42 ~(Http2FrameFlag::FLAG_END_STREAM | Http2FrameFlag::FLAG_PADDED)); | |
| 43 | |
| 44 // Special case for the hoped for common case: unpadded and fits fully into | |
| 45 // the decode buffer. TO BE SEEN if that is true. It certainly requires that | |
| 46 // the transport buffers be large (e.g. >> 16KB typically). | |
| 47 // TODO(jamessynge) Add counters. | |
| 48 DVLOG(2) << "StartDecodingPayload total_length=" << total_length; | |
| 49 if (!frame_header.IsPadded()) { | |
| 50 DVLOG(2) << "StartDecodingPayload !IsPadded"; | |
| 51 if (db->Remaining() == total_length) { | |
| 52 DVLOG(2) << "StartDecodingPayload all present"; | |
| 53 // Note that we don't cache the listener field so that the callee can | |
| 54 // replace it if the frame is bad. | |
| 55 // If this case is common enough, consider combining the 3 callbacks | |
| 56 // into one. | |
| 57 state->listener()->OnDataStart(frame_header); | |
| 58 if (total_length > 0) { | |
| 59 state->listener()->OnDataPayload(db->cursor(), total_length); | |
| 60 db->AdvanceCursor(total_length); | |
| 61 } | |
| 62 state->listener()->OnDataEnd(); | |
| 63 return DecodeStatus::kDecodeDone; | |
| 64 } | |
| 65 payload_state_ = PayloadState::kReadPayload; | |
| 66 } else { | |
| 67 payload_state_ = PayloadState::kReadPadLength; | |
| 68 } | |
| 69 state->InitializeRemainders(); | |
| 70 state->listener()->OnDataStart(frame_header); | |
| 71 return ResumeDecodingPayload(state, db); | |
| 72 } | |
| 73 | |
| 74 DecodeStatus DataPayloadDecoder::ResumeDecodingPayload(FrameDecoderState* state, | |
| 75 DecodeBuffer* db) { | |
| 76 DVLOG(2) << "DataPayloadDecoder::ResumeDecodingPayload payload_state_=" | |
| 77 << payload_state_; | |
| 78 const Http2FrameHeader& frame_header = state->frame_header(); | |
| 79 DCHECK_EQ(Http2FrameType::DATA, frame_header.type); | |
| 80 DCHECK_LE(state->remaining_payload_and_padding(), | |
| 81 frame_header.payload_length); | |
| 82 DCHECK_LE(db->Remaining(), state->remaining_payload_and_padding()); | |
| 83 DecodeStatus status; | |
| 84 size_t avail; | |
| 85 switch (payload_state_) { | |
| 86 case PayloadState::kReadPadLength: | |
| 87 // ReadPadLength handles the OnPadLength callback, and updating the | |
| 88 // remaining_payload and remaining_padding fields. If the amount of | |
| 89 // padding is too large to fit in the frame's payload, ReadPadLength | |
| 90 // instead calls OnPaddingTooLong and returns kDecodeError. | |
| 91 status = state->ReadPadLength(db, /*report_pad_length*/ true); | |
| 92 if (status != DecodeStatus::kDecodeDone) { | |
| 93 return status; | |
| 94 } | |
| 95 // FALLTHROUGH_INTENDED | |
| 96 | |
| 97 case PayloadState::kReadPayload: | |
| 98 avail = state->AvailablePayload(db); | |
| 99 if (avail > 0) { | |
| 100 state->listener()->OnDataPayload(db->cursor(), avail); | |
| 101 db->AdvanceCursor(avail); | |
| 102 state->ConsumePayload(avail); | |
| 103 } | |
| 104 if (state->remaining_payload() > 0) { | |
| 105 payload_state_ = PayloadState::kReadPayload; | |
| 106 return DecodeStatus::kDecodeInProgress; | |
| 107 } | |
| 108 // FALLTHROUGH_INTENDED | |
| 109 | |
| 110 case PayloadState::kSkipPadding: | |
| 111 // SkipPadding handles the OnPadding callback. | |
| 112 if (state->SkipPadding(db)) { | |
| 113 state->listener()->OnDataEnd(); | |
| 114 return DecodeStatus::kDecodeDone; | |
| 115 } | |
| 116 payload_state_ = PayloadState::kSkipPadding; | |
| 117 return DecodeStatus::kDecodeInProgress; | |
| 118 } | |
| 119 HTTP2_BUG << "PayloadState: " << payload_state_; | |
| 120 return DecodeStatus::kDecodeError; | |
| 121 } | |
| 122 | |
| 123 } // namespace net | |
| OLD | NEW |