From 04ae33c4ac9821b49e071b4f25f843e56a2cb748 Mon Sep 17 00:00:00 2001 From: Lachlan Roberts Date: Wed, 25 Apr 2018 16:09:55 +1000 Subject: [PATCH] Switched getHeaderInfo from recursion to iteration. Signed-off-by: Lachlan Roberts --- .../client/AuthenticationProtocolHandler.java | 40 +++++++++---------- .../client/HttpClientAuthenticationTest.java | 5 ++- 2 files changed, 22 insertions(+), 23 deletions(-) diff --git a/jetty-client/src/main/java/org/eclipse/jetty/client/AuthenticationProtocolHandler.java b/jetty-client/src/main/java/org/eclipse/jetty/client/AuthenticationProtocolHandler.java index 0baec4a7407..c6a160ea214 100644 --- a/jetty-client/src/main/java/org/eclipse/jetty/client/AuthenticationProtocolHandler.java +++ b/jetty-client/src/main/java/org/eclipse/jetty/client/AuthenticationProtocolHandler.java @@ -49,9 +49,8 @@ public abstract class AuthenticationProtocolHandler implements ProtocolHandler private static final Pattern PARAM_PATTERN = Pattern.compile("([^=]+)=([^=]+)?"); private static final Pattern TYPE_PATTERN = Pattern.compile("([^\\s]+)(\\s+(.*))?"); - private static final Pattern MULTIPLE_CHALLENGE_PATTERN = Pattern.compile("(.*),\\s*([^=\\s,]+(\\s+[^=\\s].*)?)"); + private static final Pattern MULTIPLE_CHALLENGE_PATTERN = Pattern.compile("(.*?)\\s*,\\s*([^=\\s,]+(\\s+[^=\\s].*)?)"); private static final Pattern BASE64_PATTERN = Pattern.compile("[\\+\\-\\.\\/\\dA-Z_a-z~]+=*"); - private static final int MAX_DEPTH = 10; private final HttpClient client; private final int maxContentLength; @@ -84,30 +83,29 @@ public abstract class AuthenticationProtocolHandler implements ProtocolHandler return new AuthenticationListener(); } + + protected List getHeaderInfo(String value) throws IllegalArgumentException { - return getHeaderInfo(value, 0); - } - - protected List getHeaderInfo(String value, int depth) throws IllegalArgumentException - { - if(depth > MAX_DEPTH) - throw new IllegalStateException("too many challanges"); + String header = value; + List headerInfos = new ArrayList<>(); - Matcher m = MULTIPLE_CHALLENGE_PATTERN.matcher(value); - if (m.matches()) + while(true) { - List l = new ArrayList<>(); - l.addAll(getHeaderInfo(m.group(1), depth+1)); - l.addAll(getHeaderInfo(m.group(2), depth+1)); - return l; - } - else - { - List l = new ArrayList<>(); - l.add(newHeaderInfo(value)); - return l; + Matcher m = MULTIPLE_CHALLENGE_PATTERN.matcher(header); + if (m.matches()) + { + headerInfos.add(newHeaderInfo(m.group(1))); + header = m.group(2); + } + else + { + headerInfos.add(newHeaderInfo(header)); + break; + } } + + return headerInfos; } protected HeaderInfo newHeaderInfo(String value) throws IllegalArgumentException diff --git a/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientAuthenticationTest.java b/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientAuthenticationTest.java index c3f04e82e28..ccaacd51dbd 100644 --- a/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientAuthenticationTest.java +++ b/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientAuthenticationTest.java @@ -689,10 +689,11 @@ public class HttpClientAuthenticationTest extends AbstractHttpClientServerTest Assert.assertTrue(headerInfo.getType().equalsIgnoreCase("Scheme")); Assert.assertTrue(headerInfo.getParameter("realm") == null); - List headerInfos = aph.getHeaderInfo("Scheme1 , Scheme2"); - Assert.assertEquals(headerInfos.size(), 2); + List headerInfos = aph.getHeaderInfo("Scheme1 , Scheme2 , Scheme3"); + Assert.assertEquals(3, headerInfos.size()); Assert.assertTrue(headerInfos.get(0).getType().equalsIgnoreCase("Scheme1")); Assert.assertTrue(headerInfos.get(1).getType().equalsIgnoreCase("Scheme2")); + Assert.assertTrue(headerInfos.get(2).getType().equalsIgnoreCase("Scheme3")); headerInfo = aph.getHeaderInfo("Scheme name=\"value\", other=\"value2\"").get(0); Assert.assertTrue(headerInfo.getType().equalsIgnoreCase("Scheme"));