/// <summary>
 /// End a HttpHandler Request
 /// </summary>
 public static void EndHttpHandlerRequest(this IResponse httpRes, bool skipHeaders = false, bool skipClose = false, Action<IResponse> afterHeaders = null)
 {
     if (!skipHeaders) httpRes.ApplyGlobalResponseHeaders();
     if (afterHeaders != null) afterHeaders(httpRes);
     if (!skipClose && !httpRes.IsClosed) httpRes.Close();
     HostContext.CompleteRequest(httpRes.Request);
 }
        public static void EndHttpRequest(this IHttpResponse httpRes, bool skipHeaders = false, bool skipClose=false, Action<IHttpResponse> afterBody=null)
        {
            if (!skipHeaders) httpRes.ApplyGlobalResponseHeaders();
            if (afterBody != null) afterBody(httpRes);
            if (!skipClose) httpRes.Close();

            //skipHeaders used when Apache+mod_mono doesn't like:
            //response.OutputStream.Flush();
            //response.Close();
        }
        /// <summary>
        /// End a HttpHandler Request
        /// </summary>
        public static void EndHttpHandlerRequest(this HttpResponseBase httpRes, bool skipHeaders = false, bool skipClose = false, bool closeOutputStream = false, Action<HttpResponseBase> afterHeaders = null)
        {
            if (!skipHeaders) httpRes.ApplyGlobalResponseHeaders();
            if (afterHeaders != null) afterHeaders(httpRes);
            if (closeOutputStream) httpRes.CloseOutputStream();
            else if (!skipClose) httpRes.Close();

            //skipHeaders used when Apache+mod_mono doesn't like:
            //response.OutputStream.Flush();
            //response.Close();
        }
        public static void WriteBytesToResponse(this IResponse res, byte[] responseBytes, string contentType)
        {
            res.ContentType = HostContext.Config.AppendUtf8CharsetOnContentTypes.Contains(contentType)
                ? contentType + ContentFormat.Utf8Suffix
                : contentType;

            res.ApplyGlobalResponseHeaders();
            res.SetContentLength(responseBytes.Length);

            try
            {
                res.OutputStream.Write(responseBytes, 0, responseBytes.Length);
                res.Flush();
            }
            catch (Exception ex)
            {
                ex.HandleResponseWriteException(res.Request, res, contentType);
            }
            finally
            {
                res.EndRequest(skipHeaders: true);
            }
        }
 public static void EndServiceStackRequest(this HttpResponse httpRes, bool skipHeaders=false)
 {
     if (!skipHeaders) httpRes.ApplyGlobalResponseHeaders();
     httpRes.Close();
     EndpointHost.CompleteRequest();
 }
 /// <summary>
 /// End a ServiceStack Request
 /// </summary>
 public static void EndRequest(this HttpResponseBase httpRes, bool skipHeaders = false)
 {
     if (!skipHeaders) httpRes.ApplyGlobalResponseHeaders();
     httpRes.Close();
     HostContext.CompleteRequest(null);
 }
        public static Task WriteErrorToResponse(this IResponse httpRes, IRequest httpReq,
            string contentType, string operationName, string errorMessage, Exception ex, int statusCode)
        {
            if (ex == null)
                ex = new Exception(errorMessage);

            var errorDto = ex.ToErrorResponse();
            HostContext.AppHost.OnExceptionTypeFilter(ex, errorDto.ResponseStatus);

            if (HandleCustomErrorHandler(httpRes, httpReq, contentType, statusCode, errorDto))
                return TypeConstants.EmptyTask;

            if ((httpRes.ContentType == null || httpRes.ContentType == MimeTypes.Html) 
                && contentType != null && contentType != httpRes.ContentType)
            {
                httpRes.ContentType = contentType;
            }
            if (HostContext.Config.AppendUtf8CharsetOnContentTypes.Contains(contentType))
            {
                httpRes.ContentType += ContentFormat.Utf8Suffix;
            }

            var hold = httpRes.StatusDescription;
            var hasDefaultStatusDescription = hold == null || hold == "OK";

            httpRes.StatusCode = statusCode;

            httpRes.StatusDescription = hasDefaultStatusDescription
                ? (errorMessage ?? HttpStatus.GetStatusDescription(statusCode))
                : hold;

            httpRes.ApplyGlobalResponseHeaders();

            var serializer = HostContext.ContentTypes.GetResponseSerializer(contentType);
            serializer?.Invoke(httpReq, errorDto, httpRes);

            httpRes.EndHttpHandlerRequest(skipHeaders: true);

            return TypeConstants.EmptyTask;
        }
 public static void ReturnAuthRequired(this IHttpResponse httpRes, AuthenticationHeaderType AuthType, string authRealm)
 {
     httpRes.StatusCode = (int)HttpStatusCode.Unauthorized;
     httpRes.AddHeader(HttpHeaders.WwwAuthenticate, string.Format("{0} realm=\"{1}\"",AuthType.ToString(),authRealm));
     httpRes.ApplyGlobalResponseHeaders();
     httpRes.Close();
 }
 public static void RedirectToUrl(this IHttpResponse httpRes, string url)
 {
     httpRes.AddHeader(HttpHeaders.Location, url);
     httpRes.ApplyGlobalResponseHeaders();
     httpRes.Close();
 }
        public static void WriteFile(this IHttpResponse httpRes, string filePath)
        {
            var aspNetRes = httpRes as HttpResponseWrapper;
            if (aspNetRes != null)
            {
                aspNetRes.Response.WriteFile(filePath);
                return;
            }

            using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
            {
                fs.WriteTo(httpRes.OutputStream);
            }

            httpRes.ApplyGlobalResponseHeaders();
            httpRes.Close();
        }