From d38e2950d3c1b7d805e98a90fcfdca6d4465dbc4 Mon Sep 17 00:00:00 2001 From: "srinivasa.b" Date: Mon, 18 Nov 2013 16:34:14 +0530 Subject: [PATCH 1/4] Added a property that turn on/off gzip compression of css and js files. If suppose, gzip compression is turned on server for all the css files and granule combined css file can not be omitted from this compression. gzip compression happens two times, and the combined css / js files may not work as expected. So, there must be an option in granule to turn off gzip compression. a property called gzip.output is added to configuration. By default this value is set to true. If granule gzip compression is not required, then this parameter has to be set to false. --- .../web-app/WEB-INF/granule.properties | 4 ++++ jsp-demo/web/WEB-INF/granule.properties | 7 ++++++- tag-main/src/com/granule/CachedBundle.java | 12 +++++++++-- .../src/com/granule/CompressorSettings.java | 20 +++++++++++++++++++ 4 files changed, 40 insertions(+), 3 deletions(-) diff --git a/grails-demo/web-app/WEB-INF/granule.properties b/grails-demo/web-app/WEB-INF/granule.properties index 164ff79..152fb12 100644 --- a/grails-demo/web-app/WEB-INF/granule.properties +++ b/grails-demo/web-app/WEB-INF/granule.properties @@ -54,4 +54,8 @@ closure-compiler.add-path=js/closure-samples/ keepfirstcommentpath=js/dojo/*,js/superslider/slider.js,js/jquery*.js +#If set to true, JS and CSS files are returned after gzip compression +#By default, this is set to true. +#If server is already configured to gzip response for some reasons, then set this flag to false to avoid double gzip compression. +gzip.output=true diff --git a/jsp-demo/web/WEB-INF/granule.properties b/jsp-demo/web/WEB-INF/granule.properties index dabcf54..bd7953e 100644 --- a/jsp-demo/web/WEB-INF/granule.properties +++ b/jsp-demo/web/WEB-INF/granule.properties @@ -60,4 +60,9 @@ keepfirstcommentpath=js/dojo/*,js/superslider/slider.js,js/jquery*.js # contextroot - the constant context root of web-applications. # It need to be setup right for ant cache precompiling if there are hard-coded URLs in the pages -contextroot=web \ No newline at end of file +contextroot=web + +#If set to true, JS and CSS files are returned after gzip compression +#By default, this is set to true. +#If server is already configured to gzip response for some reasons, then set this flag to false to avoid double gzip compression. +gzip.output=true \ No newline at end of file diff --git a/tag-main/src/com/granule/CachedBundle.java b/tag-main/src/com/granule/CachedBundle.java index 2ee0cf9..4de8b60 100644 --- a/tag-main/src/com/granule/CachedBundle.java +++ b/tag-main/src/com/granule/CachedBundle.java @@ -115,7 +115,11 @@ else if (settings.getJsCompressMethod().equalsIgnoreCase(CompressorSettings.AUTO if (dep instanceof ExternalFragment && !hash.contains("/" + ((ExternalFragment) dep).getFilePath())) dependentFragments.add(dep); try { - bundleValue = gzip(text); + if(settings.isGzipOutput()){ + bundleValue = gzip(text); + } else { + bundleValue = text.getBytes(); + } } catch (IOException e) { throw new JSCompileException(e); } @@ -140,7 +144,11 @@ public void compileCss(CompressorSettings settings, IRequestProxy request) throw text = Compressor.unifyCss(fragments, dependentFragments, settings, request); try { - bundleValue = gzip(text); + if(settings.isGzipOutput()){ + bundleValue = gzip(text); + } else { + bundleValue = text.getBytes(); + } } catch (IOException e) { throw new JSCompileException(e); } diff --git a/tag-main/src/com/granule/CompressorSettings.java b/tag-main/src/com/granule/CompressorSettings.java index c8c4436..2a893e9 100644 --- a/tag-main/src/com/granule/CompressorSettings.java +++ b/tag-main/src/com/granule/CompressorSettings.java @@ -50,6 +50,7 @@ public class CompressorSettings { private String tagName = DEFAULT_TAG_NAME; private String contextRoot = null; private String basePath = null; + private boolean gzipOutput = true; public static final String NONE_VALUE = "none"; public static final String CLOSURE_COMPILER_VALUE = "closure-compiler"; @@ -88,6 +89,7 @@ public class CompressorSettings { public static final String DEFAULT_TAG_NAME = "g:compress"; public static final String CONTEXTROOT_KEY = "contextroot"; public static final String BASEPATH_KEY = "basepath"; + public static final String GZIP_OUTPUT_KEY = "gzip.output"; public String getJsCompressMethod() { return jsCompressMethod; @@ -131,6 +133,20 @@ public String getOutputWrapperMarker() { public String getTagName() { return tagName; + } + + /** + * @return Returns the gzipOutput. + */ + public boolean isGzipOutput() { + return gzipOutput; + } + + /** + * @param gzipOutput The gzipOutput to set. + */ + public void setGzipOutput(boolean gzipOutput) { + this.gzipOutput = gzipOutput; } public void load(Utf8Properties props) throws IOException { @@ -229,6 +245,10 @@ public void load(Utf8Properties props) throws IOException { if (props.containsKey(CONTEXTROOT_KEY)) contextRoot = props.getProperty(CONTEXTROOT_KEY); + + if(props.contains(GZIP_OUTPUT_KEY)){ + gzipOutput = getBoolean(props.getProperty(GZIP_OUTPUT_KEY), gzipOutput); + } } private void readFileListProperty(Utf8Properties props, String settingName, List list) throws IOException { From 3b3ba5ecda5971756bf2c1209604f12e56f0629f Mon Sep 17 00:00:00 2001 From: sboyina Date: Mon, 18 Nov 2013 17:35:20 +0530 Subject: [PATCH 2/4] Fixed a bug related to gzip output --- tag-main/src/com/granule/CompressorSettings.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tag-main/src/com/granule/CompressorSettings.java b/tag-main/src/com/granule/CompressorSettings.java index 2a893e9..2d7b19f 100644 --- a/tag-main/src/com/granule/CompressorSettings.java +++ b/tag-main/src/com/granule/CompressorSettings.java @@ -246,7 +246,7 @@ public void load(Utf8Properties props) throws IOException { if (props.containsKey(CONTEXTROOT_KEY)) contextRoot = props.getProperty(CONTEXTROOT_KEY); - if(props.contains(GZIP_OUTPUT_KEY)){ + if(props.containsKey(GZIP_OUTPUT_KEY)){ gzipOutput = getBoolean(props.getProperty(GZIP_OUTPUT_KEY), gzipOutput); } } From 0299f045fd6844c62b3560f002017f930d04bf7f Mon Sep 17 00:00:00 2001 From: sboyina Date: Mon, 18 Nov 2013 17:46:07 +0530 Subject: [PATCH 3/4] Reverted the earlier fix and made a change to decompress gzip output. Reverted the earlier fix and made a change to decompress gzip output. --- tag-main/src/com/granule/CachedBundle.java | 12 ++---------- tag-main/src/com/granule/CompressorHandler.java | 2 +- 2 files changed, 3 insertions(+), 11 deletions(-) diff --git a/tag-main/src/com/granule/CachedBundle.java b/tag-main/src/com/granule/CachedBundle.java index 4de8b60..2ee0cf9 100644 --- a/tag-main/src/com/granule/CachedBundle.java +++ b/tag-main/src/com/granule/CachedBundle.java @@ -115,11 +115,7 @@ else if (settings.getJsCompressMethod().equalsIgnoreCase(CompressorSettings.AUTO if (dep instanceof ExternalFragment && !hash.contains("/" + ((ExternalFragment) dep).getFilePath())) dependentFragments.add(dep); try { - if(settings.isGzipOutput()){ - bundleValue = gzip(text); - } else { - bundleValue = text.getBytes(); - } + bundleValue = gzip(text); } catch (IOException e) { throw new JSCompileException(e); } @@ -144,11 +140,7 @@ public void compileCss(CompressorSettings settings, IRequestProxy request) throw text = Compressor.unifyCss(fragments, dependentFragments, settings, request); try { - if(settings.isGzipOutput()){ - bundleValue = gzip(text); - } else { - bundleValue = text.getBytes(); - } + bundleValue = gzip(text); } catch (IOException e) { throw new JSCompileException(e); } diff --git a/tag-main/src/com/granule/CompressorHandler.java b/tag-main/src/com/granule/CompressorHandler.java index 23c702e..fad0af2 100644 --- a/tag-main/src/com/granule/CompressorHandler.java +++ b/tag-main/src/com/granule/CompressorHandler.java @@ -53,7 +53,7 @@ public void handle(HttpServletRequest request, HttpServletResponse response, Str OutputStream os = response.getOutputStream(); try { - if (gzipSupported(request)) { + if (settings.isGzipOutput() && gzipSupported(request)) { response.setHeader("Content-Encoding", "gzip"); os.write(bundle.getBundleValue()); } else From faba38116b52ee2366639fe018dbb74325bf5d5c Mon Sep 17 00:00:00 2001 From: Srinivasa Rao Boyina Date: Thu, 12 Dec 2013 16:54:51 +0530 Subject: [PATCH 4/4] modified regex to accept urls that start with forward slash. --- tag-main/src/com/granule/utils/CSSHandler.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tag-main/src/com/granule/utils/CSSHandler.java b/tag-main/src/com/granule/utils/CSSHandler.java index 57d4a62..203c15e 100644 --- a/tag-main/src/com/granule/utils/CSSHandler.java +++ b/tag-main/src/com/granule/utils/CSSHandler.java @@ -39,7 +39,7 @@ public final class CSSHandler { private static final String stringLiteralRegex = "(\"(?:\\.|[^\\\"])*\"|'(?:\\.|[^\\'])*')"; private static final String urlRegex = String.format( - "(?:url\\(\\s*(%s|[^)]*)\\s*\\))", stringLiteralRegex); + "(?:url\\(\\s*(?!\\s*\\'\\/)(?!\\s*\\\"\\/)(?!\\s*\\/)(%s|[^)]*)\\s*\\))", stringLiteralRegex); private static final String importRegex = String.format( "(?:@import\\s+(%s|%s))", urlRegex, stringLiteralRegex);