/// <summary>
        /// Returns null if there is not client timing stuff
        /// </summary>
        /// <param name="request">The <see cref="HttpRequest"/> to get client timings from.</param>
        public static ClientTimings GetClientTimings(this HttpRequest request)
        {
            var dict = new Dictionary <string, string>();
            var form = request.Form;

            foreach (var k in form.AllKeys)
            {
                dict.Add(k, form[k]);
            }
            return(ClientTimings.FromForm(dict));
        }
Пример #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)
        {
            bool            jsonRequest = false;
            IFormCollection form        = null;

            // When we're rendering as a button/popup in the corner, we'll pass { popup: 1 } from jQuery
            // If it's absent, we're rendering results as a full page for sharing.
            if (context.Request.HasFormContentType)
            {
                form = await context.Request.ReadFormAsync().ConfigureAwait(false);

                // TODO: Get rid of popup and switch to application/json Accept header detection
                jsonRequest = form["popup"] == "1";
            }

            // This guid is the MiniProfiler.Id property. If a guid is not supplied,
            // the last set of results needs to be displayed.
            string requestId = form?["id"] ?? context.Request.Query["id"];

            if (!Guid.TryParse(requestId, out var id) && MiniProfiler.Settings.Storage != null)
            {
                id = (await MiniProfiler.Settings.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 MiniProfiler.Settings.Storage.LoadAsync(id).ConfigureAwait(false);

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

            await MiniProfiler.Settings.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 && form != null)
            {
                var dict = new Dictionary <string, string>();
                foreach (var k in form.Keys)
                {
                    dict.Add(k, form[k]);
                }
                profiler.ClientTimings = ClientTimings.FromForm(dict);

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

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

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

            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));
        }
Пример #3
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)
        {
            var isPost = context.Request.HasFormContentType;
            // when we're rendering as a button/popup in the corner, we'll pass ?popup=1
            // if it's absent, we're rendering results as a full page for sharing
            var isPopup = isPost && context.Request.Form["popup"].FirstOrDefault() == "1";
            // this guid is the MiniProfiler.Id property
            // if this guid is not supplied, the last set of results needs to be
            // displayed. The home page doesn't have profiling otherwise.
            var requestId = isPost
                ? context.Request.Form["id"]
                : context.Request.Query["id"];

            if (!Guid.TryParse(requestId, out var id) && MiniProfiler.Settings.Storage != null)
            {
                id = (await MiniProfiler.Settings.Storage.ListAsync(1).ConfigureAwait(false)).FirstOrDefault();
            }

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

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

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

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

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

            bool needsSave = false;

            if (profiler.ClientTimings == null && isPost)
            {
                var form = context.Request.Form;
                var dict = new Dictionary <string, string>();
                foreach (var k in form.Keys)
                {
                    dict.Add(k, form[k]);
                }
                profiler.ClientTimings = ClientTimings.FromForm(dict);

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

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

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

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

            return(isPopup ? ResultsJson(context, profiler) : ResultsFullPage(context, profiler));
        }