Exemple #1
0
        /// <summary>
        /// Set the HTTP headers in the response based
        /// on values from the <see cref="SetHeadersElement"/>
        /// </summary>
        /// <param name="context">
        /// The HTTP context
        /// </param>
        public void SetHeaders(HttpContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            PipelineCapabilities caps = context.Request.Browser as PipelineCapabilities;
            var flowData = caps.FlowData;

            if (_setHeadersElement != null)
            {
                var headersToSet = flowData
                                   .GetFromElement(_setHeadersElement).ResponseHeaderDictionary;
                SetHeaders(context, headersToSet);
            }
        }
        /// <summary>
        /// Gets the <see cref="HttpBrowserCapabilities"/> object for the
        /// specified <see cref="HttpRequest"/>.
        /// </summary>
        /// <param name="request">
        /// The <see cref="HttpRequest"/> to get the browser capabilities for.
        /// </param>
        /// <returns>
        /// The browser capabilities for the specified
        /// <see cref="HttpRequest"/>.
        /// </returns>
        public override HttpBrowserCapabilities GetBrowserCapabilities(
            HttpRequest request)
        {
            HttpBrowserCapabilities caps;
            var baseCaps = base.GetBrowserCapabilities(request);

            var flowData = WebPipeline.Process(request);

            if (flowData != null)
            {
                // A provider is present so 51Degrees can be used to override
                // some of the returned values.
                caps = new PipelineCapabilities(
                    baseCaps,
                    request,
                    flowData);

                // Copy the adapters from the original.
                var adapters = baseCaps.Adapters.GetEnumerator();
                while (adapters.MoveNext())
                {
                    caps.Adapters.Add(adapters.Key, adapters.Value);
                }

                // Copy the browsers from the original to prevent the Browsers
                // property returning null.
                if (baseCaps.Browsers != null)
                {
                    foreach (string browser in baseCaps.Browsers)
                    {
                        caps.AddBrowser(browser);
                    }
                }
            }
            else
            {
                // No 51Degrees flow data is present so we have to use
                // the base capabilities only.
                caps = baseCaps;
            }
            return(caps);
        }
Exemple #3
0
        /// <summary>
        /// Add the JavaScript from the flow data object to the HttpResponse
        /// </summary>
        /// <param name="context">
        /// The HttpContext containing the HttpResponse to add the
        /// JavaScript to.
        /// </param>
        public void ServeJavascript(HttpContext context)
        {
            context.Response.Clear();
            context.Response.ClearHeaders();

            PipelineCapabilities caps = context.Request.Browser as PipelineCapabilities;

            var flowData = caps.FlowData;

            // Get the hash code.
            var hash = flowData.GenerateKey(
                flowData.Pipeline.EvidenceKeyFilter).GetHashCode();

            if (hash.ToString(CultureInfo.InvariantCulture) ==
                context.Request.Headers["If-None-Match"])
            {
                // The response hasn't changed so respond with a 304.
                context.Response.StatusCode = 304;
            }
            else
            {
                var bundler = flowData.Pipeline.GetElement <JavaScriptBuilderElement>();
                if (bundler != null)
                {
                    var bundlerData = flowData.GetFromElement(bundler);

                    // Otherwise, return the minified script to the client.
                    context.Response.Write(bundlerData.JavaScript);

                    SetHeaders(context,
                               hash.ToString(CultureInfo.InvariantCulture),
                               bundlerData.JavaScript.Length);
                }
                else
                {
                    // There is no bundler element to get the javascript from
                    // so the response will just be empty.
                }
            }
            context.ApplicationInstance.CompleteRequest();
        }
        private static IFlowData GetFlowData(HttpContext context)
        {
            PipelineCapabilities caps = context.Request.Browser as PipelineCapabilities;

            return(caps.FlowData);
        }