public static bool ReadFrame(ReadOnlyBuffer <byte> readableBuffer, Http2Frame frame, out SequencePosition consumed, out SequencePosition examined)
        {
            consumed = readableBuffer.Start;
            examined = readableBuffer.End;

            if (readableBuffer.Length < Http2Frame.HeaderLength)
            {
                return(false);
            }

            var headerSlice = readableBuffer.Slice(0, Http2Frame.HeaderLength);

            headerSlice.CopyTo(frame.Raw);

            if (readableBuffer.Length < Http2Frame.HeaderLength + frame.Length)
            {
                return(false);
            }

            readableBuffer.Slice(Http2Frame.HeaderLength, frame.Length).CopyTo(frame.Payload);
            consumed = examined = readableBuffer.GetPosition(readableBuffer.Start, Http2Frame.HeaderLength + frame.Length);

            return(true);
        }
Exemple #2
0
        public void ParseFrame(Http2Frame frame)
        {
            var settingsCount = frame.Length / 6;

            for (var i = 0; i < settingsCount; i++)
            {
                var offset = i * 6;
                var id     = (Http2SettingsParameter)((frame.Payload[offset] << 8) | frame.Payload[offset + 1]);
                var value  = (uint)((frame.Payload[offset + 2] << 24)
                                    | (frame.Payload[offset + 3] << 16)
                                    | (frame.Payload[offset + 4] << 8)
                                    | frame.Payload[offset + 5]);

                switch (id)
                {
                case Http2SettingsParameter.SETTINGS_HEADER_TABLE_SIZE:
                    HeaderTableSize = value;
                    break;

                case Http2SettingsParameter.SETTINGS_ENABLE_PUSH:
                    if (value != 0 && value != 1)
                    {
                        throw new Http2SettingsParameterOutOfRangeException(Http2SettingsParameter.SETTINGS_ENABLE_PUSH,
                                                                            lowerBound: 0,
                                                                            upperBound: 1);
                    }

                    EnablePush = value == 1;
                    break;

                case Http2SettingsParameter.SETTINGS_MAX_CONCURRENT_STREAMS:
                    MaxConcurrentStreams = value;
                    break;

                case Http2SettingsParameter.SETTINGS_INITIAL_WINDOW_SIZE:
                    if (value > int.MaxValue)
                    {
                        throw new Http2SettingsParameterOutOfRangeException(Http2SettingsParameter.SETTINGS_INITIAL_WINDOW_SIZE,
                                                                            lowerBound: 0,
                                                                            upperBound: int.MaxValue);
                    }

                    InitialWindowSize = value;
                    break;

                case Http2SettingsParameter.SETTINGS_MAX_FRAME_SIZE:
                    if (value < Http2Frame.MinAllowedMaxFrameSize || value > Http2Frame.MaxAllowedMaxFrameSize)
                    {
                        throw new Http2SettingsParameterOutOfRangeException(Http2SettingsParameter.SETTINGS_MAX_FRAME_SIZE,
                                                                            lowerBound: Http2Frame.MinAllowedMaxFrameSize,
                                                                            upperBound: Http2Frame.MaxAllowedMaxFrameSize);
                    }

                    MaxFrameSize = value;
                    break;

                case Http2SettingsParameter.SETTINGS_MAX_HEADER_LIST_SIZE:
                    MaxHeaderListSize = value;
                    break;

                default:
                    // http://httpwg.org/specs/rfc7540.html#rfc.section.6.5.2
                    //
                    // An endpoint that receives a SETTINGS frame with any unknown or unsupported identifier MUST ignore that setting.
                    break;
                }
            }
        }