diff src/core/ngx_conf_file.c @ 50:72eb30262aac NGINX_0_1_25

nginx 0.1.25 *) Bugfix: nginx did run on Linux parisc. *) Feature: nginx now does not start under FreeBSD if the sysctl kern.ipc.somaxconn value is too big. *) Bugfix: if a request was internally redirected by the ngx_http_index_module module to the ngx_http_proxy_module or ngx_http_fastcgi_module modules, then the index file was not closed after request completion. *) Feature: the "proxy_pass" can be used in location with regular expression. *) Feature: the ngx_http_rewrite_filter_module module supports the condition like "if ($HTTP_USER_AGENT ~ MSIE)". *) Bugfix: nginx started too slow if the large number of addresses and text values were used in the "geo" directive. *) Change: a variable name must be declared as "$name" in the "geo" directive. The previous variant without "$" is still supported, but will be removed soon. *) Feature: the "%{VARIABLE}v" logging parameter. *) Feature: the "set $name value" directive. *) Bugfix: gcc 4.0 compatibility. *) Feature: the --with-openssl-opt=OPTIONS autoconfiguration directive.
author Igor Sysoev <http://sysoev.ru>
date Sat, 19 Mar 2005 00:00:00 +0300
parents 6cfc63e68377
children 0d75d65c642f
line wrap: on
line diff
--- a/src/core/ngx_conf_file.c
+++ b/src/core/ngx_conf_file.c
@@ -8,6 +8,7 @@
 #include <ngx_core.h>
 
 
+static ngx_int_t ngx_conf_handler(ngx_conf_t *cf, ngx_int_t last);
 static char *ngx_conf_include(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
 
 
@@ -53,13 +54,10 @@ static ngx_int_t ngx_conf_read_token(ngx
 
 char *ngx_conf_parse(ngx_conf_t *cf, ngx_str_t *filename)
 {
-    int               m, rc, found, valid;
     char             *rv;
-    void             *conf, **confp;
     ngx_fd_t          fd;
-    ngx_str_t        *name;
+    ngx_int_t         rc;
     ngx_conf_file_t  *prev;
-    ngx_command_t    *cmd;
 
 #if (NGX_SUPPRESS_WARN)
     fd = NGX_INVALID_FILE;
@@ -78,7 +76,9 @@ char *ngx_conf_parse(ngx_conf_t *cf, ngx
         }
 
         prev = cf->conf_file;
-        if (!(cf->conf_file = ngx_palloc(cf->pool, sizeof(ngx_conf_file_t)))) {
+
+        cf->conf_file = ngx_palloc(cf->pool, sizeof(ngx_conf_file_t));
+        if (cf->conf_file == NULL) {
             return NGX_CONF_ERROR;
         }
 
@@ -130,194 +130,30 @@ char *ngx_conf_parse(ngx_conf_t *cf, ngx
             rv = (*cf->handler)(cf, NULL, cf->handler_conf);
             if (rv == NGX_CONF_OK) {
                 continue;
-
-            } else if (rv == NGX_CONF_ERROR) {
-                rc = NGX_ERROR;
-                break;
+            }
 
-            } else {
-                ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
-                             "%s in %s:%d",
-                             rv,
-                             cf->conf_file->file.name.data,
-                             cf->conf_file->line);
+            if (rv == NGX_CONF_ERROR) {
                 rc = NGX_ERROR;
                 break;
             }
-        }
 
-        name = (ngx_str_t *) cf->args->elts;
-        found = 0;
-
-        for (m = 0; rc != NGX_ERROR && !found && ngx_modules[m]; m++) {
-
-            /* look up the directive in the appropriate modules */
-
-            if (ngx_modules[m]->type != NGX_CONF_MODULE
-                && ngx_modules[m]->type != cf->module_type)
-            {
-                continue;
-            }
-
-            cmd = ngx_modules[m]->commands;
-            if (cmd == NULL) {
-                continue;
-            }
-
-            while (cmd->name.len) {
-                if (name->len == cmd->name.len
-                    && ngx_strcmp(name->data, cmd->name.data) == 0)
-                {
-
-                    found = 1;
-
-                    /* is the directive's location right ? */
-
-                    if ((cmd->type & cf->cmd_type) == 0) {
-                        ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
-                                      "directive \"%s\" in %s:%d "
-                                      "is not allowed here",
-                                      name->data,
-                                      cf->conf_file->file.name.data,
-                                      cf->conf_file->line);
-                        rc = NGX_ERROR;
-                        break;
-                    }
-
-                    if (!(cmd->type & NGX_CONF_BLOCK) && rc != NGX_OK)
-                    {
-                        ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
-                                      "directive \"%s\" in %s:%d "
-                                      "is not terminated by \";\"",
-                                      name->data,
-                                      cf->conf_file->file.name.data,
-                                      cf->conf_file->line);
-                        rc = NGX_ERROR;
-                        break;
-                    }
-
-                    if ((cmd->type & NGX_CONF_BLOCK)
-                        && rc != NGX_CONF_BLOCK_START)
-                    {
-                        ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
-                                      "directive \"%s\" in %s:%d "
-                                      "has not the opening \"{\"",
-                                      name->data,
-                                      cf->conf_file->file.name.data,
-                                      cf->conf_file->line);
-                        rc = NGX_ERROR;
-                        break;
-                    }
-
-                    /* is the directive's argument count right ? */
-
-                    if (cmd->type & NGX_CONF_ANY) {
-                        valid = 1;
-
-                    } else if (cmd->type & NGX_CONF_FLAG) {
-
-                        if (cf->args->nelts == 2) {
-                            valid = 1;
-                        } else {
-                            valid = 0;
-                        }
-
-                    } else if (cmd->type & NGX_CONF_1MORE) {
-
-                        if (cf->args->nelts > 1) {
-                            valid = 1;
-                        } else {
-                            valid = 0;
-                        }
-
-                    } else if (cmd->type & NGX_CONF_2MORE) {
-
-                        if (cf->args->nelts > 2) {
-                            valid = 1;
-                        } else {
-                            valid = 0;
-                        }
-
-                    } else if (cf->args->nelts <= 10
-                               && (cmd->type
-                                   & argument_number[cf->args->nelts - 1]))
-                    {
-                        valid = 1;
-
-                    } else {
-                        valid = 0;
-                    }
-
-                    if (!valid) {
-                        ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
-                                      "invalid number arguments in "
-                                      "directive \"%s\" in %s:%d",
-                                      name->data,
-                                      cf->conf_file->file.name.data,
-                                      cf->conf_file->line);
-                        rc = NGX_ERROR;
-                        break;
-                    }
-
-                    /* set up the directive's configuration context */
-
-                    conf = NULL;
-
-                    if (cmd->type & NGX_DIRECT_CONF) {
-                        conf = ((void **) cf->ctx)[ngx_modules[m]->index];
-
-                    } else if (cmd->type & NGX_MAIN_CONF) {
-                        conf = &(((void **) cf->ctx)[ngx_modules[m]->index]);
-
-                    } else if (cf->ctx) {
-                        confp = *(void **) ((char *) cf->ctx + cmd->conf);
-
-                        if (confp) {
-                            conf = confp[ngx_modules[m]->ctx_index];
-                        }
-                    }
-
-                    rv = cmd->set(cf, cmd, conf);
-
-                    if (rv == NGX_CONF_OK) {
-                        break;
-
-                    } else if (rv == NGX_CONF_ERROR) {
-                        rc = NGX_ERROR;
-                        break;
-
-                    } else {
-                        ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
-                                      "the \"%s\" directive %s in %s:%d",
-                                      name->data, rv,
-                                      cf->conf_file->file.name.data,
-                                      cf->conf_file->line);
-
-                        rc = NGX_ERROR;
-                        break;
-                    }
-                }
-
-                cmd++;
-            }
-        }
-
-        if (!found) {
             ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
-                          "unknown directive \"%s\" in %s:%d",
-                          name->data,
-                          cf->conf_file->file.name.data,
-                          cf->conf_file->line);
-
+                         "%s in %s:%d",
+                         rv, cf->conf_file->file.name.data,
+                         cf->conf_file->line);
             rc = NGX_ERROR;
             break;
         }
 
+
+        rc = ngx_conf_handler(cf, rc);
+
         if (rc == NGX_ERROR) {
             break;
         }
     }
 
+
     if (filename) {
         cf->conf_file = prev;
 
@@ -337,6 +173,164 @@ char *ngx_conf_parse(ngx_conf_t *cf, ngx
 }
 
 
+static ngx_int_t ngx_conf_handler(ngx_conf_t *cf, ngx_int_t last)
+{
+    char           *rv;
+    void           *conf, **confp;
+    ngx_uint_t      i, valid;
+    ngx_str_t      *name;
+    ngx_command_t  *cmd;
+
+    name = cf->args->elts;
+
+    for (i = 0; ngx_modules[i]; i++) {
+
+        /* look up the directive in the appropriate modules */
+
+        if (ngx_modules[i]->type != NGX_CONF_MODULE
+            && ngx_modules[i]->type != cf->module_type)
+        {
+            continue;
+        }
+
+        cmd = ngx_modules[i]->commands;
+        if (cmd == NULL) {
+            continue;
+        }
+
+        while (cmd->name.len) {
+
+            if (name->len == cmd->name.len
+                && ngx_strcmp(name->data, cmd->name.data) == 0)
+            {
+                /* is the directive's location right ? */
+
+                if (!(cmd->type & cf->cmd_type)) {
+                    ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
+                                  "directive \"%s\" in %s:%d "
+                                  "is not allowed here",
+                                  name->data, cf->conf_file->file.name.data,
+                                  cf->conf_file->line);
+                    return NGX_ERROR;
+                }
+
+                if (!(cmd->type & NGX_CONF_BLOCK) && last != NGX_OK) {
+                    ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
+                                  "directive \"%s\" in %s:%d "
+                                  "is not terminated by \";\"",
+                                  name->data, cf->conf_file->file.name.data,
+                                  cf->conf_file->line);
+                    return NGX_ERROR;
+                }
+
+                if ((cmd->type & NGX_CONF_BLOCK)
+                    && last != NGX_CONF_BLOCK_START)
+                {
+                    ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
+                                  "directive \"%s\" in %s:%d "
+                                  "has not the opening \"{\"",
+                                  name->data, cf->conf_file->file.name.data,
+                                  cf->conf_file->line);
+                    return NGX_ERROR;
+                }
+
+                /* is the directive's argument count right ? */
+
+                if (cmd->type & NGX_CONF_ANY) {
+                    valid = 1;
+
+                } else if (cmd->type & NGX_CONF_FLAG) {
+
+                    if (cf->args->nelts == 2) {
+                        valid = 1;
+                    } else {
+                        valid = 0;
+                    }
+
+                } else if (cmd->type & NGX_CONF_1MORE) {
+
+                    if (cf->args->nelts > 1) {
+                        valid = 1;
+                    } else {
+                        valid = 0;
+                    }
+
+                } else if (cmd->type & NGX_CONF_2MORE) {
+
+                    if (cf->args->nelts > 2) {
+                        valid = 1;
+                    } else {
+                        valid = 0;
+                    }
+
+                } else if (cf->args->nelts <= 10
+                           && (cmd->type
+                               & argument_number[cf->args->nelts - 1]))
+                {
+                    valid = 1;
+
+                } else {
+                    valid = 0;
+                }
+
+                if (!valid) {
+                    ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
+                                  "invalid number arguments in "
+                                  "directive \"%s\" in %s:%d",
+                                  name->data, cf->conf_file->file.name.data,
+                                  cf->conf_file->line);
+                    return NGX_ERROR;
+                }
+
+                /* set up the directive's configuration context */
+
+                conf = NULL;
+
+                if (cmd->type & NGX_DIRECT_CONF) {
+                    conf = ((void **) cf->ctx)[ngx_modules[i]->index];
+
+                } else if (cmd->type & NGX_MAIN_CONF) {
+                    conf = &(((void **) cf->ctx)[ngx_modules[i]->index]);
+
+                } else if (cf->ctx) {
+                    confp = *(void **) ((char *) cf->ctx + cmd->conf);
+
+                    if (confp) {
+                        conf = confp[ngx_modules[i]->ctx_index];
+                    }
+                }
+
+                rv = cmd->set(cf, cmd, conf);
+
+                if (rv == NGX_CONF_OK) {
+                    return NGX_OK;
+                }
+
+                if (rv == NGX_CONF_ERROR) {
+                    return NGX_ERROR;
+                }
+
+                ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
+                              "the \"%s\" directive %s in %s:%d",
+                              name->data, rv, cf->conf_file->file.name.data,
+                              cf->conf_file->line);
+
+                return NGX_ERROR;
+            }
+
+            cmd++;
+        }
+    }
+
+    ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
+                  "unknown directive \"%s\" in %s:%d",
+                  name->data, cf->conf_file->file.name.data,
+                  cf->conf_file->line);
+
+    return NGX_ERROR;
+}
+
+
 static ngx_int_t ngx_conf_read_token(ngx_conf_t *cf)
 {
     u_char      *start, ch, *src, *dst;
@@ -523,11 +517,13 @@ static ngx_int_t ngx_conf_read_token(ngx
             }
 
             if (found) {
-                if (!(word = ngx_push_array(cf->args))) {
+                word = ngx_array_push(cf->args);
+                if (word == NULL) {
                     return NGX_ERROR;
                 }
 
-                if (!(word->data = ngx_palloc(cf->pool, b->pos - start + 1))) {
+                word->data = ngx_palloc(cf->pool, b->pos - start + 1);
+                if (word->data == NULL) {
                     return NGX_ERROR;
                 }
 
@@ -623,7 +619,8 @@ ngx_int_t ngx_conf_full_name(ngx_cycle_t
     name->len = cycle->root.len + old.len;
 
     if (cycle->connections) {
-        if (!(name->data = ngx_palloc(cycle->pool, name->len + 1))) {
+        name->data = ngx_palloc(cycle->pool, name->len + 1);
+        if (name->data == NULL) {
             return  NGX_ERROR;
         }
 
@@ -631,7 +628,8 @@ ngx_int_t ngx_conf_full_name(ngx_cycle_t
 
         /* the init_cycle */
 
-        if (!(name->data = ngx_alloc(name->len + 1, cycle->log))) {
+        name->data = ngx_alloc(name->len + 1, cycle->log);
+        if (name->data == NULL) {
             return  NGX_ERROR;
         }
     }
@@ -686,7 +684,8 @@ ngx_open_file_t *ngx_conf_open_file(ngx_
         }
     }
 
-    if (!(file = ngx_list_push(&cycle->open_files))) {
+    file = ngx_list_push(&cycle->open_files);
+    if (file == NULL) {
         return NULL;
     }