1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
| typedef struct { ngx_flag_t enable; } ngx_http_zvoice_filter_conf_t;
static ngx_command_t ngx_http_zvoice_filter_module_commands[] = { { ngx_string("zvoice"), NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOG_CONF | NGX_CONF_FLAG, ngx_conf_set_flag_slot, NGX_HTTP_LOG_CONF_OFFSET, offsetof(ngx_http_zvoice_filter_conf_t, enable), NULL }, ngx_null_command };
static ngx_http_output_header_filter_pt ngx_http_next_header_filter; static ngx_http_output_body_filter_pt ngx_http_next_body_filter;
static ngx_int_t ngx_http_prefix_filter_header_filter(ngx_http_request_t *r) { ngx_http_prefix_filter_ctx_t *ctx; ngx_http_prefix_filter_conf_t *conf;
if (r->headers_out.status != NGX_HTTP_OK) { return ngx_http_next_header_filter(r); }
ctx = ngx_http_get_module_ctx(r, ngx_http_prefix_filter_module); if (ctx) { return ngx_http_next_header_filter(r); }
conf = ngx_http_get_module_loc_conf(r, ngx_http_prefix_filter_module); if (conf == NULL) { return ngx_http_next_header_filter(r); } if (conf->enable == 0) { return ngx_http_next_header_filter(r); }
ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_prefix_filter_ctx_t)); if (ctx == NULL) { return NGX_ERROR; } ctx->add_prefix = 0;
ngx_http_set_ctx(r, ctx, ngx_http_prefix_filter_module);
if (r->headers_out.content_type.len >= sizeof("text/html") - 1 && ngx_strncasecmp(r->headers_out.content_type.data, (u_char*)"text/html", sizeof("text/html")-1) == 0) {
ctx->add_prefix = 1; if (r->headers_out.content_length_n > 0) { r->headers_out.content_length_n += filter_prefix.len; } }
return ngx_http_prefix_filter_header_filter(r); }
static ngx_int_t ngx_http_prefix_filter_body_filter(ngx_http_request_t *r, ngx_chain_t *in) { gx_http_prefix_filter_ctx_t *ctx = ngx_http_get_module_ctx(r, ngx_http_prefix_filter_module); if (ctx == NULL || ctx->add_prefix != 1) { return ngx_http_next_body_filter(r, in); } ctx->add_prefix = 2;
ngx_buf_t *b = ngx_create_temp_buf(r->pool, filter_prefix.len); b->start = b->pos = filter_prefix.data; b->last = b->pos + filter_prefix.len;
ngx_chain_t *cl = ngx_alloc_chain_link(r->pool); cl->buf = b; cl->next = in; return ngx_http_next_body_filter(r, cl); }
ngx_int_t ngx_http_zvoice_filter_postconfiguration(ngx_conf_t *cf) { ngx_http_next_header_filter = ngx_http_top_header_filter; ngx_http_top_header_filter = ngx_http_prefix_filter_header_filter;
ngx_http_next_body_filter = ngx_http_top_body_filter; ngx_http_top_body_filter = ngx_http_prefix_filter_body_filter;
return NGX_OK; }
void *ngx_http_zvoice_filter_create_loc_conf(ngx_conf_t *cf){ ngx_http_zvoice_filter_conf_t *conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_zvoice_filter_conf_t)); if(conf == NULL) return NULL; conf->enable = NGX_CONF_UNSET; return conf; } char *ngx_http_zvoice_filter_merge_loc_conf(ngx_conf_t *cf, void *prev, void *child) { ngx_http_zvoice_filter_conf_t *prev = (ngx_http_zvoice_filter_conf_t*)parent; ngx_http_zvoice_filter_conf_t *conf = (ngx_http_zvoice_filter_conf_t*)child;
ngx_conf_merge_value(conf->enable, prev->enable, 0); return NGX_CONF_OK; }
static ngx_http_module_t ngx_http_zvoice_filter_module_ctx = { NULL, ngx_http_zvoice_filter_postconfiguration NULL, NULL NULL, NULL ngx_http_zvoice_filter_create_loc_conf, ngx_http_zvoice_filter_merge_loc_conf };
ngx_module_t ngx_http_zvoice_filter_module = { NGX_MODULE_V1, &ngx_http_zvoice_filter_module_ctx, ngx_http_zvoice_filter_module_commands, NGX_HTTP_MODULE, NULL,NULL,NULL,NULL, NULL,NULL,NULL, NGX_MODULE_V1_PADDING };
|