internal static void WriteFrameHeaderInternal(IByteBuffer output, int payloadLength, Http2FrameTypes type, Http2Flags flags, int streamId) { _ = output.WriteMedium(payloadLength); _ = output.WriteByte((int)type); _ = output.WriteByte(flags.Value); _ = output.WriteInt(streamId); }
public void LogUnknownFrame(Direction direction, IChannelHandlerContext ctx, Http2FrameTypes frameType, int streamId, Http2Flags flags, IByteBuffer data) { if (IsEnabled()) { _logger.Log(_level, "{} {} UNKNOWN: frameType={} streamId={} flags={} length={} bytes={}", ctx.Channel, direction, (byte)frameType & 0xFF, streamId, flags.Value, data.ReadableBytes, ToString(data)); } }
public PriorityHeadersFrameHeadersContinuation(DefaultHttp2FrameReader reader, IChannelHandlerContext ctx, int streamId, int padding, int streamDependency, short weight, bool exclusive, Http2Flags headersFlags) : base(reader, ctx, streamId, padding) { _streamDependency = streamDependency; _weight = weight; _exclusive = exclusive; _headersFlags = headersFlags; }
public virtual Task WriteFrameAsync(IChannelHandlerContext ctx, Http2FrameTypes frameType, int streamId, Http2Flags flags, IByteBuffer payload, IPromise promise) { SimplePromiseAggregator promiseAggregator = new SimplePromiseAggregator(promise); try { if (streamId < 0) { ThrowHelper.ThrowArgumentException_PositiveOrZero(ExceptionArgument.StreamID); } IByteBuffer buf = ctx.Allocator.Buffer(Http2CodecUtil.FrameHeaderLength); // Assume nothing below will throw until buf is written. That way we don't have to take care of ownership // in the catch block. Http2CodecUtil.WriteFrameHeaderInternal(buf, payload.ReadableBytes, frameType, flags, streamId); _ = ctx.WriteAsync(buf, promiseAggregator.NewPromise()); } catch (Exception t) { try { _ = payload.Release(); } finally { promiseAggregator.SetException(t); _ = promiseAggregator.DoneAllocatingPromises(); } return(promiseAggregator.Task); } try { _ = ctx.WriteAsync(payload, promiseAggregator.NewPromise()); } catch (Exception t) { promiseAggregator.SetException(t); } _ = promiseAggregator.DoneAllocatingPromises(); return(promiseAggregator.Task); }
void ReadHeadersFrame(IChannelHandlerContext ctx, IByteBuffer payload, int payloadEndIndex, IHttp2FrameListener listener) { int headersStreamId = _streamId; Http2Flags headersFlags = _flags; int padding = ReadPadding(payload); VerifyPadding(padding); // The callback that is invoked is different depending on whether priority information // is present in the headers frame. if (headersFlags.PriorityPresent()) { long word1 = payload.ReadUnsignedInt(); bool exclusive = (word1 & 0x80000000L) != 0; int streamDependency = (int)(uint)(word1 & 0x7FFFFFFFL); if (streamDependency == headersStreamId) { ThrowHelper.ThrowStreamError_AStreamCannotDependOnItself(headersStreamId); } short weight = (short)(payload.ReadByte() + 1); int lenToRead = LengthWithoutTrailingPadding(payloadEndIndex - payload.ReaderIndex, padding); // Create a handler that invokes the listener when the header block is complete. _headersContinuation = new PriorityHeadersFrameHeadersContinuation(this, ctx, headersStreamId, padding, streamDependency, weight, exclusive, headersFlags); // Process the initial fragment, invoking the listener's callback if end of headers. _headersContinuation.ProcessFragment(headersFlags.EndOfHeaders(), payload, lenToRead, listener); ResetHeadersContinuationIfEnd(headersFlags.EndOfHeaders()); return; } // The priority fields are not present in the frame. Prepare a continuation that invokes // the listener callback without priority information. _headersContinuation = new HeadersFrameHeadersContinuation(this, ctx, headersStreamId, padding, headersFlags); // Process the initial fragment, invoking the listener's callback if end of headers. int dataLength = LengthWithoutTrailingPadding(payloadEndIndex - payload.ReaderIndex, padding); _headersContinuation.ProcessFragment(headersFlags.EndOfHeaders(), payload, dataLength, listener); ResetHeadersContinuationIfEnd(headersFlags.EndOfHeaders()); }
/// <summary> /// Writes as many continuation frames as needed until <paramref name="headerBlock"/> are consumed. /// </summary> /// <param name="ctx"></param> /// <param name="streamId"></param> /// <param name="headerBlock"></param> /// <param name="promiseAggregator"></param> /// <returns></returns> Task WriteContinuationFramesAsync(IChannelHandlerContext ctx, int streamId, IByteBuffer headerBlock, SimplePromiseAggregator promiseAggregator) { Http2Flags flags = new Http2Flags(); if (headerBlock.IsReadable()) { // The frame header (and padding) only changes on the last frame, so allocate it once and re-use int fragmentReadableBytes = Math.Min(headerBlock.ReadableBytes, _maxFrameSize); IByteBuffer buf = ctx.Allocator.Buffer(Http2CodecUtil.ContinuationFrameHeaderLength); Http2CodecUtil.WriteFrameHeaderInternal(buf, fragmentReadableBytes, Http2FrameTypes.Continuation, flags, streamId); do { fragmentReadableBytes = Math.Min(headerBlock.ReadableBytes, _maxFrameSize); IByteBuffer fragment = headerBlock.ReadRetainedSlice(fragmentReadableBytes); if (headerBlock.IsReadable()) { _ = ctx.WriteAsync(buf.Retain(), promiseAggregator.NewPromise()); } else { // The frame header is different for the last frame, so re-allocate and release the old buffer flags = flags.EndOfHeaders(true); _ = buf.Release(); buf = ctx.Allocator.Buffer(Http2CodecUtil.ContinuationFrameHeaderLength); Http2CodecUtil.WriteFrameHeaderInternal(buf, fragmentReadableBytes, Http2FrameTypes.Continuation, flags, streamId); _ = ctx.WriteAsync(buf, promiseAggregator.NewPromise()); } _ = ctx.WriteAsync(fragment, promiseAggregator.NewPromise()); }while (headerBlock.IsReadable()); } return(promiseAggregator.Task); }
Task WriteHeadersInternal(IChannelHandlerContext ctx, int streamId, IHttp2Headers headers, int padding, bool endOfStream, bool hasPriority, int streamDependency, short weight, bool exclusive, IPromise promise) { IByteBuffer headerBlock = null; SimplePromiseAggregator promiseAggregator = new SimplePromiseAggregator(promise); try { if ((uint)(streamId - 1) > SharedConstants.TooBigOrNegative) { ThrowHelper.ThrowArgumentException_Positive(ExceptionArgument.StreamID); } if (hasPriority) { if (streamDependency < 0) { ThrowHelper.ThrowArgumentException_PositiveOrZero(ExceptionArgument.StreamDependency); } Http2CodecUtil.VerifyPadding(padding); VerifyWeight(weight); } // Encode the entire header block. headerBlock = ctx.Allocator.Buffer(); _headersEncoder.EncodeHeaders(streamId, headers, headerBlock); Http2Flags flags = new Http2Flags().EndOfStream(endOfStream).PriorityPresent(hasPriority).PaddingPresent(padding > 0); // Read the first fragment (possibly everything). int nonFragmentBytes = padding + flags.GetNumPriorityBytes(); int maxFragmentLength = _maxFrameSize - nonFragmentBytes; IByteBuffer fragment = headerBlock.ReadRetainedSlice(Math.Min(headerBlock.ReadableBytes, maxFragmentLength)); // Set the end of headers flag for the first frame. _ = flags.EndOfHeaders(!headerBlock.IsReadable()); int payloadLength = fragment.ReadableBytes + nonFragmentBytes; IByteBuffer buf = ctx.Allocator.Buffer(Http2CodecUtil.HeadersFrameHeaderLength); Http2CodecUtil.WriteFrameHeaderInternal(buf, payloadLength, Http2FrameTypes.Headers, flags, streamId); WritePaddingLength(buf, padding); if (hasPriority) { _ = buf.WriteInt(exclusive ? (int)(uint)(0x80000000L | streamDependency) : streamDependency); // Adjust the weight so that it fits into a single byte on the wire. _ = buf.WriteByte(weight - 1); } _ = ctx.WriteAsync(buf, promiseAggregator.NewPromise()); // Write the first fragment. _ = ctx.WriteAsync(fragment, promiseAggregator.NewPromise()); // Write out the padding, if any. if (PaddingBytes(padding) > 0) { _ = ctx.WriteAsync(ZeroBuffer.Slice(0, PaddingBytes(padding)), promiseAggregator.NewPromise()); } if (!flags.EndOfHeaders()) { _ = WriteContinuationFramesAsync(ctx, streamId, headerBlock, promiseAggregator); } } catch (Http2Exception e) { promiseAggregator.SetException(e); } catch (Exception t) { promiseAggregator.SetException(t); _ = promiseAggregator.DoneAllocatingPromises(); throw; } finally { _ = (headerBlock?.Release()); } _ = promiseAggregator.DoneAllocatingPromises(); return(promiseAggregator.Task); }
public virtual Task WritePushPromiseAsync(IChannelHandlerContext ctx, int streamId, int promisedStreamId, IHttp2Headers headers, int padding, IPromise promise) { IByteBuffer headerBlock = null; SimplePromiseAggregator promiseAggregator = new SimplePromiseAggregator(promise); try { if ((uint)(streamId - 1) > SharedConstants.TooBigOrNegative) { ThrowHelper.ThrowArgumentException_Positive(ExceptionArgument.StreamID); } if (promisedStreamId <= 0) { ThrowHelper.ThrowArgumentException_Positive(ExceptionArgument.PromisedStreamId); } Http2CodecUtil.VerifyPadding(padding); // Encode the entire header block into an intermediate buffer. headerBlock = ctx.Allocator.Buffer(); _headersEncoder.EncodeHeaders(streamId, headers, headerBlock); // Read the first fragment (possibly everything). Http2Flags flags = new Http2Flags().PaddingPresent(padding > 0); // IntFieldLength is for the length of the promisedStreamId int nonFragmentLength = Http2CodecUtil.IntFieldLength + padding; int maxFragmentLength = _maxFrameSize - nonFragmentLength; IByteBuffer fragment = headerBlock.ReadRetainedSlice(Math.Min(headerBlock.ReadableBytes, maxFragmentLength)); _ = flags.EndOfHeaders(!headerBlock.IsReadable()); int payloadLength = fragment.ReadableBytes + nonFragmentLength; IByteBuffer buf = ctx.Allocator.Buffer(Http2CodecUtil.PushPromiseFrameHeaderLength); Http2CodecUtil.WriteFrameHeaderInternal(buf, payloadLength, Http2FrameTypes.PushPromise, flags, streamId); WritePaddingLength(buf, padding); // Write out the promised stream ID. _ = buf.WriteInt(promisedStreamId); _ = ctx.WriteAsync(buf, promiseAggregator.NewPromise()); // Write the first fragment. _ = ctx.WriteAsync(fragment, promiseAggregator.NewPromise()); // Write out the padding, if any. if (PaddingBytes(padding) > 0) { _ = ctx.WriteAsync(ZeroBuffer.Slice(0, PaddingBytes(padding)), promiseAggregator.NewPromise()); } if (!flags.EndOfHeaders()) { _ = WriteContinuationFramesAsync(ctx, streamId, headerBlock, promiseAggregator); } } catch (Http2Exception e) { promiseAggregator.SetException(e); } catch (Exception t) { promiseAggregator.SetException(t); _ = promiseAggregator.DoneAllocatingPromises(); throw; } finally { if (headerBlock is object) { _ = headerBlock.Release(); } } _ = promiseAggregator.DoneAllocatingPromises(); return(promiseAggregator.Task); }
public Task WriteFrameAsync(IChannelHandlerContext ctx, Http2FrameTypes frameType, int streamId, Http2Flags flags, IByteBuffer payload, IPromise promise) { _logger.LogUnknownFrame(Direction.Outbound, ctx, frameType, streamId, flags, payload); return(_writer.WriteFrameAsync(ctx, frameType, streamId, flags, payload, promise)); }
public DefaultHttp2UnknownFrame(Http2FrameTypes frameType, Http2Flags flags, IByteBuffer data) : base(data) { _frameType = frameType; _flags = flags; }
public void OnUnknownFrame(IChannelHandlerContext ctx, Http2FrameTypes frameType, int streamId, Http2Flags flags, IByteBuffer payload) { _logger.LogUnknownFrame(Direction.Inbound, ctx, frameType, streamId, flags, payload); _listener.OnUnknownFrame(ctx, frameType, streamId, flags, payload); }
public HeadersFrameHeadersContinuation(DefaultHttp2FrameReader reader, IChannelHandlerContext ctx, int streamId, int padding, Http2Flags headersFlags) : base(reader, ctx, streamId, padding) { _headersFlags = headersFlags; }
void ProcessHeaderState(IByteBuffer input) { if (input.ReadableBytes < Http2CodecUtil.FrameHeaderLength) { // Wait until the entire frame header has been read. return; } // Read the header and prepare the unmarshaller to read the frame. _payloadLength = input.ReadUnsignedMedium(); if (_payloadLength > _maxFrameSize) { ThrowHelper.ThrowConnectionError_FrameLengthExceedsMaximum(_payloadLength, _maxFrameSize); } _frameType = (Http2FrameTypes)input.ReadByte(); _flags = new Http2Flags(input.ReadByte()); _streamId = Http2CodecUtil.ReadUnsignedInt(input); // We have consumed the data, next time we read we will be expecting to read the frame payload. _readingHeaders = false; switch (_frameType) { case Http2FrameTypes.Data: VerifyDataFrame(); break; case Http2FrameTypes.Headers: VerifyHeadersFrame(); break; case Http2FrameTypes.Priority: VerifyPriorityFrame(); break; case Http2FrameTypes.RstStream: VerifyRstStreamFrame(); break; case Http2FrameTypes.Settings: VerifySettingsFrame(); break; case Http2FrameTypes.PushPromise: VerifyPushPromiseFrame(); break; case Http2FrameTypes.Ping: VerifyPingFrame(); break; case Http2FrameTypes.GoAway: VerifyGoAwayFrame(); break; case Http2FrameTypes.WindowUpdate: VerifyWindowUpdateFrame(); break; case Http2FrameTypes.Continuation: VerifyContinuationFrame(); break; default: // Unknown frame type, could be an extension. VerifyUnknownFrame(); break; } }
/// <summary> /// Writes an HTTP/2 frame header to the output buffer. /// </summary> /// <param name="output"></param> /// <param name="payloadLength"></param> /// <param name="type"></param> /// <param name="flags"></param> /// <param name="streamId"></param> public static void WriteFrameHeader(IByteBuffer output, int payloadLength, Http2FrameTypes type, Http2Flags flags, int streamId) { _ = output.EnsureWritable(FrameHeaderLength + payloadLength); WriteFrameHeaderInternal(output, payloadLength, type, flags, streamId); }
public virtual Task WriteDataAsync(IChannelHandlerContext ctx, int streamId, IByteBuffer data, int padding, bool endOfStream, IPromise promise) { SimplePromiseAggregator promiseAggregator = new SimplePromiseAggregator(promise); IByteBuffer frameHeader = null; try { if ((uint)(streamId - 1) > SharedConstants.TooBigOrNegative) { ThrowHelper.ThrowArgumentException_Positive(ExceptionArgument.StreamID); } Http2CodecUtil.VerifyPadding(padding); int remainingData = data.ReadableBytes; Http2Flags flags = new Http2Flags(); _ = flags.EndOfStream(false); _ = flags.PaddingPresent(false); // Fast path to write frames of payload size maxFrameSize first. if (remainingData > _maxFrameSize) { frameHeader = ctx.Allocator.Buffer(Http2CodecUtil.FrameHeaderLength); Http2CodecUtil.WriteFrameHeaderInternal(frameHeader, _maxFrameSize, Http2FrameTypes.Data, flags, streamId); do { // Write the header. _ = ctx.WriteAsync(frameHeader.RetainedSlice(), promiseAggregator.NewPromise()); // Write the payload. _ = ctx.WriteAsync(data.ReadRetainedSlice(_maxFrameSize), promiseAggregator.NewPromise()); remainingData -= _maxFrameSize; // Stop iterating if remainingData == _maxFrameSize so we can take care of reference counts below. }while (remainingData > _maxFrameSize); } if (0u >= (uint)padding) { // Write the header. if (frameHeader is object) { _ = frameHeader.Release(); frameHeader = null; } IByteBuffer frameHeader2 = ctx.Allocator.Buffer(Http2CodecUtil.FrameHeaderLength); _ = flags.EndOfStream(endOfStream); Http2CodecUtil.WriteFrameHeaderInternal(frameHeader2, remainingData, Http2FrameTypes.Data, flags, streamId); _ = ctx.WriteAsync(frameHeader2, promiseAggregator.NewPromise()); // Write the payload. IByteBuffer lastFrame = data.ReadSlice(remainingData); data = null; _ = ctx.WriteAsync(lastFrame, promiseAggregator.NewPromise()); } else { if (remainingData != _maxFrameSize) { if (frameHeader is object) { _ = frameHeader.Release(); frameHeader = null; } } else { remainingData -= _maxFrameSize; // Write the header. IByteBuffer lastFrame; if (frameHeader is null) { lastFrame = ctx.Allocator.Buffer(Http2CodecUtil.FrameHeaderLength); Http2CodecUtil.WriteFrameHeaderInternal(lastFrame, _maxFrameSize, Http2FrameTypes.Data, flags, streamId); } else { lastFrame = frameHeader.Slice(); frameHeader = null; } _ = ctx.WriteAsync(lastFrame, promiseAggregator.NewPromise()); // Write the payload. lastFrame = data.ReadableBytes != _maxFrameSize?data.ReadSlice(_maxFrameSize) : data; data = null; _ = ctx.WriteAsync(lastFrame, promiseAggregator.NewPromise()); } do { int frameDataBytes = Math.Min(remainingData, _maxFrameSize); int framePaddingBytes = Math.Min(padding, Math.Max(0, (_maxFrameSize - 1) - frameDataBytes)); // Decrement the remaining counters. padding -= framePaddingBytes; remainingData -= frameDataBytes; // Write the header. IByteBuffer frameHeader2 = ctx.Allocator.Buffer(Http2CodecUtil.DataFrameHeaderLength); _ = flags.EndOfStream(endOfStream && 0u >= (uint)remainingData && 0u >= (uint)padding); _ = flags.PaddingPresent(framePaddingBytes > 0); Http2CodecUtil.WriteFrameHeaderInternal(frameHeader2, framePaddingBytes + frameDataBytes, Http2FrameTypes.Data, flags, streamId); WritePaddingLength(frameHeader2, framePaddingBytes); _ = ctx.WriteAsync(frameHeader2, promiseAggregator.NewPromise()); // Write the payload. if (frameDataBytes != 0) { if (0u >= (uint)remainingData) { IByteBuffer lastFrame = data.ReadSlice(frameDataBytes); data = null; _ = ctx.WriteAsync(lastFrame, promiseAggregator.NewPromise()); } else { _ = ctx.WriteAsync(data.ReadRetainedSlice(frameDataBytes), promiseAggregator.NewPromise()); } } // Write the frame padding. if (PaddingBytes(framePaddingBytes) > 0) { _ = ctx.WriteAsync( ZeroBuffer.Slice(0, PaddingBytes(framePaddingBytes)), promiseAggregator.NewPromise()); } }while (remainingData != 0 || padding != 0); } } catch (Exception cause) { if (frameHeader is object) { _ = frameHeader.Release(); } // Use a try/finally here in case the data has been released before calling this method. This is not // necessary above because we internally allocate frameHeader. try { if (data is object) { _ = data.Release(); } } finally { promiseAggregator.SetException(cause); _ = promiseAggregator.DoneAllocatingPromises(); } return(promiseAggregator.Task); } _ = promiseAggregator.DoneAllocatingPromises(); return(promiseAggregator.Task); }
public virtual void OnUnknownFrame(IChannelHandlerContext ctx, Http2FrameTypes frameType, int streamId, Http2Flags flags, IByteBuffer payload) { _listener.OnUnknownFrame(ctx, frameType, streamId, flags, payload); }
public virtual Task WriteFrameAsync(IChannelHandlerContext ctx, Http2FrameTypes frameType, int streamId, Http2Flags flags, IByteBuffer payload, IPromise promise) { return(_innerWriter.WriteFrameAsync(ctx, frameType, streamId, flags, payload, promise)); }
public DefaultHttp2UnknownFrame(Http2FrameTypes frameType, Http2Flags flags) : this(frameType, flags, Unpooled.Empty) { }