|
28 | 28 | package org.apache.hc.client5.http.impl.auth; |
29 | 29 |
|
30 | 30 | import java.util.HashMap; |
31 | | -import java.util.Iterator; |
32 | 31 | import java.util.LinkedList; |
33 | 32 | import java.util.List; |
34 | 33 | import java.util.Locale; |
|
47 | 46 | import org.apache.hc.core5.annotation.Contract; |
48 | 47 | import org.apache.hc.core5.annotation.Internal; |
49 | 48 | import org.apache.hc.core5.annotation.ThreadingBehavior; |
50 | | -import org.apache.hc.core5.http.FormattedHeader; |
51 | 49 | import org.apache.hc.core5.http.Header; |
52 | 50 | import org.apache.hc.core5.http.HttpHeaders; |
53 | 51 | import org.apache.hc.core5.http.HttpHost; |
|
56 | 54 | import org.apache.hc.core5.http.HttpStatus; |
57 | 55 | import org.apache.hc.core5.http.ParseException; |
58 | 56 | 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; |
60 | 58 | import org.apache.hc.core5.http.protocol.HttpContext; |
61 | 59 | import org.apache.hc.core5.util.Asserts; |
62 | | -import org.apache.hc.core5.util.CharArrayBuffer; |
63 | 60 | import org.slf4j.Logger; |
64 | 61 | import org.slf4j.LoggerFactory; |
65 | 62 |
|
@@ -169,43 +166,27 @@ public Map<String, AuthChallenge> extractChallengeMap( |
169 | 166 | final HttpResponse response, |
170 | 167 | final HttpClientContext context) { |
171 | 168 | 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 | + }); |
209 | 190 | return challengeMap; |
210 | 191 | } |
211 | 192 |
|
|
0 commit comments