diff src/http/modules/ngx_http_sub_filter_module.c @ 394:05981f639d21 NGINX_0_7_9

nginx 0.7.9 *) Change: now ngx_http_charset_module works by default with following MIME types: text/html, text/css, text/xml, text/plain, text/vnd.wap.wml, application/x-javascript, and application/rss+xml. *) Feature: the "charset_types" and "addition_types" directives. *) Feature: now the "gzip_types", "ssi_types", and "sub_filter_types" directives use hash. *) Feature: the ngx_cpp_test_module. *) Feature: the "expires" directive supports daily time. *) Feature: the ngx_http_xslt_module improvements and bug fixing. Thanks to Denis F. Latypoff and Maxim Dounin. *) Bugfix: the "log_not_found" directive did not work for index files tests. *) Bugfix: HTTPS connections might hang, if kqueue, epoll, rtsig, or eventport methods were used; the bug had appeared in 0.7.7. *) Bugfix: if the "server_name", "valid_referers", and "map" directives used an "*.domain.tld" wildcard and exact name "domain.tld" was not set, then the exact name was matched by the wildcard; the bugs had appeared in 0.3.18.
author Igor Sysoev <http://sysoev.ru>
date Tue, 12 Aug 2008 00:00:00 +0400
parents 34fb3a573548
children fd759445d8a8
line wrap: on
line diff
--- a/src/http/modules/ngx_http_sub_filter_module.c
+++ b/src/http/modules/ngx_http_sub_filter_module.c
@@ -13,12 +13,14 @@ typedef struct {
     ngx_str_t      match;
     ngx_str_t      sub;
 
-    ngx_array_t   *types;     /* array of ngx_str_t */
+    ngx_hash_t     types;
 
     ngx_array_t   *sub_lengths;
     ngx_array_t   *sub_values;
 
     ngx_flag_t     once;
+
+    ngx_array_t   *types_keys;
 } ngx_http_sub_loc_conf_t;
 
 
@@ -60,7 +62,6 @@ static ngx_int_t ngx_http_sub_parse(ngx_
 
 static char * ngx_http_sub_filter(ngx_conf_t *cf, ngx_command_t *cmd,
     void *conf);
-static char *ngx_http_sub_types(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
 static void *ngx_http_sub_create_conf(ngx_conf_t *cf);
 static char *ngx_http_sub_merge_conf(ngx_conf_t *cf,
     void *parent, void *child);
@@ -78,10 +79,10 @@ static ngx_command_t  ngx_http_sub_filte
 
     { ngx_string("sub_filter_types"),
       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
-      ngx_http_sub_types,
+      ngx_http_types_slot,
       NGX_HTTP_LOC_CONF_OFFSET,
-      0,
-      NULL },
+      offsetof(ngx_http_sub_loc_conf_t, types_keys),
+      &ngx_http_html_default_types[0] },
 
     { ngx_string("sub_filter_once"),
       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
@@ -132,34 +133,18 @@ static ngx_http_output_body_filter_pt   
 static ngx_int_t
 ngx_http_sub_header_filter(ngx_http_request_t *r)
 {
-    ngx_str_t                *type;
-    ngx_uint_t                i;
     ngx_http_sub_ctx_t        *ctx;
     ngx_http_sub_loc_conf_t  *slcf;
 
     slcf = ngx_http_get_module_loc_conf(r, ngx_http_sub_filter_module);
 
     if (slcf->match.len == 0
-        || r->headers_out.content_type.len == 0
-        || r->headers_out.content_length_n == 0)
+        || r->headers_out.content_length_n == 0
+        || ngx_http_test_content_type(r, &slcf->types) == NULL)
     {
         return ngx_http_next_header_filter(r);
     }
 
-    type = slcf->types->elts;
-    for (i = 0; i < slcf->types->nelts; i++) {
-        if (r->headers_out.content_type.len >= type[i].len
-            && ngx_strncasecmp(r->headers_out.content_type.data,
-                               type[i].data, type[i].len) == 0)
-        {
-            goto found;
-        }
-    }
-
-    return ngx_http_next_header_filter(r);
-
-found:
-
     ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_sub_ctx_t));
     if (ctx == NULL) {
         return NGX_ERROR;
@@ -669,56 +654,6 @@ ngx_http_sub_filter(ngx_conf_t *cf, ngx_
 }
 
 
-static char *
-ngx_http_sub_types(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
-{
-    ngx_http_sub_loc_conf_t *slcf = conf;
-
-    ngx_str_t   *value, *type;
-    ngx_uint_t   i;
-
-    if (slcf->types == NULL) {
-        slcf->types = ngx_array_create(cf->pool, 4, sizeof(ngx_str_t));
-        if (slcf->types == NULL) {
-            return NGX_CONF_ERROR;
-        }
-
-        type = ngx_array_push(slcf->types);
-        if (type == NULL) {
-            return NGX_CONF_ERROR;
-        }
-
-        type->len = sizeof("text/html") - 1;
-        type->data = (u_char *) "text/html";
-    }
-
-    value = cf->args->elts;
-
-    for (i = 1; i < cf->args->nelts; i++) {
-
-        if (ngx_strcmp(value[i].data, "text/html") == 0) {
-            continue;
-        }
-
-        type = ngx_array_push(slcf->types);
-        if (type == NULL) {
-            return NGX_CONF_ERROR;
-        }
-
-        type->len = value[i].len;
-
-        type->data = ngx_pnalloc(cf->pool, type->len + 1);
-        if (type->data == NULL) {
-            return NGX_CONF_ERROR;
-        }
-
-        ngx_cpystrn(type->data, value[i].data, type->len + 1);
-    }
-
-    return NGX_CONF_OK;
-}
-
-
 static void *
 ngx_http_sub_create_conf(ngx_conf_t *cf)
 {
@@ -732,13 +667,12 @@ ngx_http_sub_create_conf(ngx_conf_t *cf)
     /*
      * set by ngx_pcalloc():
      *
-     *     conf->match.len = 0;
-     *     conf->match.data = NULL;
-     *     conf->sub.len = 0;
-     *     conf->sub.data = NULL;
+     *     conf->match = { 0, NULL };
+     *     conf->sub = { 0, NULL };
      *     conf->sub_lengths = NULL;
      *     conf->sub_values = NULL;
-     *     conf->types = NULL;
+     *     conf->types = { NULL };
+     *     conf->types_keys = NULL;
      */
 
     slcf->once = NGX_CONF_UNSET;
@@ -753,8 +687,6 @@ ngx_http_sub_merge_conf(ngx_conf_t *cf, 
     ngx_http_sub_loc_conf_t *prev = parent;
     ngx_http_sub_loc_conf_t *conf = child;
 
-    ngx_str_t  *type;
-
     ngx_conf_merge_value(conf->once, prev->once, 1);
     ngx_conf_merge_str_value(conf->match, prev->match, "");
 
@@ -764,24 +696,12 @@ ngx_http_sub_merge_conf(ngx_conf_t *cf, 
         conf->sub_values = prev->sub_values;
     }
 
-    if (conf->types == NULL) {
-        if (prev->types == NULL) {
-            conf->types = ngx_array_create(cf->pool, 1, sizeof(ngx_str_t));
-            if (conf->types == NULL) {
-                return NGX_CONF_ERROR;
-            }
-
-            type = ngx_array_push(conf->types);
-            if (type == NULL) {
-                return NGX_CONF_ERROR;
-            }
-
-            type->len = sizeof("text/html") - 1;
-            type->data = (u_char *) "text/html";
-
-        } else {
-            conf->types = prev->types;
-        }
+    if (ngx_http_merge_types(cf, conf->types_keys, &conf->types,
+                             prev->types_keys, &prev->types,
+                             ngx_http_html_default_types)
+        != NGX_OK)
+    {
+        return NGX_CONF_ERROR;
     }
 
     return NGX_CONF_OK;