From 522ac2c9fd4956d3b77f43038541a3c99a1b6b95 Mon Sep 17 00:00:00 2001 From: fanqibing Date: Tue, 1 Jul 2025 23:46:09 +0800 Subject: [PATCH 01/10] add tcp option for http Signed-off-by: fanqibing --- .../js/napi/http/http_exec/src/http_exec.cpp | 23 +++++++ .../options/include/http_request_options.h | 64 ++++++++++++++++++ .../http/options/src/http_request_options.cpp | 66 +++++++++++++++++++ 3 files changed, 153 insertions(+) diff --git a/frameworks/js/napi/http/http_exec/src/http_exec.cpp b/frameworks/js/napi/http/http_exec/src/http_exec.cpp index eceea5a62..6ec3f9410 100755 --- a/frameworks/js/napi/http/http_exec/src/http_exec.cpp +++ b/frameworks/js/napi/http/http_exec/src/http_exec.cpp @@ -1900,6 +1900,29 @@ bool HttpExec::SetDnsCacheOption(CURL *curl, RequestContext *context) return true; } +bool HttpExec::SetTCPOption(CURL *curl, RequestContext *context) +{ + if (!context) { + NETSTACK_LOGE("context is nullptr"); + return false; + } + NETSTACK_CURL_EASY_SET_OPTION(curl, CURLOPT_SOCKOPTDATA, &context->options, context); + NETSTACK_CURL_EASY_SET_OPTION(curl, CURLOPT_SOCKOPTFUNCTION, + +[](void *clientp, curl_socket_t sock, curlsocktype type) -> int { + if (!clientp) { + return CURL_SOCKOPT_OK; + } + auto resp = reinterpret_cast(clientp); + HttpRequestOptions::TcpConfiguration config = resp->GetTCPOption(); + if (config.SetOptionToSocket(sock)) { + NETSTACK_LOGI("SetOptionToSocket userTimeout = %{public}d", config.userTimeout); + } + + return CURL_SOCKOPT_OK; + }, context); + return true; +} + bool HttpExec::SetIpResolve(CURL *curl, RequestContext *context) { std::string addressFamily = context->options.GetAddressFamily(); diff --git a/frameworks/js/napi/http/options/include/http_request_options.h b/frameworks/js/napi/http/options/include/http_request_options.h index 77942a843..aa407ee0b 100644 --- a/frameworks/js/napi/http/options/include/http_request_options.h +++ b/frameworks/js/napi/http/options/include/http_request_options.h @@ -86,6 +86,67 @@ class HttpRequestOptions final { public: HttpRequestOptions(); + // https://man7.org/linux/man-pages/man7/tcp.7.html + struct TcpConfiguration { + friend class HttpRequestOptions; + // TCP_KEEPIDLE (since Linux 2.4) + // The time (in seconds) the connection needs to remain idle + // before TCP starts sending keepalive probes, if the socket + // option SO_KEEPALIVE has been set on this socket. This + // option should not be used in code intended to be portable. + // For example, if the server will send FIN packet after 60 seconds, we suggest that set to 61. + int keepIdle = 60 * 5; // Default value according to Cloud Service. + // TCP_KEEPINTVL (since Linux 2.4) + // The time (in seconds) between individual keepalive probes. + // This option should not be used in code intended to be + // portable. + // == 1 means that we just probe once, if it fails, we close the connection. + int keepCnt = 1; + // TCP_KEEPINTVL (since Linux 2.4) + // The time (in seconds) between individual keepalive probes. + // This option should not be used in code intended to be + // portable. + int keepInterval = 1; + // TCP_USER_TIMEOUT (since Linux 2.6.37) + // This option takes an unsigned int as an argument. When + // the value is greater than 0, it specifies the maximum + // amount of time in milliseconds that transmitted data may + // remain unacknowledged, or buffered data may remain + // untransmitted (due to zero window size) before TCP will + // forcibly close the corresponding connection and return + // ETIMEDOUT to the application. If the option value is + // specified as 0, TCP will use the system default. + // + // Increasing user timeouts allows a TCP connection to + // survive extended periods without end-to-end connectivity. + // Decreasing user timeouts allows applications to "fail + // fast", if so desired. Otherwise, failure may take up to + // 20 minutes with the current system defaults in a normal + // WAN environment. + // + // This option can be set during any state of a TCP + // connection, but is effective only during the synchronized + // states of a connection (ESTABLISHED, FIN-WAIT-1, FIN- + // WAIT-2, CLOSE-WAIT, CLOSING, and LAST-ACK). Moreover, + // when used with the TCP keepalive (SO_KEEPALIVE) option, + // TCP_USER_TIMEOUT will override keepalive to determine when + // to close a connection due to keepalive failure. + // + // The option has no effect on when TCP retransmits a packet, + // nor when a keepalive probe is sent. + // + // This option, like many others, will be inherited by the + // socket returned by accept(2), if it was set on the + // listening socket. + // + // Further details on the user timeout feature can be found + // in RFC 793 and RFC 5482 ("TCP User Timeout Option"). + int userTimeout = HttpConstant::DEFAULT_READ_TIMEOUT; + + bool SetOptionToSocket(int sock); + void SetTcpUserTimeout(const uint32_t &timeout); + }; + void SetUrl(const std::string &url); void SetMethod(const std::string &method); @@ -179,6 +240,7 @@ public: std::vector GetMultiPartDataList(); [[nodiscard]] const TlsOption GetTlsOption() const; + [[nodiscard]] const TcpConfiguration GetTCPOption() const; [[nodiscard]] const ServerAuthentication GetServerAuthentication() const; @@ -247,6 +309,8 @@ private: ServerAuthentication serverAuthentication_; std::string addressFamily_; + + TcpConfiguration tcpOption_; }; } // namespace OHOS::NetStack::Http diff --git a/frameworks/js/napi/http/options/src/http_request_options.cpp b/frameworks/js/napi/http/options/src/http_request_options.cpp index 55967ff33..c432f562c 100644 --- a/frameworks/js/napi/http/options/src/http_request_options.cpp +++ b/frameworks/js/napi/http/options/src/http_request_options.cpp @@ -66,6 +66,7 @@ void HttpRequestOptions::SetHeader(const std::string &key, const std::string &va void HttpRequestOptions::SetReadTimeout(uint32_t readTimeout) { readTimeout_ = readTimeout; + tcpOption_.SetTcpUserTimeout(readTimeout); } void HttpRequestOptions::SetMaxLimit(uint32_t maxLimit) @@ -273,6 +274,11 @@ const TlsOption HttpRequestOptions::GetTlsOption() const return tlsOption_; } +const HttpRequestOptions::TcpConfiguration HttpRequestOptions::GetTCPOption() const +{ + return tcpOption_; +} + void HttpRequestOptions::SetServerAuthentication(const ServerAuthentication &serverAuthentication) { serverAuthentication_.authenticationType = serverAuthentication.authenticationType; @@ -359,4 +365,64 @@ std::string HttpRequestOptions::GetAddressFamily() const { return addressFamily_; } + +bool HttpRequestOptions::TcpConfiguration::SetOptionToSocket(int sock) +{ + int proto = -1; + auto len = static_cast(sizeof(proto)); + // https://man7.org/linux/man-pages/man2/getsockopt.2.html + // RETURN VALUE top + // On success, zero is returned for the standard options. On error, + // -1 is returned, and errno is set to indicate the error. + // + // Netfilter allows the programmer to define custom socket options + // with associated handlers; for such options, the return value on + // success is the value returned by the handler. + auto res = getsockopt(sock, SOL_SOCKET, SO_PROTOCOL, &proto, &len); + if (res != 0 || proto != IPPROTO_TCP) { + return false; + } + if (setsockopt(sock, SOL_TCP, TCP_USER_TIMEOUT, &userTimeout, sizeof(userTimeout)) != 0) { + NETSTACK_LOGE("set TCP_USER_TIMEOUT failed, errno = %{public}d userTimeout = %{public}d sock = %{public}d", + errno, userTimeout, sock); + return false; + } + + int keepAlive = 1; + // https://man7.org/linux/man-pages/man7/socket.7.html + // SO_KEEPALIVE + // Enable sending of keep-alive messages on connection- + // oriented sockets. Expects an integer boolean flag. + // + // https://man7.org/linux/man-pages/man3/setsockopt.3p.html + // RETURN VALUE top + // Upon successful completion, setsockopt() shall return 0. + // Otherwise, -1 shall be returned and errno set to indicate the + // error. + if (setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &keepAlive, sizeof(keepAlive)) != 0) { + NETSTACK_LOGE("set SO_KEEPALIVE failed, errno = %{public}d sock = %{public}d", errno, sock); + return false; + } + if (setsockopt(sock, SOL_TCP, TCP_KEEPIDLE, &keepIdle, sizeof(keepIdle)) != 0) { + NETSTACK_LOGE("set TCP_KEEPIDLE failed, errno = %{public}d keepIdle = %{public}d sock = %{public}d", + errno, keepIdle, sock); + return false; + } + if (setsockopt(sock, SOL_TCP, TCP_KEEPCNT, &keepCnt, sizeof(keepCnt)) != 0) { + NETSTACK_LOGE("set TCP_KEEPCNT failed, errno = %{public}d keepCnt = %{public}d sock = %{public}d", + errno, keepCnt, sock); + return false; + } + if (setsockopt(sock, SOL_TCP, TCP_KEEPINTVL, &keepInterval, sizeof(keepInterval)) != 0) { + NETSTACK_LOGE("set TCP_KEEPINTVL failed, errno = %{public}d keepInterval = %{public}d sock = %{public}d", + errno, keepInterval, sock); + return false; + } + return true; +} + +void HttpRequestOptions::TcpConfiguration::SetTcpUserTimeout(const uint32_t &timeout) +{ + userTimeout = timeout; +} } // namespace OHOS::NetStack::Http \ No newline at end of file -- Gitee From 7e803d60507be7354f8e21baef67510a673531f4 Mon Sep 17 00:00:00 2001 From: fanqibing Date: Wed, 2 Jul 2025 00:28:47 +0800 Subject: [PATCH 02/10] bugfix Signed-off-by: fanqibing --- frameworks/js/napi/http/http_exec/include/http_exec.h | 2 ++ frameworks/js/napi/http/http_exec/src/http_exec.cpp | 1 + 2 files changed, 3 insertions(+) diff --git a/frameworks/js/napi/http/http_exec/include/http_exec.h b/frameworks/js/napi/http/http_exec/include/http_exec.h index a16f94206..62bf7eace 100644 --- a/frameworks/js/napi/http/http_exec/include/http_exec.h +++ b/frameworks/js/napi/http/http_exec/include/http_exec.h @@ -104,6 +104,8 @@ private: static bool SetDnsResolvOption(CURL *curl, RequestContext *context); + static bool SetTCPOption(CURL *curl, RequestContext *context); + static bool SetCertPinnerOption(CURL *curl, RequestContext *context); static size_t OnWritingMemoryBody(const void *data, size_t size, size_t memBytes, void *userData); diff --git a/frameworks/js/napi/http/http_exec/src/http_exec.cpp b/frameworks/js/napi/http/http_exec/src/http_exec.cpp index 6ec3f9410..d0ad9462b 100755 --- a/frameworks/js/napi/http/http_exec/src/http_exec.cpp +++ b/frameworks/js/napi/http/http_exec/src/http_exec.cpp @@ -1379,6 +1379,7 @@ bool HttpExec::SetRequestOption(CURL *curl, RequestContext *context) SetDnsResolvOption(curl, context); SetDnsCacheOption(curl, context); SetIpResolve(curl, context); + SetTCPOption(curl, context); return true; } -- Gitee From 6301ed3b8ae8d23dda95ac660f402b63289329c8 Mon Sep 17 00:00:00 2001 From: fanqibing Date: Wed, 2 Jul 2025 21:31:41 +0800 Subject: [PATCH 03/10] add include Signed-off-by: fanqibing --- frameworks/js/napi/http/options/include/http_request_options.h | 1 + 1 file changed, 1 insertion(+) diff --git a/frameworks/js/napi/http/options/include/http_request_options.h b/frameworks/js/napi/http/options/include/http_request_options.h index aa407ee0b..4b406a0b4 100644 --- a/frameworks/js/napi/http/options/include/http_request_options.h +++ b/frameworks/js/napi/http/options/include/http_request_options.h @@ -20,6 +20,7 @@ #include #include +#include #include "constant.h" #include "secure_char.h" #include "http_tls_config.h" -- Gitee From 2e28715dfb3b7a825652fa47a55b367d8aaeb69d Mon Sep 17 00:00:00 2001 From: fanqibing Date: Wed, 2 Jul 2025 21:41:33 +0800 Subject: [PATCH 04/10] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=A4=9A=E8=A1=8C?= =?UTF-8?q?=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: fanqibing --- .../options/include/http_request_options.h | 107 ++++++++++-------- .../http/options/src/http_request_options.cpp | 40 ++++--- 2 files changed, 80 insertions(+), 67 deletions(-) diff --git a/frameworks/js/napi/http/options/include/http_request_options.h b/frameworks/js/napi/http/options/include/http_request_options.h index 4b406a0b4..262ce9099 100644 --- a/frameworks/js/napi/http/options/include/http_request_options.h +++ b/frameworks/js/napi/http/options/include/http_request_options.h @@ -90,58 +90,67 @@ public: // https://man7.org/linux/man-pages/man7/tcp.7.html struct TcpConfiguration { friend class HttpRequestOptions; - // TCP_KEEPIDLE (since Linux 2.4) - // The time (in seconds) the connection needs to remain idle - // before TCP starts sending keepalive probes, if the socket - // option SO_KEEPALIVE has been set on this socket. This - // option should not be used in code intended to be portable. - // For example, if the server will send FIN packet after 60 seconds, we suggest that set to 61. + /* + TCP_KEEPIDLE (since Linux 2.4) + The time (in seconds) the connection needs to remain idle + before TCP starts sending keepalive probes, if the socket + option SO_KEEPALIVE has been set on this socket. This + option should not be used in code intended to be portable. + For example, if the server will send FIN packet after 60 seconds, we suggest that set to 61. + */ int keepIdle = 60 * 5; // Default value according to Cloud Service. - // TCP_KEEPINTVL (since Linux 2.4) - // The time (in seconds) between individual keepalive probes. - // This option should not be used in code intended to be - // portable. - // == 1 means that we just probe once, if it fails, we close the connection. + /* + TCP_KEEPINTVL (since Linux 2.4) + The time (in seconds) between individual keepalive probes. + This option should not be used in code intended to be + portable. + */ + == 1 means that we just probe once, if it fails, we close the connection. int keepCnt = 1; - // TCP_KEEPINTVL (since Linux 2.4) - // The time (in seconds) between individual keepalive probes. - // This option should not be used in code intended to be - // portable. + /* + TCP_KEEPINTVL (since Linux 2.4) + The time (in seconds) between individual keepalive probes. + This option should not be used in code intended to be + portable. + */ int keepInterval = 1; - // TCP_USER_TIMEOUT (since Linux 2.6.37) - // This option takes an unsigned int as an argument. When - // the value is greater than 0, it specifies the maximum - // amount of time in milliseconds that transmitted data may - // remain unacknowledged, or buffered data may remain - // untransmitted (due to zero window size) before TCP will - // forcibly close the corresponding connection and return - // ETIMEDOUT to the application. If the option value is - // specified as 0, TCP will use the system default. - // - // Increasing user timeouts allows a TCP connection to - // survive extended periods without end-to-end connectivity. - // Decreasing user timeouts allows applications to "fail - // fast", if so desired. Otherwise, failure may take up to - // 20 minutes with the current system defaults in a normal - // WAN environment. - // - // This option can be set during any state of a TCP - // connection, but is effective only during the synchronized - // states of a connection (ESTABLISHED, FIN-WAIT-1, FIN- - // WAIT-2, CLOSE-WAIT, CLOSING, and LAST-ACK). Moreover, - // when used with the TCP keepalive (SO_KEEPALIVE) option, - // TCP_USER_TIMEOUT will override keepalive to determine when - // to close a connection due to keepalive failure. - // - // The option has no effect on when TCP retransmits a packet, - // nor when a keepalive probe is sent. - // - // This option, like many others, will be inherited by the - // socket returned by accept(2), if it was set on the - // listening socket. - // - // Further details on the user timeout feature can be found - // in RFC 793 and RFC 5482 ("TCP User Timeout Option"). + + /* + TCP_USER_TIMEOUT (since Linux 2.6.37) + This option takes an unsigned int as an argument. When + the value is greater than 0, it specifies the maximum + amount of time in milliseconds that transmitted data may + remain unacknowledged, or buffered data may remain + untransmitted (due to zero window size) before TCP will + forcibly close the corresponding connection and return + ETIMEDOUT to the application. If the option value is + specified as 0, TCP will use the system default. + + Increasing user timeouts allows a TCP connection to + survive extended periods without end-to-end connectivity. + Decreasing user timeouts allows applications to "fail + fast", if so desired. Otherwise, failure may take up to + 20 minutes with the current system defaults in a normal + WAN environment. + + This option can be set during any state of a TCP + connection, but is effective only during the synchronized + states of a connection (ESTABLISHED, FIN-WAIT-1, FIN- + WAIT-2, CLOSE-WAIT, CLOSING, and LAST-ACK). Moreover, + when used with the TCP keepalive (SO_KEEPALIVE) option, + TCP_USER_TIMEOUT will override keepalive to determine when + to close a connection due to keepalive failure. + + The option has no effect on when TCP retransmits a packet, + nor when a keepalive probe is sent. + + This option, like many others, will be inherited by the + socket returned by accept(2), if it was set on the + listening socket. + + Further details on the user timeout feature can be found + in RFC 793 and RFC 5482 ("TCP User Timeout Option"). + */ int userTimeout = HttpConstant::DEFAULT_READ_TIMEOUT; bool SetOptionToSocket(int sock); diff --git a/frameworks/js/napi/http/options/src/http_request_options.cpp b/frameworks/js/napi/http/options/src/http_request_options.cpp index c432f562c..dd68061d3 100644 --- a/frameworks/js/napi/http/options/src/http_request_options.cpp +++ b/frameworks/js/napi/http/options/src/http_request_options.cpp @@ -370,14 +370,16 @@ bool HttpRequestOptions::TcpConfiguration::SetOptionToSocket(int sock) { int proto = -1; auto len = static_cast(sizeof(proto)); - // https://man7.org/linux/man-pages/man2/getsockopt.2.html - // RETURN VALUE top - // On success, zero is returned for the standard options. On error, - // -1 is returned, and errno is set to indicate the error. - // - // Netfilter allows the programmer to define custom socket options - // with associated handlers; for such options, the return value on - // success is the value returned by the handler. + /* + https://man7.org/linux/man-pages/man2/getsockopt.2.html + RETURN VALUE top + On success, zero is returned for the standard options. On error, + -1 is returned, and errno is set to indicate the error. + + Netfilter allows the programmer to define custom socket options + with associated handlers; for such options, the return value on + success is the value returned by the handler. + */ auto res = getsockopt(sock, SOL_SOCKET, SO_PROTOCOL, &proto, &len); if (res != 0 || proto != IPPROTO_TCP) { return false; @@ -389,16 +391,18 @@ bool HttpRequestOptions::TcpConfiguration::SetOptionToSocket(int sock) } int keepAlive = 1; - // https://man7.org/linux/man-pages/man7/socket.7.html - // SO_KEEPALIVE - // Enable sending of keep-alive messages on connection- - // oriented sockets. Expects an integer boolean flag. - // - // https://man7.org/linux/man-pages/man3/setsockopt.3p.html - // RETURN VALUE top - // Upon successful completion, setsockopt() shall return 0. - // Otherwise, -1 shall be returned and errno set to indicate the - // error. + /* + https://man7.org/linux/man-pages/man7/socket.7.html + SO_KEEPALIVE + Enable sending of keep-alive messages on connection- + oriented sockets. Expects an integer boolean flag. + + https://man7.org/linux/man-pages/man3/setsockopt.3p.html + RETURN VALUE top + Upon successful completion, setsockopt() shall return 0. + Otherwise, -1 shall be returned and errno set to indicate the + error. + */ if (setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &keepAlive, sizeof(keepAlive)) != 0) { NETSTACK_LOGE("set SO_KEEPALIVE failed, errno = %{public}d sock = %{public}d", errno, sock); return false; -- Gitee From 16f6a464885d443b3cbcd33ec4443ff26e7fe9c1 Mon Sep 17 00:00:00 2001 From: fanqibing Date: Wed, 2 Jul 2025 21:47:11 +0800 Subject: [PATCH 05/10] =?UTF-8?q?=E5=8F=98=E9=87=8F=E8=A7=84=E8=8C=83?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: fanqibing --- .../options/include/http_request_options.h | 10 ++++----- .../http/options/src/http_request_options.cpp | 22 +++++++++---------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/frameworks/js/napi/http/options/include/http_request_options.h b/frameworks/js/napi/http/options/include/http_request_options.h index 262ce9099..3a47a9655 100644 --- a/frameworks/js/napi/http/options/include/http_request_options.h +++ b/frameworks/js/napi/http/options/include/http_request_options.h @@ -98,22 +98,22 @@ public: option should not be used in code intended to be portable. For example, if the server will send FIN packet after 60 seconds, we suggest that set to 61. */ - int keepIdle = 60 * 5; // Default value according to Cloud Service. + int keepIdle_ = 60 * 5; // Default value according to Cloud Service. /* TCP_KEEPINTVL (since Linux 2.4) The time (in seconds) between individual keepalive probes. This option should not be used in code intended to be portable. - */ == 1 means that we just probe once, if it fails, we close the connection. - int keepCnt = 1; + */ + int keepCnt_ = 1; /* TCP_KEEPINTVL (since Linux 2.4) The time (in seconds) between individual keepalive probes. This option should not be used in code intended to be portable. */ - int keepInterval = 1; + int keepInterval_ = 1; /* TCP_USER_TIMEOUT (since Linux 2.6.37) @@ -151,7 +151,7 @@ public: Further details on the user timeout feature can be found in RFC 793 and RFC 5482 ("TCP User Timeout Option"). */ - int userTimeout = HttpConstant::DEFAULT_READ_TIMEOUT; + int userTimeout_ = HttpConstant::DEFAULT_READ_TIMEOUT; bool SetOptionToSocket(int sock); void SetTcpUserTimeout(const uint32_t &timeout); diff --git a/frameworks/js/napi/http/options/src/http_request_options.cpp b/frameworks/js/napi/http/options/src/http_request_options.cpp index dd68061d3..ea0253794 100644 --- a/frameworks/js/napi/http/options/src/http_request_options.cpp +++ b/frameworks/js/napi/http/options/src/http_request_options.cpp @@ -384,13 +384,13 @@ bool HttpRequestOptions::TcpConfiguration::SetOptionToSocket(int sock) if (res != 0 || proto != IPPROTO_TCP) { return false; } - if (setsockopt(sock, SOL_TCP, TCP_USER_TIMEOUT, &userTimeout, sizeof(userTimeout)) != 0) { + if (setsockopt(sock, SOL_TCP, TCP_USER_TIMEOUT, &userTimeout_, sizeof(userTimeout_)) != 0) { NETSTACK_LOGE("set TCP_USER_TIMEOUT failed, errno = %{public}d userTimeout = %{public}d sock = %{public}d", - errno, userTimeout, sock); + errno, userTimeout_, sock); return false; } - int keepAlive = 1; + int keepAlive_ = 1; /* https://man7.org/linux/man-pages/man7/socket.7.html SO_KEEPALIVE @@ -403,23 +403,23 @@ bool HttpRequestOptions::TcpConfiguration::SetOptionToSocket(int sock) Otherwise, -1 shall be returned and errno set to indicate the error. */ - if (setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &keepAlive, sizeof(keepAlive)) != 0) { + if (setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &keepAlive_, sizeof(keepAlive_)) != 0) { NETSTACK_LOGE("set SO_KEEPALIVE failed, errno = %{public}d sock = %{public}d", errno, sock); return false; } - if (setsockopt(sock, SOL_TCP, TCP_KEEPIDLE, &keepIdle, sizeof(keepIdle)) != 0) { + if (setsockopt(sock, SOL_TCP, TCP_KEEPIDLE, &keepIdle_, sizeof(keepIdle_)) != 0) { NETSTACK_LOGE("set TCP_KEEPIDLE failed, errno = %{public}d keepIdle = %{public}d sock = %{public}d", - errno, keepIdle, sock); + errno, keepIdle_, sock); return false; } - if (setsockopt(sock, SOL_TCP, TCP_KEEPCNT, &keepCnt, sizeof(keepCnt)) != 0) { + if (setsockopt(sock, SOL_TCP, TCP_KEEPCNT, &keepCnt_, sizeof(keepCnt_)) != 0) { NETSTACK_LOGE("set TCP_KEEPCNT failed, errno = %{public}d keepCnt = %{public}d sock = %{public}d", - errno, keepCnt, sock); + errno, keepCnt_, sock); return false; } - if (setsockopt(sock, SOL_TCP, TCP_KEEPINTVL, &keepInterval, sizeof(keepInterval)) != 0) { + if (setsockopt(sock, SOL_TCP, TCP_KEEPINTVL, &keepInterval_, sizeof(keepInterval_)) != 0) { NETSTACK_LOGE("set TCP_KEEPINTVL failed, errno = %{public}d keepInterval = %{public}d sock = %{public}d", - errno, keepInterval, sock); + errno, keepInterval_, sock); return false; } return true; @@ -427,6 +427,6 @@ bool HttpRequestOptions::TcpConfiguration::SetOptionToSocket(int sock) void HttpRequestOptions::TcpConfiguration::SetTcpUserTimeout(const uint32_t &timeout) { - userTimeout = timeout; + userTimeout_ = timeout; } } // namespace OHOS::NetStack::Http \ No newline at end of file -- Gitee From 9db8e81ac6573857cc72b7bf0f01686b5e915c96 Mon Sep 17 00:00:00 2001 From: fanqibing Date: Wed, 2 Jul 2025 21:49:41 +0800 Subject: [PATCH 06/10] bugfix Signed-off-by: fanqibing --- frameworks/js/napi/http/http_exec/src/http_exec.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frameworks/js/napi/http/http_exec/src/http_exec.cpp b/frameworks/js/napi/http/http_exec/src/http_exec.cpp index d0ad9462b..ad4432ece 100755 --- a/frameworks/js/napi/http/http_exec/src/http_exec.cpp +++ b/frameworks/js/napi/http/http_exec/src/http_exec.cpp @@ -1916,7 +1916,7 @@ bool HttpExec::SetTCPOption(CURL *curl, RequestContext *context) auto resp = reinterpret_cast(clientp); HttpRequestOptions::TcpConfiguration config = resp->GetTCPOption(); if (config.SetOptionToSocket(sock)) { - NETSTACK_LOGI("SetOptionToSocket userTimeout = %{public}d", config.userTimeout); + NETSTACK_LOGI("SetOptionToSocket userTimeout = %{public}d", config.userTimeout_); } return CURL_SOCKOPT_OK; -- Gitee From 29de3112358fc25f461f25dab93cf3b51554098e Mon Sep 17 00:00:00 2001 From: fanqibing Date: Thu, 3 Jul 2025 10:50:23 +0800 Subject: [PATCH 07/10] bugfix Signed-off-by: fanqibing --- frameworks/js/napi/http/options/include/http_request_options.h | 1 - frameworks/js/napi/http/options/src/http_request_options.cpp | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/frameworks/js/napi/http/options/include/http_request_options.h b/frameworks/js/napi/http/options/include/http_request_options.h index 3a47a9655..386e7c060 100644 --- a/frameworks/js/napi/http/options/include/http_request_options.h +++ b/frameworks/js/napi/http/options/include/http_request_options.h @@ -20,7 +20,6 @@ #include #include -#include #include "constant.h" #include "secure_char.h" #include "http_tls_config.h" diff --git a/frameworks/js/napi/http/options/src/http_request_options.cpp b/frameworks/js/napi/http/options/src/http_request_options.cpp index ea0253794..2e1f55435 100644 --- a/frameworks/js/napi/http/options/src/http_request_options.cpp +++ b/frameworks/js/napi/http/options/src/http_request_options.cpp @@ -18,6 +18,7 @@ #include "netstack_common_utils.h" #include "netstack_log.h" +#include #include "http_request_options.h" #include "secure_char.h" -- Gitee From 30bfa55c5964ce961875f89ab0d8b824e87b8be7 Mon Sep 17 00:00:00 2001 From: fanqibing Date: Thu, 3 Jul 2025 11:16:58 +0800 Subject: [PATCH 08/10] bugfix Signed-off-by: fanqibing --- frameworks/js/napi/http/options/src/http_request_options.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/frameworks/js/napi/http/options/src/http_request_options.cpp b/frameworks/js/napi/http/options/src/http_request_options.cpp index 2e1f55435..45782bfa7 100644 --- a/frameworks/js/napi/http/options/src/http_request_options.cpp +++ b/frameworks/js/napi/http/options/src/http_request_options.cpp @@ -18,7 +18,9 @@ #include "netstack_common_utils.h" #include "netstack_log.h" +#if !defined(WINDOWS_PLATFORM) && !defined(MAC_PLATFORM) #include +#endif #include "http_request_options.h" #include "secure_char.h" -- Gitee From 76cafd2d00dae0b7e7c25ecd68b73aa4b3376389 Mon Sep 17 00:00:00 2001 From: fanqibing Date: Thu, 3 Jul 2025 11:24:11 +0800 Subject: [PATCH 09/10] =?UTF-8?q?=E9=9A=94=E7=A6=BBmac=E5=92=8Cwin?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: fanqibing --- frameworks/js/napi/http/options/src/http_request_options.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/frameworks/js/napi/http/options/src/http_request_options.cpp b/frameworks/js/napi/http/options/src/http_request_options.cpp index 45782bfa7..914139fd4 100644 --- a/frameworks/js/napi/http/options/src/http_request_options.cpp +++ b/frameworks/js/napi/http/options/src/http_request_options.cpp @@ -371,6 +371,7 @@ std::string HttpRequestOptions::GetAddressFamily() const bool HttpRequestOptions::TcpConfiguration::SetOptionToSocket(int sock) { +#if !defined(WINDOWS_PLATFORM) && !defined(MAC_PLATFORM) int proto = -1; auto len = static_cast(sizeof(proto)); /* @@ -425,6 +426,7 @@ bool HttpRequestOptions::TcpConfiguration::SetOptionToSocket(int sock) errno, keepInterval_, sock); return false; } +#endif return true; } -- Gitee From 208a89705da3f7559797aa1bf4ee20f969928387 Mon Sep 17 00:00:00 2001 From: fanqibing Date: Thu, 3 Jul 2025 11:57:56 +0800 Subject: [PATCH 10/10] add more info Signed-off-by: fanqibing --- .../js/napi/http/http_exec/src/http_exec.cpp | 16 ++++++++++++++-- .../common_utils/include/netstack_common_utils.h | 2 ++ utils/common_utils/src/netstack_common_utils.cpp | 16 ++++++++++++++++ 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/frameworks/js/napi/http/http_exec/src/http_exec.cpp b/frameworks/js/napi/http/http_exec/src/http_exec.cpp index ad4432ece..60f8b6f10 100755 --- a/frameworks/js/napi/http/http_exec/src/http_exec.cpp +++ b/frameworks/js/napi/http/http_exec/src/http_exec.cpp @@ -475,18 +475,30 @@ void HttpExec::CacheCurlPerformanceTiming(CURL *handle, RequestContext *context) char *ip = nullptr; curl_easy_getinfo(handle, CURLINFO_PRIMARY_IP, &ip); int32_t errCode = context->IsExecOK() ? 0 : context->GetErrorCode(); + char *daddr = nullptr; + char *saddr = nullptr; + long dport = 0; + long sport = 0; + curl_easy_getinfo(handle, CURLINFO_LOCAL_IP, &saddr); + std::string anomSaddr = CommonUtils::ToAnonymousIp(saddr); + curl_easy_getinfo(handle, CURLINFO_LOCAL_PORT, &sport); + curl_easy_getinfo(handle, CURLINFO_PRIMARY_IP, &daddr); + std::string anomDaddr = CommonUtils::ToAnonymousIp(daddr); + curl_easy_getinfo(handle, CURLINFO_PRIMARY_PORT, &dport); NETSTACK_LOGI( "taskid=%{public}d" ", size:%{public}" CURL_FORMAT_CURL_OFF_T ", dns:%{public}.3f, connect:%{public}.3f, tls:%{public}.3f, firstSend:%{public}.3f" ", firstRecv:%{public}.3f, total:%{public}.3f, redirect:%{public}.3f" - ", errCode:%{public}d, RespCode:%{public}s, httpVer:%{public}s, method:%{public}s, osErr:%{public}ld", + ", errCode:%{public}d, RespCode:%{public}s, httpVer:%{public}s, method:%{public}s, osErr:%{public}ld" + ", saddr:%{public}s, sport:%{public}ld, daddr:%{public}s, dport:%{public}ld", context->GetTaskId(), size, dnsTime, connectTime == 0 ? 0 : connectTime - dnsTime, tlsTime == 0 ? 0 : tlsTime - connectTime, firstSendTime == 0 ? 0 : firstSendTime - std::max({dnsTime, connectTime, tlsTime}), firstRecvTime == 0 ? 0 : firstRecvTime - firstSendTime, totalTime, redirectTime, errCode, std::to_string(responseCode).c_str(), - std::to_string(httpVer).c_str(), context->options.GetMethod().c_str(), osErr); + std::to_string(httpVer).c_str(), context->options.GetMethod().c_str(), osErr, + anomSaddr.c_str(), sport, anomDaddr.c_str(), dport); #if HAS_NETMANAGER_BASE if (EventReport::GetInstance().IsValid()) { HttpPerfInfo httpPerfInfo; diff --git a/utils/common_utils/include/netstack_common_utils.h b/utils/common_utils/include/netstack_common_utils.h index e8728d43a..5c8a2e6a8 100644 --- a/utils/common_utils/include/netstack_common_utils.h +++ b/utils/common_utils/include/netstack_common_utils.h @@ -103,5 +103,7 @@ bool IsCertPubKeyInPinned(const std::string &certPubKeyDigest, const std::string bool IsCleartextPermitted(const std::string &url, const std::string &protocol); bool IsValidPort(const uint32_t &Port); + +std::string ToAnonymousIp(const std::string &input); } // namespace OHOS::NetStack::CommonUtils #endif /* COMMUNICATIONNETSTACK_COMMON_UTILS_H */ diff --git a/utils/common_utils/src/netstack_common_utils.cpp b/utils/common_utils/src/netstack_common_utils.cpp index 793a3ea7b..69018132a 100644 --- a/utils/common_utils/src/netstack_common_utils.cpp +++ b/utils/common_utils/src/netstack_common_utils.cpp @@ -612,4 +612,20 @@ bool IsValidPort(const uint32_t &port) } return true; } + +std::string ToAnonymousIp(const std::string &input) +{ + std::string maskedResult = input; + // Mask ipv4 address. + if (std::regex_match(maskedResult, IP_PATTERN) || std::regex_match(maskedResult, IP_MASK_PATTERN)) { + MaskIpv4(maskedResult); + return maskedResult; + } + // Mask ipv6 address. + if (std::regex_match(maskedResult, IPV6_PATTERN) || std::regex_match(maskedResult, IPV6_MASK_PATTERN)) { + MaskIpv6(maskedResult); + return maskedResult; + } + return input; +} } // namespace OHOS::NetStack::CommonUtils \ No newline at end of file -- Gitee