1 31 32 package org.apache.commons.httpclient.auth; 33 34 import java.util.Map ; 35 import java.util.HashMap ; 36 37 45 public final class AuthChallengeParser { 46 58 public static String extractScheme(final String challengeStr) 59 throws MalformedChallengeException { 60 if (challengeStr == null) { 61 throw new IllegalArgumentException ("Challenge may not be null"); 62 } 63 int i = challengeStr.indexOf(' '); 64 String s = null; 65 if (i == -1) { 66 s = challengeStr; 67 } else { 68 s = challengeStr.substring(0, i); 69 } 70 if (s.equals("")) { 71 throw new MalformedChallengeException("Invalid challenge: " + challengeStr); 72 } 73 return s.toLowerCase(); 74 } 75 76 87 public static Map extractParams(final String challengeStr) 88 throws MalformedChallengeException { 89 if (challengeStr == null) { 90 throw new IllegalArgumentException ("Challenge may not be null"); 91 } 92 int i = challengeStr.indexOf(' '); 93 if (i == -1) { 94 throw new MalformedChallengeException("Invalid challenge: " + challengeStr); 95 } 96 97 Map elements = new HashMap (); 98 99 i++; 100 int len = challengeStr.length(); 101 102 String name = null; 103 String value = null; 104 105 StringBuffer buffer = new StringBuffer (); 106 107 boolean parsingName = true; 108 boolean inQuote = false; 109 boolean gotIt = false; 110 111 while (i < len) { 112 char ch = challengeStr.charAt(i); 114 i++; 115 if (parsingName) { 117 if (ch == '=') { 119 name = buffer.toString().trim(); 120 parsingName = false; 121 buffer.setLength(0); 122 } else if (ch == ',') { 123 name = buffer.toString().trim(); 124 value = null; 125 gotIt = true; 126 buffer.setLength(0); 127 } else { 128 buffer.append(ch); 129 } 130 if (i == len) { 132 name = buffer.toString().trim(); 133 value = null; 134 gotIt = true; 135 } 136 } else { 137 if (!inQuote) { 139 if (ch == ',') { 141 value = buffer.toString().trim(); 142 gotIt = true; 143 buffer.setLength(0); 144 } else { 145 if (buffer.length() == 0) { 147 if (ch == ' ') { 148 } else if (ch == '\t') { 150 } else if (ch == '\n') { 152 } else if (ch == '\r') { 154 } else { 156 buffer.append(ch); 158 if (ch == '"') { 159 inQuote = true; 160 } 161 } 162 } else { 163 buffer.append(ch); 166 } 167 } 168 } else { 169 buffer.append(ch); 172 if (ch == '"') { 173 inQuote = false; 174 } 175 } 176 if (i == len) { 178 value = buffer.toString().trim(); 179 gotIt = true; 180 } 181 } 182 if (gotIt) { 183 if ((name == null) || (name.equals(""))) { 185 throw new MalformedChallengeException("Invalid challenge: " + challengeStr); 186 } 187 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); 192 } 193 } 194 195 elements.put(name.toLowerCase(), value); 196 parsingName = true; 197 gotIt = false; 198 } 199 } 200 return elements; 201 } 202 } 203 | Popular Tags |