KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > http > util > HeaderParser


1 package com.quadcap.http.util;
2
3 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
4  *
5  * This software is distributed under the Quadcap Free Software License.
6  * This software may be used or modified for any purpose, personal or
7  * commercial. Open Source redistributions are permitted. Commercial
8  * redistribution of larger works derived from, or works which bundle
9  * this software requires a "Commercial Redistribution License"; see
10  * http://www.quadcap.com/purchase.
11  *
12  * Redistributions qualify as "Open Source" under one of the following terms:
13  *
14  * Redistributions are made at no charge beyond the reasonable cost of
15  * materials and delivery.
16  *
17  * Redistributions are accompanied by a copy of the Source Code or by an
18  * irrevocable offer to provide a copy of the Source Code for up to three
19  * years at the cost of materials and delivery. Such redistributions
20  * must allow further use, modification, and redistribution of the Source
21  * Code under substantially the same terms as this license.
22  *
23  * Redistributions of source code must retain the copyright notices as they
24  * appear in each source code file, these license terms, and the
25  * disclaimer/limitation of liability set forth as paragraph 6 below.
26  *
27  * Redistributions in binary form must reproduce this Copyright Notice,
28  * these license terms, and the disclaimer/limitation of liability set
29  * forth as paragraph 6 below, in the documentation and/or other materials
30  * provided with the distribution.
31  *
32  * The Software is provided on an "AS IS" basis. No warranty is
33  * provided that the Software is free of defects, or fit for a
34  * particular purpose.
35  *
36  * Limitation of Liability. Quadcap Software shall not be liable
37  * for any damages suffered by the Licensee or any third party resulting
38  * from use of the Software.
39  */

40
41 import java.io.IOException JavaDoc;
42 import java.io.InputStream JavaDoc;
43
44 import java.util.HashMap JavaDoc;
45 import java.util.Map JavaDoc;
46
47 import com.quadcap.util.text.OctetMap;
48 import com.quadcap.util.text.Scanner;
49
50 import com.quadcap.util.Debug;
51
52 /**
53  * An HTTP header parser.
54  *
55  * @author Stan Bailes
56  */

57 public class HeaderParser {
58
59     /**
60      * Parsing helper: Get the next bytes all of which are in the
61      * specified map
62      *
63      * @param scanner the current scanner input source
64      * @param map the octet map specifying the bytes we want
65      */

66     public static String JavaDoc getToken(Scanner scanner, OctetMap map)
67     throws IOException JavaDoc
68     {
69     scanner.skipWhile(OctetMap.wsChars);
70     return scanner.parseWhile(map);
71     }
72
73     /**
74      * Parse the HTTP headers in this input source
75      *
76      * @exception IOException may be thrown
77      */

78     public static void parseHeaders(Scanner scanner, Map JavaDoc headers)
79     throws IOException JavaDoc
80     {
81
82         boolean done = false;
83         String JavaDoc name = null, val = null;
84         while (!done) {
85             int c = scanner.peek();
86         if (c == -1) break;
87             if (OctetMap.wsChars.has(c)) {
88                 if (val != null) {
89                     val = val + " " + getToken(scanner,
90                            OctetMap.fieldValueChars);
91                 } else {
92                     throw new IOException JavaDoc("Unexpected ws in headers");
93                 }
94             } else {
95                 if (val != null) {
96                     headers.put(name, val);
97             val = null;
98                 }
99
100                 if (c == '\r') {
101                     done = true;
102         } else if (c == '\n') {
103             scanner.matchChar('\n');
104             break;
105                 } else {
106                     name = getToken(scanner,
107                     OctetMap.tokenChars).toLowerCase();
108                     scanner.matchChar(':');
109                     val = getToken(scanner, OctetMap.fieldValueChars);
110                 }
111             }
112         parseCRLF(scanner);
113         }
114     if (val != null) {
115         headers.put(name, val);
116     }
117     }
118
119     /**
120      * Skip trailing whitespace followed by CRLF
121      *
122      * @param scanner the current scanner input source
123      * @exception IOException if anything other than CRLF is found
124      */

125     public static void parseCRLF(Scanner scanner) throws IOException JavaDoc {
126     scanner.skipWhile(OctetMap.wsChars);
127     if (scanner.peek() == '\n') {
128         scanner.matchChar('\n');
129     } else {
130         while (scanner.peek() == '\r') scanner.matchChar('\r');
131         scanner.matchChar('\n');
132     }
133     }
134
135     /**
136      * Utility method to parse HTTP headers from an input stream, leaving
137      * the stream posisitioned after the blank line following the headers.
138      */

139     public static Map JavaDoc parseHeaders(InputStream JavaDoc is) throws IOException JavaDoc {
140         Map JavaDoc map = new HashMap JavaDoc();
141         Scanner scanner = new Scanner(is, false);
142         parseHeaders(scanner, map);
143         return map;
144     }
145
146 }
147
Popular Tags