Ejemplo n.º 1
0
        /// <summary>
        /// Receives an Stdin record, passes it to the Owin Pipeline and returns a Task that when completed, indicates this FastCGI Request has been answered to and is ended.
        /// The task's result indicates if the connection needs to be closed from the application's side.
        /// </summary>
        /// <remarks>This method can return null if there are more Stdin records yet to be received. In fact, the request is only passed to the Owin pipeline after all stdin records have been received.</remarks>
        public Task ProcessRequest()
        {
            // Sign up for the first write, because we need to send the headers when that happens (Owin spec)
            // Also sign up to flush buffers for the application if we can
            stdout.OnFirstWrite += SendHeaders;
            if (FlushPeriodically)
            {
                stdout.OnStreamFill += () => stdout.Flush();
            }

            // Now pass it to the Owin pipeline
            Task applicationResponse;

            try
            {
                applicationResponse = ApplicationPipelineEntry(OwinContext);
            }
            catch (Exception e)
            {
                if (Logger != null)
                {
                    Logger.LogApplicationError(e, new RequestInfo(this));
                }

                // Show the exception to the visitor
                SendErrorPage(e);

                // End the FastCgi connection with an error code
                SendEndRequest(-1, ProtocolStatus.RequestComplete);

                // Return a task that indicates completion..
                return(Task.Factory.StartNew(() => {}));
            }

            // Now set the actions to do when the response is ready
            return(applicationResponse.ContinueWith(applicationTask =>
            {
                if (applicationTask.IsFaulted)
                {
                    Exception e = applicationTask.Exception;

                    if (Logger != null)
                    {
                        Logger.LogApplicationError(e, new RequestInfo(this));
                    }

                    // Show the exception to the visitor
                    SendErrorPage(e);

                    SendEndRequest(-1, ProtocolStatus.RequestComplete);
                }
                else if (!OwinContext.SomeResponseExists)
                {
                    // If we are here, then no response was set by the application, i.e. not a single header or response body
                    SendEmptyResponsePage();

                    SendEndRequest(-1, ProtocolStatus.RequestComplete);
                }
                else
                {
                    // If no data has been written (e.g. 304 w/- empty response), the headers won't have been written
                    if (stdout.Length == 0)
                    {
                        SendHeaders();
                    }

                    // Signal successful return status
                    SendEndRequest(0, ProtocolStatus.RequestComplete);
                }
            }));
        }