[Fixes #167]Error page/handler should reset any buffered data, clear headers, etc.

This commit is contained in:
Kiran Challa 2015-08-20 10:21:34 -07:00
parent 417c7d2374
commit 09b1c0fa63
5 changed files with 217 additions and 130 deletions

View File

@ -5,6 +5,7 @@ using System;
using System.Threading.Tasks;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Http.Features;
using Microsoft.Framework.Logging;
namespace Microsoft.AspNet.Diagnostics
@ -60,7 +61,12 @@ namespace Microsoft.AspNet.Diagnostics
context.Response.Headers.Clear();
context.Response.OnStarting(_clearCacheHeadersDelegate, context.Response);
// TODO: Try clearing any buffered data. The buffering feature/middleware has not been designed yet.
// if buffering is enabled, then clear it as data could have been written into it.
if (context.Response.Body.CanSeek)
{
context.Response.Body.SetLength(0);
}
await _options.ErrorHandler(context);
// TODO: Optional re-throw? We'll re-throw the original exception by default if the error handler throws.
return;

View File

@ -13,6 +13,7 @@ using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Diagnostics.Views;
using Microsoft.AspNet.FileProviders;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Http.Features;
using Microsoft.Dnx.Compilation;
using Microsoft.Dnx.Runtime;
using Microsoft.Framework.Internal;
@ -63,8 +64,23 @@ namespace Microsoft.AspNet.Diagnostics
catch (Exception ex)
{
_logger.LogError("An unhandled exception has occurred while executing the request", ex);
if (context.Response.HasStarted)
{
_logger.LogWarning("The response has already started, the error page middleware will not be executed.");
throw;
}
try
{
context.Response.StatusCode = 500;
context.Response.Headers.Clear();
// if buffering is enabled, then clear it as data could have been written into it.
if (context.Response.Body.CanSeek)
{
context.Response.Body.SetLength(0);
}
await DisplayException(context, ex);
return;
}

View File

@ -55,40 +55,38 @@ using Views
{
#line 15 "ErrorPage.cshtml"
Response.StatusCode = 500;
// TODO: Response.ReasonPhrase = "Internal Server Error";
Response.ContentType = "text/html; charset=utf-8";
Response.ContentLength = null; // Clear any prior Content-Length
string location = string.Empty;
#line default
#line hidden
WriteLiteral("\r\n<!DOCTYPE html>\r\n<html");
WriteAttribute("lang", Tuple.Create(" lang=\"", 533), Tuple.Create("\"", 594),
Tuple.Create(Tuple.Create("", 540), Tuple.Create<System.Object, System.Int32>(CultureInfo.CurrentUICulture.TwoLetterISOLanguageName, 540), false));
WriteAttribute("lang", Tuple.Create(" lang=\"", 431), Tuple.Create("\"", 492),
Tuple.Create(Tuple.Create("", 438), Tuple.Create<System.Object, System.Int32>(CultureInfo.CurrentUICulture.TwoLetterISOLanguageName, 438), false));
WriteLiteral(" xmlns=\"http://www.w3.org/1999/xhtml\">\r\n <head>\r\n <meta charset=\"utf-8\"" +
" />\r\n <title>");
#line 26 "ErrorPage.cshtml"
#line 24 "ErrorPage.cshtml"
Write(Resources.ErrorPageHtml_Title);
#line default
#line hidden
WriteLiteral("</title>\r\n <style>\r\n body {\r\n font-family: 'Segoe UI', Tahoma, Arial, Helvetica, sans-serif;\r\n font-size: .813em;\r\n line-height: 1.4em;\r\n color: #222;\r\n}\r\n\r\nh1, h2, h3, h4, h5 {\r\n /*font-family: 'Segoe UI',Tahoma,Arial,Helvetica,sans-serif;*/\r\n font-weight: 100;\r\n}\r\n\r\nh1 {\r\n color: #44525e;\r\n margin: 15px 0 15px 0;\r\n}\r\n\r\nh2 {\r\n margin: 10px 5px 0 0;\r\n}\r\n\r\nh3 {\r\n color: #363636;\r\n margin: 5px 5px 0 0;\r\n}\r\n\r\ncode {\r\n font-family: Consolas, \"Courier New\", courier, monospace;\r\n}\r\n\r\nbody .titleerror {\r\n padding: 3px;\r\n display: block;\r\n font-size: 1.5em;\r\n font-weight: 100;\r\n}\r\n\r\nbody .location {\r\n margin: 3px 0 10px 30px;\r\n}\r\n\r\n#header {\r\n font-size: 18px;\r\n padding: 15px 0;\r\n border-top: 1px #ddd solid;\r\n border-bottom: 1px #ddd solid;\r\n margin-bottom: 0;\r\n}\r\n\r\n #header li {\r\n display: inline;\r\n margin: 5px;\r\n padding: 5px;\r\n color: #a0a0a0;\r\n cursor: pointer;\r\n }\r\n\r\n #header li:hover {\r\n background: #a9e4f9;\r\n color: #fff;\r\n }\r\n\r\n #header .selected {\r\n background: #44c5f2;\r\n color: #fff;\r\n }\r\n\r\n#stackpage ul {\r\n list-style: none;\r\n padding-left: 0;\r\n margin: 0;\r\n /*border-bottom: 1px #ddd solid;*/\r\n}\r\n\r\n#stackpage .stackerror {\r\n padding: 5px;\r\n border-bottom: 1px #ddd solid;\r\n}\r\n\r\n #stackpage .stackerror:hover {\r\n background-color: #f0f0f0;\r\n }\r\n\r\n#stackpage .frame:hover {\r\n background-color: #f0f0f0;\r\n text-decoration: none;\r\n}\r\n\r\n#stackpage .frame {\r\n padding: 2px;\r\n margin: 0 0 0 30px;\r\n border-bottom: 1px #ddd solid;\r\n cursor: pointer;\r\n}\r\n\r\n #stackpage .frame h3 {\r\n padding: 5px;\r\n margin: 0;\r\n }\r\n\r\n#stackpage .source {\r\n padding: 0;\r\n}\r\n\r\n #stackpage .source ol li {\r\n font-family: Consolas, \"Courier New\", courier, monospace;\r\n white-space: pre;\r\n }\r\n\r\n#stackpage .frame:hover .source .highlight li span {\r\n color: #fff;\r\n background: #b20000;\r\n}\r\n\r\n#stackpage .source ol.collapsible li {\r\n color: #888;\r\n}\r\n\r\n #stackpage .source ol.collapsible li span {\r\n color: #606060;\r\n }\r\n\r\n.page table {\r\n border-collapse: separate;\r\n border-spacing: 0;\r\n margin: 0 0 20px;\r\n}\r\n\r\n.page th {\r\n vertical-align: bottom;\r\n padding: 10px 5px 5px 5px;\r\n font-weight: 400;\r\n color: #a0a0a0;\r\n text-align: left;\r\n}\r\n\r\n.page td {\r\n padding: 3px 10px;\r\n}\r\n\r\n.page th, .page td {\r\n border-right: 1px #ddd solid;\r\n border-bottom: 1px #ddd solid;\r\n border-left: 1px transparent solid;\r\n border-top: 1px transparent solid;\r\n box-sizing: border-box;\r\n}\r\n\r\n .page th:last-child, .page td:last-child {\r\n border-right: 1px transparent solid;\r\n }\r\n\r\n .page .length {\r\n text-align: right;\r\n }\r\n\r\na {\r\n color: #1ba1e2;\r\n text-decoration: none;\r\n}\r\n\r\n a:hover {\r\n color: #13709e;\r\n text-decoration: underline;\r\n }\r\n\r\n </s" +
"tyle>\r\n </head>\r\n <body>\r\n <h1>");
#line 32 "ErrorPage.cshtml"
#line 30 "ErrorPage.cshtml"
Write(Resources.ErrorPageHtml_UnhandledException);
#line default
#line hidden
WriteLiteral("</h1>\r\n");
#line 33 "ErrorPage.cshtml"
#line 31 "ErrorPage.cshtml"
#line default
#line hidden
#line 33 "ErrorPage.cshtml"
#line 31 "ErrorPage.cshtml"
foreach (var errorDetail in Model.ErrorDetails)
{
@ -96,26 +94,26 @@ using Views
#line hidden
WriteLiteral(" <div class=\"titleerror\">");
#line 35 "ErrorPage.cshtml"
#line 33 "ErrorPage.cshtml"
Write(errorDetail.Error.GetType().Name);
#line default
#line hidden
WriteLiteral(": ");
#line 35 "ErrorPage.cshtml"
#line 33 "ErrorPage.cshtml"
Output.Write(HtmlEncodeAndReplaceLineBreaks(errorDetail.Error.Message));
#line default
#line hidden
WriteLiteral("</div>\r\n");
#line 36 "ErrorPage.cshtml"
#line 34 "ErrorPage.cshtml"
#line default
#line hidden
#line 36 "ErrorPage.cshtml"
#line 34 "ErrorPage.cshtml"
StackFrame firstFrame = null;
firstFrame = errorDetail.StackFrames.FirstOrDefault();
@ -132,7 +130,7 @@ using Views
#line default
#line hidden
#line 47 "ErrorPage.cshtml"
#line 45 "ErrorPage.cshtml"
if (!string.IsNullOrEmpty(location) && firstFrame != null && !string.IsNullOrEmpty(firstFrame.File))
{
@ -141,28 +139,28 @@ using Views
#line hidden
WriteLiteral(" <p class=\"location\">");
#line 50 "ErrorPage.cshtml"
#line 48 "ErrorPage.cshtml"
Write(location);
#line default
#line hidden
WriteLiteral(" in <code");
WriteAttribute("title", Tuple.Create(" title=\"", 1910), Tuple.Create("\"", 1934),
Tuple.Create(Tuple.Create("", 1918), Tuple.Create<System.Object, System.Int32>(firstFrame.File, 1918), false));
WriteAttribute("title", Tuple.Create(" title=\"", 1808), Tuple.Create("\"", 1832),
Tuple.Create(Tuple.Create("", 1816), Tuple.Create<System.Object, System.Int32>(firstFrame.File, 1816), false));
WriteLiteral(">");
#line 50 "ErrorPage.cshtml"
#line 48 "ErrorPage.cshtml"
Write(System.IO.Path.GetFileName(firstFrame.File));
#line default
#line hidden
WriteLiteral("</code>, line ");
#line 50 "ErrorPage.cshtml"
#line 48 "ErrorPage.cshtml"
Write(firstFrame.Line);
#line default
#line hidden
WriteLiteral("</p>\r\n");
#line 51 "ErrorPage.cshtml"
#line 49 "ErrorPage.cshtml"
}
else if (!string.IsNullOrEmpty(location))
{
@ -171,13 +169,13 @@ using Views
#line hidden
WriteLiteral(" <p class=\"location\">");
#line 54 "ErrorPage.cshtml"
#line 52 "ErrorPage.cshtml"
Write(location);
#line default
#line hidden
WriteLiteral("</p>\r\n");
#line 55 "ErrorPage.cshtml"
#line 53 "ErrorPage.cshtml"
}
else
{
@ -186,13 +184,13 @@ using Views
#line hidden
WriteLiteral(" <p class=\"location\">");
#line 58 "ErrorPage.cshtml"
#line 56 "ErrorPage.cshtml"
Write(Resources.ErrorPageHtml_UnknownLocation);
#line default
#line hidden
WriteLiteral("</p>\r\n");
#line 59 "ErrorPage.cshtml"
#line 57 "ErrorPage.cshtml"
}
}
@ -201,53 +199,53 @@ using Views
WriteLiteral(" <ul id=\"header\">\r\n <li id=\"stack\" tabindex=\"1\" class=\"selected" +
"\">\r\n ");
#line 63 "ErrorPage.cshtml"
#line 61 "ErrorPage.cshtml"
Write(Resources.ErrorPageHtml_StackButton);
#line default
#line hidden
WriteLiteral("\r\n </li>\r\n <li id=\"query\" tabindex=\"2\">\r\n ");
#line 66 "ErrorPage.cshtml"
#line 64 "ErrorPage.cshtml"
Write(Resources.ErrorPageHtml_QueryButton);
#line default
#line hidden
WriteLiteral("\r\n </li>\r\n <li id=\"cookies\" tabindex=\"3\">\r\n " +
"");
#line 69 "ErrorPage.cshtml"
#line 67 "ErrorPage.cshtml"
Write(Resources.ErrorPageHtml_CookiesButton);
#line default
#line hidden
WriteLiteral("\r\n </li>\r\n <li id=\"headers\" tabindex=\"4\">\r\n " +
"");
#line 72 "ErrorPage.cshtml"
#line 70 "ErrorPage.cshtml"
Write(Resources.ErrorPageHtml_HeadersButton);
#line default
#line hidden
WriteLiteral("\r\n </li>\r\n </ul>\r\n \r\n <div id=\"stackpage\" class=\"page" +
"\">\r\n <ul>\r\n");
#line 78 "ErrorPage.cshtml"
WriteLiteral("\r\n </li>\r\n </ul>\r\n\r\n <div id=\"stackpage\" class=\"page\">\r\n" +
" <ul>\r\n");
#line 76 "ErrorPage.cshtml"
#line default
#line hidden
#line 78 "ErrorPage.cshtml"
#line 76 "ErrorPage.cshtml"
int tabIndex = 6;
#line default
#line hidden
WriteLiteral("\r\n");
#line 79 "ErrorPage.cshtml"
#line 77 "ErrorPage.cshtml"
#line default
#line hidden
#line 79 "ErrorPage.cshtml"
#line 77 "ErrorPage.cshtml"
foreach (var errorDetail in Model.ErrorDetails)
{
@ -255,25 +253,25 @@ using Views
#line hidden
WriteLiteral(" <li>\r\n <h2 class=\"stackerror\">");
#line 82 "ErrorPage.cshtml"
#line 80 "ErrorPage.cshtml"
Write(errorDetail.Error.GetType().Name);
#line default
#line hidden
WriteLiteral(": ");
#line 82 "ErrorPage.cshtml"
#line 80 "ErrorPage.cshtml"
Write(errorDetail.Error.Message);
#line default
#line hidden
WriteLiteral("</h2>\r\n <ul>\r\n");
#line 84 "ErrorPage.cshtml"
#line 82 "ErrorPage.cshtml"
#line default
#line hidden
#line 84 "ErrorPage.cshtml"
#line 82 "ErrorPage.cshtml"
foreach (var frame in errorDetail.StackFrames)
{
@ -281,29 +279,29 @@ using Views
#line hidden
WriteLiteral(" <li class=\"frame\"");
WriteAttribute("tabindex", Tuple.Create(" tabindex=\"", 3342), Tuple.Create("\"", 3362),
Tuple.Create(Tuple.Create("", 3353), Tuple.Create<System.Object, System.Int32>(tabIndex, 3353), false));
WriteAttribute("tabindex", Tuple.Create(" tabindex=\"", 3236), Tuple.Create("\"", 3256),
Tuple.Create(Tuple.Create("", 3247), Tuple.Create<System.Object, System.Int32>(tabIndex, 3247), false));
WriteLiteral(">\r\n");
#line 87 "ErrorPage.cshtml"
#line 85 "ErrorPage.cshtml"
#line default
#line hidden
#line 87 "ErrorPage.cshtml"
#line 85 "ErrorPage.cshtml"
tabIndex++;
#line default
#line hidden
WriteLiteral("\r\n");
#line 88 "ErrorPage.cshtml"
#line 86 "ErrorPage.cshtml"
#line default
#line hidden
#line 88 "ErrorPage.cshtml"
#line 86 "ErrorPage.cshtml"
if (string.IsNullOrEmpty(frame.File))
{
@ -311,13 +309,13 @@ using Views
#line hidden
WriteLiteral(" <h3>");
#line 90 "ErrorPage.cshtml"
#line 88 "ErrorPage.cshtml"
Write(frame.Function);
#line default
#line hidden
WriteLiteral("</h3>\r\n");
#line 91 "ErrorPage.cshtml"
#line 89 "ErrorPage.cshtml"
}
else
{
@ -326,35 +324,35 @@ using Views
#line hidden
WriteLiteral(" <h3>");
#line 94 "ErrorPage.cshtml"
#line 92 "ErrorPage.cshtml"
Write(frame.Function);
#line default
#line hidden
WriteLiteral(" in <code");
WriteAttribute("title", Tuple.Create(" title=\"", 3757), Tuple.Create("\"", 3776),
Tuple.Create(Tuple.Create("", 3765), Tuple.Create<System.Object, System.Int32>(frame.File, 3765), false));
WriteAttribute("title", Tuple.Create(" title=\"", 3651), Tuple.Create("\"", 3670),
Tuple.Create(Tuple.Create("", 3659), Tuple.Create<System.Object, System.Int32>(frame.File, 3659), false));
WriteLiteral(">");
#line 94 "ErrorPage.cshtml"
#line 92 "ErrorPage.cshtml"
Write(System.IO.Path.GetFileName(frame.File));
#line default
#line hidden
WriteLiteral("</code></h3>\r\n");
#line 95 "ErrorPage.cshtml"
#line 93 "ErrorPage.cshtml"
}
#line default
#line hidden
WriteLiteral("\r\n");
#line 97 "ErrorPage.cshtml"
#line 95 "ErrorPage.cshtml"
#line default
#line hidden
#line 97 "ErrorPage.cshtml"
#line 95 "ErrorPage.cshtml"
if (frame.Line != 0 && frame.ContextCode.Any())
{
@ -362,13 +360,13 @@ using Views
#line hidden
WriteLiteral(" <div class=\"source\">\r\n");
#line 100 "ErrorPage.cshtml"
#line 98 "ErrorPage.cshtml"
#line default
#line hidden
#line 100 "ErrorPage.cshtml"
#line 98 "ErrorPage.cshtml"
if (frame.PreContextCode.Any())
{
@ -376,16 +374,16 @@ using Views
#line hidden
WriteLiteral(" <ol");
WriteAttribute("start", Tuple.Create(" start=\"", 4207), Tuple.Create("\"", 4236),
Tuple.Create(Tuple.Create("", 4215), Tuple.Create<System.Object, System.Int32>(frame.PreContextLine, 4215), false));
WriteAttribute("start", Tuple.Create(" start=\"", 4101), Tuple.Create("\"", 4130),
Tuple.Create(Tuple.Create("", 4109), Tuple.Create<System.Object, System.Int32>(frame.PreContextLine, 4109), false));
WriteLiteral(" class=\"collapsible\">\r\n");
#line 103 "ErrorPage.cshtml"
#line 101 "ErrorPage.cshtml"
#line default
#line hidden
#line 103 "ErrorPage.cshtml"
#line 101 "ErrorPage.cshtml"
foreach (var line in frame.PreContextCode)
{
@ -393,36 +391,36 @@ using Views
#line hidden
WriteLiteral(" <li><span>");
#line 105 "ErrorPage.cshtml"
#line 103 "ErrorPage.cshtml"
Write(line);
#line default
#line hidden
WriteLiteral("</span></li>\r\n");
#line 106 "ErrorPage.cshtml"
#line 104 "ErrorPage.cshtml"
}
#line default
#line hidden
WriteLiteral(" </ol>\r\n");
#line 108 "ErrorPage.cshtml"
}
#line 106 "ErrorPage.cshtml"
}
#line default
#line hidden
WriteLiteral("\r\n <ol");
WriteAttribute("start", Tuple.Create(" start=\"", 4676), Tuple.Create("\"", 4695),
Tuple.Create(Tuple.Create("", 4684), Tuple.Create<System.Object, System.Int32>(frame.Line, 4684), false));
WriteAttribute("start", Tuple.Create(" start=\"", 4569), Tuple.Create("\"", 4588),
Tuple.Create(Tuple.Create("", 4577), Tuple.Create<System.Object, System.Int32>(frame.Line, 4577), false));
WriteLiteral(" class=\"highlight\">\r\n");
#line 111 "ErrorPage.cshtml"
#line 109 "ErrorPage.cshtml"
#line default
#line hidden
#line 111 "ErrorPage.cshtml"
#line 109 "ErrorPage.cshtml"
foreach (var line in frame.ContextCode)
{
@ -430,26 +428,26 @@ using Views
#line hidden
WriteLiteral(" <li><span>");
#line 113 "ErrorPage.cshtml"
#line 111 "ErrorPage.cshtml"
Write(line);
#line default
#line hidden
WriteLiteral("</span></li>\r\n");
#line 114 "ErrorPage.cshtml"
#line 112 "ErrorPage.cshtml"
}
#line default
#line hidden
WriteLiteral(" </ol>\r\n\r\n");
#line 117 "ErrorPage.cshtml"
#line 115 "ErrorPage.cshtml"
#line default
#line hidden
#line 117 "ErrorPage.cshtml"
#line 115 "ErrorPage.cshtml"
if (frame.PostContextCode.Any())
{
@ -457,16 +455,16 @@ using Views
#line hidden
WriteLiteral(" <ol");
WriteAttribute("start", Tuple.Create(" start=\'", 5188), Tuple.Create("\'", 5213),
Tuple.Create(Tuple.Create("", 5196), Tuple.Create<System.Object, System.Int32>(frame.Line + 1, 5196), false));
WriteAttribute("start", Tuple.Create(" start=\'", 5081), Tuple.Create("\'", 5106),
Tuple.Create(Tuple.Create("", 5089), Tuple.Create<System.Object, System.Int32>(frame.Line + 1, 5089), false));
WriteLiteral(" class=\"collapsible\">\r\n");
#line 120 "ErrorPage.cshtml"
#line 118 "ErrorPage.cshtml"
#line default
#line hidden
#line 120 "ErrorPage.cshtml"
#line 118 "ErrorPage.cshtml"
foreach (var line in frame.PostContextCode)
{
@ -474,55 +472,54 @@ using Views
#line hidden
WriteLiteral(" <li><span>");
#line 122 "ErrorPage.cshtml"
#line 120 "ErrorPage.cshtml"
Write(line);
#line default
#line hidden
WriteLiteral("</span></li>\r\n");
#line 123 "ErrorPage.cshtml"
#line 121 "ErrorPage.cshtml"
}
#line default
#line hidden
WriteLiteral(" </ol>\r\n");
#line 125 "ErrorPage.cshtml"
}
#line 123 "ErrorPage.cshtml"
}
#line default
#line hidden
WriteLiteral(" </div>\r\n");
#line 127 "ErrorPage.cshtml"
}
#line 125 "ErrorPage.cshtml"
}
#line default
#line hidden
WriteLiteral(" </li>\r\n");
#line 129 "ErrorPage.cshtml"
#line 127 "ErrorPage.cshtml"
}
#line default
#line hidden
WriteLiteral(" </ul>\r\n </li>\r\n");
#line 132 "ErrorPage.cshtml"
#line 130 "ErrorPage.cshtml"
}
#line default
#line hidden
WriteLiteral(" </ul>\r\n </div>\r\n \r\n <div id=\"querypage\" class=\"pa" +
"ge\">\r\n");
#line 137 "ErrorPage.cshtml"
WriteLiteral(" </ul>\r\n </div>\r\n\r\n <div id=\"querypage\" class=\"page\">\r\n");
#line 135 "ErrorPage.cshtml"
#line default
#line hidden
#line 137 "ErrorPage.cshtml"
#line 135 "ErrorPage.cshtml"
if (Model.Query.Any())
{
@ -531,26 +528,26 @@ using Views
WriteLiteral(" <table>\r\n <thead>\r\n <tr" +
">\r\n <th>");
#line 142 "ErrorPage.cshtml"
#line 140 "ErrorPage.cshtml"
Write(Resources.ErrorPageHtml_VariableColumn);
#line default
#line hidden
WriteLiteral("</th>\r\n <th>");
#line 143 "ErrorPage.cshtml"
#line 141 "ErrorPage.cshtml"
Write(Resources.ErrorPageHtml_ValueColumn);
#line default
#line hidden
WriteLiteral("</th>\r\n </tr>\r\n </thead>\r\n " +
" <tbody>\r\n");
#line 147 "ErrorPage.cshtml"
#line 145 "ErrorPage.cshtml"
#line default
#line hidden
#line 147 "ErrorPage.cshtml"
#line 145 "ErrorPage.cshtml"
foreach (var kv in Model.Query.OrderBy(kv => kv.Key))
{
foreach (var v in kv.Value)
@ -560,19 +557,19 @@ using Views
#line hidden
WriteLiteral(" <tr>\r\n <td>");
#line 152 "ErrorPage.cshtml"
#line 150 "ErrorPage.cshtml"
Write(kv.Key);
#line default
#line hidden
WriteLiteral("</td>\r\n <td>");
#line 153 "ErrorPage.cshtml"
#line 151 "ErrorPage.cshtml"
Write(v);
#line default
#line hidden
WriteLiteral("</td>\r\n </tr>\r\n");
#line 155 "ErrorPage.cshtml"
#line 153 "ErrorPage.cshtml"
}
}
@ -580,7 +577,7 @@ using Views
#line hidden
WriteLiteral(" </tbody>\r\n </table>\r\n");
#line 159 "ErrorPage.cshtml"
#line 157 "ErrorPage.cshtml"
}
else
{
@ -589,26 +586,26 @@ using Views
#line hidden
WriteLiteral(" <p>");
#line 162 "ErrorPage.cshtml"
#line 160 "ErrorPage.cshtml"
Write(Resources.ErrorPageHtml_NoQueryStringData);
#line default
#line hidden
WriteLiteral("</p>\r\n");
#line 163 "ErrorPage.cshtml"
#line 161 "ErrorPage.cshtml"
}
#line default
#line hidden
WriteLiteral(" </div>\r\n \r\n <div id=\"cookiespage\" class=\"page\">\r\n");
#line 167 "ErrorPage.cshtml"
WriteLiteral(" </div>\r\n\r\n <div id=\"cookiespage\" class=\"page\">\r\n");
#line 165 "ErrorPage.cshtml"
#line default
#line hidden
#line 167 "ErrorPage.cshtml"
#line 165 "ErrorPage.cshtml"
if (Model.Cookies.Any())
{
@ -617,26 +614,26 @@ using Views
WriteLiteral(" <table>\r\n <thead>\r\n <tr" +
">\r\n <th>");
#line 172 "ErrorPage.cshtml"
#line 170 "ErrorPage.cshtml"
Write(Resources.ErrorPageHtml_VariableColumn);
#line default
#line hidden
WriteLiteral("</th>\r\n <th>");
#line 173 "ErrorPage.cshtml"
#line 171 "ErrorPage.cshtml"
Write(Resources.ErrorPageHtml_ValueColumn);
#line default
#line hidden
WriteLiteral("</th>\r\n </tr>\r\n </thead>\r\n " +
" <tbody>\r\n");
#line 177 "ErrorPage.cshtml"
#line 175 "ErrorPage.cshtml"
#line default
#line hidden
#line 177 "ErrorPage.cshtml"
#line 175 "ErrorPage.cshtml"
foreach (var kv in Model.Cookies.OrderBy(kv => kv.Key))
{
foreach (var v in kv.Value)
@ -646,19 +643,19 @@ using Views
#line hidden
WriteLiteral(" <tr>\r\n <td>");
#line 182 "ErrorPage.cshtml"
#line 180 "ErrorPage.cshtml"
Write(kv.Key);
#line default
#line hidden
WriteLiteral("</td>\r\n <td>");
#line 183 "ErrorPage.cshtml"
#line 181 "ErrorPage.cshtml"
Write(v);
#line default
#line hidden
WriteLiteral("</td>\r\n </tr>\r\n");
#line 185 "ErrorPage.cshtml"
#line 183 "ErrorPage.cshtml"
}
}
@ -666,7 +663,7 @@ using Views
#line hidden
WriteLiteral(" </tbody>\r\n </table>\r\n");
#line 189 "ErrorPage.cshtml"
#line 187 "ErrorPage.cshtml"
}
else
{
@ -675,26 +672,26 @@ using Views
#line hidden
WriteLiteral(" <p>");
#line 192 "ErrorPage.cshtml"
#line 190 "ErrorPage.cshtml"
Write(Resources.ErrorPageHtml_NoCookieData);
#line default
#line hidden
WriteLiteral("</p>\r\n");
#line 193 "ErrorPage.cshtml"
#line 191 "ErrorPage.cshtml"
}
#line default
#line hidden
WriteLiteral(" </div>\r\n <div id=\"headerspage\" class=\"page\">\r\n");
#line 196 "ErrorPage.cshtml"
#line 194 "ErrorPage.cshtml"
#line default
#line hidden
#line 196 "ErrorPage.cshtml"
#line 194 "ErrorPage.cshtml"
if (Model.Headers.Any())
{
@ -703,26 +700,26 @@ using Views
WriteLiteral(" <table>\r\n <thead>\r\n <tr" +
">\r\n <th>");
#line 201 "ErrorPage.cshtml"
#line 199 "ErrorPage.cshtml"
Write(Resources.ErrorPageHtml_VariableColumn);
#line default
#line hidden
WriteLiteral("</th>\r\n <th>");
#line 202 "ErrorPage.cshtml"
#line 200 "ErrorPage.cshtml"
Write(Resources.ErrorPageHtml_ValueColumn);
#line default
#line hidden
WriteLiteral("</th>\r\n </tr>\r\n </thead>\r\n " +
" <tbody>\r\n");
#line 206 "ErrorPage.cshtml"
#line 204 "ErrorPage.cshtml"
#line default
#line hidden
#line 206 "ErrorPage.cshtml"
#line 204 "ErrorPage.cshtml"
foreach (var kv in Model.Headers.OrderBy(kv => kv.Key))
{
foreach (var v in kv.Value)
@ -732,19 +729,19 @@ using Views
#line hidden
WriteLiteral(" <tr>\r\n <td>");
#line 211 "ErrorPage.cshtml"
#line 209 "ErrorPage.cshtml"
Write(kv.Key);
#line default
#line hidden
WriteLiteral("</td>\r\n <td>");
#line 212 "ErrorPage.cshtml"
#line 210 "ErrorPage.cshtml"
Write(v);
#line default
#line hidden
WriteLiteral("</td>\r\n </tr>\r\n");
#line 214 "ErrorPage.cshtml"
#line 212 "ErrorPage.cshtml"
}
}
@ -752,7 +749,7 @@ using Views
#line hidden
WriteLiteral(" </tbody>\r\n </table>\r\n");
#line 218 "ErrorPage.cshtml"
#line 216 "ErrorPage.cshtml"
}
else
{
@ -761,13 +758,13 @@ using Views
#line hidden
WriteLiteral(" <p>");
#line 221 "ErrorPage.cshtml"
#line 219 "ErrorPage.cshtml"
Write(Resources.ErrorPageHtml_NoHeaderData);
#line default
#line hidden
WriteLiteral("</p>\r\n");
#line 222 "ErrorPage.cshtml"
#line 220 "ErrorPage.cshtml"
}
#line default

View File

@ -13,10 +13,8 @@
public ErrorPageModel Model { get; set; }
}
@{
Response.StatusCode = 500;
// TODO: Response.ReasonPhrase = "Internal Server Error";
Response.ContentType = "text/html; charset=utf-8";
Response.ContentLength = null; // Clear any prior Content-Length
string location = string.Empty;
}
<!DOCTYPE html>
@ -72,7 +70,7 @@
@Resources.ErrorPageHtml_HeadersButton
</li>
</ul>
<div id="stackpage" class="page">
<ul>
@{ int tabIndex = 6; }
@ -105,7 +103,7 @@
<li><span>@line</span></li>
}
</ol>
}
}
<ol start="@frame.Line" class="highlight">
@foreach (var line in frame.ContextCode)
@ -122,9 +120,9 @@
<li><span>@line</span></li>
}
</ol>
}
}
</div>
}
}
</li>
}
</ul>
@ -132,7 +130,7 @@
}
</ul>
</div>
<div id="querypage" class="page">
@if (Model.Query.Any())
{
@ -162,7 +160,7 @@
<p>@Resources.ErrorPageHtml_NoQueryStringData</p>
}
</div>
<div id="cookiespage" class="page">
@if (Model.Cookies.Any())
{

View File

@ -3,12 +3,14 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Http.Features;
using Microsoft.AspNet.TestHost;
using Xunit;
@ -94,6 +96,74 @@ namespace Microsoft.AspNet.Diagnostics
}
}
[Fact]
public async Task ClearsResponseBuffer_BeforeRequestIsReexecuted()
{
var expectedResponseBody = "New response body";
using (var server = TestServer.Create(app =>
{
// add response buffering
app.Use(async (httpContext, next) =>
{
var response = httpContext.Response;
var originalResponseBody = response.Body;
var bufferingStream = new MemoryStream();
response.Body = bufferingStream;
try
{
await next();
response.Body = originalResponseBody;
bufferingStream.Seek(0, SeekOrigin.Begin);
await bufferingStream.CopyToAsync(response.Body);
}
finally
{
response.Body = originalResponseBody;
}
});
app.UseErrorHandler("/handle-errors");
app.Map("/handle-errors", (innerAppBuilder) =>
{
innerAppBuilder.Run(async (httpContext) =>
{
Assert.True(httpContext.Response.Body.CanSeek);
Assert.Equal(0, httpContext.Response.Body.Position);
await httpContext.Response.WriteAsync(expectedResponseBody);
});
});
app.Run(async (context) =>
{
// Write some content into the response before throwing exception
await context.Response.WriteAsync(new string('a', 100));
throw new InvalidOperationException("Invalid input provided.");
});
}))
{
var client = server.CreateClient();
var response = await client.GetAsync(string.Empty);
Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);
Assert.Equal(expectedResponseBody, await response.Content.ReadAsStringAsync());
IEnumerable<string> values;
Assert.True(response.Headers.TryGetValues("Cache-Control", out values));
Assert.Single(values);
Assert.Equal("no-cache", values.First());
Assert.True(response.Headers.TryGetValues("Pragma", out values));
Assert.Single(values);
Assert.Equal("no-cache", values.First());
Assert.True(response.Content.Headers.TryGetValues("Expires", out values));
Assert.Single(values);
Assert.Equal("-1", values.First());
Assert.False(response.Headers.TryGetValues("ETag", out values));
}
}
[Fact]
public async Task ClearsCacheHeaders_SetByReexecutionPathHandlers()
{