| 1 | | /* | | 1 | | /* |
| 2 | | * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/connector/http/SocketInputStream.java,v 1.9 2001/08/08 20:02:17 remm Exp $ | | 2 | | * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/connector/http/SocketInputStream.java,v 1.10 2002/03/18 07:15:40 remm Exp $ |
| 3 | | * $Revision: 1.9 $ | | 3 | | * $Revision: 1.10 $ |
| 4 | | * $Date: 2001/08/08 20:02:17 $ | | 4 | | * $Date: 2002/03/18 07:15:40 $ |
| 5 | | * | | 5 | | * |
| 6 | | * ==================================================================== | | 6 | | * ==================================================================== |
| 7 | | * | | 7 | | * |
| 8 | | * The Apache Software License, Version 1.1 | | 8 | | * The Apache Software License, Version 1.1 |
| 9 | | * | | 9 | | * |
| 10 | | * Copyright (c) 1999 The Apache Software Foundation. All rights | | 10 | | * Copyright (c) 1999 The Apache Software Foundation. All rights |
| 11 | | * reserved. | | 11 | | * reserved. |
| 12 | | * | | 12 | | * |
| 13 | | * Redistribution and use in source and binary forms, with or without | | 13 | | * Redistribution and use in source and binary forms, with or without |
| 14 | | * modification, are permitted provided that the following conditions | | 14 | | * modification, are permitted provided that the following conditions |
| 15 | | * are met: | | 15 | | * are met: |
| 16 | | * | | 16 | | * |
| 17 | | * 1. Redistributions of source code must retain the above copyright | | 17 | | * 1. Redistributions of source code must retain the above copyright |
| 18 | | * notice, this list of conditions and the following disclaimer. | | 18 | | * notice, this list of conditions and the following disclaimer. |
| 19 | | * | | 19 | | * |
| 20 | | * 2. Redistributions in binary form must reproduce the above copyright | | 20 | | * 2. Redistributions in binary form must reproduce the above copyright |
| 21 | | * notice, this list of conditions and the following disclaimer in | | 21 | | * notice, this list of conditions and the following disclaimer in |
| 22 | | * the documentation and/or other materials provided with the | | 22 | | * the documentation and/or other materials provided with the |
| 23 | | * distribution. | | 23 | | * distribution. |
| 24 | | * | | 24 | | * |
| 25 | | * 3. The end-user documentation included with the redistribution, if | | 25 | | * 3. The end-user documentation included with the redistribution, if |
| 26 | | * any, must include the following acknowlegement: | | 26 | | * any, must include the following acknowlegement: |
| 27 | | * "This product includes software developed by the | | 27 | | * "This product includes software developed by the |
| 28 | | * Apache Software Foundation (http://www.apache.org/)." | | 28 | | * Apache Software Foundation (http://www.apache.org/)." |
| 29 | | * Alternately, this acknowlegement may appear in the software itself, | | 29 | | * Alternately, this acknowlegement may appear in the software itself, |
| 30 | | * if and wherever such third-party acknowlegements normally appear. | | 30 | | * if and wherever such third-party acknowlegements normally appear. |
| 31 | | * | | 31 | | * |
| 32 | | * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software | | 32 | | * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software |
| 33 | | * Foundation" must not be used to endorse or promote products derived | | 33 | | * Foundation" must not be used to endorse or promote products derived |
| 34 | | * from this software without prior written permission. For written | | 34 | | * from this software without prior written permission. For written |
| 35 | | * permission, please contact apache@apache.org. | | 35 | | * permission, please contact apache@apache.org. |
| 36 | | * | | 36 | | * |
| 37 | | * 5. Products derived from this software may not be called "Apache" | | 37 | | * 5. Products derived from this software may not be called "Apache" |
| 38 | | * nor may "Apache" appear in their names without prior written | | 38 | | * nor may "Apache" appear in their names without prior written |
| 39 | | * permission of the Apache Group. | | 39 | | * permission of the Apache Group. |
| 40 | | * | | 40 | | * |
| 41 | | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED | | 41 | | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED |
| 42 | | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | | 42 | | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 43 | | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | | 43 | | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 44 | | * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR | | 44 | | * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR |
| 45 | | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | | 45 | | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 46 | | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | | 46 | | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 47 | | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF | | 47 | | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
| 48 | | * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | | 48 | | * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 49 | | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | | 49 | | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 50 | | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | | 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 | | 51 | | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 52 | | * SUCH DAMAGE. | | 52 | | * SUCH DAMAGE. |
| 53 | | * ==================================================================== | | 53 | | * ==================================================================== |
| 54 | | * | | 54 | | * |
| 55 | | * This software consists of voluntary contributions made by many | | 55 | | * This software consists of voluntary contributions made by many |
| 56 | | * individuals on behalf of the Apache Software Foundation. For more | | 56 | | * individuals on behalf of the Apache Software Foundation. For more |
| 57 | | * information on the Apache Software Foundation, please see | | 57 | | * information on the Apache Software Foundation, please see |
| 58 | | * <http://www.apache.org/>. | | 58 | | * <http://www.apache.org/>. |
| 59 | | * | | 59 | | * |
| 60 | | * [Additional notices, if required by prior licensing conditions] | | 60 | | * [Additional notices, if required by prior licensing conditions] |
| 61 | | * | | 61 | | * |
| 62 | | */ | | 62 | | */ |
| 63 | | | | 63 | | |
| 64 | | | | 64 | | |
| 65 | | package org.apache.catalina.connector.http; | | 65 | | package org.apache.catalina.connector.http; |
| 66 | | | | 66 | | |
| 67 | | import java.io.IOException; | | 67 | | import java.io.IOException; |
| 68 | | import java.io.InputStream; | | 68 | | import java.io.InputStream; |
| 69 | | import java.io.EOFException; | | 69 | | import java.io.EOFException; |
| 70 | | import org.apache.catalina.util.StringManager; | | 70 | | import org.apache.catalina.util.StringManager; |
| 71 | | | | 71 | | |
| 72 | | /** | | 72 | | /** |
| 73 | | * Extends InputStream to be more efficient reading lines during HTTP | | 73 | | * Extends InputStream to be more efficient reading lines during HTTP |
| 74 | | * header processing. | | 74 | | * header processing. |
| 75 | | * | | 75 | | * |
| 76 | | * @author <a href="mailto:remm@apache.org">Remy Maucherat</a> | | 76 | | * @author <a href="mailto:remm@apache.org">Remy Maucherat</a> |
| | | | | 77 | | * @deprecated |
| 77 | | */ | | 78 | | */ |
| 78 | | public class SocketInputStream extends InputStream { | | 79 | | public class SocketInputStream extends InputStream { |
| 79 | | | | 80 | | |
| 80 | | | | 81 | | |
| 81 | | // -------------------------------------------------------------- Constants | | 82 | | // -------------------------------------------------------------- Constants |
| 82 | | | | 83 | | |
| 83 | | | | 84 | | |
| 84 | | /** | | 85 | | /** |
| 85 | | * CR. | | 86 | | * CR. |
| 86 | | */ | | 87 | | */ |
| 87 | | private static final byte CR = (byte) '\r'; | | 88 | | private static final byte CR = (byte) '\r'; |
| 88 | | | | 89 | | |
| 89 | | | | 90 | | |
| 90 | | /** | | 91 | | /** |
| 91 | | * LF. | | 92 | | * LF. |
| 92 | | */ | | 93 | | */ |
| 93 | | private static final byte LF = (byte) '\n'; | | 94 | | private static final byte LF = (byte) '\n'; |
| 94 | | | | 95 | | |
| 95 | | | | 96 | | |
| 96 | | /** | | 97 | | /** |
| 97 | | * SP. | | 98 | | * SP. |
| 98 | | */ | | 99 | | */ |
| 99 | | private static final byte SP = (byte) ' '; | | 100 | | private static final byte SP = (byte) ' '; |
| 100 | | | | 101 | | |
| 101 | | | | 102 | | |
| 102 | | /** | | 103 | | /** |
| 103 | | * HT. | | 104 | | * HT. |
| 104 | | */ | | 105 | | */ |
| 105 | | private static final byte HT = (byte) '\t'; | | 106 | | private static final byte HT = (byte) '\t'; |
| 106 | | | | 107 | | |
| 107 | | | | 108 | | |
| 108 | | /** | | 109 | | /** |
| 109 | | * COLON. | | 110 | | * COLON. |
| 110 | | */ | | 111 | | */ |
| 111 | | private static final byte COLON = (byte) ':'; | | 112 | | private static final byte COLON = (byte) ':'; |
| 112 | | | | 113 | | |
| 113 | | | | 114 | | |
| 114 | | /** | | 115 | | /** |
| 115 | | * Lower case offset. | | 116 | | * Lower case offset. |
| 116 | | */ | | 117 | | */ |
| 117 | | private static final int LC_OFFSET = 'A' - 'a'; | | 118 | | private static final int LC_OFFSET = 'A' - 'a'; |
| 118 | | | | 119 | | |
| 119 | | | | 120 | | |
| 120 | | /** | | 121 | | /** |
| 121 | | * Internal buffer. | | 122 | | * Internal buffer. |
| 122 | | */ | | 123 | | */ |
| 123 | | protected byte buf[]; | | 124 | | protected byte buf[]; |
| 124 | | | | 125 | | |
| 125 | | | | 126 | | |
| 126 | | /** | | 127 | | /** |
| 127 | | * Last valid byte. | | 128 | | * Last valid byte. |
| 128 | | */ | | 129 | | */ |
| 129 | | protected int count; | | 130 | | protected int count; |
| 130 | | | | 131 | | |
| 131 | | | | 132 | | |
| 132 | | /** | | 133 | | /** |
| 133 | | * Position in the buffer. | | 134 | | * Position in the buffer. |
| 134 | | */ | | 135 | | */ |
| 135 | | protected int pos; | | 136 | | protected int pos; |
| 136 | | | | 137 | | |
| 137 | | | | 138 | | |
| 138 | | /** | | 139 | | /** |
| 139 | | * Underlying input stream. | | 140 | | * Underlying input stream. |
| 140 | | */ | | 141 | | */ |
| 141 | | protected InputStream is; | | 142 | | protected InputStream is; |
| 142 | | | | 143 | | |
| 143 | | | | 144 | | |
| 144 | | // ----------------------------------------------------------- Constructors | | 145 | | // ----------------------------------------------------------- Constructors |
| 145 | | | | 146 | | |
| 146 | | | | 147 | | |
| 147 | | /** | | 148 | | /** |
| 148 | | * Construct a servlet input stream associated with the specified socket | | 149 | | * Construct a servlet input stream associated with the specified socket |
| 149 | | * input. | | 150 | | * input. |
| 150 | | * | | 151 | | * |
| 151 | | * @param is socket input stream | | 152 | | * @param is socket input stream |
| 152 | | * @param bufferSize size of the internal buffer | | 153 | | * @param bufferSize size of the internal buffer |
| 153 | | */ | | 154 | | */ |
| 154 | | public SocketInputStream(InputStream is, int bufferSize) { | | 155 | | public SocketInputStream(InputStream is, int bufferSize) { |
| 155 | | | | 156 | | |
| 156 | | this.is = is; | | 157 | | this.is = is; |
| 157 | | buf = new byte[bufferSize]; | | 158 | | buf = new byte[bufferSize]; |
| 158 | | | | 159 | | |
| 159 | | } | | 160 | | } |
| 160 | | | | 161 | | |
| 161 | | | | 162 | | |
| 162 | | // -------------------------------------------------------------- Variables | | 163 | | // -------------------------------------------------------------- Variables |
| 163 | | | | 164 | | |
| 164 | | | | 165 | | |
| 165 | | /** | | 166 | | /** |
| 166 | | * The string manager for this package. | | 167 | | * The string manager for this package. |
| 167 | | */ | | 168 | | */ |
| 168 | | protected static StringManager sm = | | 169 | | protected static StringManager sm = |
| 169 | | StringManager.getManager(Constants.Package); | | 170 | | StringManager.getManager(Constants.Package); |
| 170 | | | | 171 | | |
| 171 | | | | 172 | | |
| 172 | | // ----------------------------------------------------- Instance Variables | | 173 | | // ----------------------------------------------------- Instance Variables |
| 173 | | | | 174 | | |
| 174 | | | | 175 | | |
| 175 | | // --------------------------------------------------------- Public Methods | | 176 | | // --------------------------------------------------------- Public Methods |
| 176 | | | | 177 | | |
| 177 | | | | 178 | | |
| 178 | | /** | | 179 | | /** |
| 179 | | * Read the request line, and copies it to the given buffer. This | | 180 | | * Read the request line, and copies it to the given buffer. This |
| 180 | | * function is meant to be used during the HTTP request header parsing. | | 181 | | * function is meant to be used during the HTTP request header parsing. |
| 181 | | * Do NOT attempt to read the request body using it. | | 182 | | * Do NOT attempt to read the request body using it. |
| 182 | | * | | 183 | | * |
| 183 | | * @param requestLine Request line object | | 184 | | * @param requestLine Request line object |
| 184 | | * @throws IOException If an exception occurs during the underlying socket | | 185 | | * @throws IOException If an exception occurs during the underlying socket |
| 185 | | * read operations, or if the given buffer is not big enough to accomodate | | 186 | | * read operations, or if the given buffer is not big enough to accomodate |
| 186 | | * the whole line. | | 187 | | * the whole line. |
| 187 | | */ | | 188 | | */ |
| 188 | | public void readRequestLine(HttpRequestLine requestLine) | | 189 | | public void readRequestLine(HttpRequestLine requestLine) |
| 189 | | throws IOException { | | 190 | | throws IOException { |
| 190 | | | | 191 | | |
| 191 | | // Recycling check | | 192 | | // Recycling check |
| 192 | | if (requestLine.methodEnd != 0) | | 193 | | if (requestLine.methodEnd != 0) |
| 193 | | requestLine.recycle(); | | 194 | | requestLine.recycle(); |
| 194 | | | | 195 | | |
| 195 | | // Checking for a blank line | | 196 | | // Checking for a blank line |
| 196 | | int chr = 0; | | 197 | | int chr = 0; |
| 197 | | do { // Skipping CR or LF | | 198 | | do { // Skipping CR or LF |
| 198 | | try { | | 199 | | try { |
| 199 | | chr = read(); | | 200 | | chr = read(); |
| 200 | | } catch (IOException e) { | | 201 | | } catch (IOException e) { |
| 201 | | chr = -1; | | 202 | | chr = -1; |
| 202 | | } | | 203 | | } |
| 203 | | } while ((chr == CR) || (chr == LF)); | | 204 | | } while ((chr == CR) || (chr == LF)); |
| 204 | | if (chr == -1) | | 205 | | if (chr == -1) |
| 205 | | throw new EOFException | | 206 | | throw new EOFException |
| 206 | | (sm.getString("requestStream.readline.error")); | | 207 | | (sm.getString("requestStream.readline.error")); |
| 207 | | pos--; | | 208 | | pos--; |
| 208 | | | | 209 | | |
| 209 | | // Reading the method name | | 210 | | // Reading the method name |
| 210 | | | | 211 | | |
| 211 | | int maxRead = requestLine.method.length; | | 212 | | int maxRead = requestLine.method.length; |
| 212 | | int readStart = pos; | | 213 | | int readStart = pos; |
| 213 | | int readCount = 0; | | 214 | | int readCount = 0; |
| 214 | | | | 215 | | |
| 215 | | boolean space = false; | | 216 | | boolean space = false; |
| 216 | | | | 217 | | |
| 217 | | while (!space) { | | 218 | | while (!space) { |
| 218 | | // if the buffer is full, extend it | | 219 | | // if the buffer is full, extend it |
| 219 | | if (readCount >= maxRead) { | | 220 | | if (readCount >= maxRead) { |
| 220 | | if ((2 * maxRead) <= HttpRequestLine.MAX_METHOD_SIZE) { | | 221 | | if ((2 * maxRead) <= HttpRequestLine.MAX_METHOD_SIZE) { |
| 221 | | char[] newBuffer = new char[2 * maxRead]; | | 222 | | char[] newBuffer = new char[2 * maxRead]; |
| 222 | | System.arraycopy(requestLine.method, 0, newBuffer, 0, | | 223 | | System.arraycopy(requestLine.method, 0, newBuffer, 0, |
| 223 | | maxRead); | | 224 | | maxRead); |
| 224 | | requestLine.method = newBuffer; | | 225 | | requestLine.method = newBuffer; |
| 225 | | maxRead = requestLine.method.length; | | 226 | | maxRead = requestLine.method.length; |
| 226 | | } else { | | 227 | | } else { |
| 227 | | throw new IOException | | 228 | | throw new IOException |
| 228 | | (sm.getString("requestStream.readline.toolong")); | | 229 | | (sm.getString("requestStream.readline.toolong")); |
| 229 | | } | | 230 | | } |
| 230 | | } | | 231 | | } |
| 231 | | // We're at the end of the internal buffer | | 232 | | // We're at the end of the internal buffer |
| 232 | | if (pos >= count) { | | 233 | | if (pos >= count) { |
| 233 | | int val = read(); | | 234 | | int val = read(); |
| 234 | | if (val == -1) { | | 235 | | if (val == -1) { |
| 235 | | throw new IOException | | 236 | | throw new IOException |
| 236 | | (sm.getString("requestStream.readline.error")); | | 237 | | (sm.getString("requestStream.readline.error")); |
| 237 | | } | | 238 | | } |
| 238 | | pos = 0; | | 239 | | pos = 0; |
| 239 | | readStart = 0; | | 240 | | readStart = 0; |
| 240 | | } | | 241 | | } |
| 241 | | if (buf[pos] == SP) { | | 242 | | if (buf[pos] == SP) { |
| 242 | | space = true; | | 243 | | space = true; |
| 243 | | } | | 244 | | } |
| 244 | | requestLine.method[readCount] = (char) buf[pos]; | | 245 | | requestLine.method[readCount] = (char) buf[pos]; |
| 245 | | readCount++; | | 246 | | readCount++; |
| 246 | | pos++; | | 247 | | pos++; |
| 247 | | } | | 248 | | } |
| 248 | | | | 249 | | |
| 249 | | requestLine.methodEnd = readCount - 1; | | 250 | | requestLine.methodEnd = readCount - 1; |
| 250 | | | | 251 | | |
| 251 | | // Reading URI | | 252 | | // Reading URI |
| 252 | | | | 253 | | |
| 253 | | maxRead = requestLine.uri.length; | | 254 | | maxRead = requestLine.uri.length; |
| 254 | | readStart = pos; | | 255 | | readStart = pos; |
| 255 | | readCount = 0; | | 256 | | readCount = 0; |
| 256 | | | | 257 | | |
| 257 | | space = false; | | 258 | | space = false; |
| 258 | | | | 259 | | |
| 259 | | boolean eol = false; | | 260 | | boolean eol = false; |
| 260 | | | | 261 | | |
| 261 | | while (!space) { | | 262 | | while (!space) { |
| 262 | | // if the buffer is full, extend it | | 263 | | // if the buffer is full, extend it |
| 263 | | if (readCount >= maxRead) { | | 264 | | if (readCount >= maxRead) { |
| 264 | | if ((2 * maxRead) <= HttpRequestLine.MAX_URI_SIZE) { | | 265 | | if ((2 * maxRead) <= HttpRequestLine.MAX_URI_SIZE) { |
| 265 | | char[] newBuffer = new char[2 * maxRead]; | | 266 | | char[] newBuffer = new char[2 * maxRead]; |
| 266 | | System.arraycopy(requestLine.uri, 0, newBuffer, 0, | | 267 | | System.arraycopy(requestLine.uri, 0, newBuffer, 0, |
| 267 | | maxRead); | | 268 | | maxRead); |
| 268 | | requestLine.uri = newBuffer; | | 269 | | requestLine.uri = newBuffer; |
| 269 | | maxRead = requestLine.uri.length; | | 270 | | maxRead = requestLine.uri.length; |
| 270 | | } else { | | 271 | | } else { |
| 271 | | throw new IOException | | 272 | | throw new IOException |
| 272 | | (sm.getString("requestStream.readline.toolong")); | | 273 | | (sm.getString("requestStream.readline.toolong")); |
| 273 | | } | | 274 | | } |
| 274 | | } | | 275 | | } |
| 275 | | // We're at the end of the internal buffer | | 276 | | // We're at the end of the internal buffer |
| 276 | | if (pos >= count) { | | 277 | | if (pos >= count) { |
| 277 | | int val = read(); | | 278 | | int val = read(); |
| 278 | | if (val == -1) | | 279 | | if (val == -1) |
| 279 | | throw new IOException | | 280 | | throw new IOException |
| 280 | | (sm.getString("requestStream.readline.error")); | | 281 | | (sm.getString("requestStream.readline.error")); |
| 281 | | pos = 0; | | 282 | | pos = 0; |
| 282 | | readStart = 0; | | 283 | | readStart = 0; |
| 283 | | } | | 284 | | } |
| 284 | | if (buf[pos] == SP) { | | 285 | | if (buf[pos] == SP) { |
| 285 | | space = true; | | 286 | | space = true; |
| 286 | | } else if ((buf[pos] == CR) || (buf[pos] == LF)) { | | 287 | | } else if ((buf[pos] == CR) || (buf[pos] == LF)) { |
| 287 | | // HTTP/0.9 style request | | 288 | | // HTTP/0.9 style request |
| 288 | | eol = true; | | 289 | | eol = true; |
| 289 | | space = true; | | 290 | | space = true; |
| 290 | | } | | 291 | | } |
| 291 | | requestLine.uri[readCount] = (char) buf[pos]; | | 292 | | requestLine.uri[readCount] = (char) buf[pos]; |
| 292 | | readCount++; | | 293 | | readCount++; |
| 293 | | pos++; | | 294 | | pos++; |
| 294 | | } | | 295 | | } |
| 295 | | | | 296 | | |
| 296 | | requestLine.uriEnd = readCount - 1; | | 297 | | requestLine.uriEnd = readCount - 1; |
| 297 | | | | 298 | | |
| 298 | | // Reading protocol | | 299 | | // Reading protocol |
| 299 | | | | 300 | | |
| 300 | | maxRead = requestLine.protocol.length; | | 301 | | maxRead = requestLine.protocol.length; |
| 301 | | readStart = pos; | | 302 | | readStart = pos; |
| 302 | | readCount = 0; | | 303 | | readCount = 0; |
| 303 | | | | 304 | | |
| 304 | | while (!eol) { | | 305 | | while (!eol) { |
| 305 | | // if the buffer is full, extend it | | 306 | | // if the buffer is full, extend it |
| 306 | | if (readCount >= maxRead) { | | 307 | | if (readCount >= maxRead) { |
| 307 | | if ((2 * maxRead) <= HttpRequestLine.MAX_PROTOCOL_SIZE) { | | 308 | | if ((2 * maxRead) <= HttpRequestLine.MAX_PROTOCOL_SIZE) { |
| 308 | | char[] newBuffer = new char[2 * maxRead]; | | 309 | | char[] newBuffer = new char[2 * maxRead]; |
| 309 | | System.arraycopy(requestLine.protocol, 0, newBuffer, 0, | | 310 | | System.arraycopy(requestLine.protocol, 0, newBuffer, 0, |
| 310 | | maxRead); | | 311 | | maxRead); |
| 311 | | requestLine.protocol = newBuffer; | | 312 | | requestLine.protocol = newBuffer; |
| 312 | | maxRead = requestLine.protocol.length; | | 313 | | maxRead = requestLine.protocol.length; |
| 313 | | } else { | | 314 | | } else { |
| 314 | | throw new IOException | | 315 | | throw new IOException |
| 315 | | (sm.getString("requestStream.readline.toolong")); | | 316 | | (sm.getString("requestStream.readline.toolong")); |
| 316 | | } | | 317 | | } |
| 317 | | } | | 318 | | } |
| 318 | | // We're at the end of the internal buffer | | 319 | | // We're at the end of the internal buffer |
| 319 | | if (pos >= count) { | | 320 | | if (pos >= count) { |
| 320 | | // Copying part (or all) of the internal buffer to the line | | 321 | | // Copying part (or all) of the internal buffer to the line |
| 321 | | // buffer | | 322 | | // buffer |
| 322 | | int val = read(); | | 323 | | int val = read(); |
| 323 | | if (val == -1) | | 324 | | if (val == -1) |
| 324 | | throw new IOException | | 325 | | throw new IOException |
| 325 | | (sm.getString("requestStream.readline.error")); | | 326 | | (sm.getString("requestStream.readline.error")); |
| 326 | | pos = 0; | | 327 | | pos = 0; |
| 327 | | readStart = 0; | | 328 | | readStart = 0; |
| 328 | | } | | 329 | | } |
| 329 | | if (buf[pos] == CR) { | | 330 | | if (buf[pos] == CR) { |
| 330 | | // Skip CR. | | 331 | | // Skip CR. |
| 331 | | } else if (buf[pos] == LF) { | | 332 | | } else if (buf[pos] == LF) { |
| 332 | | eol = true; | | 333 | | eol = true; |
| 333 | | } else { | | 334 | | } else { |
| 334 | | requestLine.protocol[readCount] = (char) buf[pos]; | | 335 | | requestLine.protocol[readCount] = (char) buf[pos]; |
| 335 | | readCount++; | | 336 | | readCount++; |
| 336 | | } | | 337 | | } |
| 337 | | pos++; | | 338 | | pos++; |
| 338 | | } | | 339 | | } |
| 339 | | | | 340 | | |
| 340 | | requestLine.protocolEnd = readCount; | | 341 | | requestLine.protocolEnd = readCount; |
| 341 | | | | 342 | | |
| 342 | | } | | 343 | | } |
| 343 | | | | 344 | | |
| 344 | | | | 345 | | |
| 345 | | /** | | 346 | | /** |
| 346 | | * Read a header, and copies it to the given buffer. This | | 347 | | * Read a header, and copies it to the given buffer. This |
| 347 | | * function is meant to be used during the HTTP request header parsing. | | 348 | | * function is meant to be used during the HTTP request header parsing. |
| 348 | | * Do NOT attempt to read the request body using it. | | 349 | | * Do NOT attempt to read the request body using it. |
| 349 | | * | | 350 | | * |
| 350 | | * @param requestLine Request line object | | 351 | | * @param requestLine Request line object |
| 351 | | * @throws IOException If an exception occurs during the underlying socket | | 352 | | * @throws IOException If an exception occurs during the underlying socket |
| 352 | | * read operations, or if the given buffer is not big enough to accomodate | | 353 | | * read operations, or if the given buffer is not big enough to accomodate |
| 353 | | * the whole line. | | 354 | | * the whole line. |
| 354 | | */ | | 355 | | */ |
| 355 | | public void readHeader(HttpHeader header) | | 356 | | public void readHeader(HttpHeader header) |
| 356 | | throws IOException { | | 357 | | throws IOException { |
| 357 | | | | 358 | | |
| 358 | | // Recycling check | | 359 | | // Recycling check |
| 359 | | if (header.nameEnd != 0) | | 360 | | if (header.nameEnd != 0) |
| 360 | | header.recycle(); | | 361 | | header.recycle(); |
| 361 | | | | 362 | | |
| 362 | | // Checking for a blank line | | 363 | | // Checking for a blank line |
| 363 | | int chr = read(); | | 364 | | int chr = read(); |
| 364 | | if ((chr == CR) || (chr == LF)) { // Skipping CR | | 365 | | if ((chr == CR) || (chr == LF)) { // Skipping CR |
| 365 | | if (chr == CR) | | 366 | | if (chr == CR) |
| 366 | | read(); // Skipping LF | | 367 | | read(); // Skipping LF |
| 367 | | header.nameEnd = 0; | | 368 | | header.nameEnd = 0; |
| 368 | | header.valueEnd = 0; | | 369 | | header.valueEnd = 0; |
| 369 | | return; | | 370 | | return; |
| 370 | | } else { | | 371 | | } else { |
| 371 | | pos--; | | 372 | | pos--; |
| 372 | | } | | 373 | | } |
| 373 | | | | 374 | | |
| 374 | | // Reading the header name | | 375 | | // Reading the header name |
| 375 | | | | 376 | | |
| 376 | | int maxRead = header.name.length; | | 377 | | int maxRead = header.name.length; |
| 377 | | int readStart = pos; | | 378 | | int readStart = pos; |
| 378 | | int readCount = 0; | | 379 | | int readCount = 0; |
| 379 | | | | 380 | | |
| 380 | | boolean colon = false; | | 381 | | boolean colon = false; |
| 381 | | | | 382 | | |
| 382 | | while (!colon) { | | 383 | | while (!colon) { |
| 383 | | // if the buffer is full, extend it | | 384 | | // if the buffer is full, extend it |
| 384 | | if (readCount >= maxRead) { | | 385 | | if (readCount >= maxRead) { |
| 385 | | if ((2 * maxRead) <= HttpHeader.MAX_NAME_SIZE) { | | 386 | | if ((2 * maxRead) <= HttpHeader.MAX_NAME_SIZE) { |
| 386 | | char[] newBuffer = new char[2 * maxRead]; | | 387 | | char[] newBuffer = new char[2 * maxRead]; |
| 387 | | System.arraycopy(header.name, 0, newBuffer, 0, maxRead); | | 388 | | System.arraycopy(header.name, 0, newBuffer, 0, maxRead); |
| 388 | | header.name = newBuffer; | | 389 | | header.name = newBuffer; |
| 389 | | maxRead = header.name.length; | | 390 | | maxRead = header.name.length; |
| 390 | | } else { | | 391 | | } else { |
| 391 | | throw new IOException | | 392 | | throw new IOException |
| 392 | | (sm.getString("requestStream.readline.toolong")); | | 393 | | (sm.getString("requestStream.readline.toolong")); |
| 393 | | } | | 394 | | } |
| 394 | | } | | 395 | | } |
| 395 | | // We're at the end of the internal buffer | | 396 | | // We're at the end of the internal buffer |
| 396 | | if (pos >= count) { | | 397 | | if (pos >= count) { |
| 397 | | int val = read(); | | 398 | | int val = read(); |
| 398 | | if (val == -1) { | | 399 | | if (val == -1) { |
| 399 | | throw new IOException | | 400 | | throw new IOException |
| 400 | | (sm.getString("requestStream.readline.error")); | | 401 | | (sm.getString("requestStream.readline.error")); |
| 401 | | } | | 402 | | } |
| 402 | | pos = 0; | | 403 | | pos = 0; |
| 403 | | readStart = 0; | | 404 | | readStart = 0; |
| 404 | | } | | 405 | | } |
| 405 | | if (buf[pos] == COLON) { | | 406 | | if (buf[pos] == COLON) { |
| 406 | | colon = true; | | 407 | | colon = true; |
| 407 | | } | | 408 | | } |
| 408 | | char val = (char) buf[pos]; | | 409 | | char val = (char) buf[pos]; |
| 409 | | if ((val >= 'A') && (val <= 'Z')) { | | 410 | | if ((val >= 'A') && (val <= 'Z')) { |
| 410 | | val = (char) (val - LC_OFFSET); | | 411 | | val = (char) (val - LC_OFFSET); |
| 411 | | } | | 412 | | } |
| 412 | | header.name[readCount] = val; | | 413 | | header.name[readCount] = val; |
| 413 | | readCount++; | | 414 | | readCount++; |
| 414 | | pos++; | | 415 | | pos++; |
| 415 | | } | | 416 | | } |
| 416 | | | | 417 | | |
| 417 | | header.nameEnd = readCount - 1; | | 418 | | header.nameEnd = readCount - 1; |
| 418 | | | | 419 | | |
| 419 | | // Reading the header value (which can be spanned over multiple lines) | | 420 | | // Reading the header value (which can be spanned over multiple lines) |
| 420 | | | | 421 | | |
| 421 | | maxRead = header.value.length; | | 422 | | maxRead = header.value.length; |
| 422 | | readStart = pos; | | 423 | | readStart = pos; |
| 423 | | readCount = 0; | | 424 | | readCount = 0; |
| 424 | | | | 425 | | |
| 425 | | int crPos = -2; | | 426 | | int crPos = -2; |
| 426 | | | | 427 | | |
| 427 | | boolean eol = false; | | 428 | | boolean eol = false; |
| 428 | | boolean validLine = true; | | 429 | | boolean validLine = true; |
| 429 | | | | 430 | | |
| 430 | | while (validLine) { | | 431 | | while (validLine) { |
| 431 | | | | 432 | | |
| 432 | | boolean space = true; | | 433 | | boolean space = true; |
| 433 | | | | 434 | | |
| 434 | | // Skipping spaces | | 435 | | // Skipping spaces |
| 435 | | // Note : Only leading white spaces are removed. Trailing white | | 436 | | // Note : Only leading white spaces are removed. Trailing white |
| 436 | | // spaces are not. | | 437 | | // spaces are not. |
| 437 | | while (space) { | | 438 | | while (space) { |
| 438 | | // We're at the end of the internal buffer | | 439 | | // We're at the end of the internal buffer |
| 439 | | if (pos >= count) { | | 440 | | if (pos >= count) { |
| 440 | | // Copying part (or all) of the internal buffer to the line | | 441 | | // Copying part (or all) of the internal buffer to the line |
| 441 | | // buffer | | 442 | | // buffer |
| 442 | | int val = read(); | | 443 | | int val = read(); |
| 443 | | if (val == -1) | | 444 | | if (val == -1) |
| 444 | | throw new IOException | | 445 | | throw new IOException |
| 445 | | (sm.getString("requestStream.readline.error")); | | 446 | | (sm.getString("requestStream.readline.error")); |
| 446 | | pos = 0; | | 447 | | pos = 0; |
| 447 | | readStart = 0; | | 448 | | readStart = 0; |
| 448 | | } | | 449 | | } |
| 449 | | if ((buf[pos] == SP) || (buf[pos] == HT)) { | | 450 | | if ((buf[pos] == SP) || (buf[pos] == HT)) { |
| 450 | | pos++; | | 451 | | pos++; |
| 451 | | } else { | | 452 | | } else { |
| 452 | | space = false; | | 453 | | space = false; |
| 453 | | } | | 454 | | } |
| 454 | | } | | 455 | | } |
| 455 | | | | 456 | | |
| 456 | | while (!eol) { | | 457 | | while (!eol) { |
| 457 | | // if the buffer is full, extend it | | 458 | | // if the buffer is full, extend it |
| 458 | | if (readCount >= maxRead) { | | 459 | | if (readCount >= maxRead) { |
| 459 | | if ((2 * maxRead) <= HttpHeader.MAX_VALUE_SIZE) { | | 460 | | if ((2 * maxRead) <= HttpHeader.MAX_VALUE_SIZE) { |
| 460 | | char[] newBuffer = new char[2 * maxRead]; | | 461 | | char[] newBuffer = new char[2 * maxRead]; |
| 461 | | System.arraycopy(header.value, 0, newBuffer, 0, | | 462 | | System.arraycopy(header.value, 0, newBuffer, 0, |
| 462 | | maxRead); | | 463 | | maxRead); |
| 463 | | header.value = newBuffer; | | 464 | | header.value = newBuffer; |
| 464 | | maxRead = header.value.length; | | 465 | | maxRead = header.value.length; |
| 465 | | } else { | | 466 | | } else { |
| 466 | | throw new IOException | | 467 | | throw new IOException |
| 467 | | (sm.getString("requestStream.readline.toolong")); | | 468 | | (sm.getString("requestStream.readline.toolong")); |
| 468 | | } | | 469 | | } |
| 469 | | } | | 470 | | } |
| 470 | | // We're at the end of the internal buffer | | 471 | | // We're at the end of the internal buffer |
| 471 | | if (pos >= count) { | | 472 | | if (pos >= count) { |
| 472 | | // Copying part (or all) of the internal buffer to the line | | 473 | | // Copying part (or all) of the internal buffer to the line |
| 473 | | // buffer | | 474 | | // buffer |
| 474 | | int val = read(); | | 475 | | int val = read(); |
| 475 | | if (val == -1) | | 476 | | if (val == -1) |
| 476 | | throw new IOException | | 477 | | throw new IOException |
| 477 | | (sm.getString("requestStream.readline.error")); | | 478 | | (sm.getString("requestStream.readline.error")); |
| 478 | | pos = 0; | | 479 | | pos = 0; |
| 479 | | readStart = 0; | | 480 | | readStart = 0; |
| 480 | | } | | 481 | | } |
| 481 | | if (buf[pos] == CR) { | | 482 | | if (buf[pos] == CR) { |
| 482 | | } else if (buf[pos] == LF) { | | 483 | | } else if (buf[pos] == LF) { |
| 483 | | eol = true; | | 484 | | eol = true; |
| 484 | | } else { | | 485 | | } else { |
| 485 | | // FIXME : Check if binary conversion is working fine | | 486 | | // FIXME : Check if binary conversion is working fine |
| 486 | | int ch = buf[pos] & 0xff; | | 487 | | int ch = buf[pos] & 0xff; |
| 487 | | header.value[readCount] = (char) ch; | | 488 | | header.value[readCount] = (char) ch; |
| 488 | | readCount++; | | 489 | | readCount++; |
| 489 | | } | | 490 | | } |
| 490 | | pos++; | | 491 | | pos++; |
| 491 | | } | | 492 | | } |
| 492 | | | | 493 | | |
| 493 | | int nextChr = read(); | | 494 | | int nextChr = read(); |
| 494 | | | | 495 | | |
| 495 | | if ((nextChr != SP) && (nextChr != HT)) { | | 496 | | if ((nextChr != SP) && (nextChr != HT)) { |
| 496 | | pos--; | | 497 | | pos--; |
| 497 | | validLine = false; | | 498 | | validLine = false; |
| 498 | | } else { | | 499 | | } else { |
| 499 | | eol = false; | | 500 | | eol = false; |
| 500 | | // if the buffer is full, extend it | | 501 | | // if the buffer is full, extend it |
| 501 | | if (readCount >= maxRead) { | | 502 | | if (readCount >= maxRead) { |
| 502 | | if ((2 * maxRead) <= HttpHeader.MAX_VALUE_SIZE) { | | 503 | | if ((2 * maxRead) <= HttpHeader.MAX_VALUE_SIZE) { |
| 503 | | char[] newBuffer = new char[2 * maxRead]; | | 504 | | char[] newBuffer = new char[2 * maxRead]; |
| 504 | | System.arraycopy(header.value, 0, newBuffer, 0, | | 505 | | System.arraycopy(header.value, 0, newBuffer, 0, |
| 505 | | maxRead); | | 506 | | maxRead); |
| 506 | | header.value = newBuffer; | | 507 | | header.value = newBuffer; |
| 507 | | maxRead = header.value.length; | | 508 | | maxRead = header.value.length; |
| 508 | | } else { | | 509 | | } else { |
| 509 | | throw new IOException | | 510 | | throw new IOException |
| 510 | | (sm.getString("requestStream.readline.toolong")); | | 511 | | (sm.getString("requestStream.readline.toolong")); |
| 511 | | } | | 512 | | } |
| 512 | | } | | 513 | | } |
| 513 | | header.value[readCount] = ' '; | | 514 | | header.value[readCount] = ' '; |
| 514 | | readCount++; | | 515 | | readCount++; |
| 515 | | } | | 516 | | } |
| 516 | | | | 517 | | |
| 517 | | } | | 518 | | } |
| 518 | | | | 519 | | |
| 519 | | header.valueEnd = readCount; | | 520 | | header.valueEnd = readCount; |
| 520 | | | | 521 | | |
| 521 | | } | | 522 | | } |
| 522 | | | | 523 | | |
| 523 | | | | 524 | | |
| 524 | | /** | | 525 | | /** |
| 525 | | * Read byte. | | 526 | | * Read byte. |
| 526 | | */ | | 527 | | */ |
| 527 | | public int read() | | 528 | | public int read() |
| 528 | | throws IOException { | | 529 | | throws IOException { |
| 529 | | if (pos >= count) { | | 530 | | if (pos >= count) { |
| 530 | | fill(); | | 531 | | fill(); |
| 531 | | if (pos >= count) | | 532 | | if (pos >= count) |
| 532 | | return -1; | | 533 | | return -1; |
| 533 | | } | | 534 | | } |
| 534 | | return buf[pos++] & 0xff; | | 535 | | return buf[pos++] & 0xff; |
| 535 | | } | | 536 | | } |
| 536 | | | | 537 | | |
| 537 | | | | 538 | | |
| 538 | | /** | | 539 | | /** |
| 539 | | * | | 540 | | * |
| 540 | | */ | | 541 | | */ |
| 541 | | /* | | 542 | | /* |
| 542 | | public int read(byte b[], int off, int len) | | 543 | | public int read(byte b[], int off, int len) |
| 543 | | throws IOException { | | 544 | | throws IOException { |
| 544 | | | | 545 | | |
| 545 | | } | | 546 | | } |
| 546 | | */ | | 547 | | */ |
| 547 | | | | 548 | | |
| 548 | | | | 549 | | |
| 549 | | /** | | 550 | | /** |
| 550 | | * | | 551 | | * |
| 551 | | */ | | 552 | | */ |
| 552 | | /* | | 553 | | /* |
| 553 | | public long skip(long n) | | 554 | | public long skip(long n) |
| 554 | | throws IOException { | | 555 | | throws IOException { |
| 555 | | | | 556 | | |
| 556 | | } | | 557 | | } |
| 557 | | */ | | 558 | | */ |
| 558 | | | | 559 | | |
| 559 | | | | 560 | | |
| 560 | | /** | | 561 | | /** |
| 561 | | * Returns the number of bytes that can be read from this input | | 562 | | * Returns the number of bytes that can be read from this input |
| 562 | | * stream without blocking. | | 563 | | * stream without blocking. |
| 563 | | */ | | 564 | | */ |
| 564 | | public int available() | | 565 | | public int available() |
| 565 | | throws IOException { | | 566 | | throws IOException { |
| 566 | | return (count - pos) + is.available(); | | 567 | | return (count - pos) + is.available(); |
| 567 | | } | | 568 | | } |
| 568 | | | | 569 | | |
| 569 | | | | 570 | | |
| 570 | | /** | | 571 | | /** |
| 571 | | * Close the input stream. | | 572 | | * Close the input stream. |
| 572 | | */ | | 573 | | */ |
| 573 | | public void close() | | 574 | | public void close() |
| 574 | | throws IOException { | | 575 | | throws IOException { |
| 575 | | if (is == null) | | 576 | | if (is == null) |
| 576 | | return; | | 577 | | return; |
| 577 | | is.close(); | | 578 | | is.close(); |
| 578 | | is = null; | | 579 | | is = null; |
| 579 | | buf = null; | | 580 | | buf = null; |
| 580 | | } | | 581 | | } |
| 581 | | | | 582 | | |
| 582 | | | | 583 | | |
| 583 | | // ------------------------------------------------------ Protected Methods | | 584 | | // ------------------------------------------------------ Protected Methods |
| 584 | | | | 585 | | |
| 585 | | | | 586 | | |
| 586 | | /** | | 587 | | /** |
| 587 | | * Fill the internal buffer using data from the undelying input stream. | | 588 | | * Fill the internal buffer using data from the undelying input stream. |
| 588 | | */ | | 589 | | */ |
| 589 | | protected void fill() | | 590 | | protected void fill() |
| 590 | | throws IOException { | | 591 | | throws IOException { |
| 591 | | pos = 0; | | 592 | | pos = 0; |
| 592 | | count = 0; | | 593 | | count = 0; |
| 593 | | int nRead = is.read(buf, 0, buf.length); | | 594 | | int nRead = is.read(buf, 0, buf.length); |
| 594 | | if (nRead > 0) { | | 595 | | if (nRead > 0) { |
| 595 | | count = nRead; | | 596 | | count = nRead; |
| 596 | | } | | 597 | | } |
| 597 | | } | | 598 | | } |
| 598 | | | | 599 | | |
| 599 | | | | 600 | | |
| 600 | | } | | 601 | | } |