Igor Sysoev | 74e95c2 | 2003-11-09 20:03:38 +0000 | [diff] [blame] | 1 | |
| 2 | #include <ngx_config.h> |
| 3 | #include <ngx_core.h> |
| 4 | #include <ngx_http.h> |
| 5 | #include <nginx.h> |
| 6 | |
| 7 | |
Igor Sysoev | 10a543a | 2004-03-16 07:10:12 +0000 | [diff] [blame] | 8 | static u_char *ngx_http_log_addr(ngx_http_request_t *r, u_char *buf, |
Igor Sysoev | 74e95c2 | 2003-11-09 20:03:38 +0000 | [diff] [blame] | 9 | uintptr_t data); |
Igor Sysoev | 10a543a | 2004-03-16 07:10:12 +0000 | [diff] [blame] | 10 | static u_char *ngx_http_log_connection(ngx_http_request_t *r, u_char *buf, |
| 11 | uintptr_t data); |
| 12 | static u_char *ngx_http_log_pipe(ngx_http_request_t *r, u_char *buf, |
Igor Sysoev | 74e95c2 | 2003-11-09 20:03:38 +0000 | [diff] [blame] | 13 | uintptr_t data); |
Igor Sysoev | 10a543a | 2004-03-16 07:10:12 +0000 | [diff] [blame] | 14 | static u_char *ngx_http_log_time(ngx_http_request_t *r, u_char *buf, |
| 15 | uintptr_t data); |
Igor Sysoev | a7c4a2a | 2004-08-29 03:55:41 +0000 | [diff] [blame] | 16 | static u_char *ngx_http_log_msec(ngx_http_request_t *r, u_char *buf, |
| 17 | uintptr_t data); |
Igor Sysoev | 10a543a | 2004-03-16 07:10:12 +0000 | [diff] [blame] | 18 | static u_char *ngx_http_log_request(ngx_http_request_t *r, u_char *buf, |
Igor Sysoev | 74e95c2 | 2003-11-09 20:03:38 +0000 | [diff] [blame] | 19 | uintptr_t data); |
Igor Sysoev | 10a543a | 2004-03-16 07:10:12 +0000 | [diff] [blame] | 20 | static u_char *ngx_http_log_status(ngx_http_request_t *r, u_char *buf, |
| 21 | uintptr_t data); |
| 22 | static u_char *ngx_http_log_length(ngx_http_request_t *r, u_char *buf, |
| 23 | uintptr_t data); |
| 24 | static u_char *ngx_http_log_apache_length(ngx_http_request_t *r, u_char *buf, |
| 25 | uintptr_t data); |
| 26 | static u_char *ngx_http_log_header_in(ngx_http_request_t *r, u_char *buf, |
| 27 | uintptr_t data); |
| 28 | static u_char *ngx_http_log_connection_header_out(ngx_http_request_t *r, |
| 29 | u_char *buf, uintptr_t data); |
| 30 | static u_char *ngx_http_log_transfer_encoding_header_out(ngx_http_request_t *r, |
| 31 | u_char *buf, |
| 32 | uintptr_t data); |
| 33 | static u_char *ngx_http_log_unknown_header_in(ngx_http_request_t *r, |
| 34 | u_char *buf, uintptr_t data); |
| 35 | static u_char *ngx_http_log_header_out(ngx_http_request_t *r, u_char *buf, |
| 36 | uintptr_t data); |
| 37 | static u_char *ngx_http_log_unknown_header_out(ngx_http_request_t *r, u_char *buf, |
| 38 | uintptr_t data); |
Igor Sysoev | 74e95c2 | 2003-11-09 20:03:38 +0000 | [diff] [blame] | 39 | |
Igor Sysoev | 2f65722 | 2004-06-16 15:32:11 +0000 | [diff] [blame] | 40 | static ngx_int_t ngx_http_log_pre_conf(ngx_conf_t *cf); |
Igor Sysoev | 74e95c2 | 2003-11-09 20:03:38 +0000 | [diff] [blame] | 41 | static void *ngx_http_log_create_main_conf(ngx_conf_t *cf); |
| 42 | static void *ngx_http_log_create_loc_conf(ngx_conf_t *cf); |
| 43 | static char *ngx_http_log_merge_loc_conf(ngx_conf_t *cf, void *parent, |
| 44 | void *child); |
| 45 | static char *ngx_http_log_set_log(ngx_conf_t *cf, ngx_command_t *cmd, |
| 46 | void *conf); |
| 47 | static char *ngx_http_log_set_format(ngx_conf_t *cf, ngx_command_t *cmd, |
| 48 | void *conf); |
Igor Sysoev | 2f65722 | 2004-06-16 15:32:11 +0000 | [diff] [blame] | 49 | static ngx_int_t ngx_http_log_parse_format(ngx_conf_t *cf, ngx_array_t *ops, |
| 50 | ngx_str_t *line); |
Igor Sysoev | 74e95c2 | 2003-11-09 20:03:38 +0000 | [diff] [blame] | 51 | |
| 52 | |
| 53 | static ngx_command_t ngx_http_log_commands[] = { |
| 54 | |
| 55 | {ngx_string("log_format"), |
| 56 | NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_2MORE, |
| 57 | ngx_http_log_set_format, |
| 58 | NGX_HTTP_MAIN_CONF_OFFSET, |
| 59 | 0, |
| 60 | NULL}, |
| 61 | |
| 62 | {ngx_string("access_log"), |
| 63 | NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE12, |
| 64 | ngx_http_log_set_log, |
| 65 | NGX_HTTP_LOC_CONF_OFFSET, |
| 66 | 0, |
| 67 | NULL}, |
| 68 | |
| 69 | ngx_null_command |
| 70 | }; |
| 71 | |
| 72 | |
| 73 | ngx_http_module_t ngx_http_log_module_ctx = { |
Igor Sysoev | 7832933 | 2003-11-10 17:17:31 +0000 | [diff] [blame] | 74 | ngx_http_log_pre_conf, /* pre conf */ |
| 75 | |
Igor Sysoev | 74e95c2 | 2003-11-09 20:03:38 +0000 | [diff] [blame] | 76 | ngx_http_log_create_main_conf, /* create main configuration */ |
| 77 | NULL, /* init main configuration */ |
| 78 | |
| 79 | NULL, /* create server configuration */ |
| 80 | NULL, /* merge server configuration */ |
| 81 | |
| 82 | ngx_http_log_create_loc_conf, /* create location configration */ |
| 83 | ngx_http_log_merge_loc_conf /* merge location configration */ |
| 84 | }; |
| 85 | |
| 86 | |
| 87 | ngx_module_t ngx_http_log_module = { |
| 88 | NGX_MODULE, |
| 89 | &ngx_http_log_module_ctx, /* module context */ |
| 90 | ngx_http_log_commands, /* module directives */ |
| 91 | NGX_HTTP_MODULE, /* module type */ |
| 92 | NULL, /* init module */ |
| 93 | NULL /* init child */ |
| 94 | }; |
| 95 | |
| 96 | |
Igor Sysoev | 980a924 | 2004-09-05 19:54:02 +0000 | [diff] [blame] | 97 | static ngx_str_t http_access_log = ngx_string(NGX_HTTP_LOG_PATH); |
Igor Sysoev | 74e95c2 | 2003-11-09 20:03:38 +0000 | [diff] [blame] | 98 | |
| 99 | |
Igor Sysoev | 74e95c2 | 2003-11-09 20:03:38 +0000 | [diff] [blame] | 100 | static ngx_str_t ngx_http_combined_fmt = |
Igor Sysoev | dc867cd | 2003-12-14 20:10:27 +0000 | [diff] [blame] | 101 | ngx_string("%addr - - [%time] \"%request\" %status %apache_length " |
Igor Sysoev | fc68ea6 | 2003-12-22 17:27:24 +0000 | [diff] [blame] | 102 | "\"%{Referer}i\" \"%{User-Agent}i\""); |
Igor Sysoev | 74e95c2 | 2003-11-09 20:03:38 +0000 | [diff] [blame] | 103 | |
| 104 | |
Igor Sysoev | 7832933 | 2003-11-10 17:17:31 +0000 | [diff] [blame] | 105 | ngx_http_log_op_name_t ngx_http_log_fmt_ops[] = { |
Igor Sysoev | 74e95c2 | 2003-11-09 20:03:38 +0000 | [diff] [blame] | 106 | { ngx_string("addr"), INET_ADDRSTRLEN - 1, ngx_http_log_addr }, |
| 107 | { ngx_string("conn"), NGX_INT32_LEN, ngx_http_log_connection }, |
| 108 | { ngx_string("pipe"), 1, ngx_http_log_pipe }, |
| 109 | { ngx_string("time"), sizeof("28/Sep/1970:12:00:00") - 1, |
| 110 | ngx_http_log_time }, |
Igor Sysoev | a7c4a2a | 2004-08-29 03:55:41 +0000 | [diff] [blame] | 111 | { ngx_string("msec"), TIME_T_LEN + 4, ngx_http_log_msec }, |
Igor Sysoev | 74e95c2 | 2003-11-09 20:03:38 +0000 | [diff] [blame] | 112 | { ngx_string("request"), 0, ngx_http_log_request }, |
| 113 | { ngx_string("status"), 3, ngx_http_log_status }, |
Igor Sysoev | 5f80078 | 2003-12-08 20:48:12 +0000 | [diff] [blame] | 114 | { ngx_string("length"), NGX_OFF_T_LEN, ngx_http_log_length }, |
Igor Sysoev | dc867cd | 2003-12-14 20:10:27 +0000 | [diff] [blame] | 115 | { ngx_string("apache_length"), NGX_OFF_T_LEN, ngx_http_log_apache_length }, |
Igor Sysoev | 74e95c2 | 2003-11-09 20:03:38 +0000 | [diff] [blame] | 116 | { ngx_string("i"), NGX_HTTP_LOG_ARG, ngx_http_log_header_in }, |
| 117 | { ngx_string("o"), NGX_HTTP_LOG_ARG, ngx_http_log_header_out }, |
| 118 | { ngx_null_string, 0, NULL } |
| 119 | }; |
| 120 | |
| 121 | |
Igor Sysoev | 2f65722 | 2004-06-16 15:32:11 +0000 | [diff] [blame] | 122 | ngx_int_t ngx_http_log_handler(ngx_http_request_t *r) |
Igor Sysoev | 74e95c2 | 2003-11-09 20:03:38 +0000 | [diff] [blame] | 123 | { |
Igor Sysoev | 10a543a | 2004-03-16 07:10:12 +0000 | [diff] [blame] | 124 | ngx_uint_t i, l; |
| 125 | uintptr_t data; |
| 126 | u_char *line, *p; |
Igor Sysoev | 74e95c2 | 2003-11-09 20:03:38 +0000 | [diff] [blame] | 127 | size_t len; |
| 128 | ngx_http_log_t *log; |
| 129 | ngx_http_log_op_t *op; |
| 130 | ngx_http_log_loc_conf_t *lcf; |
| 131 | #if (WIN32) |
Igor Sysoev | ab9d5fd | 2004-03-05 08:34:24 +0000 | [diff] [blame] | 132 | u_long written; |
Igor Sysoev | 74e95c2 | 2003-11-09 20:03:38 +0000 | [diff] [blame] | 133 | #endif |
| 134 | |
Igor Sysoev | dc867cd | 2003-12-14 20:10:27 +0000 | [diff] [blame] | 135 | ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
| 136 | "http log handler"); |
Igor Sysoev | 74e95c2 | 2003-11-09 20:03:38 +0000 | [diff] [blame] | 137 | |
| 138 | lcf = ngx_http_get_module_loc_conf(r, ngx_http_log_module); |
| 139 | |
Igor Sysoev | 85cccfb | 2004-09-15 16:00:43 +0000 | [diff] [blame^] | 140 | if (lcf->off) { |
| 141 | return NGX_OK; |
| 142 | } |
| 143 | |
Igor Sysoev | 74e95c2 | 2003-11-09 20:03:38 +0000 | [diff] [blame] | 144 | log = lcf->logs->elts; |
| 145 | for (l = 0; l < lcf->logs->nelts; l++) { |
| 146 | |
| 147 | len = 0; |
| 148 | op = log[l].ops->elts; |
| 149 | for (i = 0; i < log[l].ops->nelts; i++) { |
| 150 | if (op[i].len == 0) { |
| 151 | len += (size_t) op[i].op(r, NULL, op[i].data); |
| 152 | |
| 153 | } else { |
| 154 | len += op[i].len; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | #if (WIN32) |
| 159 | len += 2; |
| 160 | #else |
| 161 | len++; |
| 162 | #endif |
| 163 | |
| 164 | ngx_test_null(line, ngx_palloc(r->pool, len), NGX_ERROR); |
| 165 | p = line; |
| 166 | |
| 167 | for (i = 0; i < log[l].ops->nelts; i++) { |
| 168 | if (op[i].op == NGX_HTTP_LOG_COPY_SHORT) { |
| 169 | len = op[i].len; |
| 170 | data = op[i].data; |
| 171 | while (len--) { |
Igor Sysoev | 10a543a | 2004-03-16 07:10:12 +0000 | [diff] [blame] | 172 | *p++ = (char) (data & 0xff); |
Igor Sysoev | 74e95c2 | 2003-11-09 20:03:38 +0000 | [diff] [blame] | 173 | data >>= 8; |
| 174 | } |
| 175 | |
| 176 | } else if (op[i].op == NGX_HTTP_LOG_COPY_LONG) { |
| 177 | p = ngx_cpymem(p, (void *) op[i].data, op[i].len); |
| 178 | |
| 179 | } else { |
| 180 | p = op[i].op(r, p, op[i].data); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | #if (WIN32) |
| 185 | *p++ = CR; *p++ = LF; |
| 186 | WriteFile(log[l].file->fd, line, p - line, &written, NULL); |
| 187 | #else |
| 188 | *p++ = LF; |
| 189 | write(log[l].file->fd, line, p - line); |
| 190 | #endif |
| 191 | } |
| 192 | |
| 193 | return NGX_OK; |
| 194 | } |
| 195 | |
| 196 | |
Igor Sysoev | 10a543a | 2004-03-16 07:10:12 +0000 | [diff] [blame] | 197 | static u_char *ngx_http_log_addr(ngx_http_request_t *r, u_char *buf, |
| 198 | uintptr_t data) |
Igor Sysoev | 74e95c2 | 2003-11-09 20:03:38 +0000 | [diff] [blame] | 199 | { |
| 200 | return ngx_cpymem(buf, r->connection->addr_text.data, |
| 201 | r->connection->addr_text.len); |
| 202 | } |
| 203 | |
| 204 | |
Igor Sysoev | 10a543a | 2004-03-16 07:10:12 +0000 | [diff] [blame] | 205 | static u_char *ngx_http_log_connection(ngx_http_request_t *r, u_char *buf, |
Igor Sysoev | 74e95c2 | 2003-11-09 20:03:38 +0000 | [diff] [blame] | 206 | uintptr_t data) |
| 207 | { |
Igor Sysoev | 2f65722 | 2004-06-16 15:32:11 +0000 | [diff] [blame] | 208 | return buf + ngx_snprintf((char *) buf, NGX_INT_T_LEN + 1, |
| 209 | "%" NGX_UINT_T_FMT, |
Igor Sysoev | 74e95c2 | 2003-11-09 20:03:38 +0000 | [diff] [blame] | 210 | r->connection->number); |
| 211 | } |
| 212 | |
| 213 | |
Igor Sysoev | 10a543a | 2004-03-16 07:10:12 +0000 | [diff] [blame] | 214 | static u_char *ngx_http_log_pipe(ngx_http_request_t *r, u_char *buf, |
| 215 | uintptr_t data) |
Igor Sysoev | 74e95c2 | 2003-11-09 20:03:38 +0000 | [diff] [blame] | 216 | { |
| 217 | if (r->pipeline) { |
| 218 | *buf = 'p'; |
| 219 | } else { |
| 220 | *buf = '.'; |
| 221 | } |
| 222 | |
| 223 | return buf + 1; |
| 224 | } |
| 225 | |
| 226 | |
Igor Sysoev | 10a543a | 2004-03-16 07:10:12 +0000 | [diff] [blame] | 227 | static u_char *ngx_http_log_time(ngx_http_request_t *r, u_char *buf, |
| 228 | uintptr_t data) |
Igor Sysoev | 74e95c2 | 2003-11-09 20:03:38 +0000 | [diff] [blame] | 229 | { |
Igor Sysoev | d59a047 | 2003-11-10 21:09:22 +0000 | [diff] [blame] | 230 | return ngx_cpymem(buf, ngx_cached_http_log_time.data, |
| 231 | ngx_cached_http_log_time.len); |
Igor Sysoev | 74e95c2 | 2003-11-09 20:03:38 +0000 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | |
Igor Sysoev | a7c4a2a | 2004-08-29 03:55:41 +0000 | [diff] [blame] | 235 | static u_char *ngx_http_log_msec(ngx_http_request_t *r, u_char *buf, |
| 236 | uintptr_t data) |
| 237 | { |
| 238 | struct timeval tv; |
| 239 | |
| 240 | ngx_gettimeofday(&tv); |
| 241 | |
| 242 | return buf + ngx_snprintf((char *) buf, TIME_T_LEN + 5, "%ld.%03ld", |
| 243 | tv.tv_sec, tv.tv_usec / 1000); |
| 244 | } |
| 245 | |
| 246 | |
Igor Sysoev | 10a543a | 2004-03-16 07:10:12 +0000 | [diff] [blame] | 247 | static u_char *ngx_http_log_request(ngx_http_request_t *r, u_char *buf, |
| 248 | uintptr_t data) |
Igor Sysoev | 74e95c2 | 2003-11-09 20:03:38 +0000 | [diff] [blame] | 249 | { |
| 250 | if (buf == NULL) { |
| 251 | /* find the request line length */ |
Igor Sysoev | 10a543a | 2004-03-16 07:10:12 +0000 | [diff] [blame] | 252 | return (u_char *) r->request_line.len; |
Igor Sysoev | 74e95c2 | 2003-11-09 20:03:38 +0000 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | return ngx_cpymem(buf, r->request_line.data, r->request_line.len); |
| 256 | } |
| 257 | |
| 258 | |
Igor Sysoev | 10a543a | 2004-03-16 07:10:12 +0000 | [diff] [blame] | 259 | static u_char *ngx_http_log_status(ngx_http_request_t *r, u_char *buf, |
| 260 | uintptr_t data) |
Igor Sysoev | 74e95c2 | 2003-11-09 20:03:38 +0000 | [diff] [blame] | 261 | { |
Igor Sysoev | 2f65722 | 2004-06-16 15:32:11 +0000 | [diff] [blame] | 262 | return buf + ngx_snprintf((char *) buf, 4, "%" NGX_UINT_T_FMT, |
Igor Sysoev | d59a047 | 2003-11-10 21:09:22 +0000 | [diff] [blame] | 263 | r->err_status ? r->err_status : r->headers_out.status); |
Igor Sysoev | 74e95c2 | 2003-11-09 20:03:38 +0000 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | |
Igor Sysoev | 10a543a | 2004-03-16 07:10:12 +0000 | [diff] [blame] | 267 | static u_char *ngx_http_log_length(ngx_http_request_t *r, u_char *buf, |
| 268 | uintptr_t data) |
Igor Sysoev | 74e95c2 | 2003-11-09 20:03:38 +0000 | [diff] [blame] | 269 | { |
Igor Sysoev | 10a543a | 2004-03-16 07:10:12 +0000 | [diff] [blame] | 270 | return buf + ngx_snprintf((char *) buf, NGX_OFF_T_LEN + 1, OFF_T_FMT, |
Igor Sysoev | 74e95c2 | 2003-11-09 20:03:38 +0000 | [diff] [blame] | 271 | r->connection->sent); |
| 272 | } |
| 273 | |
| 274 | |
Igor Sysoev | 10a543a | 2004-03-16 07:10:12 +0000 | [diff] [blame] | 275 | static u_char *ngx_http_log_apache_length(ngx_http_request_t *r, u_char *buf, |
| 276 | uintptr_t data) |
Igor Sysoev | dc867cd | 2003-12-14 20:10:27 +0000 | [diff] [blame] | 277 | { |
Igor Sysoev | 10a543a | 2004-03-16 07:10:12 +0000 | [diff] [blame] | 278 | return buf + ngx_snprintf((char *) buf, NGX_OFF_T_LEN + 1, OFF_T_FMT, |
Igor Sysoev | dc867cd | 2003-12-14 20:10:27 +0000 | [diff] [blame] | 279 | r->connection->sent - r->header_size); |
| 280 | } |
| 281 | |
| 282 | |
Igor Sysoev | 10a543a | 2004-03-16 07:10:12 +0000 | [diff] [blame] | 283 | static u_char *ngx_http_log_header_in(ngx_http_request_t *r, u_char *buf, |
| 284 | uintptr_t data) |
Igor Sysoev | 74e95c2 | 2003-11-09 20:03:38 +0000 | [diff] [blame] | 285 | { |
Igor Sysoev | 10a543a | 2004-03-16 07:10:12 +0000 | [diff] [blame] | 286 | ngx_uint_t i; |
Igor Sysoev | 74e95c2 | 2003-11-09 20:03:38 +0000 | [diff] [blame] | 287 | ngx_str_t *s; |
| 288 | ngx_table_elt_t *h; |
| 289 | ngx_http_log_op_t *op; |
| 290 | |
| 291 | if (r) { |
| 292 | h = *(ngx_table_elt_t **) ((char *) &r->headers_in + data); |
| 293 | |
| 294 | if (h == NULL) { |
| 295 | |
| 296 | /* no header */ |
| 297 | |
| 298 | if (buf) { |
| 299 | *buf = '-'; |
| 300 | } |
| 301 | |
| 302 | return buf + 1; |
| 303 | } |
| 304 | |
| 305 | if (buf == NULL) { |
| 306 | /* find the header length */ |
Igor Sysoev | 10a543a | 2004-03-16 07:10:12 +0000 | [diff] [blame] | 307 | return (u_char *) h->value.len; |
Igor Sysoev | 74e95c2 | 2003-11-09 20:03:38 +0000 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | return ngx_cpymem(buf, h->value.data, h->value.len); |
| 311 | } |
| 312 | |
| 313 | /* find an offset while a format string compilation */ |
| 314 | |
| 315 | op = (ngx_http_log_op_t *) buf; |
| 316 | s = (ngx_str_t *) data; |
| 317 | |
| 318 | op->len = 0; |
| 319 | |
| 320 | for (i = 0; ngx_http_headers_in[i].name.len != 0; i++) { |
| 321 | if (ngx_http_headers_in[i].name.len != s->len) { |
| 322 | continue; |
| 323 | } |
| 324 | |
| 325 | if (ngx_strncasecmp(ngx_http_headers_in[i].name.data, s->data, s->len) |
| 326 | == 0) |
| 327 | { |
| 328 | op->op = ngx_http_log_header_in; |
| 329 | op->data = ngx_http_headers_in[i].offset; |
| 330 | return NULL; |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | op->op = ngx_http_log_unknown_header_in; |
| 335 | op->data = (uintptr_t) s; |
| 336 | |
| 337 | return NULL; |
| 338 | } |
| 339 | |
| 340 | |
Igor Sysoev | 10a543a | 2004-03-16 07:10:12 +0000 | [diff] [blame] | 341 | static u_char *ngx_http_log_unknown_header_in(ngx_http_request_t *r, |
| 342 | u_char *buf, uintptr_t data) |
Igor Sysoev | 74e95c2 | 2003-11-09 20:03:38 +0000 | [diff] [blame] | 343 | { |
Igor Sysoev | 10a543a | 2004-03-16 07:10:12 +0000 | [diff] [blame] | 344 | ngx_uint_t i; |
Igor Sysoev | 74e95c2 | 2003-11-09 20:03:38 +0000 | [diff] [blame] | 345 | ngx_str_t *s; |
Igor Sysoev | 980a924 | 2004-09-05 19:54:02 +0000 | [diff] [blame] | 346 | ngx_list_part_t *part; |
Igor Sysoev | 74e95c2 | 2003-11-09 20:03:38 +0000 | [diff] [blame] | 347 | ngx_table_elt_t *h; |
| 348 | |
| 349 | s = (ngx_str_t *) data; |
| 350 | |
Igor Sysoev | 980a924 | 2004-09-05 19:54:02 +0000 | [diff] [blame] | 351 | part = &r->headers_in.headers.part; |
| 352 | h = part->elts; |
| 353 | |
| 354 | for (i = 0; /* void */; i++) { |
| 355 | |
| 356 | if (i >= part->nelts) { |
| 357 | if (part->next == NULL) { |
| 358 | break; |
| 359 | } |
| 360 | |
| 361 | part = part->next; |
| 362 | h = part->elts; |
| 363 | i = 0; |
| 364 | } |
| 365 | |
Igor Sysoev | 74e95c2 | 2003-11-09 20:03:38 +0000 | [diff] [blame] | 366 | if (h[i].key.len != s->len) { |
| 367 | continue; |
| 368 | } |
| 369 | |
| 370 | if (ngx_strncasecmp(h[i].key.data, s->data, s->len) == 0) { |
| 371 | if (buf == NULL) { |
| 372 | /* find the header length */ |
Igor Sysoev | 10a543a | 2004-03-16 07:10:12 +0000 | [diff] [blame] | 373 | return (u_char *) h[i].value.len; |
Igor Sysoev | 74e95c2 | 2003-11-09 20:03:38 +0000 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | return ngx_cpymem(buf, h[i].value.data, h[i].value.len); |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | /* no header */ |
| 381 | |
| 382 | if (buf) { |
| 383 | *buf = '-'; |
| 384 | } |
| 385 | |
| 386 | return buf + 1; |
| 387 | } |
| 388 | |
| 389 | |
Igor Sysoev | 10a543a | 2004-03-16 07:10:12 +0000 | [diff] [blame] | 390 | static u_char *ngx_http_log_header_out(ngx_http_request_t *r, u_char *buf, |
| 391 | uintptr_t data) |
Igor Sysoev | 74e95c2 | 2003-11-09 20:03:38 +0000 | [diff] [blame] | 392 | { |
Igor Sysoev | 10a543a | 2004-03-16 07:10:12 +0000 | [diff] [blame] | 393 | ngx_uint_t i; |
Igor Sysoev | 74e95c2 | 2003-11-09 20:03:38 +0000 | [diff] [blame] | 394 | ngx_str_t *s; |
| 395 | ngx_table_elt_t *h; |
| 396 | ngx_http_log_op_t *op; |
| 397 | |
| 398 | if (r) { |
Igor Sysoev | 5f80078 | 2003-12-08 20:48:12 +0000 | [diff] [blame] | 399 | |
| 400 | /* run-time execution */ |
| 401 | |
| 402 | if (r->http_version < NGX_HTTP_VERSION_10) { |
| 403 | if (buf) { |
| 404 | *buf = '-'; |
| 405 | } |
| 406 | |
| 407 | return buf + 1; |
| 408 | } |
| 409 | |
Igor Sysoev | 74e95c2 | 2003-11-09 20:03:38 +0000 | [diff] [blame] | 410 | h = *(ngx_table_elt_t **) ((char *) &r->headers_out + data); |
| 411 | |
| 412 | if (h == NULL) { |
| 413 | |
Igor Sysoev | 5f80078 | 2003-12-08 20:48:12 +0000 | [diff] [blame] | 414 | /* |
| 415 | * No header pointer was found. |
| 416 | * However, some headers: "Date", "Server", "Content-Length", |
| 417 | * and "Last-Modified" have a special handling in the header filter |
| 418 | * but we do not set up their pointers in the filter because |
| 419 | * they are too seldom needed to be logged. |
| 420 | */ |
| 421 | |
| 422 | if (data == offsetof(ngx_http_headers_out_t, date)) { |
| 423 | if (buf == NULL) { |
Igor Sysoev | 10a543a | 2004-03-16 07:10:12 +0000 | [diff] [blame] | 424 | return (u_char *) ngx_cached_http_time.len; |
Igor Sysoev | 5f80078 | 2003-12-08 20:48:12 +0000 | [diff] [blame] | 425 | } |
| 426 | return ngx_cpymem(buf, ngx_cached_http_time.data, |
| 427 | ngx_cached_http_time.len); |
| 428 | } |
Igor Sysoev | 74e95c2 | 2003-11-09 20:03:38 +0000 | [diff] [blame] | 429 | |
| 430 | if (data == offsetof(ngx_http_headers_out_t, server)) { |
| 431 | if (buf == NULL) { |
Igor Sysoev | 10a543a | 2004-03-16 07:10:12 +0000 | [diff] [blame] | 432 | return (u_char *) (sizeof(NGINX_VER) - 1); |
Igor Sysoev | 74e95c2 | 2003-11-09 20:03:38 +0000 | [diff] [blame] | 433 | } |
| 434 | return ngx_cpymem(buf, NGINX_VER, sizeof(NGINX_VER) - 1); |
| 435 | } |
| 436 | |
Igor Sysoev | 5f80078 | 2003-12-08 20:48:12 +0000 | [diff] [blame] | 437 | if (data == offsetof(ngx_http_headers_out_t, content_length)) { |
| 438 | if (r->headers_out.content_length_n == -1) { |
| 439 | if (buf) { |
| 440 | *buf = '-'; |
| 441 | } |
| 442 | return buf + 1; |
| 443 | } |
| 444 | |
| 445 | if (buf == NULL) { |
Igor Sysoev | 10a543a | 2004-03-16 07:10:12 +0000 | [diff] [blame] | 446 | return (u_char *) NGX_OFF_T_LEN; |
Igor Sysoev | 5f80078 | 2003-12-08 20:48:12 +0000 | [diff] [blame] | 447 | } |
Igor Sysoev | 10a543a | 2004-03-16 07:10:12 +0000 | [diff] [blame] | 448 | return buf + ngx_snprintf((char *) buf, |
| 449 | NGX_OFF_T_LEN + 2, OFF_T_FMT, |
Igor Sysoev | 5f80078 | 2003-12-08 20:48:12 +0000 | [diff] [blame] | 450 | r->headers_out.content_length_n); |
| 451 | } |
| 452 | |
| 453 | if (data == offsetof(ngx_http_headers_out_t, last_modified)) { |
| 454 | if (r->headers_out.last_modified_time == -1) { |
| 455 | if (buf) { |
| 456 | *buf = '-'; |
| 457 | } |
| 458 | return buf + 1; |
| 459 | } |
| 460 | |
| 461 | if (buf == NULL) { |
Igor Sysoev | 10a543a | 2004-03-16 07:10:12 +0000 | [diff] [blame] | 462 | return (u_char *) |
| 463 | sizeof("Mon, 28 Sep 1970 06:00:00 GMT") - 1; |
Igor Sysoev | 5f80078 | 2003-12-08 20:48:12 +0000 | [diff] [blame] | 464 | } |
| 465 | return buf + ngx_http_time(buf, |
| 466 | r->headers_out.last_modified_time); |
| 467 | } |
| 468 | |
Igor Sysoev | 74e95c2 | 2003-11-09 20:03:38 +0000 | [diff] [blame] | 469 | if (buf) { |
| 470 | *buf = '-'; |
| 471 | } |
| 472 | |
| 473 | return buf + 1; |
| 474 | } |
| 475 | |
| 476 | if (buf == NULL) { |
| 477 | /* find the header length */ |
Igor Sysoev | 10a543a | 2004-03-16 07:10:12 +0000 | [diff] [blame] | 478 | return (u_char *) h->value.len; |
Igor Sysoev | 74e95c2 | 2003-11-09 20:03:38 +0000 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | return ngx_cpymem(buf, h->value.data, h->value.len); |
| 482 | } |
| 483 | |
| 484 | /* find an offset while a format string compilation */ |
| 485 | |
| 486 | op = (ngx_http_log_op_t *) buf; |
| 487 | s = (ngx_str_t *) data; |
| 488 | |
| 489 | op->len = 0; |
| 490 | |
| 491 | for (i = 0; ngx_http_headers_out[i].name.len != 0; i++) { |
| 492 | if (ngx_http_headers_out[i].name.len != s->len) { |
| 493 | continue; |
| 494 | } |
| 495 | |
| 496 | if (ngx_strncasecmp(ngx_http_headers_out[i].name.data, s->data, s->len) |
| 497 | == 0) |
| 498 | { |
| 499 | op->op = ngx_http_log_header_out; |
| 500 | op->data = ngx_http_headers_out[i].offset; |
| 501 | return NULL; |
| 502 | } |
| 503 | } |
| 504 | |
Igor Sysoev | 5f80078 | 2003-12-08 20:48:12 +0000 | [diff] [blame] | 505 | if (s->len == sizeof("Connection") - 1 |
| 506 | && ngx_strncasecmp(s->data, "Connection", s->len) == 0) |
| 507 | { |
| 508 | op->op = ngx_http_log_connection_header_out; |
Igor Sysoev | 3b30a90 | 2003-12-25 20:26:58 +0000 | [diff] [blame] | 509 | op->data = (uintptr_t) NULL; |
Igor Sysoev | 5f80078 | 2003-12-08 20:48:12 +0000 | [diff] [blame] | 510 | return NULL; |
| 511 | } |
| 512 | |
| 513 | if (s->len == sizeof("Transfer-Encoding") - 1 |
| 514 | && ngx_strncasecmp(s->data, "Transfer-Encoding", s->len) == 0) { |
| 515 | op->op = ngx_http_log_transfer_encoding_header_out; |
Igor Sysoev | 3b30a90 | 2003-12-25 20:26:58 +0000 | [diff] [blame] | 516 | op->data = (uintptr_t) NULL; |
Igor Sysoev | 5f80078 | 2003-12-08 20:48:12 +0000 | [diff] [blame] | 517 | return NULL; |
| 518 | } |
| 519 | |
Igor Sysoev | 74e95c2 | 2003-11-09 20:03:38 +0000 | [diff] [blame] | 520 | op->op = ngx_http_log_unknown_header_out; |
| 521 | op->data = (uintptr_t) s; |
| 522 | |
| 523 | return NULL; |
| 524 | } |
| 525 | |
| 526 | |
Igor Sysoev | 10a543a | 2004-03-16 07:10:12 +0000 | [diff] [blame] | 527 | static u_char *ngx_http_log_connection_header_out(ngx_http_request_t *r, |
| 528 | u_char *buf, uintptr_t data) |
Igor Sysoev | 5f80078 | 2003-12-08 20:48:12 +0000 | [diff] [blame] | 529 | { |
| 530 | if (buf == NULL) { |
Igor Sysoev | 10a543a | 2004-03-16 07:10:12 +0000 | [diff] [blame] | 531 | return (u_char *) ((r->keepalive) ? sizeof("keep-alive") - 1: |
| 532 | sizeof("close") - 1); |
Igor Sysoev | 5f80078 | 2003-12-08 20:48:12 +0000 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | if (r->keepalive) { |
| 536 | return ngx_cpymem(buf, "keep-alive", sizeof("keep-alive") - 1); |
| 537 | |
| 538 | } else { |
| 539 | return ngx_cpymem(buf, "close", sizeof("close") - 1); |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | |
Igor Sysoev | 10a543a | 2004-03-16 07:10:12 +0000 | [diff] [blame] | 544 | static u_char *ngx_http_log_transfer_encoding_header_out(ngx_http_request_t *r, |
| 545 | u_char *buf, |
| 546 | uintptr_t data) |
Igor Sysoev | 5f80078 | 2003-12-08 20:48:12 +0000 | [diff] [blame] | 547 | { |
| 548 | if (buf == NULL) { |
Igor Sysoev | 10a543a | 2004-03-16 07:10:12 +0000 | [diff] [blame] | 549 | return (u_char *) ((r->chunked) ? sizeof("chunked") - 1 : 1); |
Igor Sysoev | 5f80078 | 2003-12-08 20:48:12 +0000 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | if (r->chunked) { |
| 553 | return ngx_cpymem(buf, "chunked", sizeof("chunked") - 1); |
| 554 | } |
| 555 | |
| 556 | *buf = '-'; |
| 557 | |
| 558 | return buf + 1; |
| 559 | } |
| 560 | |
| 561 | |
Igor Sysoev | 10a543a | 2004-03-16 07:10:12 +0000 | [diff] [blame] | 562 | static u_char *ngx_http_log_unknown_header_out(ngx_http_request_t *r, |
| 563 | u_char *buf, |
| 564 | uintptr_t data) |
Igor Sysoev | 74e95c2 | 2003-11-09 20:03:38 +0000 | [diff] [blame] | 565 | { |
Igor Sysoev | 10a543a | 2004-03-16 07:10:12 +0000 | [diff] [blame] | 566 | ngx_uint_t i; |
Igor Sysoev | 74e95c2 | 2003-11-09 20:03:38 +0000 | [diff] [blame] | 567 | ngx_str_t *s; |
Igor Sysoev | 980a924 | 2004-09-05 19:54:02 +0000 | [diff] [blame] | 568 | ngx_list_part_t *part; |
Igor Sysoev | 74e95c2 | 2003-11-09 20:03:38 +0000 | [diff] [blame] | 569 | ngx_table_elt_t *h; |
| 570 | |
| 571 | s = (ngx_str_t *) data; |
| 572 | |
Igor Sysoev | 980a924 | 2004-09-05 19:54:02 +0000 | [diff] [blame] | 573 | part = &r->headers_out.headers.part; |
| 574 | h = part->elts; |
| 575 | |
| 576 | for (i = 0; /* void */; i++) { |
| 577 | |
| 578 | if (i >= part->nelts) { |
| 579 | if (part->next == NULL) { |
| 580 | break; |
| 581 | } |
| 582 | |
| 583 | part = part->next; |
| 584 | h = part->elts; |
| 585 | i = 0; |
| 586 | } |
| 587 | |
Igor Sysoev | 74e95c2 | 2003-11-09 20:03:38 +0000 | [diff] [blame] | 588 | if (h[i].key.len != s->len) { |
| 589 | continue; |
| 590 | } |
| 591 | |
| 592 | if (ngx_strncasecmp(h[i].key.data, s->data, s->len) == 0) { |
| 593 | if (buf == NULL) { |
| 594 | /* find the header length */ |
Igor Sysoev | 10a543a | 2004-03-16 07:10:12 +0000 | [diff] [blame] | 595 | return (u_char *) h[i].value.len; |
Igor Sysoev | 74e95c2 | 2003-11-09 20:03:38 +0000 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | return ngx_cpymem(buf, h[i].value.data, h[i].value.len); |
| 599 | } |
| 600 | } |
| 601 | |
| 602 | /* no header */ |
| 603 | |
| 604 | if (buf) { |
| 605 | *buf = '-'; |
| 606 | } |
| 607 | |
| 608 | return buf + 1; |
| 609 | } |
| 610 | |
| 611 | |
Igor Sysoev | 2f65722 | 2004-06-16 15:32:11 +0000 | [diff] [blame] | 612 | static ngx_int_t ngx_http_log_pre_conf(ngx_conf_t *cf) |
Igor Sysoev | 7832933 | 2003-11-10 17:17:31 +0000 | [diff] [blame] | 613 | { |
| 614 | ngx_http_log_op_name_t *op; |
| 615 | |
| 616 | for (op = ngx_http_log_fmt_ops; op->name.len; op++) { /* void */ } |
| 617 | op->op = NULL; |
| 618 | |
| 619 | return NGX_OK; |
| 620 | } |
| 621 | |
| 622 | |
Igor Sysoev | 74e95c2 | 2003-11-09 20:03:38 +0000 | [diff] [blame] | 623 | static void *ngx_http_log_create_main_conf(ngx_conf_t *cf) |
| 624 | { |
| 625 | ngx_http_log_main_conf_t *conf; |
| 626 | |
| 627 | char *rc; |
| 628 | ngx_str_t *value; |
| 629 | |
| 630 | if (!(conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_log_main_conf_t)))) { |
| 631 | return NGX_CONF_ERROR; |
| 632 | } |
| 633 | |
| 634 | ngx_init_array(conf->formats, cf->pool, 5, sizeof(ngx_http_log_fmt_t), |
| 635 | NGX_CONF_ERROR); |
| 636 | |
| 637 | cf->args->nelts = 0; |
| 638 | |
| 639 | if (!(value = ngx_push_array(cf->args))) { |
| 640 | return NGX_CONF_ERROR; |
| 641 | } |
| 642 | |
| 643 | if (!(value = ngx_push_array(cf->args))) { |
| 644 | return NGX_CONF_ERROR; |
| 645 | } |
| 646 | |
| 647 | value->len = sizeof("combined") - 1; |
Igor Sysoev | da85f7f | 2004-03-16 21:26:01 +0000 | [diff] [blame] | 648 | value->data = (u_char *) "combined"; |
Igor Sysoev | 74e95c2 | 2003-11-09 20:03:38 +0000 | [diff] [blame] | 649 | |
| 650 | if (!(value = ngx_push_array(cf->args))) { |
| 651 | return NGX_CONF_ERROR; |
| 652 | } |
| 653 | |
| 654 | *value = ngx_http_combined_fmt; |
| 655 | |
| 656 | rc = ngx_http_log_set_format(cf, NULL, conf); |
| 657 | if (rc != NGX_CONF_OK) { |
Igor Sysoev | 7832933 | 2003-11-10 17:17:31 +0000 | [diff] [blame] | 658 | return NULL; |
Igor Sysoev | 74e95c2 | 2003-11-09 20:03:38 +0000 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | return conf; |
| 662 | } |
| 663 | |
| 664 | |
| 665 | static void *ngx_http_log_create_loc_conf(ngx_conf_t *cf) |
| 666 | { |
| 667 | ngx_http_log_loc_conf_t *conf; |
| 668 | |
Igor Sysoev | 85cccfb | 2004-09-15 16:00:43 +0000 | [diff] [blame^] | 669 | if (!(conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_log_loc_conf_t)))) { |
| 670 | return NGX_CONF_ERROR; |
| 671 | } |
Igor Sysoev | 74e95c2 | 2003-11-09 20:03:38 +0000 | [diff] [blame] | 672 | |
| 673 | return conf; |
| 674 | } |
| 675 | |
| 676 | |
| 677 | static char *ngx_http_log_merge_loc_conf(ngx_conf_t *cf, void *parent, |
| 678 | void *child) |
| 679 | { |
| 680 | ngx_http_log_loc_conf_t *prev = parent; |
| 681 | ngx_http_log_loc_conf_t *conf = child; |
| 682 | |
| 683 | ngx_http_log_t *log; |
| 684 | ngx_http_log_fmt_t *fmt; |
| 685 | ngx_http_log_main_conf_t *lmcf; |
| 686 | |
| 687 | if (conf->logs == NULL) { |
Igor Sysoev | 85cccfb | 2004-09-15 16:00:43 +0000 | [diff] [blame^] | 688 | |
| 689 | if (conf->off) { |
| 690 | return NGX_CONF_OK; |
| 691 | } |
| 692 | |
Igor Sysoev | 74e95c2 | 2003-11-09 20:03:38 +0000 | [diff] [blame] | 693 | if (prev->logs) { |
| 694 | conf->logs = prev->logs; |
| 695 | |
| 696 | } else { |
| 697 | |
Igor Sysoev | 85cccfb | 2004-09-15 16:00:43 +0000 | [diff] [blame^] | 698 | if (prev->off) { |
| 699 | conf->off = prev->off; |
| 700 | return NGX_CONF_OK; |
| 701 | } |
| 702 | |
| 703 | conf->logs = ngx_array_create(cf->pool, 2, sizeof(ngx_http_log_t)); |
Igor Sysoev | 74e95c2 | 2003-11-09 20:03:38 +0000 | [diff] [blame] | 704 | if (conf->logs == NULL) { |
| 705 | return NGX_CONF_ERROR; |
| 706 | } |
| 707 | |
Igor Sysoev | 85cccfb | 2004-09-15 16:00:43 +0000 | [diff] [blame^] | 708 | if (!(log = ngx_array_push(conf->logs))) { |
Igor Sysoev | 74e95c2 | 2003-11-09 20:03:38 +0000 | [diff] [blame] | 709 | return NGX_CONF_ERROR; |
| 710 | } |
| 711 | |
| 712 | log->file = ngx_conf_open_file(cf->cycle, &http_access_log); |
| 713 | if (log->file == NULL) { |
| 714 | return NGX_CONF_ERROR; |
| 715 | } |
| 716 | |
Igor Sysoev | 1c3567e | 2004-07-15 16:35:51 +0000 | [diff] [blame] | 717 | lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_log_module); |
Igor Sysoev | 74e95c2 | 2003-11-09 20:03:38 +0000 | [diff] [blame] | 718 | fmt = lmcf->formats.elts; |
Igor Sysoev | 85cccfb | 2004-09-15 16:00:43 +0000 | [diff] [blame^] | 719 | |
Igor Sysoev | 74e95c2 | 2003-11-09 20:03:38 +0000 | [diff] [blame] | 720 | /* the default "combined" format */ |
| 721 | log->ops = fmt[0].ops; |
| 722 | } |
| 723 | } |
| 724 | |
| 725 | return NGX_CONF_OK; |
| 726 | } |
| 727 | |
| 728 | |
| 729 | static char *ngx_http_log_set_log(ngx_conf_t *cf, ngx_command_t *cmd, |
| 730 | void *conf) |
| 731 | { |
| 732 | ngx_http_log_loc_conf_t *llcf = conf; |
| 733 | |
Igor Sysoev | 10a543a | 2004-03-16 07:10:12 +0000 | [diff] [blame] | 734 | ngx_uint_t i; |
Igor Sysoev | 74e95c2 | 2003-11-09 20:03:38 +0000 | [diff] [blame] | 735 | ngx_str_t *value, name; |
| 736 | ngx_http_log_t *log; |
| 737 | ngx_http_log_fmt_t *fmt; |
| 738 | ngx_http_log_main_conf_t *lmcf; |
| 739 | |
Igor Sysoev | 85cccfb | 2004-09-15 16:00:43 +0000 | [diff] [blame^] | 740 | value = cf->args->elts; |
| 741 | |
| 742 | if (ngx_strcmp(value[1].data, "off") == 0) { |
| 743 | llcf->off = 1; |
| 744 | return NGX_CONF_OK; |
| 745 | } |
| 746 | |
Igor Sysoev | 74e95c2 | 2003-11-09 20:03:38 +0000 | [diff] [blame] | 747 | if (llcf->logs == NULL) { |
Igor Sysoev | 85cccfb | 2004-09-15 16:00:43 +0000 | [diff] [blame^] | 748 | llcf->logs = ngx_array_create(cf->pool, 2, sizeof(ngx_http_log_t)); |
| 749 | if (llcf->logs == NULL) { |
Igor Sysoev | 74e95c2 | 2003-11-09 20:03:38 +0000 | [diff] [blame] | 750 | return NGX_CONF_ERROR; |
| 751 | } |
| 752 | } |
| 753 | |
Igor Sysoev | 1c3567e | 2004-07-15 16:35:51 +0000 | [diff] [blame] | 754 | lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_log_module); |
Igor Sysoev | 74e95c2 | 2003-11-09 20:03:38 +0000 | [diff] [blame] | 755 | |
Igor Sysoev | 85cccfb | 2004-09-15 16:00:43 +0000 | [diff] [blame^] | 756 | if (!(log = ngx_array_push(llcf->logs))) { |
Igor Sysoev | 74e95c2 | 2003-11-09 20:03:38 +0000 | [diff] [blame] | 757 | return NGX_CONF_ERROR; |
| 758 | } |
| 759 | |
| 760 | if (!(log->file = ngx_conf_open_file(cf->cycle, &value[1]))) { |
| 761 | return NGX_CONF_ERROR; |
| 762 | } |
| 763 | |
| 764 | if (cf->args->nelts == 3) { |
| 765 | name = value[2]; |
| 766 | } else { |
| 767 | name.len = sizeof("combined") - 1; |
Igor Sysoev | da85f7f | 2004-03-16 21:26:01 +0000 | [diff] [blame] | 768 | name.data = (u_char *) "combined"; |
Igor Sysoev | 74e95c2 | 2003-11-09 20:03:38 +0000 | [diff] [blame] | 769 | } |
| 770 | |
| 771 | fmt = lmcf->formats.elts; |
| 772 | for (i = 0; i < lmcf->formats.nelts; i++) { |
| 773 | if (fmt[i].name.len == name.len |
| 774 | && ngx_strcasecmp(fmt[i].name.data, name.data) == 0) |
| 775 | { |
| 776 | log->ops = fmt[i].ops; |
| 777 | return NGX_CONF_OK; |
| 778 | } |
| 779 | } |
| 780 | |
| 781 | return NGX_CONF_OK; |
| 782 | } |
| 783 | |
| 784 | |
| 785 | static char *ngx_http_log_set_format(ngx_conf_t *cf, ngx_command_t *cmd, |
| 786 | void *conf) |
| 787 | { |
| 788 | ngx_http_log_main_conf_t *lmcf = conf; |
| 789 | |
Igor Sysoev | 10a543a | 2004-03-16 07:10:12 +0000 | [diff] [blame] | 790 | ngx_uint_t s, f, invalid; |
| 791 | u_char *data, *p, *fname; |
Igor Sysoev | 74e95c2 | 2003-11-09 20:03:38 +0000 | [diff] [blame] | 792 | size_t i, len, fname_len; |
| 793 | ngx_str_t *value, arg, *a; |
| 794 | ngx_http_log_op_t *op; |
| 795 | ngx_http_log_fmt_t *fmt; |
| 796 | ngx_http_log_op_name_t *name; |
| 797 | |
| 798 | value = cf->args->elts; |
Igor Sysoev | 74e95c2 | 2003-11-09 20:03:38 +0000 | [diff] [blame] | 799 | |
| 800 | fmt = lmcf->formats.elts; |
| 801 | for (f = 0; f < lmcf->formats.nelts; f++) { |
| 802 | if (fmt[f].name.len == value[1].len |
| 803 | && ngx_strcmp(fmt->name.data, value[1].data) == 0) |
| 804 | { |
| 805 | return "duplicate \"log_format\" name"; |
| 806 | } |
| 807 | } |
| 808 | |
| 809 | if (!(fmt = ngx_push_array(&lmcf->formats))) { |
| 810 | return NGX_CONF_ERROR; |
| 811 | } |
| 812 | |
| 813 | fmt->name = value[1]; |
| 814 | |
| 815 | if (!(fmt->ops = ngx_create_array(cf->pool, 20, |
| 816 | sizeof(ngx_http_log_op_t)))) { |
| 817 | return NGX_CONF_ERROR; |
| 818 | } |
| 819 | |
| 820 | invalid = 0; |
| 821 | data = NULL; |
| 822 | |
| 823 | for (s = 2; s < cf->args->nelts && !invalid; s++) { |
| 824 | |
| 825 | i = 0; |
| 826 | |
| 827 | while (i < value[s].len) { |
| 828 | |
| 829 | if (!(op = ngx_push_array(fmt->ops))) { |
| 830 | return NGX_CONF_ERROR; |
| 831 | } |
| 832 | |
| 833 | data = &value[s].data[i]; |
| 834 | |
| 835 | if (value[s].data[i] == '%') { |
| 836 | i++; |
| 837 | |
| 838 | if (i == value[s].len) { |
| 839 | invalid = 1; |
| 840 | break; |
| 841 | } |
| 842 | |
| 843 | if (value[s].data[i] == '{') { |
| 844 | i++; |
| 845 | |
| 846 | arg.data = &value[s].data[i]; |
| 847 | |
| 848 | while (i < value[s].len && value[s].data[i] != '}') { |
| 849 | i++; |
| 850 | } |
| 851 | |
| 852 | arg.len = &value[s].data[i] - arg.data; |
| 853 | |
| 854 | if (i == value[s].len || arg.len == 0) { |
| 855 | invalid = 1; |
| 856 | break; |
| 857 | } |
| 858 | |
| 859 | i++; |
| 860 | |
| 861 | } else { |
| 862 | arg.len = 0; |
| 863 | } |
| 864 | |
| 865 | fname = &value[s].data[i]; |
| 866 | |
| 867 | while (i < value[s].len |
Igor Sysoev | 7832933 | 2003-11-10 17:17:31 +0000 | [diff] [blame] | 868 | && ((value[s].data[i] >= 'a' && value[s].data[i] <= 'z') |
| 869 | || value[s].data[i] == '_')) |
Igor Sysoev | 74e95c2 | 2003-11-09 20:03:38 +0000 | [diff] [blame] | 870 | { |
| 871 | i++; |
| 872 | } |
| 873 | |
| 874 | fname_len = &value[s].data[i] - fname; |
| 875 | |
| 876 | if (fname_len == 0) { |
| 877 | invalid = 1; |
| 878 | break; |
| 879 | } |
| 880 | |
Igor Sysoev | 7832933 | 2003-11-10 17:17:31 +0000 | [diff] [blame] | 881 | for (name = ngx_http_log_fmt_ops; name->op; name++) { |
| 882 | if (name->name.len == 0) { |
| 883 | name = (ngx_http_log_op_name_t *) name->op; |
| 884 | } |
| 885 | |
Igor Sysoev | 74e95c2 | 2003-11-09 20:03:38 +0000 | [diff] [blame] | 886 | if (name->name.len == fname_len |
| 887 | && ngx_strncmp(name->name.data, fname, fname_len) == 0) |
| 888 | { |
| 889 | if (name->len != NGX_HTTP_LOG_ARG) { |
| 890 | if (arg.len) { |
| 891 | fname[fname_len] = '\0'; |
| 892 | ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
| 893 | "\"%s\" must not have argument", |
| 894 | data); |
| 895 | return NGX_CONF_ERROR; |
| 896 | } |
| 897 | |
| 898 | op->len = name->len; |
| 899 | op->op = name->op; |
| 900 | op->data = 0; |
| 901 | |
| 902 | break; |
| 903 | } |
| 904 | |
| 905 | if (arg.len == 0) { |
| 906 | fname[fname_len] = '\0'; |
| 907 | ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
| 908 | "\"%s\" requires argument", |
| 909 | data); |
| 910 | return NGX_CONF_ERROR; |
| 911 | } |
| 912 | |
| 913 | if (!(a = ngx_palloc(cf->pool, sizeof(ngx_str_t)))) { |
| 914 | return NGX_CONF_ERROR; |
| 915 | } |
| 916 | |
| 917 | *a = arg; |
Igor Sysoev | 10a543a | 2004-03-16 07:10:12 +0000 | [diff] [blame] | 918 | name->op(NULL, (u_char *) op, (uintptr_t) a); |
Igor Sysoev | 74e95c2 | 2003-11-09 20:03:38 +0000 | [diff] [blame] | 919 | |
| 920 | break; |
| 921 | } |
| 922 | } |
| 923 | |
| 924 | if (name->name.len == 0) { |
| 925 | invalid = 1; |
| 926 | break; |
| 927 | } |
| 928 | |
| 929 | } else { |
| 930 | i++; |
| 931 | |
| 932 | while (i < value[s].len && value[s].data[i] != '%') { |
| 933 | i++; |
| 934 | } |
| 935 | |
| 936 | len = &value[s].data[i] - data; |
| 937 | |
| 938 | if (len) { |
| 939 | |
| 940 | op->len = len; |
| 941 | |
| 942 | if (len <= sizeof(uintptr_t)) { |
| 943 | op->op = NGX_HTTP_LOG_COPY_SHORT; |
| 944 | op->data = 0; |
| 945 | |
| 946 | while (len--) { |
| 947 | op->data <<= 8; |
| 948 | op->data |= data[len]; |
| 949 | } |
| 950 | |
| 951 | } else { |
| 952 | op->op = NGX_HTTP_LOG_COPY_LONG; |
| 953 | |
| 954 | if (!(p = ngx_palloc(cf->pool, len))) { |
| 955 | return NGX_CONF_ERROR; |
| 956 | } |
| 957 | |
| 958 | ngx_memcpy(p, data, len); |
| 959 | op->data = (uintptr_t) p; |
| 960 | } |
| 961 | } |
| 962 | } |
| 963 | } |
| 964 | } |
| 965 | |
| 966 | if (invalid) { |
| 967 | ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
| 968 | "invalid parameter \"%s\"", data); |
| 969 | return NGX_CONF_ERROR; |
| 970 | } |
| 971 | |
| 972 | return NGX_CONF_OK; |
| 973 | } |