Quadcap Embeddable Server

com/quadcap/http/util/HeaderParser.java

Go to the documentation of this file.
00001 package com.quadcap.http.util; 00002 00003 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved. 00004 * 00005 * This software is distributed under the Quadcap Free Software License. 00006 * This software may be used or modified for any purpose, personal or 00007 * commercial. Open Source redistributions are permitted. Commercial 00008 * redistribution of larger works derived from, or works which bundle 00009 * this software requires a "Commercial Redistribution License"; see 00010 * http://www.quadcap.com/purchase. 00011 * 00012 * Redistributions qualify as "Open Source" under one of the following terms: 00013 * 00014 * Redistributions are made at no charge beyond the reasonable cost of 00015 * materials and delivery. 00016 * 00017 * Redistributions are accompanied by a copy of the Source Code or by an 00018 * irrevocable offer to provide a copy of the Source Code for up to three 00019 * years at the cost of materials and delivery. Such redistributions 00020 * must allow further use, modification, and redistribution of the Source 00021 * Code under substantially the same terms as this license. 00022 * 00023 * Redistributions of source code must retain the copyright notices as they 00024 * appear in each source code file, these license terms, and the 00025 * disclaimer/limitation of liability set forth as paragraph 6 below. 00026 * 00027 * Redistributions in binary form must reproduce this Copyright Notice, 00028 * these license terms, and the disclaimer/limitation of liability set 00029 * forth as paragraph 6 below, in the documentation and/or other materials 00030 * provided with the distribution. 00031 * 00032 * The Software is provided on an "AS IS" basis. No warranty is 00033 * provided that the Software is free of defects, or fit for a 00034 * particular purpose. 00035 * 00036 * Limitation of Liability. Quadcap Software shall not be liable 00037 * for any damages suffered by the Licensee or any third party resulting 00038 * from use of the Software. 00039 */ 00040 00041 import java.io.IOException; 00042 import java.io.InputStream; 00043 00044 import java.util.HashMap; 00045 import java.util.Map; 00046 00047 import com.quadcap.util.text.OctetMap; 00048 import com.quadcap.util.text.Scanner; 00049 00050 import com.quadcap.util.Debug; 00051 00052 /** 00053 * An HTTP header parser. 00054 * 00055 * @author Stan Bailes 00056 */ 00057 public class HeaderParser { 00058 00059 /** 00060 * Parsing helper: Get the next bytes all of which are in the 00061 * specified map 00062 * 00063 * @param scanner the current scanner input source 00064 * @param map the octet map specifying the bytes we want 00065 */ 00066 public static String getToken(Scanner scanner, OctetMap map) 00067 throws IOException 00068 { 00069 scanner.skipWhile(OctetMap.wsChars); 00070 return scanner.parseWhile(map); 00071 } 00072 00073 /** 00074 * Parse the HTTP headers in this input source 00075 * 00076 * @exception IOException may be thrown 00077 */ 00078 public static void parseHeaders(Scanner scanner, Map headers) 00079 throws IOException 00080 { 00081 00082 boolean done = false; 00083 String name = null, val = null; 00084 while (!done) { 00085 int c = scanner.peek(); 00086 if (c == -1) break; 00087 if (OctetMap.wsChars.has(c)) { 00088 if (val != null) { 00089 val = val + " " + getToken(scanner, 00090 OctetMap.fieldValueChars); 00091 } else { 00092 throw new IOException("Unexpected ws in headers"); 00093 } 00094 } else { 00095 if (val != null) { 00096 headers.put(name, val); 00097 val = null; 00098 } 00099 00100 if (c == '\r') { 00101 done = true; 00102 } else if (c == '\n') { 00103 scanner.matchChar('\n'); 00104 break; 00105 } else { 00106 name = getToken(scanner, 00107 OctetMap.tokenChars).toLowerCase(); 00108 scanner.matchChar(':'); 00109 val = getToken(scanner, OctetMap.fieldValueChars); 00110 } 00111 } 00112 parseCRLF(scanner); 00113 } 00114 if (val != null) { 00115 headers.put(name, val); 00116 } 00117 } 00118 00119 /** 00120 * Skip trailing whitespace followed by CRLF 00121 * 00122 * @param scanner the current scanner input source 00123 * @exception IOException if anything other than CRLF is found 00124 */ 00125 public static void parseCRLF(Scanner scanner) throws IOException { 00126 scanner.skipWhile(OctetMap.wsChars); 00127 if (scanner.peek() == '\n') { 00128 scanner.matchChar('\n'); 00129 } else { 00130 while (scanner.peek() == '\r') scanner.matchChar('\r'); 00131 scanner.matchChar('\n'); 00132 } 00133 } 00134 00135 /** 00136 * Utility method to parse HTTP headers from an input stream, leaving 00137 * the stream posisitioned after the blank line following the headers. 00138 */ 00139 public static Map parseHeaders(InputStream is) throws IOException { 00140 Map map = new HashMap(); 00141 Scanner scanner = new Scanner(is, false); 00142 parseHeaders(scanner, map); 00143 return map; 00144 } 00145 00146 }