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