Back to home page

Nginx displayed by LXR

Source navigation ]
Diff markup ]
Identifier search ]
general search ]
 
 
Version: nginx-1.13.12 ]​[ nginx-1.12.2 ]​

0001 
0002 /*
0003  * Copyright (C) Igor Sysoev
0004  * Copyright (C) Nginx, Inc.
0005  */
0006 
0007 
0008 #include <ngx_config.h>
0009 #include <ngx_core.h>
0010 #include <ngx_http.h>
0011 
0012 #if (NGX_ZLIB)
0013 #include <zlib.h>
0014 #endif
0015 
0016 
0017 typedef struct ngx_http_log_op_s  ngx_http_log_op_t;
0018 
0019 typedef u_char *(*ngx_http_log_op_run_pt) (ngx_http_request_t *r, u_char *buf,
0020     ngx_http_log_op_t *op);
0021 
0022 typedef size_t (*ngx_http_log_op_getlen_pt) (ngx_http_request_t *r,
0023     uintptr_t data);
0024 
0025 
0026 struct ngx_http_log_op_s {
0027     size_t                      len;
0028     ngx_http_log_op_getlen_pt   getlen;
0029     ngx_http_log_op_run_pt      run;
0030     uintptr_t                   data;
0031 };
0032 
0033 
0034 typedef struct {
0035     ngx_str_t                   name;
0036     ngx_array_t                *flushes;
0037     ngx_array_t                *ops;        /* array of ngx_http_log_op_t */
0038 } ngx_http_log_fmt_t;
0039 
0040 
0041 typedef struct {
0042     ngx_array_t                 formats;    /* array of ngx_http_log_fmt_t */
0043     ngx_uint_t                  combined_used; /* unsigned  combined_used:1 */
0044 } ngx_http_log_main_conf_t;
0045 
0046 
0047 typedef struct {
0048     u_char                     *start;
0049     u_char                     *pos;
0050     u_char                     *last;
0051 
0052     ngx_event_t                *event;
0053     ngx_msec_t                  flush;
0054     ngx_int_t                   gzip;
0055 } ngx_http_log_buf_t;
0056 
0057 
0058 typedef struct {
0059     ngx_array_t                *lengths;
0060     ngx_array_t                *values;
0061 } ngx_http_log_script_t;
0062 
0063 
0064 typedef struct {
0065     ngx_open_file_t            *file;
0066     ngx_http_log_script_t      *script;
0067     time_t                      disk_full_time;
0068     time_t                      error_log_time;
0069     ngx_syslog_peer_t          *syslog_peer;
0070     ngx_http_log_fmt_t         *format;
0071     ngx_http_complex_value_t   *filter;
0072 } ngx_http_log_t;
0073 
0074 
0075 typedef struct {
0076     ngx_array_t                *logs;       /* array of ngx_http_log_t */
0077 
0078     ngx_open_file_cache_t      *open_file_cache;
0079     time_t                      open_file_cache_valid;
0080     ngx_uint_t                  open_file_cache_min_uses;
0081 
0082     ngx_uint_t                  off;        /* unsigned  off:1 */
0083 } ngx_http_log_loc_conf_t;
0084 
0085 
0086 typedef struct {
0087     ngx_str_t                   name;
0088     size_t                      len;
0089     ngx_http_log_op_run_pt      run;
0090 } ngx_http_log_var_t;
0091 
0092 
0093 #define NGX_HTTP_LOG_ESCAPE_DEFAULT  0
0094 #define NGX_HTTP_LOG_ESCAPE_JSON     1
0095 #define NGX_HTTP_LOG_ESCAPE_NONE     2
0096 
0097 
0098 static void ngx_http_log_write(ngx_http_request_t *r, ngx_http_log_t *log,
0099     u_char *buf, size_t len);
0100 static ssize_t ngx_http_log_script_write(ngx_http_request_t *r,
0101     ngx_http_log_script_t *script, u_char **name, u_char *buf, size_t len);
0102 
0103 #if (NGX_ZLIB)
0104 static ssize_t ngx_http_log_gzip(ngx_fd_t fd, u_char *buf, size_t len,
0105     ngx_int_t level, ngx_log_t *log);
0106 
0107 static void *ngx_http_log_gzip_alloc(void *opaque, u_int items, u_int size);
0108 static void ngx_http_log_gzip_free(void *opaque, void *address);
0109 #endif
0110 
0111 static void ngx_http_log_flush(ngx_open_file_t *file, ngx_log_t *log);
0112 static void ngx_http_log_flush_handler(ngx_event_t *ev);
0113 
0114 static u_char *ngx_http_log_pipe(ngx_http_request_t *r, u_char *buf,
0115     ngx_http_log_op_t *op);
0116 static u_char *ngx_http_log_time(ngx_http_request_t *r, u_char *buf,
0117     ngx_http_log_op_t *op);
0118 static u_char *ngx_http_log_iso8601(ngx_http_request_t *r, u_char *buf,
0119     ngx_http_log_op_t *op);
0120 static u_char *ngx_http_log_msec(ngx_http_request_t *r, u_char *buf,
0121     ngx_http_log_op_t *op);
0122 static u_char *ngx_http_log_request_time(ngx_http_request_t *r, u_char *buf,
0123     ngx_http_log_op_t *op);
0124 static u_char *ngx_http_log_status(ngx_http_request_t *r, u_char *buf,
0125     ngx_http_log_op_t *op);
0126 static u_char *ngx_http_log_bytes_sent(ngx_http_request_t *r, u_char *buf,
0127     ngx_http_log_op_t *op);
0128 static u_char *ngx_http_log_body_bytes_sent(ngx_http_request_t *r,
0129     u_char *buf, ngx_http_log_op_t *op);
0130 static u_char *ngx_http_log_request_length(ngx_http_request_t *r, u_char *buf,
0131     ngx_http_log_op_t *op);
0132 
0133 static ngx_int_t ngx_http_log_variable_compile(ngx_conf_t *cf,
0134     ngx_http_log_op_t *op, ngx_str_t *value, ngx_uint_t escape);
0135 static size_t ngx_http_log_variable_getlen(ngx_http_request_t *r,
0136     uintptr_t data);
0137 static u_char *ngx_http_log_variable(ngx_http_request_t *r, u_char *buf,
0138     ngx_http_log_op_t *op);
0139 static uintptr_t ngx_http_log_escape(u_char *dst, u_char *src, size_t size);
0140 static size_t ngx_http_log_json_variable_getlen(ngx_http_request_t *r,
0141     uintptr_t data);
0142 static u_char *ngx_http_log_json_variable(ngx_http_request_t *r, u_char *buf,
0143     ngx_http_log_op_t *op);
0144 static size_t ngx_http_log_unescaped_variable_getlen(ngx_http_request_t *r,
0145     uintptr_t data);
0146 static u_char *ngx_http_log_unescaped_variable(ngx_http_request_t *r,
0147     u_char *buf, ngx_http_log_op_t *op);
0148 
0149 
0150 static void *ngx_http_log_create_main_conf(ngx_conf_t *cf);
0151 static void *ngx_http_log_create_loc_conf(ngx_conf_t *cf);
0152 static char *ngx_http_log_merge_loc_conf(ngx_conf_t *cf, void *parent,
0153     void *child);
0154 static char *ngx_http_log_set_log(ngx_conf_t *cf, ngx_command_t *cmd,
0155     void *conf);
0156 static char *ngx_http_log_set_format(ngx_conf_t *cf, ngx_command_t *cmd,
0157     void *conf);
0158 static char *ngx_http_log_compile_format(ngx_conf_t *cf,
0159     ngx_array_t *flushes, ngx_array_t *ops, ngx_array_t *args, ngx_uint_t s);
0160 static char *ngx_http_log_open_file_cache(ngx_conf_t *cf, ngx_command_t *cmd,
0161     void *conf);
0162 static ngx_int_t ngx_http_log_init(ngx_conf_t *cf);
0163 
0164 
0165 static ngx_command_t  ngx_http_log_commands[] = {
0166 
0167     { ngx_string("log_format"),
0168       NGX_HTTP_MAIN_CONF|NGX_CONF_2MORE,
0169       ngx_http_log_set_format,
0170       NGX_HTTP_MAIN_CONF_OFFSET,
0171       0,
0172       NULL },
0173 
0174     { ngx_string("access_log"),
0175       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
0176                         |NGX_HTTP_LMT_CONF|NGX_CONF_1MORE,
0177       ngx_http_log_set_log,
0178       NGX_HTTP_LOC_CONF_OFFSET,
0179       0,
0180       NULL },
0181 
0182     { ngx_string("open_log_file_cache"),
0183       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1234,
0184       ngx_http_log_open_file_cache,
0185       NGX_HTTP_LOC_CONF_OFFSET,
0186       0,
0187       NULL },
0188 
0189       ngx_null_command
0190 };
0191 
0192 
0193 static ngx_http_module_t  ngx_http_log_module_ctx = {
0194     NULL,                                  /* preconfiguration */
0195     ngx_http_log_init,                     /* postconfiguration */
0196 
0197     ngx_http_log_create_main_conf,         /* create main configuration */
0198     NULL,                                  /* init main configuration */
0199 
0200     NULL,                                  /* create server configuration */
0201     NULL,                                  /* merge server configuration */
0202 
0203     ngx_http_log_create_loc_conf,          /* create location configuration */
0204     ngx_http_log_merge_loc_conf            /* merge location configuration */
0205 };
0206 
0207 
0208 ngx_module_t  ngx_http_log_module = {
0209     NGX_MODULE_V1,
0210     &ngx_http_log_module_ctx,              /* module context */
0211     ngx_http_log_commands,                 /* module directives */
0212     NGX_HTTP_MODULE,                       /* module type */
0213     NULL,                                  /* init master */
0214     NULL,                                  /* init module */
0215     NULL,                                  /* init process */
0216     NULL,                                  /* init thread */
0217     NULL,                                  /* exit thread */
0218     NULL,                                  /* exit process */
0219     NULL,                                  /* exit master */
0220     NGX_MODULE_V1_PADDING
0221 };
0222 
0223 
0224 static ngx_str_t  ngx_http_access_log = ngx_string(NGX_HTTP_LOG_PATH);
0225 
0226 
0227 static ngx_str_t  ngx_http_combined_fmt =
0228     ngx_string("$remote_addr - $remote_user [$time_local] "
0229                "\"$request\" $status $body_bytes_sent "
0230                "\"$http_referer\" \"$http_user_agent\"");
0231 
0232 
0233 static ngx_http_log_var_t  ngx_http_log_vars[] = {
0234     { ngx_string("pipe"), 1, ngx_http_log_pipe },
0235     { ngx_string("time_local"), sizeof("28/Sep/1970:12:00:00 +0600") - 1,
0236                           ngx_http_log_time },
0237     { ngx_string("time_iso8601"), sizeof("1970-09-28T12:00:00+06:00") - 1,
0238                           ngx_http_log_iso8601 },
0239     { ngx_string("msec"), NGX_TIME_T_LEN + 4, ngx_http_log_msec },
0240     { ngx_string("request_time"), NGX_TIME_T_LEN + 4,
0241                           ngx_http_log_request_time },
0242     { ngx_string("status"), NGX_INT_T_LEN, ngx_http_log_status },
0243     { ngx_string("bytes_sent"), NGX_OFF_T_LEN, ngx_http_log_bytes_sent },
0244     { ngx_string("body_bytes_sent"), NGX_OFF_T_LEN,
0245                           ngx_http_log_body_bytes_sent },
0246     { ngx_string("request_length"), NGX_SIZE_T_LEN,
0247                           ngx_http_log_request_length },
0248 
0249     { ngx_null_string, 0, NULL }
0250 };
0251 
0252 
0253 static ngx_int_t
0254 ngx_http_log_handler(ngx_http_request_t *r)
0255 {
0256     u_char                   *line, *p;
0257     size_t                    len, size;
0258     ssize_t                   n;
0259     ngx_str_t                 val;
0260     ngx_uint_t                i, l;
0261     ngx_http_log_t           *log;
0262     ngx_http_log_op_t        *op;
0263     ngx_http_log_buf_t       *buffer;
0264     ngx_http_log_loc_conf_t  *lcf;
0265 
0266     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
0267                    "http log handler");
0268 
0269     lcf = ngx_http_get_module_loc_conf(r, ngx_http_log_module);
0270 
0271     if (lcf->off) {
0272         return NGX_OK;
0273     }
0274 
0275     log = lcf->logs->elts;
0276     for (l = 0; l < lcf->logs->nelts; l++) {
0277 
0278         if (log[l].filter) {
0279             if (ngx_http_complex_value(r, log[l].filter, &val) != NGX_OK) {
0280                 return NGX_ERROR;
0281             }
0282 
0283             if (val.len == 0 || (val.len == 1 && val.data[0] == '0')) {
0284                 continue;
0285             }
0286         }
0287 
0288         if (ngx_time() == log[l].disk_full_time) {
0289 
0290             /*
0291              * on FreeBSD writing to a full filesystem with enabled softupdates
0292              * may block process for much longer time than writing to non-full
0293              * filesystem, so we skip writing to a log for one second
0294              */
0295 
0296             continue;
0297         }
0298 
0299         ngx_http_script_flush_no_cacheable_variables(r, log[l].format->flushes);
0300 
0301         len = 0;
0302         op = log[l].format->ops->elts;
0303         for (i = 0; i < log[l].format->ops->nelts; i++) {
0304             if (op[i].len == 0) {
0305                 len += op[i].getlen(r, op[i].data);
0306 
0307             } else {
0308                 len += op[i].len;
0309             }
0310         }
0311 
0312         if (log[l].syslog_peer) {
0313 
0314             /* length of syslog's PRI and HEADER message parts */
0315             len += sizeof("<255>Jan 01 00:00:00 ") - 1
0316                    + ngx_cycle->hostname.len + 1
0317                    + log[l].syslog_peer->tag.len + 2;
0318 
0319             goto alloc_line;
0320         }
0321 
0322         len += NGX_LINEFEED_SIZE;
0323 
0324         buffer = log[l].file ? log[l].file->data : NULL;
0325 
0326         if (buffer) {
0327 
0328             if (len > (size_t) (buffer->last - buffer->pos)) {
0329 
0330                 ngx_http_log_write(r, &log[l], buffer->start,
0331                                    buffer->pos - buffer->start);
0332 
0333                 buffer->pos = buffer->start;
0334             }
0335 
0336             if (len <= (size_t) (buffer->last - buffer->pos)) {
0337 
0338                 p = buffer->pos;
0339 
0340                 if (buffer->event && p == buffer->start) {
0341                     ngx_add_timer(buffer->event, buffer->flush);
0342                 }
0343 
0344                 for (i = 0; i < log[l].format->ops->nelts; i++) {
0345                     p = op[i].run(r, p, &op[i]);
0346                 }
0347 
0348                 ngx_linefeed(p);
0349 
0350                 buffer->pos = p;
0351 
0352                 continue;
0353             }
0354 
0355             if (buffer->event && buffer->event->timer_set) {
0356                 ngx_del_timer(buffer->event);
0357             }
0358         }
0359 
0360     alloc_line:
0361 
0362         line = ngx_pnalloc(r->pool, len);
0363         if (line == NULL) {
0364             return NGX_ERROR;
0365         }
0366 
0367         p = line;
0368 
0369         if (log[l].syslog_peer) {
0370             p = ngx_syslog_add_header(log[l].syslog_peer, line);
0371         }
0372 
0373         for (i = 0; i < log[l].format->ops->nelts; i++) {
0374             p = op[i].run(r, p, &op[i]);
0375         }
0376 
0377         if (log[l].syslog_peer) {
0378 
0379             size = p - line;
0380 
0381             n = ngx_syslog_send(log[l].syslog_peer, line, size);
0382 
0383             if (n < 0) {
0384                 ngx_log_error(NGX_LOG_WARN, r->connection->log, 0,
0385                               "send() to syslog failed");
0386 
0387             } else if ((size_t) n != size) {
0388                 ngx_log_error(NGX_LOG_WARN, r->connection->log, 0,
0389                               "send() to syslog has written only %z of %uz",
0390                               n, size);
0391             }
0392 
0393             continue;
0394         }
0395 
0396         ngx_linefeed(p);
0397 
0398         ngx_http_log_write(r, &log[l], line, p - line);
0399     }
0400 
0401     return NGX_OK;
0402 }
0403 
0404 
0405 static void
0406 ngx_http_log_write(ngx_http_request_t *r, ngx_http_log_t *log, u_char *buf,
0407     size_t len)
0408 {
0409     u_char              *name;
0410     time_t               now;
0411     ssize_t              n;
0412     ngx_err_t            err;
0413 #if (NGX_ZLIB)
0414     ngx_http_log_buf_t  *buffer;
0415 #endif
0416 
0417     if (log->script == NULL) {
0418         name = log->file->name.data;
0419 
0420 #if (NGX_ZLIB)
0421         buffer = log->file->data;
0422 
0423         if (buffer && buffer->gzip) {
0424             n = ngx_http_log_gzip(log->file->fd, buf, len, buffer->gzip,
0425                                   r->connection->log);
0426         } else {
0427             n = ngx_write_fd(log->file->fd, buf, len);
0428         }
0429 #else
0430         n = ngx_write_fd(log->file->fd, buf, len);
0431 #endif
0432 
0433     } else {
0434         name = NULL;
0435         n = ngx_http_log_script_write(r, log->script, &name, buf, len);
0436     }
0437 
0438     if (n == (ssize_t) len) {
0439         return;
0440     }
0441 
0442     now = ngx_time();
0443 
0444     if (n == -1) {
0445         err = ngx_errno;
0446 
0447         if (err == NGX_ENOSPC) {
0448             log->disk_full_time = now;
0449         }
0450 
0451         if (now - log->error_log_time > 59) {
0452             ngx_log_error(NGX_LOG_ALERT, r->connection->log, err,
0453                           ngx_write_fd_n " to \"%s\" failed", name);
0454 
0455             log->error_log_time = now;
0456         }
0457 
0458         return;
0459     }
0460 
0461     if (now - log->error_log_time > 59) {
0462         ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
0463                       ngx_write_fd_n " to \"%s\" was incomplete: %z of %uz",
0464                       name, n, len);
0465 
0466         log->error_log_time = now;
0467     }
0468 }
0469 
0470 
0471 static ssize_t
0472 ngx_http_log_script_write(ngx_http_request_t *r, ngx_http_log_script_t *script,
0473     u_char **name, u_char *buf, size_t len)
0474 {
0475     size_t                     root;
0476     ssize_t                    n;
0477     ngx_str_t                  log, path;
0478     ngx_open_file_info_t       of;
0479     ngx_http_log_loc_conf_t   *llcf;
0480     ngx_http_core_loc_conf_t  *clcf;
0481 
0482     clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
0483 
0484     if (!r->root_tested) {
0485 
0486         /* test root directory existence */
0487 
0488         if (ngx_http_map_uri_to_path(r, &path, &root, 0) == NULL) {
0489             /* simulate successful logging */
0490             return len;
0491         }
0492 
0493         path.data[root] = '\0';
0494 
0495         ngx_memzero(&of, sizeof(ngx_open_file_info_t));
0496 
0497         of.valid = clcf->open_file_cache_valid;
0498         of.min_uses = clcf->open_file_cache_min_uses;
0499         of.test_dir = 1;
0500         of.test_only = 1;
0501         of.errors = clcf->open_file_cache_errors;
0502         of.events = clcf->open_file_cache_events;
0503 
0504         if (ngx_http_set_disable_symlinks(r, clcf, &path, &of) != NGX_OK) {
0505             /* simulate successful logging */
0506             return len;
0507         }
0508 
0509         if (ngx_open_cached_file(clcf->open_file_cache, &path, &of, r->pool)
0510             != NGX_OK)
0511         {
0512             if (of.err == 0) {
0513                 /* simulate successful logging */
0514                 return len;
0515             }
0516 
0517             ngx_log_error(NGX_LOG_ERR, r->connection->log, of.err,
0518                           "testing \"%s\" existence failed", path.data);
0519 
0520             /* simulate successful logging */
0521             return len;
0522         }
0523 
0524         if (!of.is_dir) {
0525             ngx_log_error(NGX_LOG_ERR, r->connection->log, NGX_ENOTDIR,
0526                           "testing \"%s\" existence failed", path.data);
0527 
0528             /* simulate successful logging */
0529             return len;
0530         }
0531     }
0532 
0533     if (ngx_http_script_run(r, &log, script->lengths->elts, 1,
0534                             script->values->elts)
0535         == NULL)
0536     {
0537         /* simulate successful logging */
0538         return len;
0539     }
0540 
0541     log.data[log.len - 1] = '\0';
0542     *name = log.data;
0543 
0544     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
0545                    "http log \"%s\"", log.data);
0546 
0547     llcf = ngx_http_get_module_loc_conf(r, ngx_http_log_module);
0548 
0549     ngx_memzero(&of, sizeof(ngx_open_file_info_t));
0550 
0551     of.log = 1;
0552     of.valid = llcf->open_file_cache_valid;
0553     of.min_uses = llcf->open_file_cache_min_uses;
0554     of.directio = NGX_OPEN_FILE_DIRECTIO_OFF;
0555 
0556     if (ngx_http_set_disable_symlinks(r, clcf, &log, &of) != NGX_OK) {
0557         /* simulate successful logging */
0558         return len;
0559     }
0560 
0561     if (ngx_open_cached_file(llcf->open_file_cache, &log, &of, r->pool)
0562         != NGX_OK)
0563     {
0564         if (of.err == 0) {
0565             /* simulate successful logging */
0566             return len;
0567         }
0568 
0569         ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno,
0570                       "%s \"%s\" failed", of.failed, log.data);
0571         /* simulate successful logging */
0572         return len;
0573     }
0574 
0575     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
0576                    "http log #%d", of.fd);
0577 
0578     n = ngx_write_fd(of.fd, buf, len);
0579 
0580     return n;
0581 }
0582 
0583 
0584 #if (NGX_ZLIB)
0585 
0586 static ssize_t
0587 ngx_http_log_gzip(ngx_fd_t fd, u_char *buf, size_t len, ngx_int_t level,
0588     ngx_log_t *log)
0589 {
0590     int          rc, wbits, memlevel;
0591     u_char      *out;
0592     size_t       size;
0593     ssize_t      n;
0594     z_stream     zstream;
0595     ngx_err_t    err;
0596     ngx_pool_t  *pool;
0597 
0598     wbits = MAX_WBITS;
0599     memlevel = MAX_MEM_LEVEL - 1;
0600 
0601     while ((ssize_t) len < ((1 << (wbits - 1)) - 262)) {
0602         wbits--;
0603         memlevel--;
0604     }
0605 
0606     /*
0607      * This is a formula from deflateBound() for conservative upper bound of
0608      * compressed data plus 18 bytes of gzip wrapper.
0609      */
0610 
0611     size = len + ((len + 7) >> 3) + ((len + 63) >> 6) + 5 + 18;
0612 
0613     ngx_memzero(&zstream, sizeof(z_stream));
0614 
0615     pool = ngx_create_pool(256, log);
0616     if (pool == NULL) {
0617         /* simulate successful logging */
0618         return len;
0619     }
0620 
0621     pool->log = log;
0622 
0623     zstream.zalloc = ngx_http_log_gzip_alloc;
0624     zstream.zfree = ngx_http_log_gzip_free;
0625     zstream.opaque = pool;
0626 
0627     out = ngx_pnalloc(pool, size);
0628     if (out == NULL) {
0629         goto done;
0630     }
0631 
0632     zstream.next_in = buf;
0633     zstream.avail_in = len;
0634     zstream.next_out = out;
0635     zstream.avail_out = size;
0636 
0637     rc = deflateInit2(&zstream, (int) level, Z_DEFLATED, wbits + 16, memlevel,
0638                       Z_DEFAULT_STRATEGY);
0639 
0640     if (rc != Z_OK) {
0641         ngx_log_error(NGX_LOG_ALERT, log, 0, "deflateInit2() failed: %d", rc);
0642         goto done;
0643     }
0644 
0645     ngx_log_debug4(NGX_LOG_DEBUG_HTTP, log, 0,
0646                    "deflate in: ni:%p no:%p ai:%ud ao:%ud",
0647                    zstream.next_in, zstream.next_out,
0648                    zstream.avail_in, zstream.avail_out);
0649 
0650     rc = deflate(&zstream, Z_FINISH);
0651 
0652     if (rc != Z_STREAM_END) {
0653         ngx_log_error(NGX_LOG_ALERT, log, 0,
0654                       "deflate(Z_FINISH) failed: %d", rc);
0655         goto done;
0656     }
0657 
0658     ngx_log_debug5(NGX_LOG_DEBUG_HTTP, log, 0,
0659                    "deflate out: ni:%p no:%p ai:%ud ao:%ud rc:%d",
0660                    zstream.next_in, zstream.next_out,
0661                    zstream.avail_in, zstream.avail_out,
0662                    rc);
0663 
0664     size -= zstream.avail_out;
0665 
0666     rc = deflateEnd(&zstream);
0667 
0668     if (rc != Z_OK) {
0669         ngx_log_error(NGX_LOG_ALERT, log, 0, "deflateEnd() failed: %d", rc);
0670         goto done;
0671     }
0672 
0673     n = ngx_write_fd(fd, out, size);
0674 
0675     if (n != (ssize_t) size) {
0676         err = (n == -1) ? ngx_errno : 0;
0677 
0678         ngx_destroy_pool(pool);
0679 
0680         ngx_set_errno(err);
0681         return -1;
0682     }
0683 
0684 done:
0685 
0686     ngx_destroy_pool(pool);
0687 
0688     /* simulate successful logging */
0689     return len;
0690 }
0691 
0692 
0693 static void *
0694 ngx_http_log_gzip_alloc(void *opaque, u_int items, u_int size)
0695 {
0696     ngx_pool_t *pool = opaque;
0697 
0698     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, pool->log, 0,
0699                    "gzip alloc: n:%ud s:%ud", items, size);
0700 
0701     return ngx_palloc(pool, items * size);
0702 }
0703 
0704 
0705 static void
0706 ngx_http_log_gzip_free(void *opaque, void *address)
0707 {
0708 #if 0
0709     ngx_pool_t *pool = opaque;
0710 
0711     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pool->log, 0, "gzip free: %p", address);
0712 #endif
0713 }
0714 
0715 #endif
0716 
0717 
0718 static void
0719 ngx_http_log_flush(ngx_open_file_t *file, ngx_log_t *log)
0720 {
0721     size_t               len;
0722     ssize_t              n;
0723     ngx_http_log_buf_t  *buffer;
0724 
0725     buffer = file->data;
0726 
0727     len = buffer->pos - buffer->start;
0728 
0729     if (len == 0) {
0730         return;
0731     }
0732 
0733 #if (NGX_ZLIB)
0734     if (buffer->gzip) {
0735         n = ngx_http_log_gzip(file->fd, buffer->start, len, buffer->gzip, log);
0736     } else {
0737         n = ngx_write_fd(file->fd, buffer->start, len);
0738     }
0739 #else
0740     n = ngx_write_fd(file->fd, buffer->start, len);
0741 #endif
0742 
0743     if (n == -1) {
0744         ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
0745                       ngx_write_fd_n " to \"%s\" failed",
0746                       file->name.data);
0747 
0748     } else if ((size_t) n != len) {
0749         ngx_log_error(NGX_LOG_ALERT, log, 0,
0750                       ngx_write_fd_n " to \"%s\" was incomplete: %z of %uz",
0751                       file->name.data, n, len);
0752     }
0753 
0754     buffer->pos = buffer->start;
0755 
0756     if (buffer->event && buffer->event->timer_set) {
0757         ngx_del_timer(buffer->event);
0758     }
0759 }
0760 
0761 
0762 static void
0763 ngx_http_log_flush_handler(ngx_event_t *ev)
0764 {
0765     ngx_log_debug0(NGX_LOG_DEBUG_EVENT, ev->log, 0,
0766                    "http log buffer flush handler");
0767 
0768     ngx_http_log_flush(ev->data, ev->log);
0769 }
0770 
0771 
0772 static u_char *
0773 ngx_http_log_copy_short(ngx_http_request_t *r, u_char *buf,
0774     ngx_http_log_op_t *op)
0775 {
0776     size_t     len;
0777     uintptr_t  data;
0778 
0779     len = op->len;
0780     data = op->data;
0781 
0782     while (len--) {
0783         *buf++ = (u_char) (data & 0xff);
0784         data >>= 8;
0785     }
0786 
0787     return buf;
0788 }
0789 
0790 
0791 static u_char *
0792 ngx_http_log_copy_long(ngx_http_request_t *r, u_char *buf,
0793     ngx_http_log_op_t *op)
0794 {
0795     return ngx_cpymem(buf, (u_char *) op->data, op->len);
0796 }
0797 
0798 
0799 static u_char *
0800 ngx_http_log_pipe(ngx_http_request_t *r, u_char *buf, ngx_http_log_op_t *op)
0801 {
0802     if (r->pipeline) {
0803         *buf = 'p';
0804     } else {
0805         *buf = '.';
0806     }
0807 
0808     return buf + 1;
0809 }
0810 
0811 
0812 static u_char *
0813 ngx_http_log_time(ngx_http_request_t *r, u_char *buf, ngx_http_log_op_t *op)
0814 {
0815     return ngx_cpymem(buf, ngx_cached_http_log_time.data,
0816                       ngx_cached_http_log_time.len);
0817 }
0818 
0819 static u_char *
0820 ngx_http_log_iso8601(ngx_http_request_t *r, u_char *buf, ngx_http_log_op_t *op)
0821 {
0822     return ngx_cpymem(buf, ngx_cached_http_log_iso8601.data,
0823                       ngx_cached_http_log_iso8601.len);
0824 }
0825 
0826 static u_char *
0827 ngx_http_log_msec(ngx_http_request_t *r, u_char *buf, ngx_http_log_op_t *op)
0828 {
0829     ngx_time_t  *tp;
0830 
0831     tp = ngx_timeofday();
0832 
0833     return ngx_sprintf(buf, "%T.%03M", tp->sec, tp->msec);
0834 }
0835 
0836 
0837 static u_char *
0838 ngx_http_log_request_time(ngx_http_request_t *r, u_char *buf,
0839     ngx_http_log_op_t *op)
0840 {
0841     ngx_time_t      *tp;
0842     ngx_msec_int_t   ms;
0843 
0844     tp = ngx_timeofday();
0845 
0846     ms = (ngx_msec_int_t)
0847              ((tp->sec - r->start_sec) * 1000 + (tp->msec - r->start_msec));
0848     ms = ngx_max(ms, 0);
0849 
0850     return ngx_sprintf(buf, "%T.%03M", (time_t) ms / 1000, ms % 1000);
0851 }
0852 
0853 
0854 static u_char *
0855 ngx_http_log_status(ngx_http_request_t *r, u_char *buf, ngx_http_log_op_t *op)
0856 {
0857     ngx_uint_t  status;
0858 
0859     if (r->err_status) {
0860         status = r->err_status;
0861 
0862     } else if (r->headers_out.status) {
0863         status = r->headers_out.status;
0864 
0865     } else if (r->http_version == NGX_HTTP_VERSION_9) {
0866         status = 9;
0867 
0868     } else {
0869         status = 0;
0870     }
0871 
0872     return ngx_sprintf(buf, "%03ui", status);
0873 }
0874 
0875 
0876 static u_char *
0877 ngx_http_log_bytes_sent(ngx_http_request_t *r, u_char *buf,
0878     ngx_http_log_op_t *op)
0879 {
0880     return ngx_sprintf(buf, "%O", r->connection->sent);
0881 }
0882 
0883 
0884 /*
0885  * although there is a real $body_bytes_sent variable,
0886  * this log operation code function is more optimized for logging
0887  */
0888 
0889 static u_char *
0890 ngx_http_log_body_bytes_sent(ngx_http_request_t *r, u_char *buf,
0891     ngx_http_log_op_t *op)
0892 {
0893     off_t  length;
0894 
0895     length = r->connection->sent - r->header_size;
0896 
0897     if (length > 0) {
0898         return ngx_sprintf(buf, "%O", length);
0899     }
0900 
0901     *buf = '0';
0902 
0903     return buf + 1;
0904 }
0905 
0906 
0907 static u_char *
0908 ngx_http_log_request_length(ngx_http_request_t *r, u_char *buf,
0909     ngx_http_log_op_t *op)
0910 {
0911     return ngx_sprintf(buf, "%O", r->request_length);
0912 }
0913 
0914 
0915 static ngx_int_t
0916 ngx_http_log_variable_compile(ngx_conf_t *cf, ngx_http_log_op_t *op,
0917     ngx_str_t *value, ngx_uint_t escape)
0918 {
0919     ngx_int_t  index;
0920 
0921     index = ngx_http_get_variable_index(cf, value);
0922     if (index == NGX_ERROR) {
0923         return NGX_ERROR;
0924     }
0925 
0926     op->len = 0;
0927 
0928     switch (escape) {
0929     case NGX_HTTP_LOG_ESCAPE_JSON:
0930         op->getlen = ngx_http_log_json_variable_getlen;
0931         op->run = ngx_http_log_json_variable;
0932         break;
0933 
0934     case NGX_HTTP_LOG_ESCAPE_NONE:
0935         op->getlen = ngx_http_log_unescaped_variable_getlen;
0936         op->run = ngx_http_log_unescaped_variable;
0937         break;
0938 
0939     default: /* NGX_HTTP_LOG_ESCAPE_DEFAULT */
0940         op->getlen = ngx_http_log_variable_getlen;
0941         op->run = ngx_http_log_variable;
0942     }
0943 
0944     op->data = index;
0945 
0946     return NGX_OK;
0947 }
0948 
0949 
0950 static size_t
0951 ngx_http_log_variable_getlen(ngx_http_request_t *r, uintptr_t data)
0952 {
0953     uintptr_t                   len;
0954     ngx_http_variable_value_t  *value;
0955 
0956     value = ngx_http_get_indexed_variable(r, data);
0957 
0958     if (value == NULL || value->not_found) {
0959         return 1;
0960     }
0961 
0962     len = ngx_http_log_escape(NULL, value->data, value->len);
0963 
0964     value->escape = len ? 1 : 0;
0965 
0966     return value->len + len * 3;
0967 }
0968 
0969 
0970 static u_char *
0971 ngx_http_log_variable(ngx_http_request_t *r, u_char *buf, ngx_http_log_op_t *op)
0972 {
0973     ngx_http_variable_value_t  *value;
0974 
0975     value = ngx_http_get_indexed_variable(r, op->data);
0976 
0977     if (value == NULL || value->not_found) {
0978         *buf = '-';
0979         return buf + 1;
0980     }
0981 
0982     if (value->escape == 0) {
0983         return ngx_cpymem(buf, value->data, value->len);
0984 
0985     } else {
0986         return (u_char *) ngx_http_log_escape(buf, value->data, value->len);
0987     }
0988 }
0989 
0990 
0991 static uintptr_t
0992 ngx_http_log_escape(u_char *dst, u_char *src, size_t size)
0993 {
0994     ngx_uint_t      n;
0995     static u_char   hex[] = "0123456789ABCDEF";
0996 
0997     static uint32_t   escape[] = {
0998         0xffffffff, /* 1111 1111 1111 1111  1111 1111 1111 1111 */
0999 
1000                     /* ?>=< ;:98 7654 3210  /.-, +*)( '&%$ #"!  */
1001         0x00000004, /* 0000 0000 0000 0000  0000 0000 0000 0100 */
1002 
1003                     /* _^]\ [ZYX WVUT SRQP  ONML KJIH GFED CBA@ */
1004         0x10000000, /* 0001 0000 0000 0000  0000 0000 0000 0000 */
1005 
1006                     /*  ~}| {zyx wvut srqp  onml kjih gfed cba` */
1007         0x80000000, /* 1000 0000 0000 0000  0000 0000 0000 0000 */
1008 
1009         0xffffffff, /* 1111 1111 1111 1111  1111 1111 1111 1111 */
1010         0xffffffff, /* 1111 1111 1111 1111  1111 1111 1111 1111 */
1011         0xffffffff, /* 1111 1111 1111 1111  1111 1111 1111 1111 */
1012         0xffffffff, /* 1111 1111 1111 1111  1111 1111 1111 1111 */
1013     };
1014 
1015 
1016     if (dst == NULL) {
1017 
1018         /* find the number of the characters to be escaped */
1019 
1020         n = 0;
1021 
1022         while (size) {
1023             if (escape[*src >> 5] & (1U << (*src & 0x1f))) {
1024                 n++;
1025             }
1026             src++;
1027             size--;
1028         }
1029 
1030         return (uintptr_t) n;
1031     }
1032 
1033     while (size) {
1034         if (escape[*src >> 5] & (1U << (*src & 0x1f))) {
1035             *dst++ = '\\';
1036             *dst++ = 'x';
1037             *dst++ = hex[*src >> 4];
1038             *dst++ = hex[*src & 0xf];
1039             src++;
1040 
1041         } else {
1042             *dst++ = *src++;
1043         }
1044         size--;
1045     }
1046 
1047     return (uintptr_t) dst;
1048 }
1049 
1050 
1051 static size_t
1052 ngx_http_log_json_variable_getlen(ngx_http_request_t *r, uintptr_t data)
1053 {
1054     uintptr_t                   len;
1055     ngx_http_variable_value_t  *value;
1056 
1057     value = ngx_http_get_indexed_variable(r, data);
1058 
1059     if (value == NULL || value->not_found) {
1060         return 0;
1061     }
1062 
1063     len = ngx_escape_json(NULL, value->data, value->len);
1064 
1065     value->escape = len ? 1 : 0;
1066 
1067     return value->len + len;
1068 }
1069 
1070 
1071 static u_char *
1072 ngx_http_log_json_variable(ngx_http_request_t *r, u_char *buf,
1073     ngx_http_log_op_t *op)
1074 {
1075     ngx_http_variable_value_t  *value;
1076 
1077     value = ngx_http_get_indexed_variable(r, op->data);
1078 
1079     if (value == NULL || value->not_found) {
1080         return buf;
1081     }
1082 
1083     if (value->escape == 0) {
1084         return ngx_cpymem(buf, value->data, value->len);
1085 
1086     } else {
1087         return (u_char *) ngx_escape_json(buf, value->data, value->len);
1088     }
1089 }
1090 
1091 
1092 static size_t
1093 ngx_http_log_unescaped_variable_getlen(ngx_http_request_t *r, uintptr_t data)
1094 {
1095     ngx_http_variable_value_t  *value;
1096 
1097     value = ngx_http_get_indexed_variable(r, data);
1098 
1099     if (value == NULL || value->not_found) {
1100         return 0;
1101     }
1102 
1103     value->escape = 0;
1104 
1105     return value->len;
1106 }
1107 
1108 
1109 static u_char *
1110 ngx_http_log_unescaped_variable(ngx_http_request_t *r, u_char *buf,
1111     ngx_http_log_op_t *op)
1112 {
1113     ngx_http_variable_value_t  *value;
1114 
1115     value = ngx_http_get_indexed_variable(r, op->data);
1116 
1117     if (value == NULL || value->not_found) {
1118         return buf;
1119     }
1120 
1121     return ngx_cpymem(buf, value->data, value->len);
1122 }
1123 
1124 
1125 static void *
1126 ngx_http_log_create_main_conf(ngx_conf_t *cf)
1127 {
1128     ngx_http_log_main_conf_t  *conf;
1129 
1130     ngx_http_log_fmt_t  *fmt;
1131 
1132     conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_log_main_conf_t));
1133     if (conf == NULL) {
1134         return NULL;
1135     }
1136 
1137     if (ngx_array_init(&conf->formats, cf->pool, 4, sizeof(ngx_http_log_fmt_t))
1138         != NGX_OK)
1139     {
1140         return NULL;
1141     }
1142 
1143     fmt = ngx_array_push(&conf->formats);
1144     if (fmt == NULL) {
1145         return NULL;
1146     }
1147 
1148     ngx_str_set(&fmt->name, "combined");
1149 
1150     fmt->flushes = NULL;
1151 
1152     fmt->ops = ngx_array_create(cf->pool, 16, sizeof(ngx_http_log_op_t));
1153     if (fmt->ops == NULL) {
1154         return NULL;
1155     }
1156 
1157     return conf;
1158 }
1159 
1160 
1161 static void *
1162 ngx_http_log_create_loc_conf(ngx_conf_t *cf)
1163 {
1164     ngx_http_log_loc_conf_t  *conf;
1165 
1166     conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_log_loc_conf_t));
1167     if (conf == NULL) {
1168         return NULL;
1169     }
1170 
1171     conf->open_file_cache = NGX_CONF_UNSET_PTR;
1172 
1173     return conf;
1174 }
1175 
1176 
1177 static char *
1178 ngx_http_log_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
1179 {
1180     ngx_http_log_loc_conf_t *prev = parent;
1181     ngx_http_log_loc_conf_t *conf = child;
1182 
1183     ngx_http_log_t            *log;
1184     ngx_http_log_fmt_t        *fmt;
1185     ngx_http_log_main_conf_t  *lmcf;
1186 
1187     if (conf->open_file_cache == NGX_CONF_UNSET_PTR) {
1188 
1189         conf->open_file_cache = prev->open_file_cache;
1190         conf->open_file_cache_valid = prev->open_file_cache_valid;
1191         conf->open_file_cache_min_uses = prev->open_file_cache_min_uses;
1192 
1193         if (conf->open_file_cache == NGX_CONF_UNSET_PTR) {
1194             conf->open_file_cache = NULL;
1195         }
1196     }
1197 
1198     if (conf->logs || conf->off) {
1199         return NGX_CONF_OK;
1200     }
1201 
1202     conf->logs = prev->logs;
1203     conf->off = prev->off;
1204 
1205     if (conf->logs || conf->off) {
1206         return NGX_CONF_OK;
1207     }
1208 
1209     conf->logs = ngx_array_create(cf->pool, 2, sizeof(ngx_http_log_t));
1210     if (conf->logs == NULL) {
1211         return NGX_CONF_ERROR;
1212     }
1213 
1214     log = ngx_array_push(conf->logs);
1215     if (log == NULL) {
1216         return NGX_CONF_ERROR;
1217     }
1218 
1219     ngx_memzero(log, sizeof(ngx_http_log_t));
1220 
1221     log->file = ngx_conf_open_file(cf->cycle, &ngx_http_access_log);
1222     if (log->file == NULL) {
1223         return NGX_CONF_ERROR;
1224     }
1225 
1226     lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_log_module);
1227     fmt = lmcf->formats.elts;
1228 
1229     /* the default "combined" format */
1230     log->format = &fmt[0];
1231     lmcf->combined_used = 1;
1232 
1233     return NGX_CONF_OK;
1234 }
1235 
1236 
1237 static char *
1238 ngx_http_log_set_log(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
1239 {
1240     ngx_http_log_loc_conf_t *llcf = conf;
1241 
1242     ssize_t                            size;
1243     ngx_int_t                          gzip;
1244     ngx_uint_t                         i, n;
1245     ngx_msec_t                         flush;
1246     ngx_str_t                         *value, name, s;
1247     ngx_http_log_t                    *log;
1248     ngx_syslog_peer_t                 *peer;
1249     ngx_http_log_buf_t                *buffer;
1250     ngx_http_log_fmt_t                *fmt;
1251     ngx_http_log_main_conf_t          *lmcf;
1252     ngx_http_script_compile_t          sc;
1253     ngx_http_compile_complex_value_t   ccv;
1254 
1255     value = cf->args->elts;
1256 
1257     if (ngx_strcmp(value[1].data, "off") == 0) {
1258         llcf->off = 1;
1259         if (cf->args->nelts == 2) {
1260             return NGX_CONF_OK;
1261         }
1262 
1263         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1264                            "invalid parameter \"%V\"", &value[2]);
1265         return NGX_CONF_ERROR;
1266     }
1267 
1268     if (llcf->logs == NULL) {
1269         llcf->logs = ngx_array_create(cf->pool, 2, sizeof(ngx_http_log_t));
1270         if (llcf->logs == NULL) {
1271             return NGX_CONF_ERROR;
1272         }
1273     }
1274 
1275     lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_log_module);
1276 
1277     log = ngx_array_push(llcf->logs);
1278     if (log == NULL) {
1279         return NGX_CONF_ERROR;
1280     }
1281 
1282     ngx_memzero(log, sizeof(ngx_http_log_t));
1283 
1284 
1285     if (ngx_strncmp(value[1].data, "syslog:", 7) == 0) {
1286 
1287         peer = ngx_pcalloc(cf->pool, sizeof(ngx_syslog_peer_t));
1288         if (peer == NULL) {
1289             return NGX_CONF_ERROR;
1290         }
1291 
1292         if (ngx_syslog_process_conf(cf, peer) != NGX_CONF_OK) {
1293             return NGX_CONF_ERROR;
1294         }
1295 
1296         log->syslog_peer = peer;
1297 
1298         goto process_formats;
1299     }
1300 
1301     n = ngx_http_script_variables_count(&value[1]);
1302 
1303     if (n == 0) {
1304         log->file = ngx_conf_open_file(cf->cycle, &value[1]);
1305         if (log->file == NULL) {
1306             return NGX_CONF_ERROR;
1307         }
1308 
1309     } else {
1310         if (ngx_conf_full_name(cf->cycle, &value[1], 0) != NGX_OK) {
1311             return NGX_CONF_ERROR;
1312         }
1313 
1314         log->script = ngx_pcalloc(cf->pool, sizeof(ngx_http_log_script_t));
1315         if (log->script == NULL) {
1316             return NGX_CONF_ERROR;
1317         }
1318 
1319         ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
1320 
1321         sc.cf = cf;
1322         sc.source = &value[1];
1323         sc.lengths = &log->script->lengths;
1324         sc.values = &log->script->values;
1325         sc.variables = n;
1326         sc.complete_lengths = 1;
1327         sc.complete_values = 1;
1328 
1329         if (ngx_http_script_compile(&sc) != NGX_OK) {
1330             return NGX_CONF_ERROR;
1331         }
1332     }
1333 
1334 process_formats:
1335 
1336     if (cf->args->nelts >= 3) {
1337         name = value[2];
1338 
1339         if (ngx_strcmp(name.data, "combined") == 0) {
1340             lmcf->combined_used = 1;
1341         }
1342 
1343     } else {
1344         ngx_str_set(&name, "combined");
1345         lmcf->combined_used = 1;
1346     }
1347 
1348     fmt = lmcf->formats.elts;
1349     for (i = 0; i < lmcf->formats.nelts; i++) {
1350         if (fmt[i].name.len == name.len
1351             && ngx_strcasecmp(fmt[i].name.data, name.data) == 0)
1352         {
1353             log->format = &fmt[i];
1354             break;
1355         }
1356     }
1357 
1358     if (log->format == NULL) {
1359         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1360                            "unknown log format \"%V\"", &name);
1361         return NGX_CONF_ERROR;
1362     }
1363 
1364     size = 0;
1365     flush = 0;
1366     gzip = 0;
1367 
1368     for (i = 3; i < cf->args->nelts; i++) {
1369 
1370         if (ngx_strncmp(value[i].data, "buffer=", 7) == 0) {
1371             s.len = value[i].len - 7;
1372             s.data = value[i].data + 7;
1373 
1374             size = ngx_parse_size(&s);
1375 
1376             if (size == NGX_ERROR || size == 0) {
1377                 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1378                                    "invalid buffer size \"%V\"", &s);
1379                 return NGX_CONF_ERROR;
1380             }
1381 
1382             continue;
1383         }
1384 
1385         if (ngx_strncmp(value[i].data, "flush=", 6) == 0) {
1386             s.len = value[i].len - 6;
1387             s.data = value[i].data + 6;
1388 
1389             flush = ngx_parse_time(&s, 0);
1390 
1391             if (flush == (ngx_msec_t) NGX_ERROR || flush == 0) {
1392                 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1393                                    "invalid flush time \"%V\"", &s);
1394                 return NGX_CONF_ERROR;
1395             }
1396 
1397             continue;
1398         }
1399 
1400         if (ngx_strncmp(value[i].data, "gzip", 4) == 0
1401             && (value[i].len == 4 || value[i].data[4] == '='))
1402         {
1403 #if (NGX_ZLIB)
1404             if (size == 0) {
1405                 size = 64 * 1024;
1406             }
1407 
1408             if (value[i].len == 4) {
1409                 gzip = Z_BEST_SPEED;
1410                 continue;
1411             }
1412 
1413             s.len = value[i].len - 5;
1414             s.data = value[i].data + 5;
1415 
1416             gzip = ngx_atoi(s.data, s.len);
1417 
1418             if (gzip < 1 || gzip > 9) {
1419                 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1420                                    "invalid compression level \"%V\"", &s);
1421                 return NGX_CONF_ERROR;
1422             }
1423 
1424             continue;
1425 
1426 #else
1427             ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1428                                "nginx was built without zlib support");
1429             return NGX_CONF_ERROR;
1430 #endif
1431         }
1432 
1433         if (ngx_strncmp(value[i].data, "if=", 3) == 0) {
1434             s.len = value[i].len - 3;
1435             s.data = value[i].data + 3;
1436 
1437             ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
1438 
1439             ccv.cf = cf;
1440             ccv.value = &s;
1441             ccv.complex_value = ngx_palloc(cf->pool,
1442                                            sizeof(ngx_http_complex_value_t));
1443             if (ccv.complex_value == NULL) {
1444                 return NGX_CONF_ERROR;
1445             }
1446 
1447             if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
1448                 return NGX_CONF_ERROR;
1449             }
1450 
1451             log->filter = ccv.complex_value;
1452 
1453             continue;
1454         }
1455 
1456         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1457                            "invalid parameter \"%V\"", &value[i]);
1458         return NGX_CONF_ERROR;
1459     }
1460 
1461     if (flush && size == 0) {
1462         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1463                            "no buffer is defined for access_log \"%V\"",
1464                            &value[1]);
1465         return NGX_CONF_ERROR;
1466     }
1467 
1468     if (size) {
1469 
1470         if (log->script) {
1471             ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1472                                "buffered logs cannot have variables in name");
1473             return NGX_CONF_ERROR;
1474         }
1475 
1476         if (log->syslog_peer) {
1477             ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1478                                "logs to syslog cannot be buffered");
1479             return NGX_CONF_ERROR;
1480         }
1481 
1482         if (log->file->data) {
1483             buffer = log->file->data;
1484 
1485             if (buffer->last - buffer->start != size
1486                 || buffer->flush != flush
1487                 || buffer->gzip != gzip)
1488             {
1489                 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1490                                    "access_log \"%V\" already defined "
1491                                    "with conflicting parameters",
1492                                    &value[1]);
1493                 return NGX_CONF_ERROR;
1494             }
1495 
1496             return NGX_CONF_OK;
1497         }
1498 
1499         buffer = ngx_pcalloc(cf->pool, sizeof(ngx_http_log_buf_t));
1500         if (buffer == NULL) {
1501             return NGX_CONF_ERROR;
1502         }
1503 
1504         buffer->start = ngx_pnalloc(cf->pool, size);
1505         if (buffer->start == NULL) {
1506             return NGX_CONF_ERROR;
1507         }
1508 
1509         buffer->pos = buffer->start;
1510         buffer->last = buffer->start + size;
1511 
1512         if (flush) {
1513             buffer->event = ngx_pcalloc(cf->pool, sizeof(ngx_event_t));
1514             if (buffer->event == NULL) {
1515                 return NGX_CONF_ERROR;
1516             }
1517 
1518             buffer->event->data = log->file;
1519             buffer->event->handler = ngx_http_log_flush_handler;
1520             buffer->event->log = &cf->cycle->new_log;
1521             buffer->event->cancelable = 1;
1522 
1523             buffer->flush = flush;
1524         }
1525 
1526         buffer->gzip = gzip;
1527 
1528         log->file->flush = ngx_http_log_flush;
1529         log->file->data = buffer;
1530     }
1531 
1532     return NGX_CONF_OK;
1533 }
1534 
1535 
1536 static char *
1537 ngx_http_log_set_format(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
1538 {
1539     ngx_http_log_main_conf_t *lmcf = conf;
1540 
1541     ngx_str_t           *value;
1542     ngx_uint_t           i;
1543     ngx_http_log_fmt_t  *fmt;
1544 
1545     value = cf->args->elts;
1546 
1547     fmt = lmcf->formats.elts;
1548     for (i = 0; i < lmcf->formats.nelts; i++) {
1549         if (fmt[i].name.len == value[1].len
1550             && ngx_strcmp(fmt[i].name.data, value[1].data) == 0)
1551         {
1552             ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1553                                "duplicate \"log_format\" name \"%V\"",
1554                                &value[1]);
1555             return NGX_CONF_ERROR;
1556         }
1557     }
1558 
1559     fmt = ngx_array_push(&lmcf->formats);
1560     if (fmt == NULL) {
1561         return NGX_CONF_ERROR;
1562     }
1563 
1564     fmt->name = value[1];
1565 
1566     fmt->flushes = ngx_array_create(cf->pool, 4, sizeof(ngx_int_t));
1567     if (fmt->flushes == NULL) {
1568         return NGX_CONF_ERROR;
1569     }
1570 
1571     fmt->ops = ngx_array_create(cf->pool, 16, sizeof(ngx_http_log_op_t));
1572     if (fmt->ops == NULL) {
1573         return NGX_CONF_ERROR;
1574     }
1575 
1576     return ngx_http_log_compile_format(cf, fmt->flushes, fmt->ops, cf->args, 2);
1577 }
1578 
1579 
1580 static char *
1581 ngx_http_log_compile_format(ngx_conf_t *cf, ngx_array_t *flushes,
1582     ngx_array_t *ops, ngx_array_t *args, ngx_uint_t s)
1583 {
1584     u_char              *data, *p, ch;
1585     size_t               i, len;
1586     ngx_str_t           *value, var;
1587     ngx_int_t           *flush;
1588     ngx_uint_t           bracket, escape;
1589     ngx_http_log_op_t   *op;
1590     ngx_http_log_var_t  *v;
1591 
1592     escape = NGX_HTTP_LOG_ESCAPE_DEFAULT;
1593     value = args->elts;
1594 
1595     if (s < args->nelts && ngx_strncmp(value[s].data, "escape=", 7) == 0) {
1596         data = value[s].data + 7;
1597 
1598         if (ngx_strcmp(data, "json") == 0) {
1599             escape = NGX_HTTP_LOG_ESCAPE_JSON;
1600 
1601         } else if (ngx_strcmp(data, "none") == 0) {
1602             escape = NGX_HTTP_LOG_ESCAPE_NONE;
1603 
1604         } else if (ngx_strcmp(data, "default") != 0) {
1605             ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1606                                "unknown log format escaping \"%s\"", data);
1607             return NGX_CONF_ERROR;
1608         }
1609 
1610         s++;
1611     }
1612 
1613     for ( /* void */ ; s < args->nelts; s++) {
1614 
1615         i = 0;
1616 
1617         while (i < value[s].len) {
1618 
1619             op = ngx_array_push(ops);
1620             if (op == NULL) {
1621                 return NGX_CONF_ERROR;
1622             }
1623 
1624             data = &value[s].data[i];
1625 
1626             if (value[s].data[i] == '$') {
1627 
1628                 if (++i == value[s].len) {
1629                     goto invalid;
1630                 }
1631 
1632                 if (value[s].data[i] == '{') {
1633                     bracket = 1;
1634 
1635                     if (++i == value[s].len) {
1636                         goto invalid;
1637                     }
1638 
1639                     var.data = &value[s].data[i];
1640 
1641                 } else {
1642                     bracket = 0;
1643                     var.data = &value[s].data[i];
1644                 }
1645 
1646                 for (var.len = 0; i < value[s].len; i++, var.len++) {
1647                     ch = value[s].data[i];
1648 
1649                     if (ch == '}' && bracket) {
1650                         i++;
1651                         bracket = 0;
1652                         break;
1653                     }
1654 
1655                     if ((ch >= 'A' && ch <= 'Z')
1656                         || (ch >= 'a' && ch <= 'z')
1657                         || (ch >= '0' && ch <= '9')
1658                         || ch == '_')
1659                     {
1660                         continue;
1661                     }
1662 
1663                     break;
1664                 }
1665 
1666                 if (bracket) {
1667                     ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1668                                        "the closing bracket in \"%V\" "
1669                                        "variable is missing", &var);
1670                     return NGX_CONF_ERROR;
1671                 }
1672 
1673                 if (var.len == 0) {
1674                     goto invalid;
1675                 }
1676 
1677                 for (v = ngx_http_log_vars; v->name.len; v++) {
1678 
1679                     if (v->name.len == var.len
1680                         && ngx_strncmp(v->name.data, var.data, var.len) == 0)
1681                     {
1682                         op->len = v->len;
1683                         op->getlen = NULL;
1684                         op->run = v->run;
1685                         op->data = 0;
1686 
1687                         goto found;
1688                     }
1689                 }
1690 
1691                 if (ngx_http_log_variable_compile(cf, op, &var, escape)
1692                     != NGX_OK)
1693                 {
1694                     return NGX_CONF_ERROR;
1695                 }
1696 
1697                 if (flushes) {
1698 
1699                     flush = ngx_array_push(flushes);
1700                     if (flush == NULL) {
1701                         return NGX_CONF_ERROR;
1702                     }
1703 
1704                     *flush = op->data; /* variable index */
1705                 }
1706 
1707             found:
1708 
1709                 continue;
1710             }
1711 
1712             i++;
1713 
1714             while (i < value[s].len && value[s].data[i] != '$') {
1715                 i++;
1716             }
1717 
1718             len = &value[s].data[i] - data;
1719 
1720             if (len) {
1721 
1722                 op->len = len;
1723                 op->getlen = NULL;
1724 
1725                 if (len <= sizeof(uintptr_t)) {
1726                     op->run = ngx_http_log_copy_short;
1727                     op->data = 0;
1728 
1729                     while (len--) {
1730                         op->data <<= 8;
1731                         op->data |= data[len];
1732                     }
1733 
1734                 } else {
1735                     op->run = ngx_http_log_copy_long;
1736 
1737                     p = ngx_pnalloc(cf->pool, len);
1738                     if (p == NULL) {
1739                         return NGX_CONF_ERROR;
1740                     }
1741 
1742                     ngx_memcpy(p, data, len);
1743                     op->data = (uintptr_t) p;
1744                 }
1745             }
1746         }
1747     }
1748 
1749     return NGX_CONF_OK;
1750 
1751 invalid:
1752 
1753     ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid parameter \"%s\"", data);
1754 
1755     return NGX_CONF_ERROR;
1756 }
1757 
1758 
1759 static char *
1760 ngx_http_log_open_file_cache(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
1761 {
1762     ngx_http_log_loc_conf_t *llcf = conf;
1763 
1764     time_t       inactive, valid;
1765     ngx_str_t   *value, s;
1766     ngx_int_t    max, min_uses;
1767     ngx_uint_t   i;
1768 
1769     if (llcf->open_file_cache != NGX_CONF_UNSET_PTR) {
1770         return "is duplicate";
1771     }
1772 
1773     value = cf->args->elts;
1774 
1775     max = 0;
1776     inactive = 10;
1777     valid = 60;
1778     min_uses = 1;
1779 
1780     for (i = 1; i < cf->args->nelts; i++) {
1781 
1782         if (ngx_strncmp(value[i].data, "max=", 4) == 0) {
1783 
1784             max = ngx_atoi(value[i].data + 4, value[i].len - 4);
1785             if (max == NGX_ERROR) {
1786                 goto failed;
1787             }
1788 
1789             continue;
1790         }
1791 
1792         if (ngx_strncmp(value[i].data, "inactive=", 9) == 0) {
1793 
1794             s.len = value[i].len - 9;
1795             s.data = value[i].data + 9;
1796 
1797             inactive = ngx_parse_time(&s, 1);
1798             if (inactive == (time_t) NGX_ERROR) {
1799                 goto failed;
1800             }
1801 
1802             continue;
1803         }
1804 
1805         if (ngx_strncmp(value[i].data, "min_uses=", 9) == 0) {
1806 
1807             min_uses = ngx_atoi(value[i].data + 9, value[i].len - 9);
1808             if (min_uses == NGX_ERROR) {
1809                 goto failed;
1810             }
1811 
1812             continue;
1813         }
1814 
1815         if (ngx_strncmp(value[i].data, "valid=", 6) == 0) {
1816 
1817             s.len = value[i].len - 6;
1818             s.data = value[i].data + 6;
1819 
1820             valid = ngx_parse_time(&s, 1);
1821             if (valid == (time_t) NGX_ERROR) {
1822                 goto failed;
1823             }
1824 
1825             continue;
1826         }
1827 
1828         if (ngx_strcmp(value[i].data, "off") == 0) {
1829 
1830             llcf->open_file_cache = NULL;
1831 
1832             continue;
1833         }
1834 
1835     failed:
1836 
1837         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1838                            "invalid \"open_log_file_cache\" parameter \"%V\"",
1839                            &value[i]);
1840         return NGX_CONF_ERROR;
1841     }
1842 
1843     if (llcf->open_file_cache == NULL) {
1844         return NGX_CONF_OK;
1845     }
1846 
1847     if (max == 0) {
1848         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1849                         "\"open_log_file_cache\" must have \"max\" parameter");
1850         return NGX_CONF_ERROR;
1851     }
1852 
1853     llcf->open_file_cache = ngx_open_file_cache_init(cf->pool, max, inactive);
1854 
1855     if (llcf->open_file_cache) {
1856 
1857         llcf->open_file_cache_valid = valid;
1858         llcf->open_file_cache_min_uses = min_uses;
1859 
1860         return NGX_CONF_OK;
1861     }
1862 
1863     return NGX_CONF_ERROR;
1864 }
1865 
1866 
1867 static ngx_int_t
1868 ngx_http_log_init(ngx_conf_t *cf)
1869 {
1870     ngx_str_t                  *value;
1871     ngx_array_t                 a;
1872     ngx_http_handler_pt        *h;
1873     ngx_http_log_fmt_t         *fmt;
1874     ngx_http_log_main_conf_t   *lmcf;
1875     ngx_http_core_main_conf_t  *cmcf;
1876 
1877     lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_log_module);
1878 
1879     if (lmcf->combined_used) {
1880         if (ngx_array_init(&a, cf->pool, 1, sizeof(ngx_str_t)) != NGX_OK) {
1881             return NGX_ERROR;
1882         }
1883 
1884         value = ngx_array_push(&a);
1885         if (value == NULL) {
1886             return NGX_ERROR;
1887         }
1888 
1889         *value = ngx_http_combined_fmt;
1890         fmt = lmcf->formats.elts;
1891 
1892         if (ngx_http_log_compile_format(cf, NULL, fmt->ops, &a, 0)
1893             != NGX_CONF_OK)
1894         {
1895             return NGX_ERROR;
1896         }
1897     }
1898 
1899     cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
1900 
1901     h = ngx_array_push(&cmcf->phases[NGX_HTTP_LOG_PHASE].handlers);
1902     if (h == NULL) {
1903         return NGX_ERROR;
1904     }
1905 
1906     *h = ngx_http_log_handler;
1907 
1908     return NGX_OK;
1909 }