001: package com.quadcap.http.util;
002:
003: /* Copyright 1999 - 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.IOException;
042: import java.io.InputStream;
043:
044: import java.util.HashMap;
045: import java.util.Map;
046:
047: import com.quadcap.util.text.OctetMap;
048: import com.quadcap.util.text.Scanner;
049:
050: import com.quadcap.util.Debug;
051:
052: /**
053: * An HTTP header parser.
054: *
055: * @author Stan Bailes
056: */
057: public class HeaderParser {
058:
059: /**
060: * Parsing helper: Get the next bytes all of which are in the
061: * specified map
062: *
063: * @param scanner the current scanner input source
064: * @param map the octet map specifying the bytes we want
065: */
066: public static String getToken(Scanner scanner, OctetMap map)
067: throws IOException {
068: scanner.skipWhile(OctetMap.wsChars);
069: return scanner.parseWhile(map);
070: }
071:
072: /**
073: * Parse the HTTP headers in this input source
074: *
075: * @exception IOException may be thrown
076: */
077: public static void parseHeaders(Scanner scanner, Map headers)
078: throws IOException {
079:
080: boolean done = false;
081: String name = null, val = null;
082: while (!done) {
083: int c = scanner.peek();
084: if (c == -1)
085: break;
086: if (OctetMap.wsChars.has(c)) {
087: if (val != null) {
088: val = val
089: + " "
090: + getToken(scanner,
091: OctetMap.fieldValueChars);
092: } else {
093: throw new IOException("Unexpected ws in headers");
094: }
095: } else {
096: if (val != null) {
097: headers.put(name, val);
098: val = null;
099: }
100:
101: if (c == '\r') {
102: done = true;
103: } else if (c == '\n') {
104: scanner.matchChar('\n');
105: break;
106: } else {
107: name = getToken(scanner, OctetMap.tokenChars)
108: .toLowerCase();
109: scanner.matchChar(':');
110: val = getToken(scanner, OctetMap.fieldValueChars);
111: }
112: }
113: parseCRLF(scanner);
114: }
115: if (val != null) {
116: headers.put(name, val);
117: }
118: }
119:
120: /**
121: * Skip trailing whitespace followed by CRLF
122: *
123: * @param scanner the current scanner input source
124: * @exception IOException if anything other than CRLF is found
125: */
126: public static void parseCRLF(Scanner scanner) throws IOException {
127: scanner.skipWhile(OctetMap.wsChars);
128: if (scanner.peek() == '\n') {
129: scanner.matchChar('\n');
130: } else {
131: while (scanner.peek() == '\r')
132: scanner.matchChar('\r');
133: scanner.matchChar('\n');
134: }
135: }
136:
137: /**
138: * Utility method to parse HTTP headers from an input stream, leaving
139: * the stream posisitioned after the blank line following the headers.
140: */
141: public static Map parseHeaders(InputStream is) throws IOException {
142: Map map = new HashMap();
143: Scanner scanner = new Scanner(is, false);
144: parseHeaders(scanner, map);
145: return map;
146: }
147:
148: }
|