This is an automated email from the ASF dual-hosted git repository.

tomsun28 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/hertzbeat.git


The following commit(s) were added to refs/heads/master by this push:
     new 30fbb4ddbb [bugfix] fix RFC 1123 cookie parsing (#4417)
30fbb4ddbb is described below

commit 30fbb4ddbb0ac8f4571baf9fffe1d6de506d2b8c
Author: butterfly-520-boy <[email protected]>
AuthorDate: Mon Oct 5 10:29:06 2026 +0800

    [bugfix] fix RFC 1123 cookie parsing (#4417)
    
    Co-authored-by: butterfly-520-boy 
<[email protected]>
    Co-authored-by: Tomsun28 <[email protected]>
---
 .../collect/common/http/CommonHttpClient.java      |  2 +
 .../http/CommonHttpClientCookieSpecTest.java       | 94 ++++++++++++++++++++++
 2 files changed, 96 insertions(+)

diff --git 
a/hertzbeat-collector/hertzbeat-collector-common/src/main/java/org/apache/hertzbeat/collector/collect/common/http/CommonHttpClient.java
 
b/hertzbeat-collector/hertzbeat-collector-common/src/main/java/org/apache/hertzbeat/collector/collect/common/http/CommonHttpClient.java
index 811f041023..23d055190e 100644
--- 
a/hertzbeat-collector/hertzbeat-collector-common/src/main/java/org/apache/hertzbeat/collector/collect/common/http/CommonHttpClient.java
+++ 
b/hertzbeat-collector/hertzbeat-collector-common/src/main/java/org/apache/hertzbeat/collector/collect/common/http/CommonHttpClient.java
@@ -31,6 +31,7 @@ import javax.net.ssl.SSLContext;
 import javax.net.ssl.TrustManager;
 import javax.net.ssl.X509TrustManager;
 import lombok.extern.slf4j.Slf4j;
+import org.apache.http.client.config.CookieSpecs;
 import org.apache.http.client.config.RequestConfig;
 import org.apache.http.config.Registry;
 import org.apache.http.config.RegistryBuilder;
@@ -130,6 +131,7 @@ public class CommonHttpClient {
                     .setConnectionRequestTimeout(REQUIRE_CONNECT_TIMEOUT)
                     .setConnectTimeout(CONNECT_TIMEOUT)
                     .setSocketTimeout(SOCKET_TIMEOUT)
+                    .setCookieSpec(CookieSpecs.STANDARD)
                     // auto redirect when 301 302 response status 
                     .setRedirectsEnabled(true)
                     .build();
diff --git 
a/hertzbeat-collector/hertzbeat-collector-common/src/test/java/org/apache/hertzbeat/collector/collect/common/http/CommonHttpClientCookieSpecTest.java
 
b/hertzbeat-collector/hertzbeat-collector-common/src/test/java/org/apache/hertzbeat/collector/collect/common/http/CommonHttpClientCookieSpecTest.java
new file mode 100644
index 0000000000..85f3f529f2
--- /dev/null
+++ 
b/hertzbeat-collector/hertzbeat-collector-common/src/test/java/org/apache/hertzbeat/collector/collect/common/http/CommonHttpClientCookieSpecTest.java
@@ -0,0 +1,94 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hertzbeat.collector.collect.common.http;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.net.InetAddress;
+import java.net.ServerSocket;
+import java.net.Socket;
+import java.nio.charset.StandardCharsets;
+import java.util.List;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+import org.apache.http.client.CookieStore;
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.protocol.HttpClientContext;
+import org.apache.http.cookie.Cookie;
+import org.apache.http.impl.client.BasicCookieStore;
+import org.apache.http.util.EntityUtils;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Tests for the cookie policy used by {@link CommonHttpClient}.
+ */
+class CommonHttpClientCookieSpecTest {
+
+    @Test
+    void parsesCookieWithRfc1123ExpiresDate() throws Exception {
+        InetAddress loopback = InetAddress.getByName("127.0.0.1");
+        try (ServerSocket serverSocket = new ServerSocket(0, 1, loopback);
+             ExecutorService serverExecutor = 
Executors.newSingleThreadExecutor()) {
+            Future<?> responseSent = serverExecutor.submit(() -> 
sendCookieResponse(serverSocket));
+            CookieStore cookieStore = new BasicCookieStore();
+            HttpClientContext context = HttpClientContext.create();
+            context.setCookieStore(cookieStore);
+            HttpGet request = new HttpGet("http://"; + 
loopback.getHostAddress() + ":" + serverSocket.getLocalPort());
+
+            try (CloseableHttpResponse response = 
CommonHttpClient.getHttpClient().execute(request, context)) {
+                assertEquals(200, response.getStatusLine().getStatusCode());
+                EntityUtils.consume(response.getEntity());
+            }
+            responseSent.get(5, TimeUnit.SECONDS);
+
+            List<Cookie> cookies = cookieStore.getCookies();
+            assertEquals(1, cookies.size());
+            assertEquals("__tenant", cookies.getFirst().getName());
+            assertEquals("tenant-id", cookies.getFirst().getValue());
+        }
+    }
+
+    private static void sendCookieResponse(ServerSocket serverSocket) {
+        try (Socket socket = serverSocket.accept();
+             BufferedReader reader = new BufferedReader(
+                     new InputStreamReader(socket.getInputStream(), 
StandardCharsets.US_ASCII));
+             OutputStream output = socket.getOutputStream()) {
+            String line;
+            while ((line = reader.readLine()) != null && !line.isEmpty()) {
+                // Consume the request headers before sending the response.
+            }
+            String response = "HTTP/1.1 200 OK\r\n"
+                    + "Content-Length: 2\r\n"
+                    + "Set-Cookie: __tenant=tenant-id; expires=Thu, 31 Dec 
2099 23:59:59 GMT; path=/\r\n"
+                    + "Connection: close\r\n"
+                    + "\r\n"
+                    + "OK";
+            output.write(response.getBytes(StandardCharsets.US_ASCII));
+            output.flush();
+        } catch (IOException exception) {
+            throw new IllegalStateException("Failed to serve the cookie test 
response", exception);
+        }
+    }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to