1 /*
2 * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/auth/AuthChallengeParser.java,v 1.4.2.1 2003/11/24 08:48:25 oglueck Exp $
3 * $Revision: 1.4.2.1 $
4 * $Date: 2003/11/24 08:48:25 $
5 *
6 * ====================================================================
7 *
8 * The Apache Software License, Version 1.1
9 *
10 * Copyright (c) 2002-2003 The Apache Software Foundation. All rights
11 * reserved.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 *
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 *
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in
22 * the documentation and/or other materials provided with the
23 * distribution.
24 *
25 * 3. The end-user documentation included with the redistribution, if
26 * any, must include the following acknowlegement:
27 * "This product includes software developed by the
28 * Apache Software Foundation (http://www.apache.org/)."
29 * Alternately, this acknowlegement may appear in the software itself,
30 * if and wherever such third-party acknowlegements normally appear.
31 *
32 * 4. The names "The Jakarta Project", "Commons", and "Apache Software
33 * Foundation" must not be used to endorse or promote products derived
34 * from this software without prior written permission. For written
35 * permission, please contact apache@apache.org.
36 *
37 * 5. Products derived from this software may not be called "Apache"
38 * nor may "Apache" appear in their names without prior written
39 * permission of the Apache Group.
40 *
41 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
42 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
43 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
44 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
45 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
47 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
48 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
49 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
51 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 * SUCH DAMAGE.
53 * ====================================================================
54 *
55 * This software consists of voluntary contributions made by many
56 * individuals on behalf of the Apache Software Foundation. For more
57 * information on the Apache Software Foundation, please see
58 * <http://www.apache.org/>.
59 *
60 * [Additional notices, if required by prior licensing conditions]
61 *
62 */
63
64 package org.apache.commons.httpclient.auth;
65
66 import java.util.Map;
67 import java.util.HashMap;
68
69 /***
70 * This class provides utility methods for parsing HTTP www and proxy authentication
71 * challenges.
72 *
73 * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
74 *
75 * @since 2.0beta1
76 */
77 public final class AuthChallengeParser {
78 /***
79 * Extracts authentication scheme from the given authentication
80 * challenge.
81 *
82 * @param challengeStr the authentication challenge string
83 * @return authentication scheme
84 *
85 * @throws MalformedChallengeException when the authentication challenge string
86 * is malformed
87 *
88 * @since 2.0beta1
89 */
90 public static String extractScheme(final String challengeStr)
91 throws MalformedChallengeException {
92 if (challengeStr == null) {
93 throw new IllegalArgumentException("Challenge may not be null");
94 }
95 int i = challengeStr.indexOf(' ');
96 String s = null;
97 if (i == -1) {
98 s = challengeStr;
99 } else {
100 s = challengeStr.substring(0, i);
101 }
102 if (s.equals("")) {
103 throw new MalformedChallengeException("Invalid challenge: " + challengeStr);
104 }
105 return s.toLowerCase();
106 }
107
108 /***
109 * Extracts a map of challenge parameters from an authentication challenge.
110 * Keys in the map are lower-cased
111 *
112 * @param challengeStr the authentication challenge string
113 * @return a map of authentication challenge parameters
114 * @throws MalformedChallengeException when the authentication challenge string
115 * is malformed
116 *
117 * @since 2.0beta1
118 */
119 public static Map extractParams(final String challengeStr)
120 throws MalformedChallengeException {
121 if (challengeStr == null) {
122 throw new IllegalArgumentException("Challenge may not be null");
123 }
124 int i = challengeStr.indexOf(' ');
125 if (i == -1) {
126 throw new MalformedChallengeException("Invalid challenge: " + challengeStr);
127 }
128
129 Map elements = new HashMap();
130
131 i++;
132 int len = challengeStr.length();
133
134 String name = null;
135 String value = null;
136
137 StringBuffer buffer = new StringBuffer();
138
139 boolean parsingName = true;
140 boolean inQuote = false;
141 boolean gotIt = false;
142
143 while (i < len) {
144 // Parse one char at a time
145 char ch = challengeStr.charAt(i);
146 i++;
147 // Process the char
148 if (parsingName) {
149 // parsing name
150 if (ch == '=') {
151 name = buffer.toString().trim();
152 parsingName = false;
153 buffer.setLength(0);
154 } else if (ch == ',') {
155 name = buffer.toString().trim();
156 value = null;
157 gotIt = true;
158 buffer.setLength(0);
159 } else {
160 buffer.append(ch);
161 }
162 // Have I reached the end of the challenge string?
163 if (i == len) {
164 name = buffer.toString().trim();
165 value = null;
166 gotIt = true;
167 }
168 } else {
169 //parsing value
170 if (!inQuote) {
171 // Value is not quoted or not found yet
172 if (ch == ',') {
173 value = buffer.toString().trim();
174 gotIt = true;
175 buffer.setLength(0);
176 } else {
177 // no value yet
178 if (buffer.length() == 0) {
179 if (ch == ' ') {
180 //discard
181 } else if (ch == '\t') {
182 //discard
183 } else if (ch == '\n') {
184 //discard
185 } else if (ch == '\r') {
186 //discard
187 } else {
188 // otherwise add to the buffer
189 buffer.append(ch);
190 if (ch == '"') {
191 inQuote = true;
192 }
193 }
194 } else {
195 // already got something
196 // just keep on adding to the buffer
197 buffer.append(ch);
198 }
199 }
200 } else {
201 // Value is quoted
202 // Keep on adding until closing quote is encountered
203 buffer.append(ch);
204 if (ch == '"') {
205 inQuote = false;
206 }
207 }
208 // Have I reached the end of the challenge string?
209 if (i == len) {
210 value = buffer.toString().trim();
211 gotIt = true;
212 }
213 }
214 if (gotIt) {
215 // Got something
216 if ((name == null) || (name.equals(""))) {
217 throw new MalformedChallengeException("Invalid challenge: " + challengeStr);
218 }
219 // Strip quotes when present
220 if ((value != null) && (value.length() > 1)) {
221 if ((value.charAt(0) == '"')
222 && (value.charAt(value.length() - 1) == '"')) {
223 value = value.substring(1, value.length() - 1);
224 }
225 }
226
227 elements.put(name.toLowerCase(), value);
228 parsingName = true;
229 gotIt = false;
230 }
231 }
232 return elements;
233 }
234 }
This page was automatically generated by Maven