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:10:45 UTC

[groovy] 01/01: Improve robustness of `sendHEADRequest`

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

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

commit 0732af6c3a2337f9a66a06a4d24f7d8f79e07574
Author: Daniel Sun <su...@apache.org>
AuthorDate: Fri Dec 30 11:10:28 2022 +0800

    Improve robustness of `sendHEADRequest`
---
 .../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()
         }
     }
-
 }