| 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/headers_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 HeadersPayloadDecoder::PayloadState v) { | |
| 21 switch (v) { | |
| 22 case HeadersPayloadDecoder::PayloadState::kReadPadLength: | |
| 23 return out << "kReadPadLength"; | |
| 24 case HeadersPayloadDecoder::PayloadState::kStartDecodingPriorityFields: | |
| 25 return out << "kStartDecodingPriorityFields"; | |
| 26 case HeadersPayloadDecoder::PayloadState::kResumeDecodingPriorityFields: | |
| 27 return out << "kResumeDecodingPriorityFields"; | |
| 28 case HeadersPayloadDecoder::PayloadState::kReadPayload: | |
| 29 return out << "kReadPayload"; | |
| 30 case HeadersPayloadDecoder::PayloadState::kSkipPadding: | |
| 31 return out << "kSkipPadding"; | |
| 32 } | |
| 33 return out << static_cast<int>(v); | |
| 34 } | |
| 35 | |
| 36 DecodeStatus HeadersPayloadDecoder::StartDecodingPayload( | |
| 37 FrameDecoderState* state, | |
| 38 DecodeBuffer* db) { | |
| 39 const Http2FrameHeader& frame_header = state->frame_header(); | |
| 40 const uint32_t total_length = frame_header.payload_length; | |
| 41 | |
| 42 DVLOG(2) << "HeadersPayloadDecoder::StartDecodingPayload: " << frame_header; | |
| 43 | |
| 44 DCHECK_EQ(Http2FrameType::HEADERS, frame_header.type); | |
| 45 DCHECK_LE(db->Remaining(), total_length); | |
| 46 DCHECK_EQ( | |
| 47 0, | |
| 48 frame_header.flags & | |
| 49 ~(Http2FrameFlag::FLAG_END_STREAM | Http2FrameFlag::FLAG_END_HEADERS | | |
| 50 Http2FrameFlag::FLAG_PADDED | Http2FrameFlag::FLAG_PRIORITY)); | |
| 51 | |
| 52 // Special case for HEADERS frames that contain only the HPACK block | |
| 53 // (fragment or whole) and that fit fully into the decode buffer. | |
| 54 // Why? Unencoded browser GET requests are typically under 1K and HPACK | |
| 55 // commonly shrinks request headers by 80%, so we can expect this to | |
| 56 // be common. | |
| 57 // TODO(jamessynge) Add counters here and to Spdy for determining how | |
| 58 // common this situation is. A possible approach is to create a | |
| 59 // Http2FrameDecoderListener that counts the callbacks and then forwards | |
| 60 // them on to another listener, which makes it easy to add and remove | |
| 61 // counting on a connection or even frame basis. | |
| 62 | |
| 63 // PADDED and PRIORITY both extra steps to decode, but if neither flag is | |
| 64 // set then we can decode faster. | |
| 65 const auto payload_flags = | |
| 66 Http2FrameFlag::FLAG_PADDED | Http2FrameFlag::FLAG_PRIORITY; | |
| 67 if (!frame_header.HasAnyFlags(payload_flags)) { | |
| 68 DVLOG(2) << "StartDecodingPayload !IsPadded && !HasPriority"; | |
| 69 if (db->Remaining() == total_length) { | |
| 70 DVLOG(2) << "StartDecodingPayload all present"; | |
| 71 // Note that we don't cache the listener field so that the callee can | |
| 72 // replace it if the frame is bad. | |
| 73 // If this case is common enough, consider combining the 3 callbacks | |
| 74 // into one, especially if END_HEADERS is also set. | |
| 75 state->listener()->OnHeadersStart(frame_header); | |
| 76 if (total_length > 0) { | |
| 77 state->listener()->OnHpackFragment(db->cursor(), total_length); | |
| 78 db->AdvanceCursor(total_length); | |
| 79 } | |
| 80 state->listener()->OnHeadersEnd(); | |
| 81 return DecodeStatus::kDecodeDone; | |
| 82 } | |
| 83 payload_state_ = PayloadState::kReadPayload; | |
| 84 } else if (frame_header.IsPadded()) { | |
| 85 payload_state_ = PayloadState::kReadPadLength; | |
| 86 } else { | |
| 87 DCHECK(frame_header.HasPriority()) << frame_header; | |
| 88 payload_state_ = PayloadState::kStartDecodingPriorityFields; | |
| 89 } | |
| 90 state->InitializeRemainders(); | |
| 91 state->listener()->OnHeadersStart(frame_header); | |
| 92 return ResumeDecodingPayload(state, db); | |
| 93 } | |
| 94 | |
| 95 DecodeStatus HeadersPayloadDecoder::ResumeDecodingPayload( | |
| 96 FrameDecoderState* state, | |
| 97 DecodeBuffer* db) { | |
| 98 DVLOG(2) << "HeadersPayloadDecoder::ResumeDecodingPayload " | |
| 99 << "remaining_payload=" << state->remaining_payload() | |
| 100 << "; db->Remaining=" << db->Remaining(); | |
| 101 | |
| 102 const Http2FrameHeader& frame_header = state->frame_header(); | |
| 103 | |
| 104 DCHECK_EQ(Http2FrameType::HEADERS, frame_header.type); | |
| 105 DCHECK_LE(state->remaining_payload_and_padding(), | |
| 106 frame_header.payload_length); | |
| 107 DCHECK_LE(db->Remaining(), state->remaining_payload_and_padding()); | |
| 108 DecodeStatus status; | |
| 109 size_t avail; | |
| 110 while (true) { | |
| 111 DVLOG(2) << "HeadersPayloadDecoder::ResumeDecodingPayload payload_state_=" | |
| 112 << payload_state_; | |
| 113 switch (payload_state_) { | |
| 114 case PayloadState::kReadPadLength: | |
| 115 // ReadPadLength handles the OnPadLength callback, and updating the | |
| 116 // remaining_payload and remaining_padding fields. If the amount of | |
| 117 // padding is too large to fit in the frame's payload, ReadPadLength | |
| 118 // instead calls OnPaddingTooLong and returns kDecodeError. | |
| 119 status = state->ReadPadLength(db, /*report_pad_length*/ true); | |
| 120 if (status != DecodeStatus::kDecodeDone) { | |
| 121 return status; | |
| 122 } | |
| 123 if (!frame_header.HasPriority()) { | |
| 124 payload_state_ = PayloadState::kReadPayload; | |
| 125 continue; | |
| 126 } | |
| 127 // FALLTHROUGH_INTENDED | |
| 128 | |
| 129 case PayloadState::kStartDecodingPriorityFields: | |
| 130 status = state->StartDecodingStructureInPayload(&priority_fields_, db); | |
| 131 if (status != DecodeStatus::kDecodeDone) { | |
| 132 payload_state_ = PayloadState::kResumeDecodingPriorityFields; | |
| 133 return status; | |
| 134 } | |
| 135 state->listener()->OnHeadersPriority(priority_fields_); | |
| 136 // FALLTHROUGH_INTENDED | |
| 137 | |
| 138 case PayloadState::kReadPayload: | |
| 139 avail = state->AvailablePayload(db); | |
| 140 if (avail > 0) { | |
| 141 state->listener()->OnHpackFragment(db->cursor(), avail); | |
| 142 db->AdvanceCursor(avail); | |
| 143 state->ConsumePayload(avail); | |
| 144 } | |
| 145 if (state->remaining_payload() > 0) { | |
| 146 payload_state_ = PayloadState::kReadPayload; | |
| 147 return DecodeStatus::kDecodeInProgress; | |
| 148 } | |
| 149 // FALLTHROUGH_INTENDED | |
| 150 | |
| 151 case PayloadState::kSkipPadding: | |
| 152 // SkipPadding handles the OnPadding callback. | |
| 153 if (state->SkipPadding(db)) { | |
| 154 state->listener()->OnHeadersEnd(); | |
| 155 return DecodeStatus::kDecodeDone; | |
| 156 } | |
| 157 payload_state_ = PayloadState::kSkipPadding; | |
| 158 return DecodeStatus::kDecodeInProgress; | |
| 159 | |
| 160 case PayloadState::kResumeDecodingPriorityFields: | |
| 161 status = state->ResumeDecodingStructureInPayload(&priority_fields_, db); | |
| 162 if (status != DecodeStatus::kDecodeDone) { | |
| 163 return status; | |
| 164 } | |
| 165 state->listener()->OnHeadersPriority(priority_fields_); | |
| 166 payload_state_ = PayloadState::kReadPayload; | |
| 167 continue; | |
| 168 } | |
| 169 HTTP2_BUG << "PayloadState: " << payload_state_; | |
| 170 } | |
| 171 } | |
| 172 | |
| 173 } // namespace net | |
| OLD | NEW |