Add the config directive compression-level
authorFabian Keil <fk@fabiankeil.de>
Fri, 8 Jul 2011 13:27:31 +0000 (13:27 +0000)
committerFabian Keil <fk@fabiankeil.de>
Fri, 8 Jul 2011 13:27:31 +0000 (13:27 +0000)
There isn't a single best compression level for
all situations, so hard-coding one makes no sense.

cgi.c
cgi.h
jcc.c
loadcfg.c
project.h

diff --git a/cgi.c b/cgi.c
index 8655366..028a798 100644 (file)
--- a/cgi.c
+++ b/cgi.c
@@ -1,4 +1,4 @@
-const char cgi_rcs[] = "$Id: cgi.c,v 1.136 2011/07/03 17:54:29 fabiankeil Exp $";
+const char cgi_rcs[] = "$Id: cgi.c,v 1.137 2011/07/03 17:55:23 fabiankeil Exp $";
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/cgi.c,v $
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/cgi.c,v $
@@ -1495,22 +1495,20 @@ static void get_locale_time(char *buf, size_t buffer_size)
  *                Allocates a new buffer for the result, free'ing it is
  *                up to the caller.
  *
  *                Allocates a new buffer for the result, free'ing it is
  *                up to the caller.
  *
- *                XXX: We should add a config option for the
- *                     compression level.
- *
- *
  * Parameters  :
  *          1  :  buffer = buffer whose content should be compressed
  *          2  :  buffer_length = length of the buffer
  * Parameters  :
  *          1  :  buffer = buffer whose content should be compressed
  *          2  :  buffer_length = length of the buffer
+ *          3  :  compression_level = compression level for compress2()
  *
  * Returns     :  NULL on error, otherwise a pointer to the compressed
  *                content of the input buffer.
  *
  *********************************************************************/
  *
  * Returns     :  NULL on error, otherwise a pointer to the compressed
  *                content of the input buffer.
  *
  *********************************************************************/
-char *compress_buffer(char *buffer, size_t *buffer_length)
+char *compress_buffer(char *buffer, size_t *buffer_length, int compression_level)
 {
    char *compressed_buffer;
    size_t new_length = *buffer_length;
 {
    char *compressed_buffer;
    size_t new_length = *buffer_length;
+   assert(-1 <= compression_level && compression_level <= 9);
 
    compressed_buffer = malloc(new_length);
    if (NULL == compressed_buffer)
 
    compressed_buffer = malloc(new_length);
    if (NULL == compressed_buffer)
@@ -1520,7 +1518,7 @@ char *compress_buffer(char *buffer, size_t *buffer_length)
    }
 
    if (Z_OK != compress2((Bytef *)compressed_buffer, &new_length,
    }
 
    if (Z_OK != compress2((Bytef *)compressed_buffer, &new_length,
-         (Bytef *)buffer, *buffer_length, Z_DEFAULT_COMPRESSION))
+         (Bytef *)buffer, *buffer_length, compression_level))
    {
       log_error(LOG_LEVEL_ERROR, "Error in compress2()");
       freez(compressed_buffer);
    {
       log_error(LOG_LEVEL_ERROR, "Error in compress2()");
       freez(compressed_buffer);
@@ -1528,7 +1526,8 @@ char *compress_buffer(char *buffer, size_t *buffer_length)
    }
 
    log_error(LOG_LEVEL_RE_FILTER,
    }
 
    log_error(LOG_LEVEL_RE_FILTER,
-      "Compressed content from %d to %d bytes.", *buffer_length, new_length);
+      "Compressed content from %d to %d bytes. Compression level: %d",
+      *buffer_length, new_length, compression_level);
 
    *buffer_length = new_length;
 
 
    *buffer_length = new_length;
 
@@ -1590,7 +1589,8 @@ struct http_response *finish_http_response(const struct client_state *csp, struc
    {
       char *compressed_content;
 
    {
       char *compressed_content;
 
-      compressed_content = compress_buffer(rsp->body, &rsp->content_length);
+      compressed_content = compress_buffer(rsp->body, &rsp->content_length,
+         csp->config->compression_level);
       if (NULL != compressed_content)
       {
          freez(rsp->body);
       if (NULL != compressed_content)
       {
          freez(rsp->body);
diff --git a/cgi.h b/cgi.h
index 91cd0f8..d9fe6a7 100644 (file)
--- a/cgi.h
+++ b/cgi.h
@@ -1,6 +1,6 @@
 #ifndef CGI_H_INCLUDED
 #define CGI_H_INCLUDED
 #ifndef CGI_H_INCLUDED
 #define CGI_H_INCLUDED
-#define CGI_H_VERSION "$Id: cgi.h,v 1.38 2011/06/23 14:01:01 fabiankeil Exp $"
+#define CGI_H_VERSION "$Id: cgi.h,v 1.39 2011/07/03 17:55:23 fabiankeil Exp $"
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/cgi.h,v $
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/cgi.h,v $
@@ -107,7 +107,7 @@ extern char   get_char_param(const struct map *parameters,
  * we bother to (re-)compress it. Completely arbitrary.
  */
 extern const size_t LOWER_LENGTH_LIMIT_FOR_COMPRESSION;
  * we bother to (re-)compress it. Completely arbitrary.
  */
 extern const size_t LOWER_LENGTH_LIMIT_FOR_COMPRESSION;
-extern char *compress_buffer(char *buffer, size_t *buffer_length);
+extern char *compress_buffer(char *buffer, size_t *buffer_length, int compression_level);
 #endif
 
 /*
 #endif
 
 /*
diff --git a/jcc.c b/jcc.c
index a5cf365..06418a8 100644 (file)
--- a/jcc.c
+++ b/jcc.c
@@ -1,4 +1,4 @@
-const char jcc_rcs[] = "$Id: jcc.c,v 1.353 2011/06/23 14:01:01 fabiankeil Exp $";
+const char jcc_rcs[] = "$Id: jcc.c,v 1.354 2011/07/03 17:54:29 fabiankeil Exp $";
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/jcc.c,v $
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/jcc.c,v $
@@ -2065,7 +2065,8 @@ static void chat(struct client_state *csp)
                   else if ((csp->flags & CSP_FLAG_CLIENT_SUPPORTS_DEFLATE)
                      && (csp->content_length > LOWER_LENGTH_LIMIT_FOR_COMPRESSION))
                   {
                   else if ((csp->flags & CSP_FLAG_CLIENT_SUPPORTS_DEFLATE)
                      && (csp->content_length > LOWER_LENGTH_LIMIT_FOR_COMPRESSION))
                   {
-                     char *compressed_content = compress_buffer(p, (size_t *)&csp->content_length);
+                     char *compressed_content = compress_buffer(p,
+                        (size_t *)&csp->content_length, csp->config->compression_level);
                      if (compressed_content != NULL)
                      {
                         freez(p);
                      if (compressed_content != NULL)
                      {
                         freez(p);
index 899e430..338e8ce 100644 (file)
--- a/loadcfg.c
+++ b/loadcfg.c
@@ -1,4 +1,4 @@
-const char loadcfg_rcs[] = "$Id: loadcfg.c,v 1.111 2010/08/14 23:28:52 ler762 Exp $";
+const char loadcfg_rcs[] = "$Id: loadcfg.c,v 1.112 2011/03/03 14:38:36 fabiankeil Exp $";
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/loadcfg.c,v $
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/loadcfg.c,v $
@@ -133,6 +133,7 @@ static struct file_list *current_configfile = NULL;
 #define hash_admin_address               4112573064ul /* "admin-address" */
 #define hash_allow_cgi_request_crunching  258915987ul /* "allow-cgi-request-crunching" */
 #define hash_buffer_limit                1881726070ul /* "buffer-limit */
 #define hash_admin_address               4112573064ul /* "admin-address" */
 #define hash_allow_cgi_request_crunching  258915987ul /* "allow-cgi-request-crunching" */
 #define hash_buffer_limit                1881726070ul /* "buffer-limit */
+#define hash_compression_level           2464423563ul /* "compression-level" */
 #define hash_confdir                        1978389ul /* "confdir" */
 #define hash_connection_sharing          1348841265ul /* "connection-sharing" */
 #define hash_debug                            78263ul /* "debug" */
 #define hash_confdir                        1978389ul /* "confdir" */
 #define hash_connection_sharing          1348841265ul /* "connection-sharing" */
 #define hash_debug                            78263ul /* "debug" */
@@ -364,6 +365,12 @@ struct configuration_spec * load_config(void)
    config->feature_flags            &= ~RUNTIME_FEATURE_SPLIT_LARGE_FORMS;
    config->feature_flags            &= ~RUNTIME_FEATURE_ACCEPT_INTERCEPTED_REQUESTS;
    config->feature_flags            &= ~RUNTIME_FEATURE_EMPTY_DOC_RETURNS_OK;
    config->feature_flags            &= ~RUNTIME_FEATURE_SPLIT_LARGE_FORMS;
    config->feature_flags            &= ~RUNTIME_FEATURE_ACCEPT_INTERCEPTED_REQUESTS;
    config->feature_flags            &= ~RUNTIME_FEATURE_EMPTY_DOC_RETURNS_OK;
+#ifdef FEATURE_COMPRESSION
+   /*
+    * XXX: Run some benchmarks to see if there are better default values.
+    */
+   config->compression_level         = 1;
+#endif
 
    configfp = fopen(configfile, "r");
    if (NULL == configfp)
 
    configfp = fopen(configfile, "r");
    if (NULL == configfp)
@@ -500,6 +507,32 @@ struct configuration_spec * load_config(void)
             config->confdir = make_path( NULL, arg);
             break;
 
             config->confdir = make_path( NULL, arg);
             break;
 
+/* *************************************************************************
+ * compression-level 0-9
+ * *************************************************************************/
+#ifdef FEATURE_COMPRESSION
+         case hash_compression_level :
+            if (*arg != '\0')
+            {
+               int compression_level = atoi(arg);
+               if (-1 <= compression_level && compression_level <= 9)
+               {
+                  config->compression_level = compression_level;;
+               }
+               else
+               {
+                  log_error(LOG_LEVEL_FATAL,
+                     "Invalid compression-level value: %s", arg);
+               }
+            }
+            else
+            {
+               log_error(LOG_LEVEL_FATAL,
+                  "Invalid compression-level directive. Compression value missing");
+            }
+            break;
+#endif
+
 /* *************************************************************************
  * connection-sharing (0|1)
  * *************************************************************************/
 /* *************************************************************************
  * connection-sharing (0|1)
  * *************************************************************************/
index cbd548a..d524be0 100644 (file)
--- a/project.h
+++ b/project.h
@@ -1,7 +1,7 @@
 #ifndef PROJECT_H_INCLUDED
 #define PROJECT_H_INCLUDED
 /** Version string. */
 #ifndef PROJECT_H_INCLUDED
 #define PROJECT_H_INCLUDED
 /** Version string. */
-#define PROJECT_H_VERSION "$Id: project.h,v 1.165 2011/06/23 14:01:01 fabiankeil Exp $"
+#define PROJECT_H_VERSION "$Id: project.h,v 1.167 2011/07/03 17:55:23 fabiankeil Exp $"
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/project.h,v $
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/project.h,v $
@@ -1305,6 +1305,10 @@ struct configuration_spec
    unsigned int default_server_timeout;
 #endif
 
    unsigned int default_server_timeout;
 #endif
 
+#ifdef FEATURE_COMPRESSION
+   int compression_level;
+#endif
+
    /** All options from the config file, HTML-formatted. */
    char *proxy_args;
 
    /** All options from the config file, HTML-formatted. */
    char *proxy_args;