From ba951cb6e301a56c673f00395e08e87d80422da3 Mon Sep 17 00:00:00 2001 From: Cyrille Le Clerc Date: Sun, 6 Mar 2016 18:09:36 +0100 Subject: [PATCH] Fix #252: infinite loop because the "hypertext engine" may duplicate '?' generating invalid "https://api.github.com/notifications?all=true&page=2?all=true" instead of "https://api.github.com/notifications?all=true&page=2&all=true". A better fix will be to prevent duplication of parameters ("all=true" in this case). --- .../java/org/kohsuke/github/Requester.java | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/kohsuke/github/Requester.java b/src/main/java/org/kohsuke/github/Requester.java index af4819c3a1..5096f5ff49 100644 --- a/src/main/java/org/kohsuke/github/Requester.java +++ b/src/main/java/org/kohsuke/github/Requester.java @@ -45,6 +45,7 @@ import java.util.Iterator; import java.util.LinkedHashMap; import java.util.List; +import java.util.ListIterator; import java.util.Locale; import java.util.Map; import java.util.NoSuchElementException; @@ -219,15 +220,19 @@ public T to(String tailApiUrl, Class type, String method) throws IOExcept } private T _to(String tailApiUrl, Class type, T instance) throws IOException { - while (true) {// loop while API rate limit is hit - if (METHODS_WITHOUT_BODY.contains(method) && !args.isEmpty()) { - StringBuilder qs=new StringBuilder(); - for (Entry arg : args) { - qs.append(qs.length()==0 ? '?' : '&'); - qs.append(arg.key).append('=').append(URLEncoder.encode(arg.value.toString(),"UTF-8")); + if (METHODS_WITHOUT_BODY.contains(method) && !args.isEmpty()) { + boolean questionMarkFound = tailApiUrl.indexOf('?') != -1; + tailApiUrl += questionMarkFound ? '&' : '?'; + for (Iterator it = args.listIterator(); it.hasNext();) { + Entry arg = it.next(); + tailApiUrl += arg.key + '=' + URLEncoder.encode(arg.value.toString(),"UTF-8"); + if (it.hasNext()) { + tailApiUrl += '&'; } - tailApiUrl += qs.toString(); } + } + + while (true) {// loop while API rate limit is hit setupConnection(root.getApiURL(tailApiUrl)); buildRequest();