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 #ifndef QUICHE_HTTP2_DECODER_HTTP2_FRAME_DECODER_H_ 6 #define QUICHE_HTTP2_DECODER_HTTP2_FRAME_DECODER_H_ 7 8 // Http2FrameDecoder decodes the available input until it reaches the end of 9 // the input or it reaches the end of the first frame in the input. 10 // Note that Http2FrameDecoder does only minimal validation; for example, 11 // stream ids are not checked, nor is the sequence of frames such as 12 // CONTINUATION frame placement. 13 // 14 // Http2FrameDecoder enters state kError once it has called the listener's 15 // OnFrameSizeError or OnPaddingTooLong methods, and at this time has no 16 // provision for leaving that state. While the HTTP/2 spec (RFC7540) allows 17 // for some such errors to be considered as just stream errors in some cases, 18 // this implementation treats them all as connection errors. 19 20 #include <stddef.h> 21 22 #include <cstdint> 23 24 #include "quiche/http2/decoder/decode_buffer.h" 25 #include "quiche/http2/decoder/decode_status.h" 26 #include "quiche/http2/decoder/frame_decoder_state.h" 27 #include "quiche/http2/decoder/http2_frame_decoder_listener.h" 28 #include "quiche/http2/decoder/payload_decoders/altsvc_payload_decoder.h" 29 #include "quiche/http2/decoder/payload_decoders/continuation_payload_decoder.h" 30 #include "quiche/http2/decoder/payload_decoders/data_payload_decoder.h" 31 #include "quiche/http2/decoder/payload_decoders/goaway_payload_decoder.h" 32 #include "quiche/http2/decoder/payload_decoders/headers_payload_decoder.h" 33 #include "quiche/http2/decoder/payload_decoders/ping_payload_decoder.h" 34 #include "quiche/http2/decoder/payload_decoders/priority_payload_decoder.h" 35 #include "quiche/http2/decoder/payload_decoders/priority_update_payload_decoder.h" 36 #include "quiche/http2/decoder/payload_decoders/push_promise_payload_decoder.h" 37 #include "quiche/http2/decoder/payload_decoders/rst_stream_payload_decoder.h" 38 #include "quiche/http2/decoder/payload_decoders/settings_payload_decoder.h" 39 #include "quiche/http2/decoder/payload_decoders/unknown_payload_decoder.h" 40 #include "quiche/http2/decoder/payload_decoders/window_update_payload_decoder.h" 41 #include "quiche/http2/http2_structures.h" 42 #include "quiche/common/platform/api/quiche_export.h" 43 #include "quiche/common/platform/api/quiche_logging.h" 44 45 namespace http2 { 46 namespace test { 47 class Http2FrameDecoderPeer; 48 } // namespace test 49 50 class QUICHE_EXPORT Http2FrameDecoder { 51 public: 52 explicit Http2FrameDecoder(Http2FrameDecoderListener* listener); 53 54 Http2FrameDecoder(const Http2FrameDecoder&) = delete; 55 Http2FrameDecoder& operator=(const Http2FrameDecoder&) = delete; 56 57 // The decoder will call the listener's methods as it decodes a frame. 58 void set_listener(Http2FrameDecoderListener* listener); 59 Http2FrameDecoderListener* listener() const; 60 61 // The decoder will reject frame's whose payload 62 // length field exceeds the maximum payload size. set_maximum_payload_size(size_t v)63 void set_maximum_payload_size(size_t v) { maximum_payload_size_ = v; } maximum_payload_size()64 size_t maximum_payload_size() const { return maximum_payload_size_; } 65 66 // Decodes the input up to the next frame boundary (i.e. at most one frame). 67 // 68 // Returns kDecodeDone if it decodes the final byte of a frame, OR if there 69 // is no input and it is awaiting the start of a new frame (e.g. if this 70 // is the first call to DecodeFrame, or if the previous call returned 71 // kDecodeDone). 72 // 73 // Returns kDecodeInProgress if it decodes all of the decode buffer, but has 74 // not reached the end of the frame. 75 // 76 // Returns kDecodeError if the frame's padding or length wasn't valid (i.e. if 77 // the decoder called either the listener's OnPaddingTooLong or 78 // OnFrameSizeError method). 79 // 80 // If the decode buffer contains the entirety of a frame payload or field, 81 // then the corresponding Http2FrameDecoderListener::On*Payload(), 82 // OnHpackFragment(), OnGoAwayOpaqueData(), or OnAltSvcValueData() method is 83 // guaranteed to be called exactly once, with the entire payload or field in a 84 // single chunk. 85 DecodeStatus DecodeFrame(DecodeBuffer* db); 86 87 ////////////////////////////////////////////////////////////////////////////// 88 // Methods that support Http2FrameDecoderAdapter. 89 90 // Is the remainder of the frame's payload being discarded? IsDiscardingPayload()91 bool IsDiscardingPayload() const { return state_ == State::kDiscardPayload; } 92 93 // Returns the number of bytes of the frame's payload that remain to be 94 // decoded, excluding any trailing padding. This method must only be called 95 // after the frame header has been decoded AND DecodeFrame has returned 96 // kDecodeInProgress. 97 size_t remaining_payload() const; 98 99 // Returns the number of bytes of trailing padding after the payload that 100 // remain to be decoded. This method must only be called if the frame type 101 // allows padding, and after the frame header has been decoded AND 102 // DecodeFrame has returned. Will return 0 if the Pad Length field has not 103 // yet been decoded. 104 uint32_t remaining_padding() const; 105 106 private: 107 enum class State { 108 // Ready to start decoding a new frame's header. 109 kStartDecodingHeader, 110 // Was in state kStartDecodingHeader, but unable to read the entire frame 111 // header, so needs more input to complete decoding the header. 112 kResumeDecodingHeader, 113 114 // Have decoded the frame header, and started decoding the available bytes 115 // of the frame's payload, but need more bytes to finish the job. 116 kResumeDecodingPayload, 117 118 // Decoding of the most recently started frame resulted in an error: 119 // OnPaddingTooLong or OnFrameSizeError was called to indicate that the 120 // decoder detected a problem, or OnFrameHeader returned false, indicating 121 // that the listener detected a problem. Regardless of which, the decoder 122 // will stay in state kDiscardPayload until it has been passed the rest 123 // of the bytes of the frame's payload that it hasn't yet seen, after 124 // which it will be ready to decode another frame. 125 kDiscardPayload, 126 }; 127 128 friend class test::Http2FrameDecoderPeer; 129 QUICHE_EXPORT friend std::ostream& operator<<(std::ostream& out, State v); 130 131 DecodeStatus StartDecodingPayload(DecodeBuffer* db); 132 DecodeStatus ResumeDecodingPayload(DecodeBuffer* db); 133 DecodeStatus DiscardPayload(DecodeBuffer* db); 134 frame_header()135 const Http2FrameHeader& frame_header() const { 136 return frame_decoder_state_.frame_header(); 137 } 138 139 // Clear any of the flags in the frame header that aren't set in valid_flags. 140 void RetainFlags(uint8_t valid_flags); 141 142 // Clear all of the flags in the frame header; for use with frame types that 143 // don't define any flags, such as WINDOW_UPDATE. 144 void ClearFlags(); 145 146 // These methods call the StartDecodingPayload() method of the frame type's 147 // payload decoder, after first clearing invalid flags in the header. The 148 // caller must ensure that the decode buffer does not extend beyond the 149 // end of the payload (handled by Http2FrameDecoder::StartDecodingPayload). 150 DecodeStatus StartDecodingAltSvcPayload(DecodeBuffer* db); 151 DecodeStatus StartDecodingContinuationPayload(DecodeBuffer* db); 152 DecodeStatus StartDecodingDataPayload(DecodeBuffer* db); 153 DecodeStatus StartDecodingGoAwayPayload(DecodeBuffer* db); 154 DecodeStatus StartDecodingHeadersPayload(DecodeBuffer* db); 155 DecodeStatus StartDecodingPingPayload(DecodeBuffer* db); 156 DecodeStatus StartDecodingPriorityPayload(DecodeBuffer* db); 157 DecodeStatus StartDecodingPriorityUpdatePayload(DecodeBuffer* db); 158 DecodeStatus StartDecodingPushPromisePayload(DecodeBuffer* db); 159 DecodeStatus StartDecodingRstStreamPayload(DecodeBuffer* db); 160 DecodeStatus StartDecodingSettingsPayload(DecodeBuffer* db); 161 DecodeStatus StartDecodingUnknownPayload(DecodeBuffer* db); 162 DecodeStatus StartDecodingWindowUpdatePayload(DecodeBuffer* db); 163 164 // These methods call the ResumeDecodingPayload() method of the frame type's 165 // payload decoder; they are called only if the preceding call to the 166 // corresponding Start method (above) returned kDecodeInProgress, as did any 167 // subsequent calls to the resume method. 168 // Unlike the Start methods, the decode buffer may extend beyond the 169 // end of the payload, so the method will create a DecodeBufferSubset 170 // before calling the ResumeDecodingPayload method of the frame type's 171 // payload decoder. 172 DecodeStatus ResumeDecodingAltSvcPayload(DecodeBuffer* db); 173 DecodeStatus ResumeDecodingContinuationPayload(DecodeBuffer* db); 174 DecodeStatus ResumeDecodingDataPayload(DecodeBuffer* db); 175 DecodeStatus ResumeDecodingGoAwayPayload(DecodeBuffer* db); 176 DecodeStatus ResumeDecodingHeadersPayload(DecodeBuffer* db); 177 DecodeStatus ResumeDecodingPingPayload(DecodeBuffer* db); 178 DecodeStatus ResumeDecodingPriorityPayload(DecodeBuffer* db); 179 DecodeStatus ResumeDecodingPriorityUpdatePayload(DecodeBuffer* db); 180 DecodeStatus ResumeDecodingPushPromisePayload(DecodeBuffer* db); 181 DecodeStatus ResumeDecodingRstStreamPayload(DecodeBuffer* db); 182 DecodeStatus ResumeDecodingSettingsPayload(DecodeBuffer* db); 183 DecodeStatus ResumeDecodingUnknownPayload(DecodeBuffer* db); 184 DecodeStatus ResumeDecodingWindowUpdatePayload(DecodeBuffer* db); 185 186 FrameDecoderState frame_decoder_state_; 187 188 // We only need one payload decoder at a time, so they share the same storage. 189 union { 190 AltSvcPayloadDecoder altsvc_payload_decoder_; 191 ContinuationPayloadDecoder continuation_payload_decoder_; 192 DataPayloadDecoder data_payload_decoder_; 193 GoAwayPayloadDecoder goaway_payload_decoder_; 194 HeadersPayloadDecoder headers_payload_decoder_; 195 PingPayloadDecoder ping_payload_decoder_; 196 PriorityPayloadDecoder priority_payload_decoder_; 197 PriorityUpdatePayloadDecoder priority_payload_update_decoder_; 198 PushPromisePayloadDecoder push_promise_payload_decoder_; 199 RstStreamPayloadDecoder rst_stream_payload_decoder_; 200 SettingsPayloadDecoder settings_payload_decoder_; 201 UnknownPayloadDecoder unknown_payload_decoder_; 202 WindowUpdatePayloadDecoder window_update_payload_decoder_; 203 }; 204 205 State state_; 206 size_t maximum_payload_size_; 207 208 // Listener used whenever caller passes nullptr to set_listener. 209 Http2FrameDecoderNoOpListener no_op_listener_; 210 }; 211 212 } // namespace http2 213 214 #endif // QUICHE_HTTP2_DECODER_HTTP2_FRAME_DECODER_H_ 215