From eb3c96b1d000b557ebf83ddcc0904ebb6af57e41 Mon Sep 17 00:00:00 2001 From: Shen Mengjing Date: Fri, 3 Apr 2026 15:33:44 +0800 Subject: [PATCH] docs: Update CN translation for esp_http_server.rst --- .../protocols/esp_http_server.rst | 4 ++- .../protocols/esp_http_server.rst | 31 ++++++++++++++++++- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/docs/en/api-reference/protocols/esp_http_server.rst b/docs/en/api-reference/protocols/esp_http_server.rst index 22640d28d5..12a9929530 100644 --- a/docs/en/api-reference/protocols/esp_http_server.rst +++ b/docs/en/api-reference/protocols/esp_http_server.rst @@ -12,7 +12,9 @@ The HTTP Server component provides an ability for running a lightweight web serv * :cpp:func:`httpd_stop`: This stops the server with the provided handle and frees up any associated memory/resources. This is a blocking function that first signals a halt to the server task and then waits for the task to terminate. While stopping, the task closes all open connections, removes registered URI handlers and resets all session context data to empty. * :cpp:func:`httpd_register_uri_handler`: A URI handler is registered by passing object of type ``httpd_uri_t`` structure which has members including ``uri`` name, ``method`` type (eg. ``HTTP_GET/HTTP_POST/HTTP_PUT`` etc.), function pointer of type ``esp_err_t *handler (httpd_req_t *req)`` and ``user_ctx`` pointer to user context data. -.. note:: APIs in the HTTP server are not thread-safe. If thread safety is required, it is the responsibility of the application layer to ensure proper synchronization between multiple tasks. +.. note:: + + APIs in the HTTP server are not thread-safe. If thread safety is required, it is the responsibility of the application layer to ensure proper synchronization between multiple tasks. Application Examples -------------------- diff --git a/docs/zh_CN/api-reference/protocols/esp_http_server.rst b/docs/zh_CN/api-reference/protocols/esp_http_server.rst index 6fd0a0b626..0cb6a18c9e 100644 --- a/docs/zh_CN/api-reference/protocols/esp_http_server.rst +++ b/docs/zh_CN/api-reference/protocols/esp_http_server.rst @@ -12,7 +12,9 @@ HTTP Server 组件提供了在 ESP32 上运行轻量级 Web 服务器的功能 * :cpp:func:`httpd_stop`: 根据传入的句柄停止服务器,并释放相关联的内存和资源。这是一个阻塞函数,首先给服务器任务发送停止信号,然后等待其终止。期间服务器任务会关闭所有已打开的连接,删除已注册的 URI 处理程序,并将所有会话的上下文数据重置为空。 * :cpp:func:`httpd_register_uri_handler`: 通过传入 ``httpd_uri_t`` 结构体类型的对象来注册 URI 处理程序。该结构体包含如下成员:``uri`` 名字,``method`` 类型(比如 ``HTTP_GET/HTTP_POST/HTTP_PUT`` 等等), ``esp_err_t *handler (httpd_req_t *req)`` 类型的函数指针,指向用户上下文数据的 ``user_ctx`` 指针。 -.. note:: HTTP Server 组件的 API 并不是线程安全的。如果需要线程安全,应用层需自行确保在多个任务之间进行适当的同步。 +.. note:: + + HTTP Server 组件的 API 并不是线程安全的。如果需要线程安全,应用层需自行确保在多个任务之间进行适当的同步。 应用示例 -------- @@ -21,6 +23,33 @@ HTTP Server 组件提供了在 ESP32 上运行轻量级 Web 服务器的功能 - :example:`protocols/http_server/advanced_tests` 演示了如何使用 HTTP 服务器进行高级测试。 +网络接口绑定 +------------ + +默认情况下,服务器监听所有可用接口 (``INADDR_ANY``)。当 ``httpd_config_t.if_name`` 为 ``NULL`` 时即为此行为。 + +若要将 HTTP 服务器绑定到特定的网络接口,请将 ``httpd_config_t.if_name`` 设置为指向 ``struct ifreq`` 结构体,并填充其中的 ``ifr_name`` 字段(例如 ``"eth0"``、``"en0"`` 或 ``"lo"``,具体取决于平台)。 + +.. code-block:: c + + #include + + httpd_handle_t server = NULL; + httpd_config_t config = HTTPD_DEFAULT_CONFIG(); + + struct ifreq ifr = {0}; + strncpy(ifr.ifr_name, "eth0", sizeof(ifr.ifr_name) - 1); + ifr.ifr_name[sizeof(ifr.ifr_name) - 1] = '\0'; + + config.if_name = 𝔦 + config.server_port = 80; + + ESP_ERROR_CHECK(httpd_start(&server, &config)); + +注意事项: + +- ``if_name`` 仅在 ``httpd_start()`` 调用期间使用。``ifreq`` 对象只需在该调用期间保持有效即可。 + HTTP 长连接 -----------