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;
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;
此时
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);
此时的情况:
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
) 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 是一个换行符
#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;
b->pos
是当前字符的下一个位置,-1
指向当前字符) 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
条件不成立
if (last_space) {
case '#':
sharp_comment = 1;
continue;
for ( ;; ) {
if (b->pos >= b->last) {
ch = *b->pos++;
此时 ch=u
sharp_comment=1
if (sharp_comment) {
continue;
}
ch = *b->pos++;
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
if (last_space) {
if (ch == ' ' || ch == '\t' || ch == CR || ch == LF) {
continue;
}
if (last_space) {
start = b->pos - 1;
start_line = cf->conf_file->line;
b->pos
是当前字符的下一个位置,-1
指向当前字符)start_line=3
default:
last_space = 0;
else {
if (ch == '{' && variable) {
continue;
}
variable = 0;
else if (ch == ' ' || ch == '\t' || ch == CR || ch == LF
|| ch == ';' || ch == '{')
{
last_space = 1;
found = 1;
}
if (found) {
word = ngx_array_push(cf->args);
word->data = ngx_pnalloc(cf->pool, b->pos - 1 - start + 1);
for (dst = word->data, src = start, len = 0;
src < b->pos - 1;
len++)
*dst++ = *src++;
*dst = '\0';
word->len = len;
此时
word->data=worker_processes
word->len=16
found = 0;
if (last_space) {
start = b->pos - 1;
start_line = cf->conf_file->line;
if (ch == ' ' || ch == '\t' || ch == CR || ch == LF) {
continue;
}
if (last_space) {
start = b->pos - 1;
start_line = cf->conf_file->line;
default:
last_space = 0;
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++)
if (ch == ';') {
return NGX_OK;
}