Skip to content

Commit 49549ab

Browse files
committed
Auth challenge parsing code improvement
1 parent fa6b6d7 commit 49549ab

File tree

1 file changed

+22
-41
lines changed

1 file changed

+22
-41
lines changed

httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/AuthenticationHandler.java

Lines changed: 22 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
package org.apache.hc.client5.http.impl.auth;
2929

3030
import java.util.HashMap;
31-
import java.util.Iterator;
3231
import java.util.LinkedList;
3332
import java.util.List;
3433
import java.util.Locale;
@@ -47,7 +46,6 @@
4746
import org.apache.hc.core5.annotation.Contract;
4847
import org.apache.hc.core5.annotation.Internal;
4948
import org.apache.hc.core5.annotation.ThreadingBehavior;
50-
import org.apache.hc.core5.http.FormattedHeader;
5149
import org.apache.hc.core5.http.Header;
5250
import org.apache.hc.core5.http.HttpHeaders;
5351
import org.apache.hc.core5.http.HttpHost;
@@ -56,10 +54,9 @@
5654
import org.apache.hc.core5.http.HttpStatus;
5755
import org.apache.hc.core5.http.ParseException;
5856
import org.apache.hc.core5.http.message.BasicHeader;
59-
import org.apache.hc.core5.http.message.ParserCursor;
57+
import org.apache.hc.core5.http.message.MessageSupport;
6058
import org.apache.hc.core5.http.protocol.HttpContext;
6159
import org.apache.hc.core5.util.Asserts;
62-
import org.apache.hc.core5.util.CharArrayBuffer;
6360
import org.slf4j.Logger;
6461
import org.slf4j.LoggerFactory;
6562

@@ -169,43 +166,27 @@ public Map<String, AuthChallenge> extractChallengeMap(
169166
final HttpResponse response,
170167
final HttpClientContext context) {
171168
final Map<String, AuthChallenge> challengeMap = new HashMap<>();
172-
final Iterator<Header> headerIterator = response.headerIterator(
173-
challengeType == ChallengeType.PROXY ? HttpHeaders.PROXY_AUTHENTICATE : HttpHeaders.WWW_AUTHENTICATE);
174-
while (headerIterator.hasNext()) {
175-
final Header header = headerIterator.next();
176-
final CharArrayBuffer buffer;
177-
final int pos;
178-
if (header instanceof FormattedHeader) {
179-
buffer = ((FormattedHeader) header).getBuffer();
180-
pos = ((FormattedHeader) header).getValuePos();
181-
} else {
182-
final String s = header.getValue();
183-
if (s == null) {
184-
continue;
185-
}
186-
buffer = new CharArrayBuffer(s.length());
187-
buffer.append(s);
188-
pos = 0;
189-
}
190-
final ParserCursor cursor = new ParserCursor(pos, buffer.length());
191-
final List<AuthChallenge> authChallenges;
192-
try {
193-
authChallenges = parser.parse(challengeType, buffer, cursor);
194-
} catch (final ParseException ex) {
195-
if (LOG.isWarnEnabled()) {
196-
final HttpClientContext clientContext = HttpClientContext.cast(context);
197-
final String exchangeId = clientContext.getExchangeId();
198-
LOG.warn("{} Malformed challenge: {}", exchangeId, header.getValue());
199-
}
200-
continue;
201-
}
202-
for (final AuthChallenge authChallenge : authChallenges) {
203-
final String schemeName = authChallenge.getSchemeName().toLowerCase(Locale.ROOT);
204-
if (!challengeMap.containsKey(schemeName)) {
205-
challengeMap.put(schemeName, authChallenge);
206-
}
207-
}
208-
}
169+
MessageSupport.parseHeaders(
170+
response,
171+
challengeType == ChallengeType.PROXY ? HttpHeaders.PROXY_AUTHENTICATE : HttpHeaders.WWW_AUTHENTICATE,
172+
(buffer, cursor) -> {
173+
try {
174+
final List<AuthChallenge> authChallenges = parser.parse(challengeType, buffer, cursor);
175+
for (final AuthChallenge authChallenge : authChallenges) {
176+
final String schemeName = authChallenge.getSchemeName().toLowerCase(Locale.ROOT);
177+
if (!challengeMap.containsKey(schemeName)) {
178+
challengeMap.put(schemeName, authChallenge);
179+
}
180+
}
181+
} catch (final ParseException ex) {
182+
if (LOG.isWarnEnabled()) {
183+
final HttpClientContext clientContext = HttpClientContext.cast(context);
184+
final String exchangeId = clientContext.getExchangeId();
185+
LOG.warn("{} Malformed challenge", exchangeId);
186+
}
187+
}
188+
189+
});
209190
return challengeMap;
210191
}
211192

0 commit comments

Comments
 (0)