Switched getHeaderInfo from recursion to iteration.

Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
This commit is contained in:
Lachlan Roberts 2018-04-25 16:09:55 +10:00
parent 7adad0bdc6
commit 04ae33c4ac
2 changed files with 22 additions and 23 deletions

View File

@ -49,9 +49,8 @@ public abstract class AuthenticationProtocolHandler implements ProtocolHandler
private static final Pattern PARAM_PATTERN = Pattern.compile("([^=]+)=([^=]+)?"); private static final Pattern PARAM_PATTERN = Pattern.compile("([^=]+)=([^=]+)?");
private static final Pattern TYPE_PATTERN = Pattern.compile("([^\\s]+)(\\s+(.*))?"); 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 Pattern BASE64_PATTERN = Pattern.compile("[\\+\\-\\.\\/\\dA-Z_a-z~]+=*");
private static final int MAX_DEPTH = 10;
private final HttpClient client; private final HttpClient client;
private final int maxContentLength; private final int maxContentLength;
@ -84,30 +83,29 @@ public abstract class AuthenticationProtocolHandler implements ProtocolHandler
return new AuthenticationListener(); return new AuthenticationListener();
} }
protected List<HeaderInfo> getHeaderInfo(String value) throws IllegalArgumentException protected List<HeaderInfo> getHeaderInfo(String value) throws IllegalArgumentException
{ {
return getHeaderInfo(value, 0); String header = value;
} List<HeaderInfo> headerInfos = new ArrayList<>();
protected List<HeaderInfo> getHeaderInfo(String value, int depth) throws IllegalArgumentException
{
if(depth > MAX_DEPTH)
throw new IllegalStateException("too many challanges");
Matcher m = MULTIPLE_CHALLENGE_PATTERN.matcher(value); while(true)
if (m.matches())
{ {
List<HeaderInfo> l = new ArrayList<>(); Matcher m = MULTIPLE_CHALLENGE_PATTERN.matcher(header);
l.addAll(getHeaderInfo(m.group(1), depth+1)); if (m.matches())
l.addAll(getHeaderInfo(m.group(2), depth+1)); {
return l; headerInfos.add(newHeaderInfo(m.group(1)));
} header = m.group(2);
else }
{ else
List<HeaderInfo> l = new ArrayList<>(); {
l.add(newHeaderInfo(value)); headerInfos.add(newHeaderInfo(header));
return l; break;
}
} }
return headerInfos;
} }
protected HeaderInfo newHeaderInfo(String value) throws IllegalArgumentException protected HeaderInfo newHeaderInfo(String value) throws IllegalArgumentException

View File

@ -689,10 +689,11 @@ public class HttpClientAuthenticationTest extends AbstractHttpClientServerTest
Assert.assertTrue(headerInfo.getType().equalsIgnoreCase("Scheme")); Assert.assertTrue(headerInfo.getType().equalsIgnoreCase("Scheme"));
Assert.assertTrue(headerInfo.getParameter("realm") == null); Assert.assertTrue(headerInfo.getParameter("realm") == null);
List<HeaderInfo> headerInfos = aph.getHeaderInfo("Scheme1 , Scheme2"); List<HeaderInfo> headerInfos = aph.getHeaderInfo("Scheme1 , Scheme2 , Scheme3");
Assert.assertEquals(headerInfos.size(), 2); Assert.assertEquals(3, headerInfos.size());
Assert.assertTrue(headerInfos.get(0).getType().equalsIgnoreCase("Scheme1")); Assert.assertTrue(headerInfos.get(0).getType().equalsIgnoreCase("Scheme1"));
Assert.assertTrue(headerInfos.get(1).getType().equalsIgnoreCase("Scheme2")); 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); headerInfo = aph.getHeaderInfo("Scheme name=\"value\", other=\"value2\"").get(0);
Assert.assertTrue(headerInfo.getType().equalsIgnoreCase("Scheme")); Assert.assertTrue(headerInfo.getType().equalsIgnoreCase("Scheme"));