You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficserver.apache.org by sh...@apache.org on 2017/12/21 20:26:21 UTC

[trafficserver] branch master updated: Allow for spaces in list of algorithms in configuration file for gzip plugin.

This is an automated email from the ASF dual-hosted git repository.

shinrich 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 b8b20ab  Allow for spaces in list of algorithms in configuration file for gzip plugin.
b8b20ab is described below

commit b8b20ab2a024832c88e52bf6de6b5b9e71393790
Author: Walt Karas <wk...@yahoo-inc.com>
AuthorDate: Thu Dec 21 00:24:03 2017 +0000

    Allow for spaces in list of algorithms in configuration file for gzip plugin.
---
 plugins/gzip/configuration.cc                      | 81 ++++++++++++----------
 plugins/gzip/configuration.h                       |  2 +-
 tests/gold_tests/pluginTest/gzip/gzip.test.py      |  2 +-
 tests/gold_tests/pluginTest/gzip/gzip2.config      |  7 ++
 .../gold_tests/pluginTest/url_sig/url_sig.test.py  |  1 +
 5 files changed, 55 insertions(+), 38 deletions(-)

diff --git a/plugins/gzip/configuration.cc b/plugins/gzip/configuration.cc
index 4271478..0d69f33 100644
--- a/plugins/gzip/configuration.cc
+++ b/plugins/gzip/configuration.cc
@@ -25,7 +25,6 @@
 #include <fstream>
 #include <algorithm>
 #include <vector>
-#include <sstream>
 #include <fnmatch.h>
 
 namespace Gzip
@@ -63,28 +62,38 @@ trim_if(string &s, int (*fp)(int))
   rtrim_if(s, fp);
 }
 
-vector<string>
-tokenize(const string &s, int (*fp)(int))
+string
+extractFirstToken(string &s, int (*fp)(int))
 {
-  vector<string> r;
-  string tmp;
+  int startTok{-1}, endTok{-1}, idx{0};
 
-  for (char i : s) {
-    if (fp(i)) {
-      if (tmp.size()) {
-        r.push_back(tmp);
-        tmp = "";
+  for (;; ++idx) {
+    if (idx == int(s.length())) {
+      if (endTok < 0) {
+        endTok = idx;
       }
-    } else {
-      tmp += i;
+      break;
+    } else if (fp(s[idx])) {
+      if ((startTok >= 0) and (endTok < 0)) {
+        endTok = idx;
+      }
+    } else if (endTok > 0) {
+      break;
+    } else if (startTok < 0) {
+      startTok = idx;
     }
   }
 
-  if (tmp.size()) {
-    r.push_back(tmp);
+  string tmp;
+  if (startTok >= 0) {
+    tmp = string(s, startTok, endTok - startTok);
+  }
+
+  if (idx > 0) {
+    s = string(s, idx, s.length() - idx);
   }
 
-  return r;
+  return tmp;
 }
 
 enum ParserState {
@@ -95,7 +104,6 @@ enum ParserState {
   kParseCache,
   kParseDisallow,
   kParseFlush,
-  kParseAlgorithms,
   kParseAllow
 };
 
@@ -207,22 +215,29 @@ HostConfiguration::is_content_type_compressible(const char *content_type, int co
   return is_match;
 }
 
+int
+isCommaOrSpace(int ch)
+{
+  return (ch == ',') or isspace(ch);
+}
+
 void
-HostConfiguration::add_compression_algorithms(const string &algorithms)
+HostConfiguration::add_compression_algorithms(string &line)
 {
-  istringstream compress_algo(algorithms);
-  string token;
   compression_algorithms_ = ALGORITHM_DEFAULT; // remove the default gzip.
-  while (getline(compress_algo, token, ',')) {
-    if (token.find("br") != string::npos) {
+  for (;;) {
+    string token = extractFirstToken(line, isCommaOrSpace);
+    if (token.empty()) {
+      break;
+    } else if (token == "br") {
 #ifdef HAVE_BROTLI_ENCODE_H
       compression_algorithms_ |= ALGORITHM_BROTLI;
 #else
       error("supported-algorithms: brotli support not compiled in.");
 #endif
-    } else if (token.find("gzip") != string::npos) {
+    } else if (token == "gzip") {
       compression_algorithms_ |= ALGORITHM_GZIP;
-    } else if (token.find("deflate") != string::npos) {
+    } else if (token == "deflate") {
       compression_algorithms_ |= ALGORITHM_DEFLATE;
     } else {
       error("Unknown compression type. Supported compression-algorithms <br,gzip,deflate>.");
@@ -280,18 +295,15 @@ Configuration::Parse(const char *path)
     ++lineno;
 
     trim_if(line, isspace);
-    if (line.size() == 0) {
+    if (line.empty()) {
       continue;
     }
 
-    vector<string> v = tokenize(line, isspace);
+    for (;;) {
+      string token = extractFirstToken(line, isspace);
 
-    for (auto token : v) {
-      trim_if(token, isspace);
-
-      // should not happen
-      if (!token.size()) {
-        continue;
+      if (token.empty()) {
+        break;
       }
 
       // once a comment is encountered, we are done processing the line
@@ -318,7 +330,8 @@ Configuration::Parse(const char *path)
         } else if (token == "flush") {
           state = kParseFlush;
         } else if (token == "supported-algorithms") {
-          state = kParseAlgorithms;
+          current_host_configuration->add_compression_algorithms(line);
+          state = kParseStart;
         } else if (token == "allow") {
           state = kParseAllow;
         } else {
@@ -349,10 +362,6 @@ Configuration::Parse(const char *path)
         current_host_configuration->set_flush(token == "true");
         state = kParseStart;
         break;
-      case kParseAlgorithms:
-        current_host_configuration->add_compression_algorithms(token);
-        state = kParseStart;
-        break;
       case kParseAllow:
         current_host_configuration->add_allow(token);
         state = kParseStart;
diff --git a/plugins/gzip/configuration.h b/plugins/gzip/configuration.h
index da184e0..cc294bf 100644
--- a/plugins/gzip/configuration.h
+++ b/plugins/gzip/configuration.h
@@ -116,7 +116,7 @@ public:
   void add_compressible_content_type(const std::string &content_type);
   bool is_url_allowed(const char *url, int url_len);
   bool is_content_type_compressible(const char *content_type, int content_type_length);
-  void add_compression_algorithms(const std::string &algorithms);
+  void add_compression_algorithms(std::string &algorithms);
   int compression_algorithms();
 
   // Ref-counting these host configuration objects
diff --git a/tests/gold_tests/pluginTest/gzip/gzip.test.py b/tests/gold_tests/pluginTest/gzip/gzip.test.py
index 8d55cca..f6aa067 100644
--- a/tests/gold_tests/pluginTest/gzip/gzip.test.py
+++ b/tests/gold_tests/pluginTest/gzip/gzip.test.py
@@ -80,7 +80,7 @@ def oneTs(name, AeHdr1='gzip, deflate, sdch, br'):
     ts.Disk.remap_config.AddLine(
         'map http://ae-2/ http://127.0.0.1:{}/'.format(server.Variables.Port) +
         ' @plugin=conf_remap.so @pparam=proxy.config.http.normalize_ae=2' +
-        ' @plugin=gzip.so @pparam={}/gzip.config'.format(Test.TestDirectory)
+        ' @plugin=gzip.so @pparam={}/gzip2.config'.format(Test.TestDirectory)
     )
 
     def curl(idx, encodingList):
diff --git a/tests/gold_tests/pluginTest/gzip/gzip2.config b/tests/gold_tests/pluginTest/gzip/gzip2.config
new file mode 100644
index 0000000..c808d87
--- /dev/null
+++ b/tests/gold_tests/pluginTest/gzip/gzip2.config
@@ -0,0 +1,7 @@
+cache true
+remove-accept-encoding true
+compressible-content-type text/*
+compressible-content-type application/x-javascript*
+compressible-content-type application/javascript*
+compressible-content-type application/json*
+supported-algorithms gzip, br
diff --git a/tests/gold_tests/pluginTest/url_sig/url_sig.test.py b/tests/gold_tests/pluginTest/url_sig/url_sig.test.py
index 78b0228..c1a3720 100644
--- a/tests/gold_tests/pluginTest/url_sig/url_sig.test.py
+++ b/tests/gold_tests/pluginTest/url_sig/url_sig.test.py
@@ -24,6 +24,7 @@ Test url_sig plugin
 
 Test.SkipUnless(
     Condition.HasATSFeature('TS_USE_TLS_ALPN'),
+    Condition.HasProgram("netstat", "netstat needs to be installed on system for this test to work")
 )
 
 # Skip if plugins not present.

-- 
To stop receiving notification emails like this one, please contact
['"commits@trafficserver.apache.org" <co...@trafficserver.apache.org>'].