trafficserver-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From zw...@apache.org
Subject [trafficserver] branch master updated: Allow gzip plugin to configure which status code to transform
Date Wed, 28 Mar 2018 14:37:49 GMT
This is an automated email from the ASF dual-hosted git repository.

zwoop pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficserver.git


The following commit(s) were added to refs/heads/master by this push:
     new 2d94388  Allow gzip plugin to configure which status code to transform
2d94388 is described below

commit 2d943885b916455b1b99d80999d7f25f68818e60
Author: Randall Meyer <randallmeyer@yahoo.com>
AuthorDate: Tue Mar 13 13:29:22 2018 -0700

    Allow gzip plugin to configure which status code to transform
    
    Addresses issue #2932
---
 plugins/gzip/configuration.cc   | 34 ++++++++++++++++++++++++++++++++++
 plugins/gzip/configuration.h    |  4 ++++
 plugins/gzip/gzip.cc            |  6 +++---
 plugins/gzip/sample.gzip.config |  6 ++++++
 4 files changed, 47 insertions(+), 3 deletions(-)

diff --git a/plugins/gzip/configuration.cc b/plugins/gzip/configuration.cc
index 0d69f33..6373a00 100644
--- a/plugins/gzip/configuration.cc
+++ b/plugins/gzip/configuration.cc
@@ -194,6 +194,18 @@ HostConfiguration::is_url_allowed(const char *url, int url_len)
 }
 
 bool
+HostConfiguration::is_status_code_compressible(const TSHttpStatus status_code) const
+{
+  // maintain backwards compatibility/usability out of the box
+  if (compressible_status_codes_.empty()) {
+    return status_code == 200;
+  }
+
+  std::set<TSHttpStatus>::const_iterator it = compressible_status_codes_.find(status_code);
+  return it != compressible_status_codes_.end();
+}
+
+bool
 HostConfiguration::is_content_type_compressible(const char *content_type, int content_type_length)
 {
   string scontent_type(content_type, content_type_length);
@@ -245,6 +257,25 @@ HostConfiguration::add_compression_algorithms(string &line)
   }
 }
 
+void
+HostConfiguration::add_compressible_status_codes(string &line)
+{
+  for (;;) {
+    string token = extractFirstToken(line, isCommaOrSpace);
+    if (token.empty()) {
+      break;
+    }
+
+    uint status_code = strtoul(token.c_str(), nullptr, 10);
+    if (status_code == 0) {
+      error("Invalid status code %s", token.c_str());
+      continue;
+    }
+
+    compressible_status_codes_.insert(static_cast<TSHttpStatus>(status_code));
+  }
+}
+
 int
 HostConfiguration::compression_algorithms()
 {
@@ -334,6 +365,9 @@ Configuration::Parse(const char *path)
           state = kParseStart;
         } else if (token == "allow") {
           state = kParseAllow;
+        } else if (token == "compressible-status-code") {
+          current_host_configuration->add_compressible_status_codes(line);
+          state = kParseStart;
         } else {
           warning("failed to interpret \"%s\" at line %zu", token.c_str(), lineno);
         }
diff --git a/plugins/gzip/configuration.h b/plugins/gzip/configuration.h
index cc294bf..b252296 100644
--- a/plugins/gzip/configuration.h
+++ b/plugins/gzip/configuration.h
@@ -24,6 +24,7 @@
 #ifndef GZIP_CONFIGURATION_H_
 #define GZIP_CONFIGURATION_H_
 
+#include <set>
 #include <string>
 #include <vector>
 #include "debug_macros.h"
@@ -114,8 +115,10 @@ public:
   void add_disallow(const std::string &disallow);
   void add_allow(const std::string &allow);
   void add_compressible_content_type(const std::string &content_type);
+  void add_compressible_status_codes(std::string &status_codes);
   bool is_url_allowed(const char *url, int url_len);
   bool is_content_type_compressible(const char *content_type, int content_type_length);
+  bool is_status_code_compressible(const TSHttpStatus status_code) const;
   void add_compression_algorithms(std::string &algorithms);
   int compression_algorithms();
 
@@ -146,6 +149,7 @@ private:
   StringContainer compressible_content_types_;
   StringContainer disallows_;
   StringContainer allows_;
+  std::set<TSHttpStatus> compressible_status_codes_;
 
   DISALLOW_COPY_AND_ASSIGN(HostConfiguration);
 };
diff --git a/plugins/gzip/gzip.cc b/plugins/gzip/gzip.cc
index 249db16..29406b3 100644
--- a/plugins/gzip/gzip.cc
+++ b/plugins/gzip/gzip.cc
@@ -663,9 +663,9 @@ transformable(TSHttpTxn txnp, bool server, HostConfiguration *host_configuration
 
   resp_status = TSHttpHdrStatusGet(bufp, hdr_loc);
 
-  // For now, only do gzip / brotli on 200 responses, dealing with error responses can mess
up plugins
-  // like the escalate.so plugin, and possibly the escalation feature of parent.config. See
#2913.
-  if (resp_status != 200) {
+  // NOTE: error responses can mess up plugins like the escalate.so plugin,
+  // and possibly the escalation feature of parent.config. See #2913.
+  if (!host_configuration->is_status_code_compressible(resp_status)) {
     info("http response status [%d] is not compressible", resp_status);
     TSHandleMLocRelease(bufp, TS_NULL_MLOC, hdr_loc);
     return 0;
diff --git a/plugins/gzip/sample.gzip.config b/plugins/gzip/sample.gzip.config
index 5fec6f5..11309bd 100644
--- a/plugins/gzip/sample.gzip.config
+++ b/plugins/gzip/sample.gzip.config
@@ -30,6 +30,9 @@
 # compressible-content-type: wildcard pattern for matching compressible content types
 #
 # disallow: wildcard pattern for disablign compression on urls
+#
+# compressible-status-code: a comma separated list of status codes in which to enable compression
+#
 ######################################################################
 
 #first, we configure the default/global plugin behaviour
@@ -39,6 +42,7 @@ cache false
 
 compressible-content-type text/*
 compressible-content-type *javascript*
+compressible-status-code 200
 disallow /notthis/*.js
 disallow /notthat*
 disallow */bla*
@@ -52,6 +56,8 @@ compressible-content-type text/*
 #disable a content type
 compressible-content-type !text/javascript
 
+compressible-status-code 200,206,409
+
 cache false
 disallow /notthis/*.js
 disallow /notthat*

-- 
To stop receiving notification emails like this one, please contact
zwoop@apache.org.

Mime
View raw message