/// <summary> /// Provides implementations an instance of <see cref="Section" /> to self populate any end user configuration options. /// </summary> /// <param name="section">The configuration section, <c><glimpse></c> from <c>web.config</c>.</param> /// <remarks> /// Populates the content type white list with values from <c>web.config</c>. /// A list of ratified Http status codes is available in <see href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html">Section 10 of RFC 2616</see>, the Http version 1.1 specification. /// </remarks> /// <example> /// Configure the content type white list in <c>web.config</c> with the following entries: /// <code> /// <![CDATA[ /// <glimpse defaultRuntimePolicy="On" endpointBaseUri="~/Glimpse.axd"> /// <runtimePolicies> /// <contentTypes> /// <!-- <clear /> clear to reset defaults --> /// <add contentType="{media\type}" runtimePolicy="on" /> /// </contentTypes> /// </runtimePolicies> /// </glimpse> /// ]]> /// </code> /// </example> public void Configure(Section section) { foreach (ContentTypeElement item in section.RuntimePolicies.ContentTypes) { ContentTypeWhiteList.Add(new Tuple <string, RuntimePolicy>(item.ContentType, item.RuntimePolicy)); } }
/// <summary> /// Provides implementations an instance of <see cref="Section" /> to self populate any end user configuration options. /// </summary> /// <param name="section">The configuration section, <c><glimpse></c> from <c>web.config</c>.</param> /// <remarks> /// Populates the content type white list with values from <c>web.config</c>. /// A list of ratified Http status codes is available in <see href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html">Section 10 of RFC 2616</see>, the Http version 1.1 specification. /// </remarks> /// <example> /// Configure the content type white list in <c>web.config</c> with the following entries: /// <code> /// <![CDATA[ /// <glimpse defaultRuntimePolicy="On" endpointBaseUri="~/Glimpse.axd"> /// <runtimePolicies> /// <contentTypes> /// <!-- <clear /> clear to reset defaults --> /// <add contentType="{media\type}" /> /// </contentTypes> /// </runtimePolicies> /// </glimpse> /// ]]> /// </code> /// </example> public void Configure(Section section) { foreach (ContentTypeElement item in section.RuntimePolicies.ContentTypes) { ContentTypeWhiteList.Add(item.ContentType); } }
/// <summary> /// Executes the specified policy with the given context. /// </summary> /// <param name="policyContext">The policy context.</param> /// <returns> /// <c>On</c> if the response content type is contained on the white list, otherwise <c>Off</c>. /// </returns> /// <exception cref="System.ArgumentNullException">Exception thrown if <paramref name="policyContext"/> is <c>null</c>.</exception> public RuntimePolicy Execute(IRuntimePolicyContext policyContext) { try { var contentType = policyContext.RequestMetadata.ResponseContentType.ToLowerInvariant(); // support for the following content type strings: "text/html" & "text/html; charset=utf-8" return(ContentTypeWhiteList.Any(ct => contentType.Contains(ct.ToLowerInvariant())) ? RuntimePolicy.On : RuntimePolicy.Off); } catch (Exception exception) { policyContext.Logger.Warn(Resources.ExecutePolicyWarning, exception, GetType()); return(RuntimePolicy.Off); } }