⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ngx_http_sub_filter_module.c

📁 nginx 反向代理0.7.1版本 用于实现反向代理
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * Copyright (C) Igor Sysoev */#include <ngx_config.h>#include <ngx_core.h>#include <ngx_http.h>typedef struct {    ngx_str_t      match;    ngx_str_t      sub;    ngx_array_t   *types;     /* array of ngx_str_t */    ngx_array_t   *sub_lengths;    ngx_array_t   *sub_values;    ngx_flag_t     once;} ngx_http_sub_loc_conf_t;typedef enum {    sub_start_state = 0,    sub_match_state,} ngx_http_sub_state_e;typedef struct {    ngx_str_t      match;    ngx_uint_t     once;   /* unsigned  once:1 */    ngx_buf_t     *buf;    u_char        *pos;    u_char        *copy_start;    u_char        *copy_end;    ngx_chain_t   *in;    ngx_chain_t   *out;    ngx_chain_t  **last_out;    ngx_chain_t   *busy;    ngx_chain_t   *free;    ngx_str_t      sub;    ngx_uint_t     state;    size_t         saved;    size_t         looked;} ngx_http_sub_ctx_t;static ngx_int_t ngx_http_sub_output(ngx_http_request_t *r,    ngx_http_sub_ctx_t *ctx);static ngx_int_t ngx_http_sub_parse(ngx_http_request_t *r,    ngx_http_sub_ctx_t *ctx);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);static ngx_int_t ngx_http_sub_filter_init(ngx_conf_t *cf);static ngx_command_t  ngx_http_sub_filter_commands[] = {    { ngx_string("sub_filter"),      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE2,      ngx_http_sub_filter,      NGX_HTTP_LOC_CONF_OFFSET,      0,      NULL },    { 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_LOC_CONF_OFFSET,      0,      NULL },    { ngx_string("sub_filter_once"),      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,      ngx_conf_set_flag_slot,      NGX_HTTP_LOC_CONF_OFFSET,      offsetof(ngx_http_sub_loc_conf_t, once),      NULL },      ngx_null_command};static ngx_http_module_t  ngx_http_sub_filter_module_ctx = {    NULL,                                  /* preconfiguration */    ngx_http_sub_filter_init,              /* postconfiguration */    NULL,                                  /* create main configuration */    NULL,                                  /* init main configuration */    NULL,                                  /* create server configuration */    NULL,                                  /* merge server configuration */    ngx_http_sub_create_conf,              /* create location configuration */    ngx_http_sub_merge_conf                /* merge location configuration */};ngx_module_t  ngx_http_sub_filter_module = {    NGX_MODULE_V1,    &ngx_http_sub_filter_module_ctx,       /* module context */    ngx_http_sub_filter_commands,          /* module directives */    NGX_HTTP_MODULE,                       /* module type */    NULL,                                  /* init master */    NULL,                                  /* init module */    NULL,                                  /* init process */    NULL,                                  /* init thread */    NULL,                                  /* exit thread */    NULL,                                  /* exit process */    NULL,                                  /* exit master */    NGX_MODULE_V1_PADDING};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_tngx_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)    {        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;    }    ngx_http_set_ctx(r, ctx, ngx_http_sub_filter_module);    ctx->match = slcf->match;    ctx->last_out = &ctx->out;    ctx->sub = slcf->sub;    r->filter_need_in_memory = 1;    if (r == r->main) {        ngx_http_clear_content_length(r);        ngx_http_clear_last_modified(r);    }    return ngx_http_next_header_filter(r);}static ngx_int_tngx_http_sub_body_filter(ngx_http_request_t *r, ngx_chain_t *in){    ngx_int_t                  rc;    ngx_buf_t                 *b;    ngx_chain_t               *cl;    ngx_http_sub_ctx_t        *ctx;    ngx_http_sub_loc_conf_t   *slcf;    ctx = ngx_http_get_module_ctx(r, ngx_http_sub_filter_module);    if (ctx == NULL) {        return ngx_http_next_body_filter(r, in);    }    if ((in == NULL         && ctx->buf == NULL         && ctx->in == NULL         && ctx->busy == NULL))    {        return ngx_http_next_body_filter(r, in);    }    if (ctx->once && (ctx->buf == NULL || ctx->in == NULL)) {        if (ctx->busy) {            if (ngx_http_sub_output(r, ctx) == NGX_ERROR) {                return NGX_ERROR;            }        }        return ngx_http_next_body_filter(r, in);    }    /* add the incoming chain to the chain ctx->in */    if (in) {        if (ngx_chain_add_copy(r->pool, &ctx->in, in) == NGX_ERROR) {            return NGX_ERROR;        }    }    ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,                   "http sub filter \"%V\"", &r->uri);    while (ctx->in || ctx->buf) {        if (ctx->buf == NULL ){            ctx->buf = ctx->in->buf;            ctx->in = ctx->in->next;            ctx->pos = ctx->buf->pos;        }        if (ctx->state == sub_start_state) {            ctx->copy_start = ctx->pos;            ctx->copy_end = ctx->pos;        }        b = NULL;        while (ctx->pos < ctx->buf->last) {            ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,                           "saved: %d state: %d", ctx->saved, ctx->state);            rc = ngx_http_sub_parse(r, ctx);            ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,                           "parse: %d, looked: %d %p-%p",                           rc, ctx->looked, ctx->copy_start, ctx->copy_end);            if (rc == NGX_ERROR) {                return rc;            }            if (ctx->copy_start != ctx->copy_end) {                ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,                               "saved: %d", ctx->saved);                if (ctx->saved) {                    if (ctx->free) {                        cl = ctx->free;                        ctx->free = ctx->free->next;                        b = cl->buf;                        ngx_memzero(b, sizeof(ngx_buf_t));                    } else {                        b = ngx_calloc_buf(r->pool);                        if (b == NULL) {                            return NGX_ERROR;                        }                        cl = ngx_alloc_chain_link(r->pool);                        if (cl == NULL) {                            return NGX_ERROR;                        }                        cl->buf = b;                    }                    b->memory = 1;                    b->pos = ctx->match.data;                    b->last = ctx->match.data + ctx->saved;                    *ctx->last_out = cl;                    ctx->last_out = &cl->next;                    ctx->saved = 0;                }                if (ctx->free) {                    cl = ctx->free;                    ctx->free = ctx->free->next;                    b = cl->buf;                } else {                    b = ngx_alloc_buf(r->pool);                    if (b == NULL) {                        return NGX_ERROR;                    }                    cl = ngx_alloc_chain_link(r->pool);                    if (cl == NULL) {                        return NGX_ERROR;                    }                    cl->buf = b;                }                ngx_memcpy(b, ctx->buf, sizeof(ngx_buf_t));                b->pos = ctx->copy_start;                b->last = ctx->copy_end;                b->shadow = NULL;                b->last_buf = 0;                b->recycled = 0;                if (b->in_file) {                    b->file_last = b->file_pos + (b->last - b->start);                    b->file_pos += b->pos - b->start;                }                cl->next = NULL;                *ctx->last_out = cl;                ctx->last_out = &cl->next;            }            if (ctx->state == sub_start_state) {                ctx->copy_start = ctx->pos;                ctx->copy_end = ctx->pos;            } else {                ctx->copy_start = NULL;                ctx->copy_end = NULL;            }            if (rc == NGX_AGAIN) {                continue;            }            /* rc == NGX_OK */            b = ngx_calloc_buf(r->pool);            if (b == NULL) {                return NGX_ERROR;            }            cl = ngx_alloc_chain_link(r->pool);            if (cl == NULL) {                return NGX_ERROR;            }            slcf = ngx_http_get_module_loc_conf(r, ngx_http_sub_filter_module);            if (ctx->sub.data == NULL) {                if (ngx_http_script_run(r, &ctx->sub, slcf->sub_lengths->elts,                                        0, slcf->sub_values->elts)                    == NULL)                {                    return NGX_ERROR;                }            }            if (ctx->sub.len) {                b->memory = 1;                b->pos = ctx->sub.data;                b->last = ctx->sub.data + ctx->sub.len;            } else {                b->sync = 1;            }            cl->buf = b;            cl->next = NULL;            *ctx->last_out = cl;            ctx->last_out = &cl->next;            ctx->once = slcf->once;            continue;        }        if (ctx->buf->last_buf || ngx_buf_in_memory(ctx->buf)) {            if (b == NULL) {                if (ctx->free) {                    cl = ctx->free;                    ctx->free = ctx->free->next;                    b = cl->buf;                    ngx_memzero(b, sizeof(ngx_buf_t));                } else {                    b = ngx_calloc_buf(r->pool);                    if (b == NULL) {                        return NGX_ERROR;                    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -