Permalink
Browse files

now we postpone the rewrite phase handler only once rather than on ev…

…ery main request previously. this will save some CPU cycles on every request.
  • Loading branch information...
1 parent d732166 commit fb2d8935d69875ed749f1044209289f1051b43c5 @agentzh agentzh committed Jan 24, 2011
@@ -10,17 +10,20 @@
#include <ngx_config.h>
+
ngx_flag_t ngx_http_headers_more_access_input_headers = 0;
ngx_flag_t ngx_http_headers_more_access_output_headers = 0;
/* config handlers */
-static void *ngx_http_headers_more_create_conf(ngx_conf_t *cf);
+static void * ngx_http_headers_more_create_loc_conf(ngx_conf_t *cf);
-static char *ngx_http_headers_more_merge_conf(ngx_conf_t *cf,
+static char * ngx_http_headers_more_merge_loc_conf(ngx_conf_t *cf,
void *parent, void *child);
+static void * ngx_http_headers_more_create_main_conf(ngx_conf_t *cf);
+
static ngx_int_t ngx_http_headers_more_post_config(ngx_conf_t *cf);
/* post-read-phase handler */
@@ -69,17 +72,17 @@ static ngx_command_t ngx_http_headers_more_filter_commands[] = {
};
static ngx_http_module_t ngx_http_headers_more_filter_module_ctx = {
- NULL, /* preconfiguration */
- ngx_http_headers_more_post_config, /* postconfiguration */
+ NULL, /* preconfiguration */
+ ngx_http_headers_more_post_config, /* postconfiguration */
- NULL, /* create main configuration */
- NULL, /* init main configuration */
+ ngx_http_headers_more_create_main_conf, /* create main configuration */
+ NULL, /* init main configuration */
- NULL, /* create server configuration */
- NULL, /* merge server configuration */
+ NULL, /* create server configuration */
+ NULL, /* merge server configuration */
- ngx_http_headers_more_create_conf, /* create location configuration */
- ngx_http_headers_more_merge_conf /* merge location configuration */
+ ngx_http_headers_more_create_loc_conf, /* create location configuration */
+ ngx_http_headers_more_merge_loc_conf /* merge location configuration */
};
ngx_module_t ngx_http_headers_more_filter_module = {
@@ -97,15 +100,17 @@ ngx_module_t ngx_http_headers_more_filter_module = {
NGX_MODULE_V1_PADDING
};
+
static ngx_http_output_header_filter_pt ngx_http_next_header_filter;
+
static ngx_int_t
ngx_http_headers_more_filter(ngx_http_request_t *r)
{
- ngx_int_t rc;
- ngx_uint_t i;
- ngx_http_headers_more_conf_t *conf;
- ngx_http_headers_more_cmd_t *cmd;
+ ngx_int_t rc;
+ ngx_uint_t i;
+ ngx_http_headers_more_loc_conf_t *conf;
+ ngx_http_headers_more_cmd_t *cmd;
conf = ngx_http_get_module_loc_conf(r, ngx_http_headers_more_filter_module);
@@ -127,6 +132,7 @@ ngx_http_headers_more_filter(ngx_http_request_t *r)
return ngx_http_next_header_filter(r);
}
+
static ngx_int_t
ngx_http_headers_more_filter_init(ngx_conf_t *cf)
{
@@ -136,12 +142,13 @@ ngx_http_headers_more_filter_init(ngx_conf_t *cf)
return NGX_OK;
}
+
static void *
-ngx_http_headers_more_create_conf(ngx_conf_t *cf)
+ngx_http_headers_more_create_loc_conf(ngx_conf_t *cf)
{
- ngx_http_headers_more_conf_t *conf;
+ ngx_http_headers_more_loc_conf_t *conf;
- conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_headers_more_conf_t));
+ conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_headers_more_loc_conf_t));
if (conf == NULL) {
return NULL;
}
@@ -157,26 +164,30 @@ ngx_http_headers_more_create_conf(ngx_conf_t *cf)
static char *
-ngx_http_headers_more_merge_conf(ngx_conf_t *cf, void *parent, void *child)
+ngx_http_headers_more_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
{
- ngx_uint_t i, orig_len;
- ngx_http_headers_more_cmd_t *prev_cmd, *cmd;
- ngx_http_headers_more_conf_t *prev = parent;
- ngx_http_headers_more_conf_t *conf = child;
+ ngx_uint_t i;
+ ngx_uint_t orig_len;
+ ngx_http_headers_more_cmd_t *prev_cmd, *cmd;
+ ngx_http_headers_more_loc_conf_t *prev = parent;
+ ngx_http_headers_more_loc_conf_t *conf = child;
if (conf->cmds == NULL || conf->cmds->nelts == 0) {
conf->cmds = prev->cmds;
+
} else if (prev->cmds && prev->cmds->nelts) {
orig_len = conf->cmds->nelts;
(void) ngx_array_push_n(conf->cmds, prev->cmds->nelts);
cmd = conf->cmds->elts;
+
for (i = 0; i < orig_len; i++) {
cmd[conf->cmds->nelts - 1 - i] = cmd[orig_len - 1 - i];
}
prev_cmd = prev->cmds->elts;
+
for (i = 0; i < prev->cmds->nelts; i++) {
cmd[i] = prev_cmd[i];
}
@@ -185,6 +196,7 @@ ngx_http_headers_more_merge_conf(ngx_conf_t *cf, void *parent, void *child)
return NGX_CONF_OK;
}
+
static ngx_int_t
ngx_http_headers_more_post_config(ngx_conf_t *cf)
{
@@ -215,33 +227,27 @@ ngx_http_headers_more_post_config(ngx_conf_t *cf)
return NGX_OK;
}
+
static ngx_int_t
ngx_http_headers_more_handler(ngx_http_request_t *r)
{
- ngx_int_t rc;
- ngx_uint_t i;
- ngx_http_headers_more_conf_t *conf;
- ngx_http_headers_more_cmd_t *cmd;
- ngx_http_headers_more_ctx_t *ctx;
-
- ctx = ngx_http_get_module_ctx(r, ngx_http_headers_more_filter_module);
- if (ctx == NULL) {
- ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_headers_more_filter_module));
- if (ctx == NULL) {
- return NGX_HTTP_INTERNAL_SERVER_ERROR;
- }
+ ngx_int_t rc;
+ ngx_uint_t i;
+ ngx_http_headers_more_loc_conf_t *conf;
+ ngx_http_headers_more_main_conf_t *hmcf;
+ ngx_http_headers_more_cmd_t *cmd;
- ngx_http_set_ctx(r, ctx, ngx_http_headers_more_filter_module);
- }
+ hmcf = ngx_http_get_module_main_conf(r,
+ ngx_http_headers_more_filter_module);
- if ( ! ctx->postponed_to_phase_end ) {
+ if (! hmcf->postponed_to_phase_end) {
ngx_http_core_main_conf_t *cmcf;
- ngx_http_phase_handler_t tmp;
+ ngx_http_phase_handler_t tmp;
ngx_http_phase_handler_t *ph;
ngx_http_phase_handler_t *cur_ph;
ngx_http_phase_handler_t *last_ph;
- ctx->postponed_to_phase_end = 1;
+ hmcf->postponed_to_phase_end = 1;
cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
@@ -287,3 +293,21 @@ ngx_http_headers_more_handler(ngx_http_request_t *r)
return NGX_DECLINED;
}
+
+static void *
+ngx_http_headers_more_create_main_conf(ngx_conf_t *cf)
+{
+ ngx_http_headers_more_main_conf_t *hmcf;
+
+ hmcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_headers_more_main_conf_t));
+ if (hmcf == NULL) {
+ return NULL;
+ }
+
+ /* set by ngx_pcalloc:
+ * hmcf->postponed_to_phase_end = 0
+ */
+
+ return hmcf;
+}
+
@@ -1,37 +1,50 @@
#ifndef NGX_HTTP_HEADERS_MORE_FILTER_MODULE_H
#define NGX_HTTP_HEADERS_MORE_FILTER_MODULE_H
+
#include <ngx_core.h>
#include <ngx_http.h>
+
typedef enum {
ngx_http_headers_more_opcode_set,
ngx_http_headers_more_opcode_clear
} ngx_http_headers_more_opcode_t;
+
typedef struct {
ngx_array_t *types; /* of ngx_str_t */
ngx_array_t *statuses; /* of ngx_uint_t */
ngx_array_t *headers; /* of ngx_http_header_val_t */
ngx_flag_t is_input;
} ngx_http_headers_more_cmd_t;
+
typedef struct {
ngx_array_t *cmds; /* of ngx_http_headers_more_cmd_t */
-} ngx_http_headers_more_conf_t;
+} ngx_http_headers_more_loc_conf_t;
+
+
+typedef struct {
+ unsigned postponed_to_phase_end;
+} ngx_http_headers_more_main_conf_t;
+
typedef struct ngx_http_headers_more_header_val_s
ngx_http_headers_more_header_val_t;
+
typedef ngx_int_t (*ngx_http_headers_more_set_header_pt)(ngx_http_request_t *r,
ngx_http_headers_more_header_val_t *hv, ngx_str_t *value);
+
typedef struct {
ngx_str_t name;
ngx_uint_t offset;
ngx_http_headers_more_set_header_pt handler;
} ngx_http_headers_more_set_header_t;
+
struct ngx_http_headers_more_header_val_s {
ngx_http_complex_value_t value;
ngx_uint_t hash;
@@ -42,14 +55,12 @@ struct ngx_http_headers_more_header_val_s {
ngx_flag_t wildcard;
};
-typedef struct {
- ngx_flag_t postponed_to_phase_end;
-} ngx_http_headers_more_ctx_t;
extern ngx_module_t ngx_http_headers_more_filter_module;
extern ngx_flag_t ngx_http_headers_more_access_input_headers;
extern ngx_flag_t ngx_http_headers_more_access_output_headers;
+
#endif /* NGX_HTTP_HEADERS_MORE_FILTER_MODULE_H */
@@ -418,7 +418,7 @@ static char *
ngx_http_headers_more_parse_directive(ngx_conf_t *cf, ngx_command_t *ngx_cmd,
void *conf, ngx_http_headers_more_opcode_t opcode)
{
- ngx_http_headers_more_conf_t *hcf = conf;
+ ngx_http_headers_more_loc_conf_t *hcf = conf;
ngx_uint_t i;
ngx_http_headers_more_cmd_t *cmd;
@@ -418,7 +418,7 @@ static char *
ngx_http_headers_more_parse_directive(ngx_conf_t *cf, ngx_command_t *ngx_cmd,
void *conf, ngx_http_headers_more_opcode_t opcode)
{
- ngx_http_headers_more_conf_t *hcf = conf;
+ ngx_http_headers_more_loc_conf_t *hcf = conf;
ngx_uint_t i;
ngx_http_headers_more_cmd_t *cmd;
View
@@ -1,9 +1,10 @@
-# vi:filetype=perl
+# vi:filetype=
-use lib 'lib';
use Test::Nginx::Socket; # 'no_plan';
-plan tests => 14;
+repeat_each(2);
+
+plan tests => 14 * repeat_each();
no_diff;
@@ -96,3 +97,24 @@ hello
--- response_body
hi
+
+
+=== TEST 6: range bug
+--- config
+ location /index.html {
+ more_clear_input_headers "Range*" ;
+ more_clear_input_headers "Content-Range*" ;
+
+ more_set_input_headers 'Range: bytes=1-5';
+ more_set_headers 'Content-Range: bytes 1-5/1000';
+ }
+--- request
+ GET /index.html
+--- more_headers
+Range: bytes=1-3
+--- raw_response_headers_like: Content-Range: bytes 1-5/1000$
+--- response_body chop
+html>
+--- error_code: 206
+--- SKIP
+
View
@@ -3,7 +3,9 @@
use lib 'lib';
use Test::Nginx::Socket; # 'no_plan';
-plan tests => 50;
+repeat_each(2);
+
+plan tests => 50 * repeat_each();
no_long_string();
#no_diff;
View
@@ -3,8 +3,13 @@
use lib 'lib';
use Test::Nginx::Socket;
-plan tests => 107;
+repeat_each(2);
+plan tests => repeat_each() * 107;
+
+#master_on();
+#workers(2);
+log_level("warn");
no_diff;
run_tests();
View
@@ -17,12 +17,22 @@ if [ ! -s "nginx-$version.tar.gz" ]; then
fi
#tar -xzvf nginx-$version.tar.gz || exit 1
-#cp $root/../no-pool-nginx/nginx-0.8.41-no_pool.patch ./
-#patch -p0 < nginx-0.8.41-no_pool.patch
+#cp $root/../no-pool-nginx/nginx-$version-no_pool.patch ./ || exit 1
+#patch -p0 < nginx-$version-no_pool.patch || exit 1
cd nginx-$version/
if [[ "$BUILD_CLEAN" -eq 1 || ! -f Makefile || "$root/config" -nt Makefile || "$root/util/build.sh" -nt Makefile ]]; then
./configure --prefix=/opt/nginx \
+ --without-mail_pop3_module \
+ --without-mail_imap_module \
+ --without-mail_smtp_module \
+ --without-http_upstream_ip_hash_module \
+ --without-http_empty_gif_module \
+ --without-http_memcached_module \
+ --without-http_referer_module \
+ --without-http_autoindex_module \
+ --without-http_auth_basic_module \
+ --without-http_userid_module \
--add-module=$root/../eval-nginx-module \
--add-module=$root/../echo-nginx-module \
--add-module=$root $opts \

0 comments on commit fb2d893

Please sign in to comment.