ChallengeInt parseChallenge()

in httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/AuthChallengeParser.java [125:169]


    ChallengeInt parseChallenge(
            final CharSequence buffer,
            final ParserCursor cursor,
            final ChallengeInt currentChallenge) throws ParseException {
        for (;;) {
            tokenParser.skipWhiteSpace(buffer, cursor);
            if (cursor.atEnd()) {
                return null;
            }
            final String token = parseToken(buffer, cursor);
            if (TextUtils.isBlank(token)) {
                throw new ParseException("Malformed auth challenge");
            }
            tokenParser.skipWhiteSpace(buffer, cursor);

            // it gets really messy here
            if (cursor.atEnd()) {
                // at the end of the header
                currentChallenge.params.add(new BasicNameValuePair(token, null));
            } else {
                char ch = buffer.charAt(cursor.getPos());
                if (ch == EQUAL_CHAR) {
                    cursor.updatePos(cursor.getPos() + 1);
                    final String value = tokenParser.parseValue(buffer, cursor, DELIMITER);
                    tokenParser.skipWhiteSpace(buffer, cursor);
                    if (!cursor.atEnd()) {
                        ch = buffer.charAt(cursor.getPos());
                        if (ch == COMMA_CHAR) {
                            cursor.updatePos(cursor.getPos() + 1);
                        }
                    }
                    currentChallenge.params.add(new BasicNameValuePair(token, value));
                } else if (ch == COMMA_CHAR) {
                    cursor.updatePos(cursor.getPos() + 1);
                    currentChallenge.params.add(new BasicNameValuePair(token, null));
                } else {
                    // the token represents new challenge
                    if (currentChallenge.params.isEmpty()) {
                        throw new ParseException("Malformed auth challenge");
                    }
                    return new ChallengeInt(token);
                }
            }
        }
    }