/// <summary> /// Event handler that gets triggered just before the ASP.NET Request Handler gets executed. /// </summary> /// <param name="sender">The <see cref="HttpApplication"/> sending the event.</param> /// <param name="eventArgs">The event arguments.</param> private static void OnPreRequestHandlerExecute(object sender, EventArgs eventArgs) { HttpApplication application = (HttpApplication)sender; HttpContext context = application.Context; HttpRequest request = context.Request; HttpResponse response = context.Response; string urlPath = request.Url.AbsolutePath; DateTime ifModifiedSince = Convert.ToDateTime(request.Headers["If-Modified-Since"]); using (new Tracer(sender, eventArgs, urlPath, ifModifiedSince)) { Localization localization = WebRequestContext.Localization; string staticsRootUrl = SiteConfiguration.GetLocalStaticsUrl(localization.LocalizationId); urlPath = urlPath.StartsWith("/" + staticsRootUrl) ? urlPath.Substring(staticsRootUrl.Length + 1) : urlPath; if (!localization.IsStaticContentUrl(urlPath)) { // Not a static content item; continue the HTTP pipeline. return; } try { using (StaticContentItem staticContentItem = SiteConfiguration.ContentProvider.GetStaticContentItem(urlPath, localization)) { DateTime lastModified = staticContentItem.LastModified; if (lastModified <= ifModifiedSince.AddSeconds(1)) { Log.Debug("Static content item last modified at {0} => Sending HTTP 304 (Not Modified).", lastModified); response.StatusCode = (int)HttpStatusCode.NotModified; response.SuppressContent = true; } else { // Items with a versioned URL can be cached long-term, because the URL will change if needed. bool isVersionedUrl = context.Items.Contains(IsVersionedUrlContextItem); TimeSpan maxAge = isVersionedUrl ? new TimeSpan(7, 0, 0, 0) : new TimeSpan(0, 1, 0, 0); // 1 Week or 1 Hour response.Cache.SetLastModified(lastModified); // Allows the browser to do an If-Modified-Since request next time response.Cache.SetCacheability(HttpCacheability.Public); // Allow caching response.Cache.SetMaxAge(maxAge); response.Cache.SetExpires(DateTime.UtcNow.Add(maxAge)); response.ContentType = staticContentItem.ContentType; staticContentItem.GetContentStream().CopyTo(response.OutputStream); } } } catch (DxaItemNotFoundException ex) { SendNotFoundResponse(ex.Message, response); } catch (Exception ex) { // Other exceptions: log and let ASP.NET handle them Log.Error(ex); throw; } // Terminate the HTTP pipeline. application.CompleteRequest(); } }
/// <summary> /// Event handler that gets triggered just before the ASP.NET Request Handler gets executed. /// </summary> /// <param name="sender">The <see cref="HttpApplication"/> sending the event.</param> /// <param name="eventArgs">The event arguments.</param> private static void OnPreRequestHandlerExecute(object sender, EventArgs eventArgs) { HttpApplication application = (HttpApplication)sender; HttpContext context = application.Context; HttpRequest request = context.Request; HttpResponse response = context.Response; string urlPath = request.Url.AbsolutePath; DateTime ifModifiedSince = Convert.ToDateTime(request.Headers["If-Modified-Since"]); using (new Tracer(sender, eventArgs, urlPath, ifModifiedSince)) { Localization localization = WebRequestContext.Localization; string staticsRootUrl = SiteConfiguration.GetLocalStaticsUrl(localization.LocalizationId); urlPath = urlPath.StartsWith("/" + staticsRootUrl) ? urlPath.Substring(staticsRootUrl.Length + 1) : urlPath; if (!localization.IsStaticContentUrl(urlPath)) { // Not a static content item; continue the HTTP pipeline. return; } try { using (StaticContentItem staticContentItem = SiteConfiguration.ContentProvider.GetStaticContentItem(urlPath, localization)) { DateTime lastModified = staticContentItem.LastModified; if (lastModified < ifModifiedSince) { Log.Debug("Static content item last modified at {0} => Sending HTTP 304 (Not Modified).", lastModified); response.StatusCode = (int)HttpStatusCode.NotModified; response.SuppressContent = true; } else { response.ContentType = staticContentItem.ContentType; staticContentItem.GetContentStream().CopyTo(response.OutputStream); } } } catch (DxaItemNotFoundException ex) { SendNotFoundResponse(ex.Message, response); } // Terminate the HTTP pipeline. application.CompleteRequest(); } }