Exemplo n.º 1
0
        /// <summary>
        /// Automatically sends the appropriate response to the user agent
        /// and ends execution on the current page or handler.
        /// </summary>
        /// <param name="context">The context of the HTTP request whose response should be set.
        /// Typically this is <see cref="HttpContext.Current"/>.</param>
        /// <exception cref="ThreadAbortException">Typically thrown by ASP.NET in order to prevent additional data from the page being sent to the client and corrupting the response.</exception>
        public virtual void Send(HttpContext context)
        {
            Contract.Requires <ArgumentNullException>(context != null);

            context.Response.Clear();
            context.Response.StatusCode = (int)this.Status;
            MessagingUtilities.ApplyHeadersToResponse(this.Headers, context.Response);
            if (this.ResponseStream != null)
            {
                try {
                    this.ResponseStream.CopyTo(context.Response.OutputStream);
                } catch (HttpException ex) {
                    if (ex.ErrorCode == -2147467259 && context.Response.Output != null)
                    {
                        // Test scenarios can generate this, since the stream is being spoofed:
                        // System.Web.HttpException: OutputStream is not available when a custom TextWriter is used.
                        context.Response.Output.Write(this.Body);
                    }
                    else
                    {
                        throw;
                    }
                }
            }

            context.Response.End();
        }
        /// <summary>
        /// Automatically sends the appropriate response to the user agent.
        /// </summary>
        /// <param name="response">The response to set to this message.</param>
        public virtual void Send(HttpListenerResponse response)
        {
            Requires.NotNull(response, "response");

            response.StatusCode = (int)this.Status;
            MessagingUtilities.ApplyHeadersToResponse(this.Headers, response);
            foreach (HttpCookie httpCookie in this.Cookies)
            {
                var cookie = new Cookie(httpCookie.Name, httpCookie.Value)
                {
                    Expires  = httpCookie.Expires,
                    Path     = httpCookie.Path,
                    HttpOnly = httpCookie.HttpOnly,
                    Secure   = httpCookie.Secure,
                    Domain   = httpCookie.Domain,
                };
                response.AppendCookie(cookie);
            }

            if (this.ResponseStream != null)
            {
                response.ContentLength64 = this.ResponseStream.Length;
                this.ResponseStream.CopyTo(response.OutputStream);
            }

            response.OutputStream.Close();
        }
Exemplo n.º 3
0
        /// <summary>
        /// Automatically sends the appropriate response to the user agent
        /// and ends execution on the current page or handler.
        /// </summary>
        /// <exception cref="ThreadAbortException">Thrown by ASP.NET in order to prevent additional data from the page being sent to the client and corrupting the response.</exception>
        /// <remarks>
        /// Requires a current HttpContext.
        /// </remarks>
        public virtual void Send()
        {
            ErrorUtilities.VerifyOperation(HttpContext.Current != null, MessagingStrings.CurrentHttpContextRequired);

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.StatusCode = (int)this.Status;
            MessagingUtilities.ApplyHeadersToResponse(this.Headers, HttpContext.Current.Response);
            if (this.ResponseStream != null)
            {
                try {
                    this.ResponseStream.CopyTo(HttpContext.Current.Response.OutputStream);
                } catch (HttpException ex) {
                    if (ex.ErrorCode == -2147467259 && HttpContext.Current.Response.Output != null)
                    {
                        // Test scenarios can generate this, since the stream is being spoofed:
                        // System.Web.HttpException: OutputStream is not available when a custom TextWriter is used.
                        HttpContext.Current.Response.Output.Write(this.Body);
                    }
                    else
                    {
                        throw;
                    }
                }
            }
            HttpContext.Current.Response.End();
        }
Exemplo n.º 4
0
        /// <summary>
        /// Automatically sends the appropriate response to the user agent.
        /// </summary>
        /// <param name="response">The response to set to this message.</param>
        public virtual void Send(HttpListenerResponse response)
        {
            Requires.NotNull(response, "response");

            response.StatusCode = (int)this.Status;
            MessagingUtilities.ApplyHeadersToResponse(this.Headers, response);
            if (this.ResponseStream != null)
            {
                response.ContentLength64 = this.ResponseStream.Length;
                this.ResponseStream.CopyTo(response.OutputStream);
            }

            response.OutputStream.Close();
        }
        /// <summary>
        /// Automatically sends the appropriate response to the user agent
        /// and signals ASP.NET to short-circuit the page execution pipeline
        /// now that the response has been completed.
        /// </summary>
        /// <param name="context">The context of the HTTP request whose response should be set.
        /// Typically this is <see cref="HttpContext.Current"/>.</param>
        /// <param name="endRequest">If set to <c>false</c>, this method calls
        /// <see cref="HttpApplication.CompleteRequest"/> rather than <see cref="HttpResponse.End"/>
        /// to avoid a <see cref="ThreadAbortException"/>.</param>
        protected internal virtual void Respond(HttpContextBase context, bool endRequest)
        {
            Requires.NotNull(context, "context");

            context.Response.Clear();
            context.Response.StatusCode = (int)this.Status;
            MessagingUtilities.ApplyHeadersToResponse(this.Headers, context.Response);
            if (this.ResponseStream != null)
            {
                try {
                    this.ResponseStream.CopyTo(context.Response.OutputStream);
                } catch (HttpException ex) {
                    if (ex.ErrorCode == -2147467259 && context.Response.Output != null)
                    {
                        // Test scenarios can generate this, since the stream is being spoofed:
                        // System.Web.HttpException: OutputStream is not available when a custom TextWriter is used.
                        context.Response.Output.Write(this.Body);
                    }
                    else
                    {
                        throw;
                    }
                }
            }

            foreach (string cookieName in this.Cookies)
            {
                var cookie = this.Cookies[cookieName];
                context.Response.AppendCookie(cookie);
            }

            if (endRequest)
            {
                // This approach throws an exception in order that
                // no more code is executed in the calling page.
                // Microsoft no longer recommends this approach.
                context.Response.End();
            }
            else if (context.ApplicationInstance != null)
            {
                // This approach doesn't throw an exception, but
                // still tells ASP.NET to short-circuit most of the
                // request handling pipeline to speed things up.
                context.ApplicationInstance.CompleteRequest();
            }
        }