当前位置: 首页 > article >正文

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


相关文章:

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…...

esProc SPL vs DuckDB:多源数据处理谁更胜一筹?

DuckDB 和 esProc SPL 都支持多样数据源处理&#xff0c;这里比较一下两者的差异。 支持的数据源种类 DuckDB 支持的数据源类型覆盖了常见的文件格式&#xff08;如 CSV、Parquet、JSON、Excel&#xff09;、云存储&#xff08;如 AWS S3、Azure Blob Storage&#xff09;以及…...

基于Python的selenium入门超详细教程(第1章)--WebDriver API篇

学习路线 自动化测试介绍及学习路线-CSDN博客 ​自动化测试之Web自动化&#xff08;基于pythonselenium&#xff09;-CSDN博客 参照博文&#xff1a;selenium入门超详细教程——网页自动化操作-CSDN博客 目录 前言 一、WebDriver API介绍 1.1 什么是WebDriver? 1.2 工…...

每日Attention学习26——Dynamic Weighted Feature Fusion

模块出处 [ACM MM 23] [link] [code] Efficient Parallel Multi-Scale Detail and Semantic Encoding Network for Lightweight Semantic Segmentation 模块名称 Dynamic Weighted Feature Fusion (DWFF) 模块作用 双级特征融合 模块结构 模块思想 我们提出了 DWFF 策略&am…...

接上一篇,C++中,如何设计等价于Qt的信号与槽机制。

看下面例子&#xff1a; class FileManager : public QObject {Q_OBJECTpublic:FileManager(QObject* parent nullptr) : QObject(parent) {}void changeFileName(const QString& newName) {fileName newName;emit fileNameChanged(fileName);}signals:void fileNameChan…...

Spring(6)——Spring、Spring Boot 与 Spring MVC 的关系与区别

Spring、Spring Boot 与 Spring MVC 的关系与区别 1. 核心定位 Spring 定位&#xff1a;基础框架&#xff0c;提供 IoC&#xff08;控制反转&#xff09; 和 DI&#xff08;依赖注入&#xff09; 核心功能&#xff0c;管理对象生命周期及依赖关系。功能&#xff1a;支持事务管…...

安装baselines出现的环境配置问题

该错误通常是由于环境配置问题、依赖包缺失、权限不足等原因导致 1. 更新相关工具 pip install --upgrade pip setuptools 2. 检查并安装依赖 conda install setuptools pip wheel 出现新问题&#xff1a; 3.尝试使用 Conda 安装 conda install mpi4py 再尝试安装 baseli…...

【商城实战(38)】Spring Boot:从本地事务到分布式事务,商城数据一致性的守护之旅

【商城实战】专栏重磅来袭&#xff01;这是一份专为开发者与电商从业者打造的超详细指南。从项目基础搭建&#xff0c;运用 uniapp、Element Plus、SpringBoot 搭建商城框架&#xff0c;到用户、商品、订单等核心模块开发&#xff0c;再到性能优化、安全加固、多端适配&#xf…...

当今前沿技术:人工智能与区块链的未来发展

在如今快速发展的科技时代&#xff0c;各种前沿技术正在改变的生活。人工智能AI&#xff09;就是其中之一。它在医疗、金融、制造等多个领域发挥着巨大作用。AI可以分析数据&#xff0c;识别模式&#xff0c;还能辅助决策。比如&#xff0c;在医疗方面&#xff0c;AI帮助医生更…...

perl的package中“Subroutine new redefined”问题

我在一个脚本run_PMseq.V8.pl调用了一些.pm文件 $perl -c run_PMseq.V8.pl Subroutine new redefined at /mnt/lustre/user/wubin/01.Program/Scripts/01.script/GeneLab/PMSeq/package_V3/Add_mismatch.pm line 25. Subroutine generate_shell redefined at /mnt/lustre/use…...

markdown 转 word 工具 ‌Pandoc‌

‌Pandoc‌是一个开源的文档转换工具&#xff0c;由John MacFarlane开发&#xff0c;旨在提供一个通用的文档转换解决方案。它支持多种输入和输出格式&#xff0c;能够高效地将不同格式的文档进行转换‌ 功能 Pandoc支持以下格式之间的转换&#xff1a; **Markdown、reStruct…...

英语学习(GitHub学到的分享)

【英语语法&#xff1a;https://github.com/hzpt-inet-club/english-note】 【离谱的英语学习指南&#xff1a;https://github.com/byoungd/English-level-up-tips/tree/master】 【很喜欢文中的一句话&#xff1a;如果我轻轻松松的学习&#xff0c;生活的幸福指数会提高很多…...

【eNSP实战】三层交换机使用ACL实现网络安全

拓图 要求&#xff1a; vlan1可以访问Internetvlan2和vlan3不能访问Internet和vlan1vlan2和vlan3之间可以互相访问PC配置如图所示&#xff0c;这里不展示 LSW1接口vlan配置 vlan batch 10 20 30 # interface Vlanif1ip address 192.168.40.2 255.255.255.0 # interface Vla…...

Javascript BOM,DOM 知识简介

JSON 一种数据交换格式,作为数据载体,传输数据, Json比xml 更简单,可读性更高.js的对象和Json可以相互转换. //json定义格式: var varName{"key1":value1,"key2":value2};value的数据类型为数字,字符串(在双引号中),布尔值,数组(在方括号中),对象(在花括…...

拆解 “ES 已死“ 伪命题:Agentic RAG 时代搜索引擎的终极形态

作者&#xff1a;来自 Elastic 李捷 xxx&#xff1a;“ES已死&#xff0c;#%#……” 我&#xff1a;&#xff1f;&#xff1f;&#xff1f; 最近&#xff0c;某厂商发了一堆公关文章&#xff0c;翻来覆去地炒作 “ES 已死”&#xff0c;“放弃 ES”。这哪是什么正经的技术文章&…...

关于ISP Pipeline LSC(镜头阴影校正)位置的一些想法

关于LSC校正的一些基本原理可以参考如下链接&#xff1a; ISP之LSC 【ISP】浅析Lens Shading ISP-镜头阴影校正&#xff08;LSC&#xff09; 这篇博文不打算讲具体的LSC校正原理。 主要是答复一位网友关于LSC校正在ISP Pipeline的问题。 网友问题如下&#xff1a; Rin_Cyn…...

Vue学习笔记集--六大指令

内容渲染指令 内容渲染指令用来辅助开发者渲染 DOM 元素的文本内容。常用的内容渲染指令有如下2 个&#xff1a; v-text&#xff08;类似innerText&#xff09; 使用语法&#xff1a;<p v-text"name">hello</p>&#xff0c;意思是将 name 值渲染到 p 标…...

.net 6程序在IIS中部署后点击IIS设置报错“执行此操作时出错”

.net 6写的程序&#xff0c;需要在Windows服务器的IIS中部署&#xff0c;由于是刚装的系统&#xff0c;先安装.net 6运行时&#xff0c;装了才发现没有IIS&#xff0c;于是又通过“添加角色和功能”添加与IIS相关的功能。安装完毕后&#xff0c;在IIS中添加网站&#xff0c;并将…...

《从零手写Linux Shell:详解进程控制、环境变量与内建命令实现 --- 持续更新》

承接上文Linux 进程的创建、终止、等待与程序替换保姆级讲解-CSDN博客&#xff0c;涉及所用到的代码&#xff0c;本文所绑定的资源就是上篇文章的主要代码。 完整代码在文章末尾 目录 1.实现编写代码输出一个命令行 a.如何获取自己的用户名&#xff0c;主机名&#xff0c;路径…...

【Go语言圣经2.4】

目标 理解 在 Go 中&#xff0c;赋值操作既包括最基本的形式&#xff08;左边一个变量&#xff0c;右边一个表达式&#xff09;&#xff0c;也包括复合赋值、元组赋值和隐式赋值。表达式求值的顺序、变量更新时的副作用以及如何处理多返回值和下划线&#xff08;_&#xff09…...

运维工具推荐 -- 宝塔面板:一键部署服务器

标题&#xff1a;宝塔面板&#xff1a;一键部署服务器&#xff0c;轻松管理你的云端世界 引言 在数字化时代&#xff0c;服务器管理对于个人开发者、中小企业或站长来说既是机遇也是挑战。手动配置服务器环境耗时费力&#xff0c;而 宝塔面板 作为一款 免费开源、功能全面 的服…...

C# 异常处理‌的核心概念

文章目录 一、异常处理的核心概念‌‌二、C# 异常处理的基本语法‌‌三、常见异常类型‌‌四、最佳实践‌‌五、示例&#xff1a;文件读取异常处理‌‌六、总结‌ C# 异常处理‌的详细说明&#xff0c;包括核心概念、使用方法和最佳实践&#xff1a; 一、异常处理的核心概念‌ …...

腾讯云点播key防盗链生成到期自动失效url

package com.xmkjsoft.protect_key;import java.nio.charset.StandardCharsets; import java.security.MessageDigest;public class TencentKeyAntiTheft {private static final String SECRET_KEY ""; // 请替换为腾讯云 VOD 控制台中的 Key/*** 生成腾讯云 Key 防…...

深入 Spring Boot 注解

深入 Spring Boot 注解&#xff1a;我的开发心得与常用注解详解 大家好&#xff0c;我是 [你的 CSDN 昵称/名字]&#xff0c;一位热爱 Spring Boot 的技术博主。 在多年的 Spring Boot 开发实践中&#xff0c;我深深体会到注解的强大魅力。它们不仅让代码变得更简洁&#xff0…...

k8s环境部署

四台机器 分别是 k8s-master&#xff1a;172.25.254.100 k8s-node1&#xff1a;172.25.254.10 k8s-node2&#xff1a;172.25.254.20 docker-harbor&#xff1a;172.25.254.200 reg.timinglee.org 四台机器分别配置好网络和软件仓库 做好地址解析 scp -r /etc/hosts/ root17…...

CentOS 系统安装 docker 以及常用插件

博主用的的是WindTerm软件链接的服务器&#xff0c;因为好用 1.链接上服务器登入后&#xff0c;在/root/目录下 2.执行以下命令安装docker sudo yum install -y yum-utilssudo yum-config-manager \--add-repo \https://download.docker.com/linux/centos/docker-ce.reposudo…...

谷歌云服务器:服务器怎么安装???

谷歌云服务器&#xff1a;服务器怎么安装&#xff1f;&#xff1f;&#xff1f; 以下是详细分步指南&#xff0c;帮助你在 Google Cloud Platform (GCP) 上快速创建并配置云服务器&#xff08;Compute Engine 实例&#xff09;&#xff0c;并安装所需环境&#xff1a; 一、准备…...

Redis--Zset类型

目录 一、引言 二、介绍 三、命令 1.zadd 2.zrange&#xff0c;zrevrange&#xff0c;zrangebyscore 3.zcard&#xff0c;zcount 4.zpopmax&#xff0c;bzpopmax&#xff0c;zpopmin&#xff0c;bzpopmin 5.zrank,zrevrank,zscore 6.zrem&#xff0c;zremrangebyrank&a…...

《阿里云Data+AI:开启数据智能新时代》电子书上线啦!

本书整理了阿里云在DataAI领域的最新实践案例与深度洞察&#xff0c;涵盖电商、游戏、营销、数字内容等多个行业的成功经验&#xff0c;以及技术专家对数据库与AI融合趋势的专业解读。 通过理论与实践的结合&#xff0c;我们将共同探索DataAI如何成为企业智能化转型的核心驱动…...

图像处理篇---图像预处理

文章目录 前言一、通用目的1.1 数据标准化目的实现 1.2 噪声抑制目的实现高斯滤波中值滤波双边滤波 1.3 尺寸统一化目的实现 1.4 数据增强目的实现 1.5 特征增强目的实现&#xff1a;边缘检测直方图均衡化锐化 二、分领域预处理2.1 传统机器学习&#xff08;如SVM、随机森林&am…...