Send() public method

Sends response using the specified context.
public Send ( IHttpContext context, IResponse response ) : void
context IHttpContext The context.
response IResponse The response.
return void
Ejemplo n.º 1
0
        /// <summary>
        /// Returns pure string content.
        /// </summary>
        /// <param name="context"></param>
        /// <param name="action"></param>
        /// <returns></returns>
        private ProcessingResult ProcessStringContent(RequestContext context, IActionResult action)
        {
            var content = (StringContent) action;
        	context.Response.ContentLength.Value = context.Response.Encoding.GetByteCount(content.Body);
            if (content.ContentType != null)
                context.Response.ContentType.Value = content.ContentType;

            var writer = new ResponseWriter();
            writer.SendHeaders(context.HttpContext, context.Response);
            writer.Send(context.HttpContext, content.Body, context.Response.Encoding);
            context.HttpContext.Stream.Flush();
            return ProcessingResult.Abort;
        }
Ejemplo n.º 2
0
		private void SendInternalServerError(HttpContext context, Exception exception)
		{
			var generator = new ResponseWriter();
			var response = HttpFactory.Current.Get<IResponse>();
			byte[] body = Encoding.ASCII.GetBytes("Internal server error " + exception);
			response.Body.Write(body, 0, body.Length);
			response.Status = HttpStatusCode.InternalServerError;
			response.Reason = exception.Message;
			generator.Send(context, response);
		}
Ejemplo n.º 3
0
		/// <exception cref="Exception">Throwing exception if in debug mode and not exception handler have been specified.</exception>
		private void OnRequest(object sender, RequestEventArgs e)
		{
			var context = (HttpContext) sender;
			HttpFactory.Current = Factory;
			HttpContext.Current = context;

			try
			{
				var args = new RequestEventArgs(context, e.Request, e.Response);
				RequestReceived(this, args);
				if (!args.IsHandled)
				{
					// need to respond to the context.
					var generator = new ResponseWriter();
					generator.Send(context, args.Response);
				}

				// Disconnect when done.
				if (e.Response.HttpVersion == "HTTP/1.0" || e.Response.Connection.Type == ConnectionType.Close)
					context.Disconnect();
			}
			catch (Exception err)
			{
				if (err is HttpException)
				{
					var exception = (HttpException) err;
					ErrorPage(e.Request, e.Response, exception);
				}
				else
				{
					_logger.Debug("Request failed.", err);
#if DEBUG
					if (ExceptionThrown.GetInvocationList().Length == 1)
						throw;
#endif
					ExceptionThrown(this, new ExceptionEventArgs(err));
					SendInternalServerError(context, err);
				}
			}
		}
Ejemplo n.º 4
0
		private void ErrorPage(IRequest request, IResponse response, HttpException exception)
		{
			response.Status = exception.Code;
			response.Reason = exception.Message;
			response.Body.SetLength(0);

			var args = new ErrorPageEventArgs(HttpContext.Current, request, response);
			args.Exception = exception;
			ErrorPageRequested(this, args);

			// Add default error body.
			if (!args.IsHandled)
			{
				string htmlTemplate = "<html><head><title>{1}</title></head><body>Error {0}: {1}</body></html>";
				byte[] body = Encoding.UTF8.GetBytes(string.Format(htmlTemplate, (int) response.Status, response.Reason));
				response.Body.Write(body, 0, body.Length);
			}

			try
			{
				var generator = new ResponseWriter();
				generator.Send(HttpContext.Current, response);
			}
			catch (Exception err)
			{
#if DEBUG
				if (ExceptionThrown.GetInvocationList().Length == 1)
					throw;
#endif
				ExceptionThrown(this, new ExceptionEventArgs(err));
			}
		}
Ejemplo n.º 5
0
        /// <exception cref="Exception">Throwing exception if in debug mode and not exception handler have been specified.</exception>
        private void OnRequest(object sender, RequestEventArgs e)
        {
            var context = (HttpContext) sender;
            HttpFactory.Current = Factory;
            HttpContext.Current = context;

            try
            {
                var args = new RequestEventArgs(context, e.Request, e.Response);
                RequestReceived(this, args);
                if (!args.IsHandled)
                {
                    // need to respond to the context.
                    var generator = new ResponseWriter();
                    generator.Send(context, args.Response);
                }

                // Disconnect when done.
                if (e.Response.HttpVersion == "HTTP/1.0" || e.Response.Connection.Type == ConnectionType.Close)
                    context.Disconnect();
            }
            catch (Exception err)
            {
                if (err is HttpException)
                {
                    var exception = (HttpException) err;
                    SendErrorPage(exception);
                }
                else
                {
                    _logger.Debug("Request failed.", err);
                    SendErrorPage(err);
                }
                e.IsHandled = true;
            }
        }
Ejemplo n.º 6
0
        private void SendErrorPage(Exception exception)
        {
            var httpException = exception as HttpException;
            var response = HttpContext.Current.Response;
            response.Status = httpException != null ? httpException.Code : HttpStatusCode.InternalServerError;
            response.Reason = exception.Message;

            if (response.Body.CanWrite)
                response.Body.SetLength(0);

            var args = new ErrorPageEventArgs(HttpContext.Current) {Exception = exception};
            ErrorPageRequested(this, args);

            try
            {
                var generator = new ResponseWriter();
                if (args.IsHandled)
                    generator.Send(HttpContext.Current, response);
                else
                    generator.SendErrorPage(HttpContext.Current, response, exception);
            }
            catch (Exception err)
            {
                _logger.Error("Failed to display error page", err);
            }
        }