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

📄 ngx_http_log_module.c

📁 nginx 反向代理0.7.1版本 用于实现反向代理
💻 C
📖 第 1 页 / 共 2 页
字号:
static uintptr_tngx_http_log_escape(u_char *dst, u_char *src, size_t size){    ngx_uint_t      i, n;    static u_char   hex[] = "0123456789ABCDEF";    static uint32_t   escape[] = {        0xffffffff, /* 1111 1111 1111 1111  1111 1111 1111 1111 */                    /* ?>=< ;:98 7654 3210  /.-, +*)( '&%$ #"!  */        0x00000004, /* 0000 0000 0000 0000  0000 0000 0000 0100 */                    /* _^]\ [ZYX WVUT SRQP  ONML KJIH GFED CBA@ */        0x10000000, /* 0001 0000 0000 0000  0000 0000 0000 0000 */                    /*  ~}| {zyx wvut srqp  onml kjih gfed cba` */        0x00000000, /* 0000 0000 0000 0000  0000 0000 0000 0000 */        0x00000000, /* 0000 0000 0000 0000  0000 0000 0000 0000 */        0x00000000, /* 0000 0000 0000 0000  0000 0000 0000 0000 */        0x00000000, /* 0000 0000 0000 0000  0000 0000 0000 0000 */        0x00000000, /* 0000 0000 0000 0000  0000 0000 0000 0000 */    };    if (dst == NULL) {        /* find the number of the characters to be escaped */        n  = 0;        for (i = 0; i < size; i++) {            if (escape[*src >> 5] & (1 << (*src & 0x1f))) {                n++;            }            src++;        }        return (uintptr_t) n;    }    for (i = 0; i < size; i++) {        if (escape[*src >> 5] & (1 << (*src & 0x1f))) {            *dst++ = '\\';            *dst++ = 'x';            *dst++ = hex[*src >> 4];            *dst++ = hex[*src & 0xf];            src++;        } else {            *dst++ = *src++;        }    }    return (uintptr_t) dst;}static void *ngx_http_log_create_main_conf(ngx_conf_t *cf){    ngx_http_log_main_conf_t  *conf;    ngx_http_log_fmt_t  *fmt;    conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_log_main_conf_t));    if (conf == NULL) {        return NGX_CONF_ERROR;    }    if (ngx_array_init(&conf->formats, cf->pool, 4, sizeof(ngx_http_log_fmt_t))        != NGX_OK)    {        return NGX_CONF_ERROR;    }    fmt = ngx_array_push(&conf->formats);    if (fmt == NULL) {        return NGX_CONF_ERROR;    }    fmt->name.len = sizeof("combined") - 1;    fmt->name.data = (u_char *) "combined";    fmt->ops = ngx_array_create(cf->pool, 16, sizeof(ngx_http_log_op_t));    if (fmt->ops == NULL) {        return NGX_CONF_ERROR;    }    return conf;}static void *ngx_http_log_create_loc_conf(ngx_conf_t *cf){    ngx_http_log_loc_conf_t  *conf;    conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_log_loc_conf_t));    if (conf == NULL) {        return NGX_CONF_ERROR;    }    return conf;}static char *ngx_http_log_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child){    ngx_http_log_loc_conf_t *prev = parent;    ngx_http_log_loc_conf_t *conf = child;    ngx_http_log_t            *log;    ngx_http_log_fmt_t        *fmt;    ngx_http_log_main_conf_t  *lmcf;    if (conf->logs || conf->off) {        return NGX_CONF_OK;    }    *conf = *prev;    if (conf->logs || conf->off) {        return NGX_CONF_OK;    }    conf->logs = ngx_array_create(cf->pool, 2, sizeof(ngx_http_log_t));    if (conf->logs == NULL) {        return NGX_CONF_ERROR;    }    log = ngx_array_push(conf->logs);    if (log == NULL) {        return NGX_CONF_ERROR;    }    log->file = ngx_conf_open_file(cf->cycle, &ngx_http_access_log);    if (log->file == NULL) {        return NGX_CONF_ERROR;    }    log->disk_full_time = 0;    log->error_log_time = 0;    lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_log_module);    fmt = lmcf->formats.elts;    /* the default "combined" format */    log->ops = fmt[0].ops;    lmcf->combined_used = 1;    return NGX_CONF_OK;}static char *ngx_http_log_set_log(ngx_conf_t *cf, ngx_command_t *cmd, void *conf){    ngx_http_log_loc_conf_t *llcf = conf;    ssize_t                    buf;    ngx_uint_t                 i;    ngx_str_t                 *value, name;    ngx_http_log_t            *log;    ngx_http_log_fmt_t        *fmt;    ngx_http_log_main_conf_t  *lmcf;    value = cf->args->elts;    if (ngx_strcmp(value[1].data, "off") == 0) {        llcf->off = 1;        return NGX_CONF_OK;    }    if (llcf->logs == NULL) {        llcf->logs = ngx_array_create(cf->pool, 2, sizeof(ngx_http_log_t));        if (llcf->logs == NULL) {            return NGX_CONF_ERROR;        }    }    lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_log_module);    log = ngx_array_push(llcf->logs);    if (log == NULL) {        return NGX_CONF_ERROR;    }    log->file = ngx_conf_open_file(cf->cycle, &value[1]);    if (log->file == NULL) {        return NGX_CONF_ERROR;    }    log->disk_full_time = 0;    log->error_log_time = 0;    if (cf->args->nelts >= 3) {        name = value[2];        if (ngx_strcmp(name.data, "combined") == 0) {            lmcf->combined_used = 1;        }    } else {        name.len = sizeof("combined") - 1;        name.data = (u_char *) "combined";        lmcf->combined_used = 1;    }    fmt = lmcf->formats.elts;    for (i = 0; i < lmcf->formats.nelts; i++) {        if (fmt[i].name.len == name.len            && ngx_strcasecmp(fmt[i].name.data, name.data) == 0)        {            log->ops = fmt[i].ops;            goto buffer;        }    }    ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,                       "unknown log format \"%V\"", &name);    return NGX_CONF_ERROR;buffer:    if (cf->args->nelts == 4) {        if (ngx_strncmp(value[3].data, "buffer=", 7) != 0) {            ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,                               "invalid parameter \"%V\"", &value[3]);            return NGX_CONF_ERROR;        }        name.len = value[3].len - 7;        name.data = value[3].data + 7;        buf = ngx_parse_size(&name);        if (buf == NGX_ERROR) {            ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,                               "invalid parameter \"%V\"", &value[3]);            return NGX_CONF_ERROR;        }        if (log->file->buffer && log->file->last - log->file->pos != buf) {            ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,                               "access_log \"%V\" already defined "                               "with different buffer size", &value[1]);            return NGX_CONF_ERROR;        }        log->file->buffer = ngx_palloc(cf->pool, buf);        if (log->file->buffer == NULL) {            return NGX_CONF_ERROR;        }        log->file->pos = log->file->buffer;        log->file->last = log->file->buffer + buf;    }    return NGX_CONF_OK;}static char *ngx_http_log_set_format(ngx_conf_t *cf, ngx_command_t *cmd, void *conf){    ngx_http_log_main_conf_t *lmcf = conf;    ngx_str_t           *value;    ngx_uint_t           i;    ngx_http_log_fmt_t  *fmt;    value = cf->args->elts;    fmt = lmcf->formats.elts;    for (i = 0; i < lmcf->formats.nelts; i++) {        if (fmt[i].name.len == value[1].len            && ngx_strcmp(fmt[i].name.data, value[1].data) == 0)        {            return "duplicate \"log_format\" name";        }    }    fmt = ngx_array_push(&lmcf->formats);    if (fmt == NULL) {        return NGX_CONF_ERROR;    }    fmt->name = value[1];    fmt->ops = ngx_array_create(cf->pool, 16, sizeof(ngx_http_log_op_t));    if (fmt->ops == NULL) {        return NGX_CONF_ERROR;    }    return ngx_http_log_compile_format(cf, fmt->ops, cf->args, 2);}static char *ngx_http_log_compile_format(ngx_conf_t *cf, ngx_array_t *ops,    ngx_array_t *args, ngx_uint_t s){    u_char              *data, *p, ch;    size_t               i, len;    ngx_str_t           *value, var;    ngx_uint_t           bracket;    ngx_http_log_op_t   *op;    ngx_http_log_var_t  *v;    value = args->elts;    for ( /* void */ ; s < args->nelts; s++) {        for (i = 0; i < value[s].len; i++) {            if (value[s].data[i] != '%') {                continue;            }            ch = value[s].data[i + 1];            if ((ch >= 'A' && ch <= 'Z')                 || (ch >= 'a' && ch <= 'z')                 || ch == '{')            {                ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,                     "the parameters in the \"%%name\" form are not supported, "                     "use the \"$variable\" instead");                return NGX_CONF_ERROR;            }        }        i = 0;        while (i < value[s].len) {            op = ngx_array_push(ops);            if (op == NULL) {                return NGX_CONF_ERROR;            }            data = &value[s].data[i];            if (value[s].data[i] == '$') {                if (++i == value[s].len) {                    goto invalid;                }                if (value[s].data[i] == '{') {                    bracket = 1;                    if (++i == value[s].len) {                        goto invalid;                    }                    var.data = &value[s].data[i];                } else {                    bracket = 0;                    var.data = &value[s].data[i];                }                for (var.len = 0; i < value[s].len; i++, var.len++) {                    ch = value[s].data[i];                    if (ch == '}' && bracket) {                        i++;                        bracket = 0;                        break;                    }                    if ((ch >= 'A' && ch <= 'Z')                        || (ch >= 'a' && ch <= 'z')                        || (ch >= '0' && ch <= '9')                        || ch == '_')                    {                        continue;                    }                    break;                }                if (bracket) {                    ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,                                       "the closing bracket in \"%V\" "                                       "variable is missing", &var);                    return NGX_CONF_ERROR;                }                if (var.len == 0) {                    goto invalid;                }                if (ngx_strncmp(var.data, "apache_bytes_sent", 17) == 0) {                    ngx_conf_log_error(NGX_LOG_WARN, cf, 0,                        "use \"$body_bytes_sent\" instead of "                        "\"$apache_bytes_sent\"");                }                for (v = ngx_http_log_vars; v->name.len; v++) {                    if (v->name.len == var.len                        && ngx_strncmp(v->name.data, var.data, var.len) == 0)                    {                        op->len = v->len;                        op->getlen = NULL;                        op->run = v->run;                        op->data = 0;                        goto found;                    }                }                if (ngx_http_log_variable_compile(cf, op, &var) != NGX_OK) {                    return NGX_CONF_ERROR;                }            found:                continue;            }            i++;            while (i < value[s].len && value[s].data[i] != '$') {                i++;            }            len = &value[s].data[i] - data;            if (len) {                op->len = len;                op->getlen = NULL;                if (len <= sizeof(uintptr_t)) {                    op->run = ngx_http_log_copy_short;                    op->data = 0;                    while (len--) {                        op->data <<= 8;                        op->data |= data[len];                    }                } else {                    op->run = ngx_http_log_copy_long;                    p = ngx_palloc(cf->pool, len);                    if (p == NULL) {                        return NGX_CONF_ERROR;                    }                    ngx_memcpy(p, data, len);                    op->data = (uintptr_t) p;                }            }        }    }    return NGX_CONF_OK;invalid:    ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid parameter \"%s\"", data);    return NGX_CONF_ERROR;}static ngx_int_tngx_http_log_init(ngx_conf_t *cf){    ngx_str_t                  *value;    ngx_array_t                 a;    ngx_http_handler_pt        *h;    ngx_http_log_fmt_t         *fmt;    ngx_http_log_main_conf_t   *lmcf;    ngx_http_core_main_conf_t  *cmcf;    lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_log_module);    if (lmcf->combined_used) {        if (ngx_array_init(&a, cf->pool, 1, sizeof(ngx_str_t)) != NGX_OK) {            return NGX_ERROR;        }        value = ngx_array_push(&a);        if (value == NULL) {            return NGX_ERROR;        }        *value = ngx_http_combined_fmt;        fmt = lmcf->formats.elts;        if (ngx_http_log_compile_format(cf, fmt->ops, &a, 0)            != NGX_CONF_OK)        {            return NGX_ERROR;        }    }    cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);    h = ngx_array_push(&cmcf->phases[NGX_HTTP_LOG_PHASE].handlers);    if (h == NULL) {        return NGX_ERROR;    }    *h = ngx_http_log_handler;    return NGX_OK;}

⌨️ 快捷键说明

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