ngx_conf_read_token

Ubuntu 下 nginx-1.24.0 源码分析 - ngx_conf_read_token-CSDN博客

static ngx_int_t
ngx_conf_read_token(ngx_conf_t *cf)
{
    u_char      *start, ch, *src, *dst;
    off_t        file_size;
    size_t       len;
    ssize_t      n, size;
    ngx_uint_t   found, need_space, last_space, sharp_comment, variable;
    ngx_uint_t   quoted, s_quoted, d_quoted, start_line;
    ngx_str_t   *word;
    ngx_buf_t   *b, *dump;

    found = 0;
    need_space = 0;
    last_space = 1;
    sharp_comment = 0;
    variable = 0;
    quoted = 0;
    s_quoted = 0;
    d_quoted = 0;

    cf->args->nelts = 0;


清空参数数组,准备解析新参数

    b = cf->conf_file->buffer;
    dump = cf->conf_file->dump;

 获取配置文件缓冲区和dump缓冲区指针

    start = b->pos;
    start_line = cf->conf_file->line;

 start 记录当前 token的起始位置
start = b->pos; 表示将当前解析位置标记为新 token 的起始位置

start_line 记录当前 token 的起始行号,用于错误提示和调试

    file_size = ngx_file_size(&cf->conf_file->file.info);

获取配置文件总大小,判断是否读取完毕

此时的情况:

file_size=2656

    for ( ;; ) {

实现逐字符解析配置文件内容,直到完成整个文件的解析或遇到错误

if (b->pos >= b->last) {

检测当前缓冲区是否已处理完所有数据

此时是第一次,还没有读入文件内容到缓冲区

现在 b->pos == b->last == b->start

if (cf->conf_file->file.offset >= file_size) {

 判断是否已读取完整个配置文件

此时的情况:

cf->conf_file->file.offset(=0) >= file_size(=2656)

条件不成立

len = b->pos - start;

当 b->pos >= b->last(缓冲区数据已处理完毕)时,需要从文件中读取新数据。此时:

start :指向当前 token 的起始位置(可能跨缓冲区)。
b->pos :当前处理位置
通过 len = b->pos - start;:

计算剩余未处理数据的长度 

在缓冲区耗尽时,确定需要迁移的数据量
确保 Token 完整性 :跨缓冲区的 token 能被正确拼接和解析

此时 

len = 0

if (len == NGX_CONF_BUFFER) {

 检测参数长度是否超过缓冲区容量

此时 条件不成立

            if (len) {
                ngx_memmove(b->start, start, len);
            }

此时缓存区中没有未处理完的数据

            size = (ssize_t) (file_size - cf->conf_file->file.offset);

计算文件中剩余未读取的字节数

此时的情况:

size(=2656) = file_size(=2656) - cf->conf_file->file.offset(=0)

           if (size > b->end - (b->start + len)) {
                size = b->end - (b->start + len);
            }

确保读取的字节数不超过缓冲区的剩余空间

此时的情况:

size(=2656) > 4096

条件不成立


size = 2656 不变

            n = ngx_read_file(&cf->conf_file->file, b->start + len, size,
                              cf->conf_file->file.offset);

作用:从文件中读取数据到缓冲区。


参数:
&cf->conf_file->file:文件结构体指针。
b->start + len:缓冲区中写入数据的起始位置(紧接已迁移数据后)。
size:实际读取的字节数(受缓冲区剩余空间限制)。
cf->conf_file->file.offset:文件的当前读取位置。

返回实际读取的字节数量

此时的情况:

n = 2656 

成功读取

            if (n == NGX_ERROR) {
                return NGX_ERROR;
            }

 检测 ngx_read_file 是否返回错误

此时 条件不成立

           if (n != size) {
                ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
                                   ngx_read_file_n " returned "
                                   "only %z bytes instead of %z",
                                   n, size);
                return NGX_ERROR;
            }

验证实际读取的字节数(n)是否等于预期字节数(size

此时 n == size

条件不成立

            b->pos = b->start + len;

将 pos 指针指向缓冲区中未处理数据的起始位置

len 是迁移后的未处理数据长度(如跨缓冲区的 token 部分)

b->start 是缓冲区的起始地址

            b->last = b->pos + n;

更新 last 指针,标记缓冲区中有效数据的末尾

n 是实际读取的字节数(通过 ngx_read_file 返回)

            start = b->start;

重置 start 指针,指向缓冲区的起始位置。

            if (dump) {
                dump->last = ngx_cpymem(dump->last, b->pos, size);
            }

dump 是一个可选的缓冲区,用于保存原始配置内容

此时的情况:

dump = null

条件不成立 

        ch = *b->pos++;

从缓冲区 b 的当前指针位置(b->pos)读取一个字符,并将指针后移一位

此时的情况

ch 是一个换行符

以下是 此时的配置文件 

第一行是空行,所以此时 ch 是一个换行符


#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}


接下来是:

        if (ch == LF) {
            cf->conf_file->line++;

            if (sharp_comment) {
                sharp_comment = 0;
            }
        }

遇到换行符(\n)时,行号(cf->conf_file->line)递增,用于错误定位

如果当前处于行注释状态(sharp_comment 为真),则结束注释

此时 ch == LF

进入这个 if 中 

然后 line=1 变为 line=2(接下来是第二行)
sharp_comment=0 条件不成立

        if (sharp_comment) {
            continue;
        }

此时条件不成立

        if (quoted) {
            quoted = 0;
            continue;
        }

此时条件不成立

        if (need_space) {

此时条件不成立

        if (last_space) {

 last_space 初始为 1

            start = b->pos - 1;
            start_line = cf->conf_file->line;

记录当前 token 的 起始位置b->pos 是当前字符的下一个位置,-1 指向当前字符)

记录当前 token 的 起始行号

            if (ch == ' ' || ch == '\t' || ch == CR || ch == LF) {
                continue;
            }

进入下一次循环

  for ( ;; ) {

        if (b->pos >= b->last) {

此时条件不成立

        ch = *b->pos++;

从缓冲区 b 的当前指针位置(b->pos)读取一个字符,并将指针后移一位

此时 ch=#

        if (ch == LF) {

此时条件不成立

        if (sharp_comment) {

此时 sharp_comment=0

条件不成立

此时 last_space=1

进入

        if (last_space) {

接下来: 

            case '#':
                sharp_comment = 1;
                continue;

sharp_comment 设置为 1

进入下一次循环

    for ( ;; ) {

        if (b->pos >= b->last) {

条件不成立

        ch = *b->pos++;

读取当前字符

此时 ch=u

sharp_comment=1

        if (sharp_comment) {
            continue;
        }

进入下一次循环

        ch = *b->pos++;

此时 ch=s

sharp_comment=1

        if (sharp_comment) {
            continue;
        }

进入下一次循环

如此继续

ch=e
sharp_comment=1

ch=r
sharp_comment=1

ch= (空格)

ch= (空格)
sharp_comment=1

ch=n
sharp_comment=1

ch=o
sharp_comment=1

ch=b
sharp_comment=1

ch=o
sharp_comment=1

ch=d
sharp_comment=1

ch=y
sharp_comment=1

ch=;
sharp_comment=1

下一次循环

ch= 换行符

line=2 变成 line=3

sharp_comment=1 变成 sharp_comment=0

last_space 还是 1

        if (last_space) {

进入这个if

            if (ch == ' ' || ch == '\t' || ch == CR || ch == LF) {
                continue;
            }

条件成立,进入下一次循环

ch=w

        if (last_space) {

last_space 还是 1

            start = b->pos - 1;
            start_line = cf->conf_file->line;

记录当前 token 的 起始位置 (b->pos 是当前字符的下一个位置,-1 指向当前字符)

记录当前 token 的 起始行号

start_line=3

接下来: 

            default:
                last_space = 0;

 设置 last_space = 0;

表示这次这个字符不是 空白分隔符

进入下一次循环

ch=o

last_space=0

else {
            if (ch == '{' && variable) {
                continue;
            }

            variable = 0;

然后进入下一次循环

ch=r
sharp_comment=0
last_space=0

逻辑同上

下一次循环

ch=k

ch=e

ch=r

ch=_

ch=p

ch=r

ch=o

ch=c

ch=e

ch=s

ch=s

ch=e

ch=s

再下一次循环

ch= 空格

然后

else if (ch == ' ' || ch == '\t' || ch == CR || ch == LF
                       || ch == ';' || ch == '{')
            {
                last_space = 1;
                found = 1;
            }
            if (found) {
                word = ngx_array_push(cf->args);

在存放token的数组中添加一个元素,返回地址

                word->data = ngx_pnalloc(cf->pool, b->pos - 1 - start + 1);

为token的存贮分配地址

                for (dst = word->data, src = start, len = 0;
                     src < b->pos - 1;
                     len++)

循环,准备将 token 逐个字符复制到 word->data

b->pos - 1 是当前的位置,当前是分割符(空格),小于 它,就是不要这个分隔符

*dst++ = *src++;

复制,指针向后移动,下一次循环处理下一个字符

 *dst = '\0';

整个 token 都复制过去之后

添加 字符串结束符

                word->len = len;

记录 token 长度

此时

word->data=worker_processes
word->len=16

                found = 0;

进入下一次循环

ch= 空格

last_space=1

        if (last_space) {
            start = b->pos - 1;
            start_line = cf->conf_file->line;
            if (ch == ' ' || ch == '\t' || ch == CR || ch == LF) {
                continue;
            }

上一个字符是空白分隔符,这个字符还是空白分隔符,跳过这个字符

下一次循环

ch=1
sharp_comment=0
last_space=1

       if (last_space) {

            start = b->pos - 1;
            start_line = cf->conf_file->line;
            default:
                last_space = 0;

last_space 设置为 0

进入下一次循环

ch=;  (分号)

else if (ch == ' ' || ch == '\t' || ch == CR || ch == LF
                       || ch == ';' || ch == '{')
            {
                last_space = 1;
                found = 1;
            }

是分隔符,进入这个条件

            if (found) {
                word = ngx_array_push(cf->args);
                if (word == NULL) {
                    return NGX_ERROR;
                }

                word->data = ngx_pnalloc(cf->pool, b->pos - 1 - start + 1);
                if (word->data == NULL) {
                    return NGX_ERROR;
                }

                for (dst = word->data, src = start, len = 0;
                     src < b->pos - 1;
                     len++)

复制 token

此时

word->data=1
word->len=1

                if (ch == ';') {
                    return NGX_OK;
                }

当前是结束标志 (分号)

返回 NGX_OK


你可能感兴趣的:(nginx,c语言)