001: // HttpCredential.java
002: // $Id: HttpCredential.java,v 1.15 2007/02/26 12:44:52 ylafon Exp $
003: // (c) COPYRIGHT MIT and INRIA, 1996.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005:
006: package org.w3c.www.http;
007:
008: import org.w3c.util.ArrayDictionary;
009:
010: /**
011: * This class has a hack to handle basic authentication.
012: * Basic authentication (amongst others) is broken in the HTTP spec, to handle
013: * the APIs more nicely, Jigsaw fakes a <code>cookie</code> auth param
014: * with the appropriate basic-credentials.
015: */
016:
017: public class HttpCredential extends BasicValue {
018: ArrayDictionary params = null;
019: ArrayDictionary unqparams = null;
020: String scheme = null;
021:
022: /**
023: * parse.
024: * @exception HttpParserException if parsing failed.
025: */
026: protected void parse() throws HttpParserException {
027: ParseState ps = new ParseState(roff, rlen);
028: // Get the scheme first
029: if (HttpParser.nextItem(raw, ps) < 0)
030: error("Invalid credentials: no scheme.");
031: this .scheme = ps.toString(raw);
032: // Depending on the scheme...
033: ps.prepare();
034: if (scheme.equalsIgnoreCase("basic")) {
035: // Basic Auth nasty hack
036: if (HttpParser.nextItem(raw, ps) < 0)
037: error("Invalid basic auth credentials, no basic-cookie.");
038: if (unqparams == null) {
039: unqparams = new ArrayDictionary(5, 5);
040: }
041: unqparams.put("cookie", ps.toString(raw));
042: } else {
043: // Normal credentials parsing
044: ParseState it = new ParseState();
045: it.separator = (byte) '=';
046: ps.separator = (byte) ',';
047: while (HttpParser.nextItem(raw, ps) >= 0) {
048: // Get the param name:
049: it.prepare(ps);
050: if (HttpParser.nextItem(raw, it) < 0)
051: error("Invalid credentials: bad param name.");
052: String key = it.toString(raw, true);
053: // Get the param value:
054: it.prepare();
055: if (HttpParser.nextItem(raw, it) < 0)
056: error("Invalid credentials: no param value.");
057: it.ioff = it.start;
058: int offset = HttpParser.skipSpaces(raw, it);
059: if ((offset < raw.length) && (raw[offset] == '\"')) {
060: HttpParser.unquote(raw, it);
061: if (params == null) {
062: params = new ArrayDictionary(5, 5);
063: }
064: params.put(key, it.toString(raw));
065: } else {
066: if (unqparams == null) {
067: unqparams = new ArrayDictionary(5, 5);
068: }
069: unqparams.put(key, it.toString(raw));
070: }
071: ps.prepare();
072: }
073: }
074: }
075:
076: protected void updateByteValue() {
077: HttpBuffer buf = new HttpBuffer();
078: buf.append(scheme);
079: buf.append(' ');
080: int len;
081: if (params != null) {
082: len = params.size();
083: for (int i = 0; len > 0; i++) {
084: String key = (String) params.keyAt(i);
085: if (key == null) {
086: continue;
087: }
088: if (key.equals("cookie")) {
089: buf.append((String) params.elementAt(i));
090: } else {
091: buf.appendQuoted(key, (byte) '=', (String) params
092: .elementAt(i));
093: }
094: len--;
095: if (len > 0) {
096: buf.append((byte) ',');
097: buf.append((byte) ' ');
098: }
099: }
100: }
101: if (unqparams != null) {
102: len = unqparams.size();
103: for (int i = 0; len > 0; i++) {
104: String key = (String) unqparams.keyAt(i);
105: if (key == null) {
106: continue;
107: }
108: if (key.equals("cookie")) {
109: buf.append((String) params.elementAt(i));
110: } else {
111: buf.append(key, (byte) '=', (String) params
112: .elementAt(i));
113: }
114: len--;
115: if (len > 0) {
116: buf.append((byte) ',');
117: buf.append((byte) ' ');
118: }
119: }
120: }
121: raw = buf.getByteCopy();
122: roff = 0;
123: rlen = raw.length;
124: }
125:
126: public Object getValue() {
127: validate();
128: return this ;
129: }
130:
131: /**
132: * Get the authentication scheme identifier.
133: * @return A String giving the auth scheme identifier.
134: */
135:
136: public String getScheme() {
137: validate();
138: return scheme;
139: }
140:
141: /**
142: * Set the authentication scheme.
143: * @param scheme The auth scheme for these credentials.
144: */
145:
146: public void setScheme(String scheme) {
147: if ((this .scheme != null)
148: && !this .scheme.equalsIgnoreCase(scheme))
149: invalidateByteValue();
150: this .scheme = scheme;
151: }
152:
153: /**
154: * Get an authentication parameter.
155: * @param name The name of the parameter to fetch.
156: * @return The String value, or <strong>null</strong> if undefined.
157: */
158:
159: public String getAuthParameter(String name) {
160: validate();
161: String res = null;
162: if (params != null) {
163: res = (String) params.get(name);
164: }
165: if (res == null) {
166: if (unqparams != null) {
167: res = (String) unqparams.get(name);
168: }
169: }
170: return res;
171: }
172:
173: /**
174: * Set an auth parameter value.
175: * @param name The name of the parameter to set.
176: * @param value The new value for this parameter.
177: * @param quoted If true, the value will be quoted
178: */
179:
180: public void setAuthParameter(String name, String value,
181: boolean quoted) {
182: invalidateByteValue();
183: if (quoted) {
184: if (params == null) {
185: params = new ArrayDictionary(4, 4);
186: }
187: params.put(name, value);
188: } else {
189: if (unqparams == null) {
190: unqparams = new ArrayDictionary(4, 4);
191: }
192: unqparams.put(name, value);
193: }
194: }
195:
196: /**
197: * Set an auth parameter value.
198: * @param name The name of the parameter to set.
199: * @param value The new value for this parameter.
200: * The value will be quoted
201: */
202: public void setAuthParameter(String name, String value) {
203: setAuthParameter(name, value, true);
204: }
205:
206: public HttpCredential(boolean isValid, String scheme) {
207: this .isValid = isValid;
208: this .scheme = scheme;
209: }
210:
211: public HttpCredential() {
212: this .isValid = false;
213: }
214: }
|