From e4452ed79819e230a3a89b687ade106cc760ec9b Mon Sep 17 00:00:00 2001 From: Martin Gotink Date: Sun, 10 Feb 2013 15:42:12 +0100 Subject: [PATCH] Send 304 if If-Modified-Since header is set --- .../HashAndCacheResourceMapper.groovy | 24 ++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/grails-app/resourceMappers/org/grails/plugin/cachedresources/HashAndCacheResourceMapper.groovy b/grails-app/resourceMappers/org/grails/plugin/cachedresources/HashAndCacheResourceMapper.groovy index c576604..b62606b 100644 --- a/grails-app/resourceMappers/org/grails/plugin/cachedresources/HashAndCacheResourceMapper.groovy +++ b/grails-app/resourceMappers/org/grails/plugin/cachedresources/HashAndCacheResourceMapper.groovy @@ -28,7 +28,12 @@ class HashAndCacheResourceMapper { if (log.debugEnabled) { log.debug "Setting caching headers on ${req.requestURI}" } - cacheHeadersService.cache resp, [neverExpires: true, shared: true] + + if (isCachedOnClient(req)) { + resp.sendError(304) // Not modified + } else { + cacheHeadersService.cache resp, [neverExpires: true, shared: true] + } } } @@ -51,6 +56,23 @@ class HashAndCacheResourceMapper { param } } + + boolean isCachedOnClient(request) { + def modifiedDate = -1 + + try { + modifiedDate = request.getDateHeader('If-Modified-Since') + } catch (IllegalArgumentException iae) { + log.error ("Couldn't parse If-Modified-Since header", iae) + } + + if (modifiedDate != -1) { + // Resource is static, if the If-Modified-Since header is set the resource is cached + return true + } + + false + } boolean getShortenLinks() { resourceService.getConfigParamOrDefault('shorten', true)