Example #1
0
        private void ProcessIncomingFrames(List <HTTP2FrameHeaderAndPayload> outgoingFrames)
        {
            UInt32 windowUpdate = 0;

            while (this.incomingFrames.Count > 0)
            {
                HTTP2FrameHeaderAndPayload frame = this.incomingFrames.Dequeue();

                if ((this.isRSTFrameSent || this.AssignedRequest.IsCancellationRequested) && frame.Type != HTTP2FrameTypes.HEADERS && frame.Type != HTTP2FrameTypes.CONTINUATION)
                {
                    BufferPool.Release(frame.Payload);
                    continue;
                }

                if (/*HTTPManager.Logger.Level == Logger.Loglevels.All && */ frame.Type != HTTP2FrameTypes.DATA && frame.Type != HTTP2FrameTypes.WINDOW_UPDATE)
                {
                    HTTPManager.Logger.Information("HTTP2Stream", string.Format("[{0}] Process - processing frame: {1}", this.Id, frame.ToString()), this.Context, this.AssignedRequest.Context, this.parent.Context);
                }

                switch (frame.Type)
                {
                case HTTP2FrameTypes.HEADERS:
                case HTTP2FrameTypes.CONTINUATION:
                    if (this.State != HTTP2StreamStates.HalfClosedLocal)
                    {
                        // ERROR!
                        continue;
                    }

                    // payload will be released by the view
                    frame.DontUseMemPool = true;

                    if (this.headerView == null)
                    {
                        this.AssignedRequest.Timing.Add(TimingEventNames.Waiting_TTFB);
                        this.headerView = new FramesAsStreamView(new HeaderFrameView());
                    }

                    this.headerView.AddFrame(frame);

                    // END_STREAM may arrive sooner than an END_HEADERS, so we have to store that we already received it
                    if ((frame.Flags & (byte)HTTP2HeadersFlags.END_STREAM) != 0)
                    {
                        this.isEndSTRReceived = true;
                    }

                    if ((frame.Flags & (byte)HTTP2HeadersFlags.END_HEADERS) != 0)
                    {
                        List <KeyValuePair <string, string> > headers = new List <KeyValuePair <string, string> >();

                        try
                        {
                            this.encoder.Decode(this, this.headerView, headers);
                        }
                        catch (Exception ex)
                        {
                            HTTPManager.Logger.Exception("HTTP2Stream", string.Format("[{0}] ProcessIncomingFrames - Header Frames: {1}, Encoder: {2}", this.Id, this.headerView.ToString(), this.encoder.ToString()), ex, this.Context, this.AssignedRequest.Context, this.parent.Context);
                        }

                        this.AssignedRequest.Timing.Add(TimingEventNames.Headers);

                        if (this.isRSTFrameSent)
                        {
                            this.State = HTTP2StreamStates.Closed;
                            break;
                        }

                        if (this.response == null)
                        {
                            this.AssignedRequest.Response = this.response = new HTTP2Response(this.AssignedRequest, false);
                        }

                        this.response.AddHeaders(headers);

                        this.headerView.Close();
                        this.headerView = null;

                        if (this.isEndSTRReceived)
                        {
                            // If there's any trailing header, no data frame has an END_STREAM flag
                            if (this.isStreamedDownload)
                            {
                                this.response.FinishProcessData();
                            }

                            PlatformSupport.Threading.ThreadedRunner.RunShortLiving <HTTP2Stream, FramesAsStreamView>(FinishRequest, this, this.dataView);

                            this.dataView = null;

                            if (this.State == HTTP2StreamStates.HalfClosedLocal)
                            {
                                this.State = HTTP2StreamStates.Closed;
                            }
                            else
                            {
                                this.State = HTTP2StreamStates.HalfClosedRemote;
                            }
                        }
                    }
                    break;

                case HTTP2FrameTypes.DATA:
                    if (this.State != HTTP2StreamStates.HalfClosedLocal)
                    {
                        // ERROR!
                        continue;
                    }

                    this.downloaded += frame.PayloadLength;

                    if (this.isStreamedDownload && frame.Payload != null && frame.PayloadLength > 0)
                    {
                        this.response.ProcessData(frame.Payload, (int)frame.PayloadLength);
                    }

                    // frame's buffer will be released by the frames view
                    frame.DontUseMemPool = !this.isStreamedDownload;

                    if (this.dataView == null && !this.isStreamedDownload)
                    {
                        this.dataView = new FramesAsStreamView(new DataFrameView());
                    }

                    if (!this.isStreamedDownload)
                    {
                        this.dataView.AddFrame(frame);
                    }

                    // Track received data, and if necessary(local window getting too low), send a window update frame
                    if (this.localWindow < frame.PayloadLength)
                    {
                        HTTPManager.Logger.Error("HTTP2Stream", string.Format("[{0}] Frame's PayloadLength ({1:N0}) is larger then local window ({2:N0}). Frame: {3}", this.Id, frame.PayloadLength, this.localWindow, frame), this.Context, this.AssignedRequest.Context, this.parent.Context);
                    }
                    else
                    {
                        this.localWindow -= frame.PayloadLength;
                    }

                    if ((frame.Flags & (byte)HTTP2DataFlags.END_STREAM) != 0)
                    {
                        this.isEndSTRReceived = true;
                    }

                    // Window update logic.
                    //  1.) We could use a logic to only send window update(s) after a threshold is reached.
                    //      When the initial window size is high enough to contain the whole or most of the result,
                    //      sending back two window updates (connection and stream) after every data frame is pointless.
                    //  2.) On the other hand, window updates are cheap and works even when initial window size is low.
                    //          (
                    if (this.isEndSTRReceived || this.localWindow <= this.windowUpdateThreshold)
                    {
                        windowUpdate += this.settings.MySettings[HTTP2Settings.INITIAL_WINDOW_SIZE] - this.localWindow - windowUpdate;
                    }

                    if (this.isEndSTRReceived)
                    {
                        if (this.isStreamedDownload)
                        {
                            this.response.FinishProcessData();
                        }

                        HTTPManager.Logger.Information("HTTP2Stream", string.Format("[{0}] All data arrived, data length: {1:N0}", this.Id, this.downloaded), this.Context, this.AssignedRequest.Context, this.parent.Context);

                        // create a short living thread to process the downloaded data:
                        PlatformSupport.Threading.ThreadedRunner.RunShortLiving <HTTP2Stream, FramesAsStreamView>(FinishRequest, this, this.dataView);

                        this.dataView = null;

                        if (this.State == HTTP2StreamStates.HalfClosedLocal)
                        {
                            this.State = HTTP2StreamStates.Closed;
                        }
                        else
                        {
                            this.State = HTTP2StreamStates.HalfClosedRemote;
                        }
                    }
                    else if (this.AssignedRequest.OnDownloadProgress != null)
                    {
                        RequestEventHelper.EnqueueRequestEvent(new RequestEventInfo(this.AssignedRequest,
                                                                                    RequestEvents.DownloadProgress,
                                                                                    downloaded,
                                                                                    this.response.ExpectedContentLength));
                    }

                    break;

                case HTTP2FrameTypes.WINDOW_UPDATE:
                    HTTP2WindowUpdateFrame windowUpdateFrame = HTTP2FrameHelper.ReadWindowUpdateFrame(frame);

                    if (HTTPManager.Logger.Level == Logger.Loglevels.All)
                    {
                        HTTPManager.Logger.Information("HTTP2Stream", string.Format("[{0}] Received Window Update: {1:N0}, new remoteWindow: {2:N0}, initial remote window: {3:N0}, total data sent: {4:N0}", this.Id, windowUpdateFrame.WindowSizeIncrement, this.remoteWindow + windowUpdateFrame.WindowSizeIncrement, this.settings.RemoteSettings[HTTP2Settings.INITIAL_WINDOW_SIZE], this.sentData), this.Context, this.AssignedRequest.Context, this.parent.Context);
                    }

                    this.remoteWindow += windowUpdateFrame.WindowSizeIncrement;
                    break;

                case HTTP2FrameTypes.RST_STREAM:
                    // https://httpwg.org/specs/rfc7540.html#RST_STREAM

                    // It's possible to receive an RST_STREAM on a closed stream. In this case, we have to ignore it.
                    if (this.State == HTTP2StreamStates.Closed)
                    {
                        break;
                    }

                    var rstStreamFrame = HTTP2FrameHelper.ReadRST_StreamFrame(frame);

                    HTTPManager.Logger.Error("HTTP2Stream", string.Format("[{0}] RST Stream frame ({1}) received in state {2}!", this.Id, rstStreamFrame, this.State), this.Context, this.AssignedRequest.Context, this.parent.Context);

                    Abort(string.Format("RST_STREAM frame received! Error code: {0}({1})", rstStreamFrame.Error.ToString(), rstStreamFrame.ErrorCode));
                    break;

                default:
                    HTTPManager.Logger.Warning("HTTP2Stream", string.Format("[{0}] Unexpected frame ({1}, Payload: {2}) in state {3}!", this.Id, frame, frame.PayloadAsHex(), this.State), this.Context, this.AssignedRequest.Context, this.parent.Context);
                    break;
                }

                if (!frame.DontUseMemPool)
                {
                    BufferPool.Release(frame.Payload);
                }
            }

            if (windowUpdate > 0)
            {
                if (HTTPManager.Logger.Level <= Logger.Loglevels.All)
                {
                    HTTPManager.Logger.Information("HTTP2Stream", string.Format("[{0}] Sending window update: {1:N0}, current window: {2:N0}, initial window size: {3:N0}", this.Id, windowUpdate, this.localWindow, this.settings.MySettings[HTTP2Settings.INITIAL_WINDOW_SIZE]), this.Context, this.AssignedRequest.Context, this.parent.Context);
                }

                this.localWindow += windowUpdate;

                outgoingFrames.Add(HTTP2FrameHelper.CreateWindowUpdateFrame(this.Id, windowUpdate));
            }
        }