static string GetHtml(IRequest request, string requestName) { var info = request.QueryString["template"] ?? request.QueryString["Template"] ?? requestName; const string template = "/{0}/{1}.html"; var file = HostContext.VirtualPathProvider.GetFile(template.Fmt(InfoDir, info)) ?? HostContext.VirtualPathProvider.GetFile(template.Fmt(InfoDir, requestName)) ?? HostContext.VirtualPathProvider.GetFile(template.Fmt(InfoDir, InfoFormatFile)); return((file != null) ? file.ReadAllText() : HtmlTemplates.GetHtmlFormatTemplate()); }
public void SerializeToStream(IRequest request, object response, IResponse httpRes) { var httpResult = request.GetItem("HttpResult") as IHttpResult; if (httpResult != null && httpResult.Headers.ContainsKey(HttpHeaders.Location)) { return; } if (AppHost.ViewEngines.Any(x => x.ProcessRequest(request, httpRes, response))) { return; } if (request.ResponseContentType != MimeTypes.Html && request.ResponseContentType != MimeTypes.JsonReport) { return; } var dto = response.GetDto(); var html = dto as string; if (html == null) { // Serialize then escape any potential script tags to avoid XSS when displaying as HTML var json = JsonDataContractSerializer.Instance.SerializeToString(dto) ?? "null"; json = json.Replace("<", "<").Replace(">", ">"); var url = request.AbsoluteUri .Replace("format=html", "") .Replace("format=shtm", "") .TrimEnd('?', '&'); url += url.Contains("?") ? "&" : "?"; var now = DateTime.UtcNow; var requestName = request.OperationName ?? dto.GetType().Name; html = HtmlTemplates.GetHtmlFormatTemplate() .Replace("${Dto}", json) .Replace("${Title}", string.Format(TitleFormat, requestName, now)) .Replace("${MvcIncludes}", MiniProfiler.Profiler.RenderIncludes().ToString()) .Replace("${Header}", string.Format(HtmlTitleFormat, requestName, now)) .Replace("${ServiceUrl}", url); } var utf8Bytes = html.ToUtf8Bytes(); httpRes.OutputStream.Write(utf8Bytes, 0, utf8Bytes.Length); }
public void SerializeToStream(IRequest req, object response, IResponse res) { var httpResult = req.GetItem("HttpResult") as IHttpResult; if (httpResult != null && httpResult.Headers.ContainsKey(HttpHeaders.Location) && httpResult.StatusCode != System.Net.HttpStatusCode.Created) { return; } try { if (res.StatusCode >= 400) { var responseStatus = response.GetResponseStatus(); req.Items[ErrorStatusKey] = responseStatus; } if (response is CompressedResult) { if (res.Dto != null) { response = res.Dto; } else { throw new ArgumentException("Cannot use Cached Result as ViewModel"); } } if (AppHost.ViewEngines.Any(x => x.ProcessRequest(req, res, response))) { return; } } catch (Exception ex) { if (res.StatusCode < 400) { throw; } //If there was an exception trying to render a Error with a View, //It can't handle errors so just write it out here. response = DtoUtils.CreateErrorResponse(req.Dto, ex); } //Handle Exceptions returning string if (req.ResponseContentType == MimeTypes.PlainText) { req.ResponseContentType = MimeTypes.Html; res.ContentType = MimeTypes.Html; } if (req.ResponseContentType != MimeTypes.Html && req.ResponseContentType != MimeTypes.JsonReport) { return; } var dto = response.GetDto(); var html = dto as string; if (html == null) { // Serialize then escape any potential script tags to avoid XSS when displaying as HTML var json = JsonDataContractSerializer.Instance.SerializeToString(dto) ?? "null"; json = json.Replace("<", "<").Replace(">", ">"); var url = req.AbsoluteUri .Replace("format=html", "") .Replace("format=shtm", "") .TrimEnd('?', '&'); url += url.Contains("?") ? "&" : "?"; var now = DateTime.UtcNow; var requestName = req.OperationName ?? dto.GetType().GetOperationName(); html = HtmlTemplates.GetHtmlFormatTemplate() .Replace("${Dto}", json) .Replace("${Title}", string.Format(TitleFormat, requestName, now)) .Replace("${MvcIncludes}", MiniProfiler.Profiler.RenderIncludes().ToString()) .Replace("${Header}", string.Format(HtmlTitleFormat, requestName, now)) .Replace("${ServiceUrl}", url) .Replace("${Humanize}", Humanize.ToString().ToLower()); } var utf8Bytes = html.ToUtf8Bytes(); res.OutputStream.Write(utf8Bytes, 0, utf8Bytes.Length); }
public async Task SerializeToStreamAsync(IRequest req, object response, Stream outputStream) { var res = req.Response; if (req.GetItem("HttpResult") is IHttpResult httpResult && httpResult.Headers.ContainsKey(HttpHeaders.Location) && httpResult.StatusCode != System.Net.HttpStatusCode.Created) { return; } try { if (res.StatusCode >= 400) { var responseStatus = response.GetResponseStatus(); req.Items[ErrorStatusKey] = responseStatus; } if (response is CompressedResult) { if (res.Dto != null) { response = res.Dto; } else { throw new ArgumentException("Cannot use Cached Result as ViewModel"); } } foreach (var viewEngine in ViewEngines) { var handled = await viewEngine.ProcessRequestAsync(req, response, outputStream); if (handled) { return; } } } catch (Exception ex) { if (res.StatusCode < 400) { throw; } //If there was an exception trying to render a Error with a View, //It can't handle errors so just write it out here. response = DtoUtils.CreateErrorResponse(req.Dto, ex); } //Handle Exceptions returning string if (req.ResponseContentType == MimeTypes.PlainText) { req.ResponseContentType = MimeTypes.Html; res.ContentType = MimeTypes.Html; } if (req.ResponseContentType != MimeTypes.Html && req.ResponseContentType != MimeTypes.JsonReport) { return; } var dto = response.GetDto(); if (!(dto is string html)) { // Serialize then escape any potential script tags to avoid XSS when displaying as HTML var json = JsonDataContractSerializer.Instance.SerializeToString(dto) ?? "null"; json = json.Replace("<", "<").Replace(">", ">"); var url = req.ResolveAbsoluteUrl(); var index = url.IndexOf("?"); var formatUrl = index != -1 ? url.Substring(0, index + 1) : url + "?"; foreach (var key in req.QueryString.AllKeys) { if (key == Keywords.Format) { continue; } formatUrl += (key.IsNullOrEmpty() ? "" : key + "=") + req.QueryString[key] + "&"; } var now = DateTime.Now; var requestName = req.OperationName ?? dto.GetType().GetOperationName(); html = HtmlTemplates.GetHtmlFormatTemplate() .Replace("${Dto}", json) .Replace("${Title}", string.Format(TitleFormat, requestName, now)) .Replace("${MvcIncludes}", MiniProfiler.Profiler.RenderIncludes()?.ToString()) .Replace("${Header}", string.Format(HtmlTitleFormat, requestName, now)) .Replace("${ServiceUrl}", req.AbsoluteUri) .Replace("${FormatUrl}", formatUrl) .Replace("${Humanize}", Humanize.ToString().ToLower()); } var utf8Bytes = html.ToUtf8Bytes(); await outputStream.WriteAsync(utf8Bytes, 0, utf8Bytes.Length); }