mobile/android/thirdparty/ch/boye/httpclientandroidlib/impl/io/AbstractMessageParser.java

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

michael@0 1 /*
michael@0 2 * ====================================================================
michael@0 3 * Licensed to the Apache Software Foundation (ASF) under one
michael@0 4 * or more contributor license agreements. See the NOTICE file
michael@0 5 * distributed with this work for additional information
michael@0 6 * regarding copyright ownership. The ASF licenses this file
michael@0 7 * to you under the Apache License, Version 2.0 (the
michael@0 8 * "License"); you may not use this file except in compliance
michael@0 9 * with the License. You may obtain a copy of the License at
michael@0 10 *
michael@0 11 * http://www.apache.org/licenses/LICENSE-2.0
michael@0 12 *
michael@0 13 * Unless required by applicable law or agreed to in writing,
michael@0 14 * software distributed under the License is distributed on an
michael@0 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
michael@0 16 * KIND, either express or implied. See the License for the
michael@0 17 * specific language governing permissions and limitations
michael@0 18 * under the License.
michael@0 19 * ====================================================================
michael@0 20 *
michael@0 21 * This software consists of voluntary contributions made by many
michael@0 22 * individuals on behalf of the Apache Software Foundation. For more
michael@0 23 * information on the Apache Software Foundation, please see
michael@0 24 * <http://www.apache.org/>.
michael@0 25 *
michael@0 26 */
michael@0 27
michael@0 28 package ch.boye.httpclientandroidlib.impl.io;
michael@0 29
michael@0 30 import java.io.IOException;
michael@0 31 import java.util.ArrayList;
michael@0 32 import java.util.List;
michael@0 33
michael@0 34 import ch.boye.httpclientandroidlib.Header;
michael@0 35 import ch.boye.httpclientandroidlib.HttpException;
michael@0 36 import ch.boye.httpclientandroidlib.HttpMessage;
michael@0 37 import ch.boye.httpclientandroidlib.ParseException;
michael@0 38 import ch.boye.httpclientandroidlib.ProtocolException;
michael@0 39 import ch.boye.httpclientandroidlib.io.HttpMessageParser;
michael@0 40 import ch.boye.httpclientandroidlib.io.SessionInputBuffer;
michael@0 41 import ch.boye.httpclientandroidlib.message.LineParser;
michael@0 42 import ch.boye.httpclientandroidlib.message.BasicLineParser;
michael@0 43 import ch.boye.httpclientandroidlib.params.CoreConnectionPNames;
michael@0 44 import ch.boye.httpclientandroidlib.params.HttpParams;
michael@0 45 import ch.boye.httpclientandroidlib.util.CharArrayBuffer;
michael@0 46
michael@0 47 /**
michael@0 48 * Abstract base class for HTTP message parsers that obtain input from
michael@0 49 * an instance of {@link SessionInputBuffer}.
michael@0 50 * <p>
michael@0 51 * The following parameters can be used to customize the behavior of this
michael@0 52 * class:
michael@0 53 * <ul>
michael@0 54 * <li>{@link ch.boye.httpclientandroidlib.params.CoreConnectionPNames#MAX_HEADER_COUNT}</li>
michael@0 55 * <li>{@link ch.boye.httpclientandroidlib.params.CoreConnectionPNames#MAX_LINE_LENGTH}</li>
michael@0 56 * </ul>
michael@0 57 *
michael@0 58 * @since 4.0
michael@0 59 */
michael@0 60 public abstract class AbstractMessageParser implements HttpMessageParser {
michael@0 61
michael@0 62 private static final int HEAD_LINE = 0;
michael@0 63 private static final int HEADERS = 1;
michael@0 64
michael@0 65 private final SessionInputBuffer sessionBuffer;
michael@0 66 private final int maxHeaderCount;
michael@0 67 private final int maxLineLen;
michael@0 68 private final List headerLines;
michael@0 69 protected final LineParser lineParser;
michael@0 70
michael@0 71 private int state;
michael@0 72 private HttpMessage message;
michael@0 73
michael@0 74 /**
michael@0 75 * Creates an instance of this class.
michael@0 76 *
michael@0 77 * @param buffer the session input buffer.
michael@0 78 * @param parser the line parser.
michael@0 79 * @param params HTTP parameters.
michael@0 80 */
michael@0 81 public AbstractMessageParser(
michael@0 82 final SessionInputBuffer buffer,
michael@0 83 final LineParser parser,
michael@0 84 final HttpParams params) {
michael@0 85 super();
michael@0 86 if (buffer == null) {
michael@0 87 throw new IllegalArgumentException("Session input buffer may not be null");
michael@0 88 }
michael@0 89 if (params == null) {
michael@0 90 throw new IllegalArgumentException("HTTP parameters may not be null");
michael@0 91 }
michael@0 92 this.sessionBuffer = buffer;
michael@0 93 this.maxHeaderCount = params.getIntParameter(
michael@0 94 CoreConnectionPNames.MAX_HEADER_COUNT, -1);
michael@0 95 this.maxLineLen = params.getIntParameter(
michael@0 96 CoreConnectionPNames.MAX_LINE_LENGTH, -1);
michael@0 97 this.lineParser = (parser != null) ? parser : BasicLineParser.DEFAULT;
michael@0 98 this.headerLines = new ArrayList();
michael@0 99 this.state = HEAD_LINE;
michael@0 100 }
michael@0 101
michael@0 102 /**
michael@0 103 * Parses HTTP headers from the data receiver stream according to the generic
michael@0 104 * format as given in Section 3.1 of RFC 822, RFC-2616 Section 4 and 19.3.
michael@0 105 *
michael@0 106 * @param inbuffer Session input buffer
michael@0 107 * @param maxHeaderCount maximum number of headers allowed. If the number
michael@0 108 * of headers received from the data stream exceeds maxCount value, an
michael@0 109 * IOException will be thrown. Setting this parameter to a negative value
michael@0 110 * or zero will disable the check.
michael@0 111 * @param maxLineLen maximum number of characters for a header line,
michael@0 112 * including the continuation lines. Setting this parameter to a negative
michael@0 113 * value or zero will disable the check.
michael@0 114 * @return array of HTTP headers
michael@0 115 * @param parser line parser to use. Can be <code>null</code>, in which case
michael@0 116 * the default implementation of this interface will be used.
michael@0 117 *
michael@0 118 * @throws IOException in case of an I/O error
michael@0 119 * @throws HttpException in case of HTTP protocol violation
michael@0 120 */
michael@0 121 public static Header[] parseHeaders(
michael@0 122 final SessionInputBuffer inbuffer,
michael@0 123 int maxHeaderCount,
michael@0 124 int maxLineLen,
michael@0 125 LineParser parser)
michael@0 126 throws HttpException, IOException {
michael@0 127 if (parser == null) {
michael@0 128 parser = BasicLineParser.DEFAULT;
michael@0 129 }
michael@0 130 List headerLines = new ArrayList();
michael@0 131 return parseHeaders(inbuffer, maxHeaderCount, maxLineLen, parser, headerLines);
michael@0 132 }
michael@0 133
michael@0 134 /**
michael@0 135 * Parses HTTP headers from the data receiver stream according to the generic
michael@0 136 * format as given in Section 3.1 of RFC 822, RFC-2616 Section 4 and 19.3.
michael@0 137 *
michael@0 138 * @param inbuffer Session input buffer
michael@0 139 * @param maxHeaderCount maximum number of headers allowed. If the number
michael@0 140 * of headers received from the data stream exceeds maxCount value, an
michael@0 141 * IOException will be thrown. Setting this parameter to a negative value
michael@0 142 * or zero will disable the check.
michael@0 143 * @param maxLineLen maximum number of characters for a header line,
michael@0 144 * including the continuation lines. Setting this parameter to a negative
michael@0 145 * value or zero will disable the check.
michael@0 146 * @param parser line parser to use.
michael@0 147 * @param headerLines List of header lines. This list will be used to store
michael@0 148 * intermediate results. This makes it possible to resume parsing of
michael@0 149 * headers in case of a {@link java.io.InterruptedIOException}.
michael@0 150 *
michael@0 151 * @return array of HTTP headers
michael@0 152 *
michael@0 153 * @throws IOException in case of an I/O error
michael@0 154 * @throws HttpException in case of HTTP protocol violation
michael@0 155 *
michael@0 156 * @since 4.1
michael@0 157 */
michael@0 158 public static Header[] parseHeaders(
michael@0 159 final SessionInputBuffer inbuffer,
michael@0 160 int maxHeaderCount,
michael@0 161 int maxLineLen,
michael@0 162 final LineParser parser,
michael@0 163 final List headerLines)
michael@0 164 throws HttpException, IOException {
michael@0 165
michael@0 166 if (inbuffer == null) {
michael@0 167 throw new IllegalArgumentException("Session input buffer may not be null");
michael@0 168 }
michael@0 169 if (parser == null) {
michael@0 170 throw new IllegalArgumentException("Line parser may not be null");
michael@0 171 }
michael@0 172 if (headerLines == null) {
michael@0 173 throw new IllegalArgumentException("Header line list may not be null");
michael@0 174 }
michael@0 175
michael@0 176 CharArrayBuffer current = null;
michael@0 177 CharArrayBuffer previous = null;
michael@0 178 for (;;) {
michael@0 179 if (current == null) {
michael@0 180 current = new CharArrayBuffer(64);
michael@0 181 } else {
michael@0 182 current.clear();
michael@0 183 }
michael@0 184 int l = inbuffer.readLine(current);
michael@0 185 if (l == -1 || current.length() < 1) {
michael@0 186 break;
michael@0 187 }
michael@0 188 // Parse the header name and value
michael@0 189 // Check for folded headers first
michael@0 190 // Detect LWS-char see HTTP/1.0 or HTTP/1.1 Section 2.2
michael@0 191 // discussion on folded headers
michael@0 192 if ((current.charAt(0) == ' ' || current.charAt(0) == '\t') && previous != null) {
michael@0 193 // we have continuation folded header
michael@0 194 // so append value
michael@0 195 int i = 0;
michael@0 196 while (i < current.length()) {
michael@0 197 char ch = current.charAt(i);
michael@0 198 if (ch != ' ' && ch != '\t') {
michael@0 199 break;
michael@0 200 }
michael@0 201 i++;
michael@0 202 }
michael@0 203 if (maxLineLen > 0
michael@0 204 && previous.length() + 1 + current.length() - i > maxLineLen) {
michael@0 205 throw new IOException("Maximum line length limit exceeded");
michael@0 206 }
michael@0 207 previous.append(' ');
michael@0 208 previous.append(current, i, current.length() - i);
michael@0 209 } else {
michael@0 210 headerLines.add(current);
michael@0 211 previous = current;
michael@0 212 current = null;
michael@0 213 }
michael@0 214 if (maxHeaderCount > 0 && headerLines.size() >= maxHeaderCount) {
michael@0 215 throw new IOException("Maximum header count exceeded");
michael@0 216 }
michael@0 217 }
michael@0 218 Header[] headers = new Header[headerLines.size()];
michael@0 219 for (int i = 0; i < headerLines.size(); i++) {
michael@0 220 CharArrayBuffer buffer = (CharArrayBuffer) headerLines.get(i);
michael@0 221 try {
michael@0 222 headers[i] = parser.parseHeader(buffer);
michael@0 223 } catch (ParseException ex) {
michael@0 224 throw new ProtocolException(ex.getMessage());
michael@0 225 }
michael@0 226 }
michael@0 227 return headers;
michael@0 228 }
michael@0 229
michael@0 230 /**
michael@0 231 * Subclasses must override this method to generate an instance of
michael@0 232 * {@link HttpMessage} based on the initial input from the session buffer.
michael@0 233 * <p>
michael@0 234 * Usually this method is expected to read just the very first line or
michael@0 235 * the very first valid from the data stream and based on the input generate
michael@0 236 * an appropriate instance of {@link HttpMessage}.
michael@0 237 *
michael@0 238 * @param sessionBuffer the session input buffer.
michael@0 239 * @return HTTP message based on the input from the session buffer.
michael@0 240 * @throws IOException in case of an I/O error.
michael@0 241 * @throws HttpException in case of HTTP protocol violation.
michael@0 242 * @throws ParseException in case of a parse error.
michael@0 243 */
michael@0 244 protected abstract HttpMessage parseHead(SessionInputBuffer sessionBuffer)
michael@0 245 throws IOException, HttpException, ParseException;
michael@0 246
michael@0 247 public HttpMessage parse() throws IOException, HttpException {
michael@0 248 int st = this.state;
michael@0 249 switch (st) {
michael@0 250 case HEAD_LINE:
michael@0 251 try {
michael@0 252 this.message = parseHead(this.sessionBuffer);
michael@0 253 } catch (ParseException px) {
michael@0 254 throw new ProtocolException(px.getMessage(), px);
michael@0 255 }
michael@0 256 this.state = HEADERS;
michael@0 257 //$FALL-THROUGH$
michael@0 258 case HEADERS:
michael@0 259 Header[] headers = AbstractMessageParser.parseHeaders(
michael@0 260 this.sessionBuffer,
michael@0 261 this.maxHeaderCount,
michael@0 262 this.maxLineLen,
michael@0 263 this.lineParser,
michael@0 264 this.headerLines);
michael@0 265 this.message.setHeaders(headers);
michael@0 266 HttpMessage result = this.message;
michael@0 267 this.message = null;
michael@0 268 this.headerLines.clear();
michael@0 269 this.state = HEAD_LINE;
michael@0 270 return result;
michael@0 271 default:
michael@0 272 throw new IllegalStateException("Inconsistent parser state");
michael@0 273 }
michael@0 274 }
michael@0 275
michael@0 276 }

mercurial