001: package com.quadcap.http.server22;
002:
003: /* Copyright 1998 - 2003 Quadcap Software. All rights reserved.
004: *
005: * This software is distributed under the Quadcap Free Software License.
006: * This software may be used or modified for any purpose, personal or
007: * commercial. Open Source redistributions are permitted. Commercial
008: * redistribution of larger works derived from, or works which bundle
009: * this software requires a "Commercial Redistribution License"; see
010: * http://www.quadcap.com/purchase.
011: *
012: * Redistributions qualify as "Open Source" under one of the following terms:
013: *
014: * Redistributions are made at no charge beyond the reasonable cost of
015: * materials and delivery.
016: *
017: * Redistributions are accompanied by a copy of the Source Code or by an
018: * irrevocable offer to provide a copy of the Source Code for up to three
019: * years at the cost of materials and delivery. Such redistributions
020: * must allow further use, modification, and redistribution of the Source
021: * Code under substantially the same terms as this license.
022: *
023: * Redistributions of source code must retain the copyright notices as they
024: * appear in each source code file, these license terms, and the
025: * disclaimer/limitation of liability set forth as paragraph 6 below.
026: *
027: * Redistributions in binary form must reproduce this Copyright Notice,
028: * these license terms, and the disclaimer/limitation of liability set
029: * forth as paragraph 6 below, in the documentation and/or other materials
030: * provided with the distribution.
031: *
032: * The Software is provided on an "AS IS" basis. No warranty is
033: * provided that the Software is free of defects, or fit for a
034: * particular purpose.
035: *
036: * Limitation of Liability. Quadcap Software shall not be liable
037: * for any damages suffered by the Licensee or any third party resulting
038: * from use of the Software.
039: */
040:
041: import java.io.ByteArrayInputStream;
042: import java.io.IOException;
043:
044: import java.util.Vector;
045:
046: import javax.servlet.http.Cookie;
047:
048: import com.quadcap.util.text.OctetMap;
049: import com.quadcap.util.text.Scanner;
050:
051: import com.quadcap.util.Debug;
052:
053: /**
054: * Parse a string containing HTTP cookies.
055: *
056: * @author Stan Bailes
057: */
058: public class CookieParser {
059: Scanner s = null;
060:
061: public CookieParser(String cookieHdr) {
062: if (cookieHdr == null)
063: cookieHdr = "";
064: ByteArrayInputStream bis = new ByteArrayInputStream(cookieHdr
065: .getBytes());
066: s = new Scanner(bis);
067: }
068:
069: Cookie[] parseCookies() throws IOException {
070: Vector cookieVec = new Vector();
071: Cookie cookie = null;
072: s.skipWhile(OctetMap.wsChars);
073: while (OctetMap.tokenChars.has(s.peek())) {
074: String name = s.parseWhile(OctetMap.tokenChars);
075: String val = null;
076: if (s.peek() == '=') {
077: s.matchChar('=');
078: if (s.peek() == '"') {
079: s.matchChar('"');
080: val = s.parseUntil(OctetMap.dquoteChar);
081: s.matchChar('"');
082: } else {
083: val = s.parseWhile(OctetMap.tokenChars);
084: }
085: }
086: s.skipWhile(OctetMap.wsChars);
087: if (s.peek() == ';') {
088: s.matchChar(';');
089: s.skipWhile(OctetMap.wsChars);
090: }
091:
092: if (name.charAt(0) == '$' && cookie != null) {
093: if (name.equals("$Path")) {
094: cookie.setPath(val);
095: } else if (name.equals("$Domain")) {
096: cookie.setDomain(val);
097: }
098: } else {
099: //#ifdef DEBUG
100: if (Trace.level() > 2) {
101: Debug.println("Cookie: " + name + " = " + val);
102: }
103: //#endif
104: cookie = new Cookie(name, val);
105: cookieVec.addElement(cookie);
106: }
107: }
108: Cookie[] cookies = new Cookie[cookieVec.size()];
109: for (int i = 0; i < cookies.length; i++) {
110: cookies[i] = (Cookie) cookieVec.elementAt(i);
111: }
112: return cookies;
113: }
114: }
|