2
* $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/auth/AuthChallengeParser.java,v 1.4.2.2 2004/02/22 18:21:14 olegk Exp $
4
* $Date: 2004/02/22 18:21:14 $
6
* ====================================================================
8
* Copyright 2002-2004 The Apache Software Foundation
10
* Licensed under the Apache License, Version 2.0 (the "License");
11
* you may not use this file except in compliance with the License.
12
* You may obtain a copy of the License at
14
* http://www.apache.org/licenses/LICENSE-2.0
16
* Unless required by applicable law or agreed to in writing, software
17
* distributed under the License is distributed on an "AS IS" BASIS,
18
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
* See the License for the specific language governing permissions and
20
* limitations under the License.
21
* ====================================================================
23
* This software consists of voluntary contributions made by many
24
* individuals on behalf of the Apache Software Foundation. For more
25
* information on the Apache Software Foundation, please see
26
* <http://www.apache.org/>.
28
* [Additional notices, if required by prior licensing conditions]
32
package org.apache.commons.httpclient.auth;
35
import java.util.HashMap;
38
* This class provides utility methods for parsing HTTP www and proxy authentication
41
* @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
45
public final class AuthChallengeParser {
47
* Extracts authentication scheme from the given authentication
50
* @param challengeStr the authentication challenge string
51
* @return authentication scheme
53
* @throws MalformedChallengeException when the authentication challenge string
58
public static String extractScheme(final String challengeStr)
59
throws MalformedChallengeException {
60
if (challengeStr == null) {
61
throw new IllegalArgumentException("Challenge may not be null");
63
int i = challengeStr.indexOf(' ');
68
s = challengeStr.substring(0, i);
71
throw new MalformedChallengeException("Invalid challenge: " + challengeStr);
73
return s.toLowerCase();
77
* Extracts a map of challenge parameters from an authentication challenge.
78
* Keys in the map are lower-cased
80
* @param challengeStr the authentication challenge string
81
* @return a map of authentication challenge parameters
82
* @throws MalformedChallengeException when the authentication challenge string
87
public static Map extractParams(final String challengeStr)
88
throws MalformedChallengeException {
89
if (challengeStr == null) {
90
throw new IllegalArgumentException("Challenge may not be null");
92
int i = challengeStr.indexOf(' ');
94
throw new MalformedChallengeException("Invalid challenge: " + challengeStr);
97
Map elements = new HashMap();
100
int len = challengeStr.length();
105
StringBuffer buffer = new StringBuffer();
107
boolean parsingName = true;
108
boolean inQuote = false;
109
boolean gotIt = false;
112
// Parse one char at a time
113
char ch = challengeStr.charAt(i);
119
name = buffer.toString().trim();
122
} else if (ch == ',') {
123
name = buffer.toString().trim();
130
// Have I reached the end of the challenge string?
132
name = buffer.toString().trim();
139
// Value is not quoted or not found yet
141
value = buffer.toString().trim();
146
if (buffer.length() == 0) {
149
} else if (ch == '\t') {
151
} else if (ch == '\n') {
153
} else if (ch == '\r') {
156
// otherwise add to the buffer
163
// already got something
164
// just keep on adding to the buffer
170
// Keep on adding until closing quote is encountered
176
// Have I reached the end of the challenge string?
178
value = buffer.toString().trim();
184
if ((name == null) || (name.equals(""))) {
185
throw new MalformedChallengeException("Invalid challenge: " + challengeStr);
187
// Strip quotes when present
188
if ((value != null) && (value.length() > 1)) {
189
if ((value.charAt(0) == '"')
190
&& (value.charAt(value.length() - 1) == '"')) {
191
value = value.substring(1, value.length() - 1);
195
elements.put(name.toLowerCase(), value);