Ejemplo n.º 1
0
        /// <summary>
        /// Returns either json or full page html of a previous <see cref="MiniProfiler"/> session,
        /// identified by its <c>"?id=GUID"</c> on the query.
        /// </summary>
        /// <param name="context">The context to get a profiler response for.</param>
        private string GetSingleProfilerResult(HttpContext context)
        {
            Guid          id;
            ResultRequest clientRequest = null;
            // When we're rendering as a button/popup in the corner, it's an AJAX/JSON request.
            // If that's absent, we're rendering results as a full page for sharing.
            var jsonRequest = context.Request.Headers["Accept"]?.Contains("application/json") == true;

            // Try to parse from the JSON payload first
            if (jsonRequest &&
                context.Request.ContentLength > 0 &&
                ResultRequest.TryParse(context.Request.InputStream, out clientRequest) &&
                clientRequest.Id.HasValue)
            {
                id = clientRequest.Id.Value;
            }
            else if (Guid.TryParse(context.Request["id"], out id))
            {
                // We got the guid from the querystring
            }
            else if (Options.StopwatchProvider != null)
            {
                // Fall back to the last result
                id = Options.Storage.List(1).FirstOrDefault();
            }

            if (id == default(Guid))
            {
                return(jsonRequest ? NotFound(context) : NotFound(context, "text/plain", "No Guid id specified on the query string"));
            }

            var    profiler = Options.Storage.Load(id);
            string user     = Options.UserIdProvider?.Invoke(context.Request);

            Options.Storage.SetViewed(user, id);

            if (profiler == null)
            {
                return(jsonRequest ? NotFound(context) : NotFound(context, "text/plain", "No MiniProfiler results found with Id=" + id.ToString()));
            }

            bool needsSave = false;

            if (profiler.ClientTimings == null && clientRequest?.TimingCount > 0)
            {
                profiler.ClientTimings = ClientTimings.FromRequest(clientRequest);
                needsSave = true;
            }

            if (!profiler.HasUserViewed)
            {
                profiler.HasUserViewed = true;
                needsSave = true;
            }

            if (needsSave)
            {
                Options.Storage.Save(profiler);
            }

            if (!AuthorizeRequest(context, isList: false, message: out string authorizeMessage))
            {
                context.Response.ContentType = "application/json";
                return(@"""hidden"""); // JSON
            }

            return(jsonRequest ? ResultsJson(context, profiler) : ResultsFullPage(context, profiler));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Returns either JSON or full page HTML of a previous <c>MiniProfiler</c> session,
        /// identified by its <c>"?id=GUID"</c> on the query.
        /// </summary>
        /// <param name="context">The context to get a profiler response for.</param>
        private async Task <string> GetSingleProfilerResultAsync(HttpContext context)
        {
            Guid          id;
            ResultRequest clientRequest = null;
            // When we're rendering as a button/popup in the corner, it's an AJAX/JSON request.
            // If that's absent, we're rendering results as a full page for sharing.
            bool jsonRequest = context.Request.Headers["Accept"].FirstOrDefault()?.Contains("application/json") == true;

            // Try to parse from the JSON payload first
            if (jsonRequest &&
                context.Request.ContentLength > 0 &&
                ResultRequest.TryParse(context.Request.Body, out clientRequest) &&
                clientRequest.Id.HasValue)
            {
                id = clientRequest.Id.Value;
            }
            else if (Guid.TryParse(context.Request.Query["id"], out id))
            {
                // We got the guid from the querystring
            }
            else if (Options.StopwatchProvider != null)
            {
                // Fall back to the last result
                id = (await Options.Storage.ListAsync(1).ConfigureAwait(false)).FirstOrDefault();
            }

            if (id == default(Guid))
            {
                return(NotFound(context, jsonRequest ? null : "No GUID id specified on the query string"));
            }

            var profiler = await Options.Storage.LoadAsync(id).ConfigureAwait(false);

            string user = Options.UserIdProvider?.Invoke(context.Request);

            await Options.Storage.SetViewedAsync(user, id).ConfigureAwait(false);

            if (profiler == null)
            {
                return(NotFound(context, jsonRequest ? null : "No MiniProfiler results found with Id=" + id.ToString()));
            }

            bool needsSave = false;

            if (profiler.ClientTimings == null && clientRequest?.TimingCount > 0)
            {
                profiler.ClientTimings = ClientTimings.FromRequest(clientRequest);
                needsSave = true;
            }

            if (!profiler.HasUserViewed)
            {
                profiler.HasUserViewed = true;
                needsSave = true;
            }

            if (needsSave)
            {
                await Options.Storage.SaveAsync(profiler).ConfigureAwait(false);
            }

            if (!AuthorizeRequest(context, isList: false, message: out string authorizeMessage))
            {
                context.Response.ContentType = "application/json";
                return(@"""hidden"""); // JSON
            }

            if (jsonRequest)
            {
                context.Response.ContentType = "application/json";
                return(profiler.ToJson());
            }
            else
            {
                context.Response.ContentType = "text/html; charset=utf-8";
                return(Render.SingleResultHtml(profiler, context.Request.PathBase + Options.RouteBasePath.Value.EnsureTrailingSlash()));
            }
        }