You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by su...@apache.org on 2022/12/30 03:24:39 UTC

[groovy] branch GROOVY_4_0_X updated: Improve robustness of `sendHEADRequest`

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

sunlan pushed a commit to branch GROOVY_4_0_X
in repository https://gitbox.apache.org/repos/asf/groovy.git


The following commit(s) were added to refs/heads/GROOVY_4_0_X by this push:
     new da4c968fb1 Improve robustness of `sendHEADRequest`
da4c968fb1 is described below

commit da4c968fb1baef6f20a3e3801a7dcde7019d2dd1
Author: Daniel Sun <su...@apache.org>
AuthorDate: Fri Dec 30 11:22:35 2022 +0800

    Improve robustness of `sendHEADRequest`
    
    (cherry picked from commit 9ab5b5034e08864e2cf9368d80500b280214d086)
---
 .../groovy/groovysh/commands/DocCommand.groovy     | 26 +++++++++++++++++-----
 1 file changed, 20 insertions(+), 6 deletions(-)

diff --git a/subprojects/groovy-groovysh/src/main/groovy/org/apache/groovy/groovysh/commands/DocCommand.groovy b/subprojects/groovy-groovysh/src/main/groovy/org/apache/groovy/groovysh/commands/DocCommand.groovy
index f3c976d14c..99da8eb2b2 100644
--- a/subprojects/groovy-groovysh/src/main/groovy/org/apache/groovy/groovysh/commands/DocCommand.groovy
+++ b/subprojects/groovy-groovysh/src/main/groovy/org/apache/groovy/groovysh/commands/DocCommand.groovy
@@ -188,6 +188,25 @@ class DocCommand extends CommandSupport {
     }
 
     protected boolean sendHEADRequest(URL url, String path = null) {
+        IOException ioe
+        // try at most 3 times
+        for (int i = 0; i < 3; i++) {
+            try {
+                return doSendHEADRequest(url, path)
+            } catch (SocketTimeoutException e) {
+                ioe = e
+            } catch (IOException e) {
+                ioe = e
+                break
+            }
+        }
+
+        if (ioe) fail "Sending a HEAD request to $url failed (${e}). Please check your network settings."
+
+        return false
+    }
+
+    private boolean doSendHEADRequest(URL url, String path = null) {
         HttpURLConnection conn = null
         try {
             conn = url.openConnection() as HttpURLConnection
@@ -200,14 +219,9 @@ class DocCommand extends CommandSupport {
             // if no path given (legacy calls from third parties), treat all redirects as suspicious
             boolean successfulRedirect = path ? conn.URL.toString().endsWith(path) : url.toString() == conn.URL.toString()
             return code == 200 && successfulRedirect
-        } catch (IOException e) {
-            fail "Sending a HEAD request to $url failed (${e}). Please check your network settings."
         } finally {
-            if (null != conn) {
-                conn.disconnect()
-            }
+            conn?.disconnect()
         }
     }
-
 }