View Javadoc
1 /* 2 * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpParser.java,v 1.7 2003/05/26 21:51:37 mbecke Exp $ 3 * $Revision: 1.7 $ 4 * $Date: 2003/05/26 21:51:37 $ 5 * 6 * ==================================================================== 7 * 8 * The Apache Software License, Version 1.1 9 * 10 * Copyright (c) 1999-2003 The Apache Software Foundation. All rights 11 * reserved. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 17 * 1. Redistributions of source code must retain the above copyright 18 * notice, this list of conditions and the following disclaimer. 19 * 20 * 2. Redistributions in binary form must reproduce the above copyright 21 * notice, this list of conditions and the following disclaimer in 22 * the documentation and/or other materials provided with the 23 * distribution. 24 * 25 * 3. The end-user documentation included with the redistribution, if 26 * any, must include the following acknowlegement: 27 * "This product includes software developed by the 28 * Apache Software Foundation (http://www.apache.org/)." 29 * Alternately, this acknowlegement may appear in the software itself, 30 * if and wherever such third-party acknowlegements normally appear. 31 * 32 * 4. The names "The Jakarta Project", "Commons", and "Apache Software 33 * Foundation" must not be used to endorse or promote products derived 34 * from this software without prior written permission. For written 35 * permission, please contact apache@apache.org. 36 * 37 * 5. Products derived from this software may not be called "Apache" 38 * nor may "Apache" appear in their names without prior written 39 * permission of the Apache Group. 40 * 41 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 42 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 43 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 44 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 45 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 46 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 47 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 48 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 49 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 50 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 51 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 52 * SUCH DAMAGE. 53 * ==================================================================== 54 * 55 * This software consists of voluntary contributions made by many 56 * individuals on behalf of the Apache Software Foundation. For more 57 * information on the Apache Software Foundation, please see 58 * <http://www.apache.org/>. 59 * 60 * [Additional notices, if required by prior licensing conditions] 61 * 62 */ 63 64 package org.apache.commons.httpclient; 65 66 import java.io.IOException; 67 import java.io.InputStream; 68 import java.io.ByteArrayOutputStream; 69 import java.util.ArrayList; 70 71 import org.apache.commons.logging.Log; 72 import org.apache.commons.logging.LogFactory; 73 74 /*** 75 * A utility class for parsing http header values. 76 * 77 * @author Michael Becke 78 * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a> 79 * 80 * @since 2.0beta1 81 */ 82 public class HttpParser { 83 84 /*** Log object for this class. */ 85 private static final Log LOG = LogFactory.getLog(HttpParser.class); 86 87 /*** 88 * Constructor for HttpParser. 89 */ 90 private HttpParser() { } 91 92 /*** 93 * Return byte array from an (unchunked) input stream. 94 * Stop reading when <tt>"\n"</tt> terminator encountered 95 * If the stream ends before the line terminator is found, 96 * the last part of the string will still be returned. 97 * If no input data available, <code>null</code> is returned 98 * 99 * @param inputStream the stream to read from 100 * 101 * @throws IOException if an I/O problem occurs 102 * @return a byte array from the stream 103 */ 104 public static byte[] readRawLine(InputStream inputStream) throws IOException { 105 LOG.trace("enter HttpParser.readRawLine()"); 106 107 ByteArrayOutputStream buf = new ByteArrayOutputStream(); 108 int ch; 109 while ((ch = inputStream.read()) >= 0) { 110 buf.write(ch); 111 if (ch == '\n') { 112 break; 113 } 114 } 115 if (buf.size() == 0) { 116 return null; 117 } 118 return buf.toByteArray(); 119 } 120 121 /*** 122 * Read up to <tt>"\n"</tt> from an (unchunked) input stream. 123 * If the stream ends before the line terminator is found, 124 * the last part of the string will still be returned. 125 * If no input data available, <code>null</code> is returned 126 * 127 * @param inputStream the stream to read from 128 * 129 * @throws IOException if an I/O problem occurs 130 * @return a line from the stream 131 */ 132 133 public static String readLine(InputStream inputStream) throws IOException { 134 LOG.trace("enter HttpParser.readLine()"); 135 byte[] rawdata = readRawLine(inputStream); 136 if (rawdata == null) { 137 return null; 138 } 139 int len = rawdata.length; 140 int offset = 0; 141 if (len > 0) { 142 if (rawdata[len - 1] == '\n') { 143 offset++; 144 if (len > 1) { 145 if (rawdata[len - 2] == '\r') { 146 offset++; 147 } 148 } 149 } 150 } 151 return HttpConstants.getString(rawdata, 0, len - offset); 152 } 153 154 /*** 155 * Parses headers from the given stream. Headers with the same name are not 156 * combined. 157 * 158 * @param is the stream to read headers from 159 * 160 * @return an array of headers in the order in which they were parsed 161 * 162 * @throws IOException if an IO error occurs while reading from the stream 163 * @throws HttpException if there is an error parsing a header value 164 */ 165 public static Header[] parseHeaders(InputStream is) throws IOException, HttpException { 166 LOG.trace("enter HeaderParser.parseHeaders(HttpConnection, HeaderGroup)"); 167 168 ArrayList headers = new ArrayList(); 169 String name = null; 170 StringBuffer value = null; 171 for (; ;) { 172 String line = HttpParser.readLine(is); 173 if ((line == null) || (line.length() < 1)) { 174 break; 175 } 176 177 // Parse the header name and value 178 // Check for folded headers first 179 // Detect LWS-char see HTTP/1.0 or HTTP/1.1 Section 2.2 180 // discussion on folded headers 181 if ((line.charAt(0) == ' ') || (line.charAt(0) == '\t')) { 182 // we have continuation folded header 183 // so append value 184 if (value != null) { 185 value.append(' '); 186 value.append(line.trim()); 187 } 188 } else { 189 // make sure we save the previous name,value pair if present 190 if (name != null) { 191 headers.add(new Header(name, value.toString())); 192 } 193 194 // Otherwise we should have normal HTTP header line 195 // Parse the header name and value 196 int colon = line.indexOf(":"); 197 if (colon < 0) { 198 throw new HttpException("Unable to parse header: " + line); 199 } 200 name = line.substring(0, colon).trim(); 201 value = new StringBuffer(line.substring(colon + 1).trim()); 202 } 203 204 } 205 206 // make sure we save the last name,value pair if present 207 if (name != null) { 208 headers.add(new Header(name, value.toString())); 209 } 210 211 return (Header[]) headers.toArray(new Header[headers.size()]); 212 } 213 214 }

This page was automatically generated by Maven