github-advanced-security[bot] commented on code in PR #19855:
URL: https://github.com/apache/druid/pull/19855#discussion_r3704659855


##########
processing/src/test/java/org/apache/druid/java/util/http/client/FriendlyServersTest.java:
##########
@@ -115,6 +118,95 @@
     }
   }
 
+  @Test
+  public void testCancelRequestClosesConnection() throws Exception
+  {
+    final CountDownLatch requestReceived = new CountDownLatch(1);
+    final CountDownLatch connectionClosed = new CountDownLatch(1);
+    final ExecutorService exec = Executors.newSingleThreadExecutor();
+    final ExecutorService requestExec = Executors.newSingleThreadExecutor();
+    final ServerSocket serverSocket = new ServerSocket(0);
+    exec.submit(
+        new Runnable()
+        {
+          @Override
+          public void run()
+          {
+            try {
+              try (
+                  Socket clientSocket = serverSocket.accept();
+                  BufferedReader in = new BufferedReader(
+                      new InputStreamReader(clientSocket.getInputStream(), 
StandardCharsets.UTF_8)
+                  )
+              ) {
+                while (!in.readLine().equals("")) {

Review Comment:
   ## CodeQL / Inefficient empty string test
   
   Inefficient comparison to empty string, check for zero length instead.
   
   [Show more 
details](https://github.com/apache/druid/security/code-scanning/11472)



##########
processing/src/test/java/org/apache/druid/java/util/http/client/FriendlyServersTest.java:
##########
@@ -115,6 +118,95 @@
     }
   }
 
+  @Test
+  public void testCancelRequestClosesConnection() throws Exception
+  {
+    final CountDownLatch requestReceived = new CountDownLatch(1);
+    final CountDownLatch connectionClosed = new CountDownLatch(1);
+    final ExecutorService exec = Executors.newSingleThreadExecutor();
+    final ExecutorService requestExec = Executors.newSingleThreadExecutor();
+    final ServerSocket serverSocket = new ServerSocket(0);
+    exec.submit(
+        new Runnable()
+        {
+          @Override
+          public void run()
+          {
+            try {
+              try (
+                  Socket clientSocket = serverSocket.accept();
+                  BufferedReader in = new BufferedReader(
+                      new InputStreamReader(clientSocket.getInputStream(), 
StandardCharsets.UTF_8)
+                  )
+              ) {
+                while (!in.readLine().equals("")) {
+                  // skip lines
+                }
+                requestReceived.countDown();
+                while (in.read() != -1) {
+                  // Wait for the client to close the connection.
+                }
+              }
+              finally {
+                connectionClosed.countDown();
+              }
+
+              try (
+                  Socket clientSocket = serverSocket.accept();
+                  BufferedReader in = new BufferedReader(
+                      new InputStreamReader(clientSocket.getInputStream(), 
StandardCharsets.UTF_8)
+                  );
+                  OutputStream out = clientSocket.getOutputStream()
+              ) {
+                while (!in.readLine().equals("")) {
+                  // skip lines
+                }
+                out.write(
+                    "HTTP/1.1 200 OK\r\nContent-Length: 
6\r\n\r\nhello!".getBytes(StandardCharsets.UTF_8)
+                );
+              }
+            }
+            catch (Exception e) {
+              // Suppress
+            }
+          }
+        }
+    );
+
+    final Lifecycle lifecycle = new Lifecycle();
+    try {
+      final HttpClient client = 
HttpClientInit.createClient(HttpClientConfig.builder().build(), lifecycle);
+      final ListenableFuture<StatusResponseHolder> future = client.go(
+          new Request(
+              HttpMethod.GET,
+              new URL(StringUtils.format("http://localhost:%d/";, 
serverSocket.getLocalPort()))

Review Comment:
   ## CodeQL / Deprecated method or constructor invocation
   
   Invoking [URL.URL](1) should be avoided because it has been deprecated.
   
   [Show more 
details](https://github.com/apache/druid/security/code-scanning/11470)



##########
processing/src/test/java/org/apache/druid/java/util/http/client/FriendlyServersTest.java:
##########
@@ -115,6 +118,95 @@
     }
   }
 
+  @Test
+  public void testCancelRequestClosesConnection() throws Exception
+  {
+    final CountDownLatch requestReceived = new CountDownLatch(1);
+    final CountDownLatch connectionClosed = new CountDownLatch(1);
+    final ExecutorService exec = Executors.newSingleThreadExecutor();
+    final ExecutorService requestExec = Executors.newSingleThreadExecutor();
+    final ServerSocket serverSocket = new ServerSocket(0);
+    exec.submit(
+        new Runnable()
+        {
+          @Override
+          public void run()
+          {
+            try {
+              try (
+                  Socket clientSocket = serverSocket.accept();
+                  BufferedReader in = new BufferedReader(
+                      new InputStreamReader(clientSocket.getInputStream(), 
StandardCharsets.UTF_8)
+                  )
+              ) {
+                while (!in.readLine().equals("")) {
+                  // skip lines
+                }
+                requestReceived.countDown();
+                while (in.read() != -1) {
+                  // Wait for the client to close the connection.
+                }
+              }
+              finally {
+                connectionClosed.countDown();
+              }
+
+              try (
+                  Socket clientSocket = serverSocket.accept();
+                  BufferedReader in = new BufferedReader(
+                      new InputStreamReader(clientSocket.getInputStream(), 
StandardCharsets.UTF_8)
+                  );
+                  OutputStream out = clientSocket.getOutputStream()
+              ) {
+                while (!in.readLine().equals("")) {
+                  // skip lines
+                }
+                out.write(
+                    "HTTP/1.1 200 OK\r\nContent-Length: 
6\r\n\r\nhello!".getBytes(StandardCharsets.UTF_8)
+                );
+              }
+            }
+            catch (Exception e) {
+              // Suppress
+            }
+          }
+        }
+    );
+
+    final Lifecycle lifecycle = new Lifecycle();
+    try {
+      final HttpClient client = 
HttpClientInit.createClient(HttpClientConfig.builder().build(), lifecycle);
+      final ListenableFuture<StatusResponseHolder> future = client.go(
+          new Request(
+              HttpMethod.GET,
+              new URL(StringUtils.format("http://localhost:%d/";, 
serverSocket.getLocalPort()))
+          ),
+          StatusResponseHandler.getInstance()
+      );
+
+      Assert.assertTrue(requestReceived.await(10, TimeUnit.SECONDS));
+      Assert.assertTrue(future.cancel(true));
+      Assert.assertTrue(connectionClosed.await(10, TimeUnit.SECONDS));
+
+      final Future<StatusResponseHolder> secondResponse = requestExec.submit(
+          () -> client.go(
+              new Request(
+                  HttpMethod.GET,
+                  new URL(StringUtils.format("http://localhost:%d/";, 
serverSocket.getLocalPort()))

Review Comment:
   ## CodeQL / Deprecated method or constructor invocation
   
   Invoking [URL.URL](1) should be avoided because it has been deprecated.
   
   [Show more 
details](https://github.com/apache/druid/security/code-scanning/11471)



##########
processing/src/test/java/org/apache/druid/java/util/http/client/FriendlyServersTest.java:
##########
@@ -115,6 +118,95 @@
     }
   }
 
+  @Test
+  public void testCancelRequestClosesConnection() throws Exception
+  {
+    final CountDownLatch requestReceived = new CountDownLatch(1);
+    final CountDownLatch connectionClosed = new CountDownLatch(1);
+    final ExecutorService exec = Executors.newSingleThreadExecutor();
+    final ExecutorService requestExec = Executors.newSingleThreadExecutor();
+    final ServerSocket serverSocket = new ServerSocket(0);
+    exec.submit(
+        new Runnable()
+        {
+          @Override
+          public void run()
+          {
+            try {
+              try (
+                  Socket clientSocket = serverSocket.accept();
+                  BufferedReader in = new BufferedReader(
+                      new InputStreamReader(clientSocket.getInputStream(), 
StandardCharsets.UTF_8)
+                  )
+              ) {
+                while (!in.readLine().equals("")) {
+                  // skip lines
+                }
+                requestReceived.countDown();
+                while (in.read() != -1) {
+                  // Wait for the client to close the connection.
+                }
+              }
+              finally {
+                connectionClosed.countDown();
+              }
+
+              try (
+                  Socket clientSocket = serverSocket.accept();
+                  BufferedReader in = new BufferedReader(
+                      new InputStreamReader(clientSocket.getInputStream(), 
StandardCharsets.UTF_8)
+                  );
+                  OutputStream out = clientSocket.getOutputStream()
+              ) {
+                while (!in.readLine().equals("")) {

Review Comment:
   ## CodeQL / Inefficient empty string test
   
   Inefficient comparison to empty string, check for zero length instead.
   
   [Show more 
details](https://github.com/apache/druid/security/code-scanning/11473)



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


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

Reply via email to